forked from community/device-mgt-core
Renaming previous org.wso2.carbon.devie.mgt.analytics module and the corresponding feature to org.wso2.carbon.device.mgt.analytics.data.publisher
parent
1a43cf6a55
commit
43926b64b4
@ -0,0 +1,144 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
~
|
||||||
|
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
~ Version 2.0 (the "License"); you may not use this file except
|
||||||
|
~ in compliance with the License.
|
||||||
|
~ You may obtain a copy of the License at
|
||||||
|
~
|
||||||
|
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
~
|
||||||
|
~ Unless required by applicable law or agreed to in writing,
|
||||||
|
~ software distributed under the License is distributed on an
|
||||||
|
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
~ KIND, either express or implied. See the License for the
|
||||||
|
~ specific language governing permissions and limitations
|
||||||
|
~ under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>device-mgt</artifactId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>bundle</packaging>
|
||||||
|
<name>WSO2 Carbon - Device Analytics</name>
|
||||||
|
<description>WSO2 Carbon - Device Analytics</description>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.eclipse.osgi</groupId>
|
||||||
|
<artifactId>org.eclipse.osgi.services</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testng</groupId>
|
||||||
|
<artifactId>testng</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.tomcat</groupId>
|
||||||
|
<artifactId>tomcat</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.tomcat</groupId>
|
||||||
|
<artifactId>tomcat-servlet-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.logging</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.utils</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.core</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.databridge.agent</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics-common</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.databridge.commons</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.analytics</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.analytics.api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.registry</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.registry.indexing</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json.wso2</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-scr-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.felix</groupId>
|
||||||
|
<artifactId>maven-bundle-plugin</artifactId>
|
||||||
|
<version>1.4.0</version>
|
||||||
|
<extensions>true</extensions>
|
||||||
|
<configuration>
|
||||||
|
<instructions>
|
||||||
|
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
|
||||||
|
<Bundle-Name>${project.artifactId}</Bundle-Name>
|
||||||
|
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
|
||||||
|
<Bundle-Description>Data Publisher</Bundle-Description>
|
||||||
|
<Private-Package>org.wso2.carbon.device.mgt.analytics.internal</Private-Package>
|
||||||
|
<Export-Package>
|
||||||
|
!org.wso2.carbon.device.mgt.analytics.data.publisher.internal,
|
||||||
|
org.wso2.carbon.device.mgt.analytics.data.publisher.*;version="${carbon.device.mgt.version}"
|
||||||
|
</Export-Package>
|
||||||
|
<Import-Package>
|
||||||
|
org.osgi.framework,
|
||||||
|
org.osgi.service.component,
|
||||||
|
org.apache.commons.logging.*,
|
||||||
|
org.wso2.carbon.context;version="${carbon.kernel.version.range}",
|
||||||
|
org.wso2.carbon.utils;version="${carbon.kernel.version.range}",
|
||||||
|
org.wso2.carbon.databridge.*;version="${carbon.analytics.common.version.range}",
|
||||||
|
org.wso2.carbon.analytics.*;version="${carbon.analytics.version.range}",
|
||||||
|
org.wso2.carbon.registry.core.*;resolution:=optional,
|
||||||
|
org.wso2.carbon.registry.common.*;version="${carbon.registry.imp.pkg.version.range}",
|
||||||
|
org.wso2.carbon.registry.indexing.*; version="${carbon.registry.imp.pkg.version.range}",
|
||||||
|
org.json;version="${commons-json.version}",
|
||||||
|
javax.xml.bind,
|
||||||
|
javax.xml.bind.annotation,
|
||||||
|
javax.xml.parsers,
|
||||||
|
org.w3c.dom,
|
||||||
|
org.wso2.carbon.base
|
||||||
|
</Import-Package>
|
||||||
|
</instructions>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class AnalyticsDataRecord {
|
||||||
|
private Map<String, Object> values;
|
||||||
|
private long timestamp;
|
||||||
|
|
||||||
|
public AnalyticsDataRecord() {
|
||||||
|
values = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyticsDataRecord(Map<String, Object> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, Object> getValues() {
|
||||||
|
return this.values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValue(String name, Object value) {
|
||||||
|
values.put(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getValue(String name) {
|
||||||
|
return this.values.get(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setValues(Map<String, Object> values) {
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getTimestamp() {
|
||||||
|
return this.timestamp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTimestamp(long timestamp) {
|
||||||
|
this.timestamp = timestamp;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics;
|
||||||
|
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
||||||
|
|
||||||
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public class DeviceAnalyticsUtil {
|
||||||
|
|
||||||
|
public static Document convertToDocument(File file) throws DataPublisherConfigurationException {
|
||||||
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
||||||
|
factory.setNamespaceAware(true);
|
||||||
|
try {
|
||||||
|
DocumentBuilder docBuilder = factory.newDocumentBuilder();
|
||||||
|
return docBuilder.parse(file);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while parsing file, while converting " +
|
||||||
|
"to a org.w3c.dom.Document", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,222 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.wso2.carbon.base.MultitenantConstants;
|
||||||
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||||
|
import org.wso2.carbon.databridge.agent.DataPublisher;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointAgentConfigurationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointAuthenticationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointConfigurationException;
|
||||||
|
import org.wso2.carbon.databridge.agent.exception.DataEndpointException;
|
||||||
|
import org.wso2.carbon.databridge.commons.exception.TransportException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.config.AnalyticsConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherAlreadyExistsException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.internal.DeviceAnalyticsDataHolder;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
||||||
|
import org.wso2.carbon.registry.core.Registry;
|
||||||
|
import org.wso2.carbon.registry.core.Resource;
|
||||||
|
import org.wso2.carbon.registry.core.exceptions.RegistryException;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is used to manage data publisher per tenant.
|
||||||
|
*/
|
||||||
|
public class DeviceDataPublisher {
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(DeviceDataPublisher.class);
|
||||||
|
private static final String TENANT_DAS_CONFIG_LOCATION = "/das/config.json";
|
||||||
|
private static final String USERNAME_CONFIG_TAG = "username";
|
||||||
|
private static final String PASSWORD_CONFIG_TAG = "password";
|
||||||
|
/**
|
||||||
|
* map to store data publishers for each tenant.
|
||||||
|
*/
|
||||||
|
private static Map<String, DataPublisher> dataPublisherMap;
|
||||||
|
private static DeviceDataPublisher deviceDataPublisher;
|
||||||
|
|
||||||
|
public static DeviceDataPublisher getInstance() {
|
||||||
|
if (deviceDataPublisher == null) {
|
||||||
|
synchronized (DeviceDataPublisher.class) {
|
||||||
|
if (deviceDataPublisher == null) {
|
||||||
|
deviceDataPublisher = new DeviceDataPublisher();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return deviceDataPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DeviceDataPublisher() {
|
||||||
|
dataPublisherMap = new ConcurrentHashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* this return the data publisher for the tenant.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public DataPublisher getDataPublisher() throws DataPublisherConfigurationException {
|
||||||
|
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
|
||||||
|
//Get LoadBalancingDataPublisher which has been registered for the tenant.
|
||||||
|
DataPublisher dataPublisher = getDataPublisher(tenantDomain);
|
||||||
|
//If a LoadBalancingDataPublisher had not been registered for the tenant.
|
||||||
|
if (dataPublisher == null) {
|
||||||
|
AnalyticsConfiguration analyticsConfig = AnalyticsConfiguration.getInstance();
|
||||||
|
if (!analyticsConfig.isEnable()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
String analyticsServerUrlGroups = analyticsConfig.getReceiverServerUrl();
|
||||||
|
String analyticsServerUsername = analyticsConfig.getAdminUsername();
|
||||||
|
String analyticsServerPassword = analyticsConfig.getAdminPassword();
|
||||||
|
if (!tenantDomain.equals(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME)) {
|
||||||
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
String userInfo[] = getAnalyticsServerUserInfo(tenantId);
|
||||||
|
if (userInfo != null) {
|
||||||
|
analyticsServerUsername = userInfo[0];
|
||||||
|
analyticsServerPassword = userInfo[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//Create new DataPublisher for the tenant.
|
||||||
|
try {
|
||||||
|
dataPublisher = new DataPublisher(analyticsServerUrlGroups, analyticsServerUsername,
|
||||||
|
analyticsServerPassword);
|
||||||
|
//Add created DataPublisher.
|
||||||
|
addDataPublisher(tenantDomain, dataPublisher);
|
||||||
|
} catch (DataEndpointAgentConfigurationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Configuration Exception on data publisher for " +
|
||||||
|
"ReceiverGroup = " + analyticsServerUrlGroups + " for username " + analyticsServerUsername, e);
|
||||||
|
} catch (DataEndpointException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Invalid ReceiverGroup = " + analyticsServerUrlGroups, e);
|
||||||
|
} catch (DataEndpointConfigurationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Invalid Data endpoint configuration.", e);
|
||||||
|
} catch (DataEndpointAuthenticationException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Authentication Failed for user " +
|
||||||
|
analyticsServerUsername, e);
|
||||||
|
} catch (TransportException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while retrieving data publisher", e);
|
||||||
|
} catch (DataPublisherAlreadyExistsException e) {
|
||||||
|
log.warn("Attempting to register a data publisher for the tenant " + tenantDomain +
|
||||||
|
" when one already exists. Returning existing data publisher");
|
||||||
|
return getDataPublisher(tenantDomain);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dataPublisher;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch the data publisher which has been registered under the tenant domain.
|
||||||
|
*
|
||||||
|
* @param tenantDomain - The tenant domain under which the data publisher is registered
|
||||||
|
* @return - Instance of the DataPublisher which was registered. Null if not registered.
|
||||||
|
*/
|
||||||
|
private DataPublisher getDataPublisher(String tenantDomain) {
|
||||||
|
if (dataPublisherMap.containsKey(tenantDomain)) {
|
||||||
|
return dataPublisherMap.get(tenantDomain);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a LoadBalancingDataPublisher to the data publisher map.
|
||||||
|
*
|
||||||
|
* @param tenantDomain - The tenant domain under which the data publisher will be registered.
|
||||||
|
* @param dataPublisher - Instance of the LoadBalancingDataPublisher
|
||||||
|
* @throws DataPublisherAlreadyExistsException
|
||||||
|
* -
|
||||||
|
* If a data publisher has already been registered under the tenant
|
||||||
|
* domain
|
||||||
|
*/
|
||||||
|
private void addDataPublisher(String tenantDomain, DataPublisher dataPublisher)
|
||||||
|
throws DataPublisherAlreadyExistsException {
|
||||||
|
if (dataPublisherMap.containsKey(tenantDomain)) {
|
||||||
|
throw new DataPublisherAlreadyExistsException(
|
||||||
|
"A DataPublisher has already been created for the tenant " + tenantDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
dataPublisherMap.put(tenantDomain, dataPublisher);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* retrieve the credential from registry
|
||||||
|
*/
|
||||||
|
private String[] getAnalyticsServerUserInfo(int tenantId) throws DataPublisherConfigurationException {
|
||||||
|
try {
|
||||||
|
String config = getConfigRegistryResourceContent(tenantId, TENANT_DAS_CONFIG_LOCATION);
|
||||||
|
JSONObject jsonConfigforDas = new JSONObject(config);
|
||||||
|
String credential[] = new String[2];
|
||||||
|
credential[0] = jsonConfigforDas.getString(USERNAME_CONFIG_TAG);
|
||||||
|
credential[1] = jsonConfigforDas.getString(PASSWORD_CONFIG_TAG);
|
||||||
|
return credential;
|
||||||
|
} catch (RegistryException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Failed to load the registry for tenant " + tenantId, e);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
throw new DataPublisherConfigurationException(
|
||||||
|
"Failed to parse the credential from the registry for tenant " + tenantId, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the credential detail from the registry for tenants.
|
||||||
|
*
|
||||||
|
* @param tenantId for identify tenant space.
|
||||||
|
* @param registryLocation retrieve the config file from tenant space.
|
||||||
|
* @return the config for tenant
|
||||||
|
* @throws RegistryException
|
||||||
|
*/
|
||||||
|
private String getConfigRegistryResourceContent(int tenantId, final String registryLocation)
|
||||||
|
throws RegistryException {
|
||||||
|
String content = null;
|
||||||
|
try {
|
||||||
|
PrivilegedCarbonContext.startTenantFlow();
|
||||||
|
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
|
||||||
|
RegistryService registryService = DeviceAnalyticsDataHolder.getInstance().getRegistryService();
|
||||||
|
if (registryService != null) {
|
||||||
|
Registry registry = registryService.getConfigSystemRegistry(tenantId);
|
||||||
|
this.loadTenantRegistry(tenantId);
|
||||||
|
if (registry.resourceExists(registryLocation)) {
|
||||||
|
Resource resource = registry.get(registryLocation);
|
||||||
|
content = new String((byte[]) resource.getContent(), Charset.defaultCharset());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
PrivilegedCarbonContext.endTenantFlow();
|
||||||
|
}
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadTenantRegistry(int tenantId) throws RegistryException {
|
||||||
|
TenantRegistryLoader tenantRegistryLoader = DeviceAnalyticsDataHolder.getInstance().getTenantRegistryLoader();
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().getIndexLoaderService().loadTenantIndex(tenantId);
|
||||||
|
tenantRegistryLoader.loadTenantRegistry(tenantId);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.config;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.DeviceAnalyticsUtil;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.utils.CarbonUtils;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
import javax.xml.XMLConstants;
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import javax.xml.validation.Schema;
|
||||||
|
import javax.xml.validation.SchemaFactory;
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configurations related to DAS data publisher and DAL.
|
||||||
|
*/
|
||||||
|
@XmlRootElement(name = "AnalyticsConfiguration")
|
||||||
|
public class AnalyticsConfiguration {
|
||||||
|
|
||||||
|
private String receiverServerUrl;
|
||||||
|
private String adminUsername;
|
||||||
|
private String adminPassword;
|
||||||
|
private boolean enable;
|
||||||
|
|
||||||
|
private static AnalyticsConfiguration config;
|
||||||
|
|
||||||
|
private static final Log log = LogFactory.getLog(AnalyticsConfiguration.class);
|
||||||
|
private static final String DEVICE_ANALYTICS_CONFIG_PATH =
|
||||||
|
CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + "device-analytics-config.xml";
|
||||||
|
|
||||||
|
private AnalyticsConfiguration() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static AnalyticsConfiguration getInstance() {
|
||||||
|
if (config == null) {
|
||||||
|
throw new InvalidConfigurationStateException("Device analytics configuration is not " +
|
||||||
|
"initialized properly");
|
||||||
|
}
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@XmlElement(name = "AdminUsername", required = true)
|
||||||
|
public String getAdminUsername() {
|
||||||
|
return adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminUsername(String adminUsername) {
|
||||||
|
this.adminUsername = adminUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "AdminPassword", required = true)
|
||||||
|
public String getAdminPassword() {
|
||||||
|
return adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAdminPassword(String adminPassword) {
|
||||||
|
this.adminPassword = adminPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "ReceiverServerUrl", required = true)
|
||||||
|
public String getReceiverServerUrl() {
|
||||||
|
return receiverServerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReceiverServerUrl(String receiverServerUrl) {
|
||||||
|
this.receiverServerUrl = receiverServerUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "Enabled", required = true)
|
||||||
|
public boolean isEnable() {
|
||||||
|
return enable;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnable(boolean status) {
|
||||||
|
this.enable = status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void init() throws DataPublisherConfigurationException {
|
||||||
|
try {
|
||||||
|
File authConfig = new File(AnalyticsConfiguration.DEVICE_ANALYTICS_CONFIG_PATH);
|
||||||
|
Document doc = DeviceAnalyticsUtil.convertToDocument(authConfig);
|
||||||
|
|
||||||
|
/* Un-marshaling device analytics configuration */
|
||||||
|
JAXBContext ctx = JAXBContext.newInstance(AnalyticsConfiguration.class);
|
||||||
|
Unmarshaller unmarshaller = ctx.createUnmarshaller();
|
||||||
|
config = (AnalyticsConfiguration) unmarshaller.unmarshal(doc);
|
||||||
|
} catch (JAXBException e) {
|
||||||
|
throw new DataPublisherConfigurationException("Error occurred while un-marshalling device analytics " +
|
||||||
|
"Config", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.config;
|
||||||
|
|
||||||
|
public class InvalidConfigurationStateException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -3151274311329070297L;
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public InvalidConfigurationStateException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.exception;
|
||||||
|
|
||||||
|
public class DataPublisherAlreadyExistsException extends Exception {
|
||||||
|
public DataPublisherAlreadyExistsException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherAlreadyExistsException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DataPublisherAlreadyExistsException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.exception;
|
||||||
|
|
||||||
|
public class DataPublisherConfigurationException extends Exception {
|
||||||
|
public DataPublisherConfigurationException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DataPublisherConfigurationException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DataPublisherConfigurationException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.exception;
|
||||||
|
|
||||||
|
public class DeviceManagementAnalyticsException extends Exception {
|
||||||
|
public DeviceManagementAnalyticsException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DeviceManagementAnalyticsException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected DeviceManagementAnalyticsException(String message, Throwable cause,
|
||||||
|
boolean enableSuppression,
|
||||||
|
boolean writableStackTrace) {
|
||||||
|
super(message, cause, enableSuppression, writableStackTrace);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.internal;
|
||||||
|
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader;
|
||||||
|
|
||||||
|
public class DeviceAnalyticsDataHolder {
|
||||||
|
private static DeviceAnalyticsDataHolder thisInstance = new DeviceAnalyticsDataHolder();
|
||||||
|
/**
|
||||||
|
* AnalyticsDataAPI is service used to retrieve data from DAS.
|
||||||
|
*/
|
||||||
|
private AnalyticsDataAPI analyticsDataAPI;
|
||||||
|
private TenantRegistryLoader tenantRegistryLoader;
|
||||||
|
private TenantIndexingLoader indexLoader;
|
||||||
|
private RegistryService registryService;
|
||||||
|
private DeviceAnalyticsDataHolder() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static DeviceAnalyticsDataHolder getInstance() {
|
||||||
|
return thisInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyticsDataAPI getAnalyticsDataAPI() {
|
||||||
|
return analyticsDataAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
this.analyticsDataAPI = analyticsDataAPI;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader){
|
||||||
|
this.tenantRegistryLoader = tenantRegistryLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantRegistryLoader getTenantRegistryLoader(){
|
||||||
|
return tenantRegistryLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIndexLoaderService(TenantIndexingLoader indexLoader) {
|
||||||
|
this.indexLoader = indexLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TenantIndexingLoader getIndexLoaderService(){
|
||||||
|
return indexLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
public RegistryService getRegistryService() {
|
||||||
|
return registryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryService(RegistryService registryService) {
|
||||||
|
this.registryService = registryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,151 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.internal;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.osgi.framework.BundleContext;
|
||||||
|
import org.osgi.framework.ServiceRegistration;
|
||||||
|
import org.osgi.service.component.ComponentContext;
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.config.AnalyticsConfiguration;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsServiceImpl;
|
||||||
|
import org.wso2.carbon.registry.core.service.RegistryService;
|
||||||
|
import org.wso2.carbon.registry.core.service.TenantRegistryLoader;
|
||||||
|
import org.wso2.carbon.registry.indexing.service.TenantIndexingLoader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @scr.component name="org.wso2.carbon.device.mgt.analytics.internal.DeviceAnalyticsServiceComponent"
|
||||||
|
* immediate="true"
|
||||||
|
* @scr.reference name="device.analytics.api"
|
||||||
|
* interface="org.wso2.carbon.analytics.api.AnalyticsDataAPI"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setAnalyticsDataAPI"
|
||||||
|
* unbind="unsetAnalyticsDataAPI"
|
||||||
|
* @scr.reference name="registry.service"
|
||||||
|
* interface="org.wso2.carbon.registry.core.service.RegistryService"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setRegistryService"
|
||||||
|
* unbind="unsetRegistryService"
|
||||||
|
* @scr.reference name="tenant.registryloader"
|
||||||
|
* interface="org.wso2.carbon.registry.core.service.TenantRegistryLoader"
|
||||||
|
* cardinality="1..1" policy="dynamic"
|
||||||
|
* bind="setTenantRegistryLoader"
|
||||||
|
* unbind="unsetTenantRegistryLoader"
|
||||||
|
* @scr.reference name="tenant.indexloader"
|
||||||
|
* interface="org.wso2.carbon.registry.indexing.service.TenantIndexingLoader"
|
||||||
|
* cardinality="1..1"
|
||||||
|
* policy="dynamic"
|
||||||
|
* bind="setIndexLoader"
|
||||||
|
* unbind="unsetIndexLoader"
|
||||||
|
*/
|
||||||
|
public class DeviceAnalyticsServiceComponent {
|
||||||
|
|
||||||
|
private ServiceRegistration analyticsServiceRef;
|
||||||
|
private static Log log = LogFactory.getLog(DeviceAnalyticsServiceComponent.class);
|
||||||
|
|
||||||
|
protected void activate(ComponentContext componentCtx) {
|
||||||
|
try {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Initializing device analytics bundle");
|
||||||
|
}
|
||||||
|
AnalyticsConfiguration.init();
|
||||||
|
|
||||||
|
BundleContext bundleCtx = componentCtx.getBundleContext();
|
||||||
|
this.analyticsServiceRef =
|
||||||
|
bundleCtx.registerService(DeviceAnalyticsService.class, new DeviceAnalyticsServiceImpl(), null);
|
||||||
|
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device management analytics bundle has been successfully initialized");
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
log.error("Error occurred while initializing device analytics bundle", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deactivate(ComponentContext componentCtx) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Deactivating device analytics bundle");
|
||||||
|
}
|
||||||
|
if (analyticsServiceRef != null) {
|
||||||
|
analyticsServiceRef.unregister();
|
||||||
|
}
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Device analytics bundle has been successfully deactivated");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets AnalyticsDataAPI Service.
|
||||||
|
*
|
||||||
|
* @param analyticsDataAPI An instance of AnalyticsDataAPI
|
||||||
|
*/
|
||||||
|
protected void setAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Setting AnalyticsDataAPI Service");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setAnalyticsDataAPI(analyticsDataAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Un sets AnalyticsDataAPI Service.
|
||||||
|
*
|
||||||
|
* @param analyticsDataAPI An instance of AnalyticsDataAPI
|
||||||
|
*/
|
||||||
|
protected void unsetAnalyticsDataAPI(AnalyticsDataAPI analyticsDataAPI) {
|
||||||
|
if (log.isDebugEnabled()) {
|
||||||
|
log.debug("Un-Setting AnalyticsDataAPI Service");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setAnalyticsDataAPI(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setRegistryService(RegistryService registryService) {
|
||||||
|
if (registryService != null && log.isDebugEnabled()) {
|
||||||
|
log.debug("Registry service initialized");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setRegistryService(registryService);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetRegistryService(RegistryService registryService) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setRegistryService(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setTenantRegistryLoader(tenantRegistryLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetTenantRegistryLoader(TenantRegistryLoader tenantRegistryLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setTenantRegistryLoader(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setIndexLoader(TenantIndexingLoader indexLoader) {
|
||||||
|
if (indexLoader != null && log.isDebugEnabled()) {
|
||||||
|
log.debug("IndexLoader service initialized");
|
||||||
|
}
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setIndexLoaderService(indexLoader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void unsetIndexLoader(TenantIndexingLoader indexLoader) {
|
||||||
|
DeviceAnalyticsDataHolder.getInstance().setIndexLoaderService(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.service;
|
||||||
|
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.AnalyticsDataRecord;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DeviceManagementAnalyticsException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service can be used to publish and retreive data from the Analytics Server.
|
||||||
|
*/
|
||||||
|
public interface DeviceAnalyticsService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is used to publish an event to DAS.
|
||||||
|
* @param streamName is the name of the stream that the data needs to pushed
|
||||||
|
* @param version is the version of the stream
|
||||||
|
* @param metaDataArray - meta data that needs to pushed
|
||||||
|
* @param correlationDataArray - correlation data that needs to be pushed
|
||||||
|
* @param payloadDataArray - payload data that needs to be pushed
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*/
|
||||||
|
boolean publishEvent(String streamName, String version, Object[] metaDataArray, Object[] correlationDataArray,
|
||||||
|
Object[] payloadDataArray) throws DataPublisherConfigurationException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This service can be used to retrieve all the event for the query.
|
||||||
|
* @param tableName is the name of the table that events need to be retrieved
|
||||||
|
* @param query is query to be executed.
|
||||||
|
* @return the record list
|
||||||
|
* @throws DeviceManagementAnalyticsException
|
||||||
|
*/
|
||||||
|
List<AnalyticsDataRecord> getAllEventsForDevice(String tableName, String query) throws DeviceManagementAnalyticsException;
|
||||||
|
}
|
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
* Version 2.0 (the "License"); you may not use this file except
|
||||||
|
* in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.wso2.carbon.device.mgt.analytics.service;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.analytics.api.AnalyticsDataAPI;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDataResponse;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.AnalyticsDrillDownRequest;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.commons.SearchResultEntry;
|
||||||
|
import org.wso2.carbon.analytics.dataservice.core.AnalyticsDataServiceUtils;
|
||||||
|
import org.wso2.carbon.analytics.datasource.commons.Record;
|
||||||
|
import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException;
|
||||||
|
import org.wso2.carbon.context.CarbonContext;
|
||||||
|
import org.wso2.carbon.databridge.agent.DataPublisher;
|
||||||
|
import org.wso2.carbon.databridge.commons.utils.DataBridgeCommonsUtils;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.AnalyticsDataRecord;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.DeviceDataPublisher;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.exception.DeviceManagementAnalyticsException;
|
||||||
|
import org.wso2.carbon.device.mgt.analytics.internal.DeviceAnalyticsDataHolder;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the implementation of Osgi Service which can be used to publish and retireved
|
||||||
|
* event/records.
|
||||||
|
*/
|
||||||
|
public class DeviceAnalyticsServiceImpl implements DeviceAnalyticsService {
|
||||||
|
private static Log log = LogFactory.getLog(DeviceAnalyticsServiceImpl.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param streamName is the name of the stream that the data needs to pushed
|
||||||
|
* @param version is the version of the stream
|
||||||
|
* @param metaDataArray - meta data that needs to pushed
|
||||||
|
* @param correlationDataArray - correlation data that needs to be pushed
|
||||||
|
* @param payloadDataArray - payload data that needs to be pushed
|
||||||
|
* @return
|
||||||
|
* @throws DataPublisherConfigurationException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean publishEvent(String streamName, String version, Object[] metaDataArray,
|
||||||
|
Object[] correlationDataArray,
|
||||||
|
Object[] payloadDataArray) throws DataPublisherConfigurationException {
|
||||||
|
DataPublisher dataPublisher = DeviceDataPublisher.getInstance().getDataPublisher();
|
||||||
|
if (dataPublisher != null) {
|
||||||
|
String streamId = DataBridgeCommonsUtils.generateStreamId(streamName, version);
|
||||||
|
return dataPublisher.tryPublish(streamId, System.currentTimeMillis(), metaDataArray, correlationDataArray,
|
||||||
|
payloadDataArray);
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param tableName is the name of the table that events need to be retrieved
|
||||||
|
* @param query is query to be executed.
|
||||||
|
* @return
|
||||||
|
* @throws AnalyticsException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<AnalyticsDataRecord> getAllEventsForDevice(String tableName, String query) throws
|
||||||
|
DeviceManagementAnalyticsException {
|
||||||
|
try {
|
||||||
|
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||||
|
AnalyticsDataAPI analyticsDataAPI = DeviceAnalyticsDataHolder.getInstance().getAnalyticsDataAPI();
|
||||||
|
int eventCount = analyticsDataAPI.searchCount(tenantId, tableName, query);
|
||||||
|
if (eventCount == 0) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
AnalyticsDrillDownRequest drillDownRequest = new AnalyticsDrillDownRequest();
|
||||||
|
drillDownRequest.setQuery(query);
|
||||||
|
drillDownRequest.setTableName(tableName);
|
||||||
|
drillDownRequest.setRecordCount(eventCount);
|
||||||
|
List<SearchResultEntry> resultEntries = analyticsDataAPI.drillDownSearch(tenantId, drillDownRequest);
|
||||||
|
List<String> recordIds = getRecordIds(resultEntries);
|
||||||
|
AnalyticsDataResponse response = analyticsDataAPI.get(tenantId, tableName, 1, null, recordIds);
|
||||||
|
List<Record> records = AnalyticsDataServiceUtils.listRecords(analyticsDataAPI, response);
|
||||||
|
return getAnalyticsDataRecords(records);
|
||||||
|
} catch (AnalyticsException e) {
|
||||||
|
throw new DeviceManagementAnalyticsException(
|
||||||
|
"Failed fetch data for table " + tableName + "with the query " + query);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> getRecordIds(List<SearchResultEntry> searchResults) {
|
||||||
|
List<String> ids = new ArrayList<>();
|
||||||
|
for (SearchResultEntry searchResult : searchResults) {
|
||||||
|
ids.add(searchResult.getId());
|
||||||
|
}
|
||||||
|
return ids;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<AnalyticsDataRecord> getAnalyticsDataRecords(List<Record> records) {
|
||||||
|
List<AnalyticsDataRecord> analyticsDataRecords = new ArrayList<>();
|
||||||
|
for (Record record : records) {
|
||||||
|
AnalyticsDataRecord analyticsDataRecord = new AnalyticsDataRecord(record.getValues());
|
||||||
|
analyticsDataRecords.add(analyticsDataRecord);
|
||||||
|
}
|
||||||
|
return analyticsDataRecords;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,106 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
~
|
||||||
|
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
~ Version 2.0 (the "License"); you may not use this file except
|
||||||
|
~ in compliance with the License.
|
||||||
|
~ you may obtain a copy of the License at
|
||||||
|
~
|
||||||
|
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
~
|
||||||
|
~ Unless required by applicable law or agreed to in writing,
|
||||||
|
~ software distributed under the License is distributed on an
|
||||||
|
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
~ KIND, either express or implied. See the License for the
|
||||||
|
~ specific language governing permissions and limitations
|
||||||
|
~ under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>device-mgt-feature</artifactId>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher.feature</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
<version>1.1.0-SNAPSHOT</version>
|
||||||
|
<name>WSO2 Carbon - Device Management Server Feature</name>
|
||||||
|
<url>http://wso2.org</url>
|
||||||
|
<description>This feature contains bundles related to device analytics data publisher</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.wso2.carbon.devicemgt</groupId>
|
||||||
|
<artifactId>org.wso2.carbon.device.mgt.analytics.data.publisher</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-resources-plugin</artifactId>
|
||||||
|
<version>2.6</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-resources</id>
|
||||||
|
<phase>generate-resources</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-resources</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>src/main/resources</outputDirectory>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>resources</directory>
|
||||||
|
<includes>
|
||||||
|
<include>build.properties</include>
|
||||||
|
<include>p2.inf</include>
|
||||||
|
</includes>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.wso2.maven</groupId>
|
||||||
|
<artifactId>carbon-p2-plugin</artifactId>
|
||||||
|
<version>${carbon.p2.plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>p2-feature-generation</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>p2-feature-gen</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<id>org.wso2.carbon.device.mgt.analytics.data.publisher</id>
|
||||||
|
<propertiesFile>../../../features/etc/feature.properties</propertiesFile>
|
||||||
|
<adviceFile>
|
||||||
|
<properties>
|
||||||
|
<propertyDef>org.wso2.carbon.p2.category.type:server</propertyDef>
|
||||||
|
<propertyDef>org.eclipse.equinox.p2.type.group:false</propertyDef>
|
||||||
|
</properties>
|
||||||
|
</adviceFile>
|
||||||
|
<bundles>
|
||||||
|
<bundleDef>
|
||||||
|
org.wso2.carbon.devicemgt:org.wso2.carbon.device.mgt.analytics.data.publisher:${carbon.device.mgt.version}
|
||||||
|
</bundleDef>
|
||||||
|
</bundles>
|
||||||
|
<importFeatures>
|
||||||
|
<importFeatureDef>org.wso2.carbon.core.server:${carbon.kernel.version}</importFeatureDef>
|
||||||
|
</importFeatures>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1 @@
|
|||||||
|
custom = true
|
@ -0,0 +1,36 @@
|
|||||||
|
<?xml version="1.0" encoding="ISO-8859-1"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
~
|
||||||
|
~ WSO2 Inc. licenses this file to you under the Apache License,
|
||||||
|
~ Version 2.0 (the "License"); you may not use this file except
|
||||||
|
~ in compliance with the License.
|
||||||
|
~ you may obtain a copy of the License at
|
||||||
|
~
|
||||||
|
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
~
|
||||||
|
~ Unless required by applicable law or agreed to in writing,
|
||||||
|
~ software distributed under the License is distributed on an
|
||||||
|
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
~ KIND, either express or implied. See the License for the
|
||||||
|
~ specific language governing permissions and limitations
|
||||||
|
~ under the License.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<AnalyticsConfiguration>
|
||||||
|
<!--
|
||||||
|
Server URL of the remote DAS/BAM/CEP server used to collect statistics. Must
|
||||||
|
be specified in protocol://hostname:port/ format.
|
||||||
|
|
||||||
|
An event can also be published to multiple Receiver Groups each having 1 or more receivers. Receiver
|
||||||
|
Groups are delimited by curly braces whereas receivers are delimited by commas.
|
||||||
|
Ex - Multiple Receivers within a single group
|
||||||
|
tcp://localhost:7612/,tcp://localhost:7613/,tcp://localhost:7614/
|
||||||
|
Ex - Multiple Receiver Groups with two receivers each
|
||||||
|
{tcp://localhost:7612/,tcp://localhost:7613},{tcp://localhost:7712/,tcp://localhost:7713/}
|
||||||
|
-->
|
||||||
|
<Enabled>true</Enabled>
|
||||||
|
<ReceiverServerUrl>tcp://localhost:7611</ReceiverServerUrl>
|
||||||
|
<AdminUsername>admin</AdminUsername>
|
||||||
|
<AdminPassword>admin</AdminPassword>
|
||||||
|
</AnalyticsConfiguration>
|
@ -0,0 +1,2 @@
|
|||||||
|
instructions.configure = \
|
||||||
|
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/device-analytics-config.xml,target:${installFolder}/../../conf/etc/device-analytics-config.xml,overwrite:true);\
|
Loading…
Reference in new issue