adding policy monitor service for firealarm

merge-requests/1/head
Rasika Perera 9 years ago
parent f8dca4dd8f
commit d384942f69

@ -67,9 +67,12 @@
org.w3c.dom, org.w3c.dom,
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.device.mgt.common, org.wso2.carbon.device.mgt.common,
org.wso2.carbon.policy.mgt.common,
org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.context.*, org.wso2.carbon.context.*,
org.wso2.carbon.ndatasource.core, org.wso2.carbon.ndatasource.core,
org.wso2.carbon.device.mgt.iot.common.*, org.wso2.carbon.device.mgt.iot.common.*,
com.google.gson.*
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
@ -97,6 +100,17 @@
<artifactId>org.wso2.carbon.logging</artifactId> <artifactId>org.wso2.carbon.logging</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.common</artifactId>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.mgt.common</artifactId> <artifactId>org.wso2.carbon.device.mgt.common</artifactId>
@ -111,6 +125,10 @@
<artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId> <artifactId>org.wso2.carbon.device.mgt.iot.common</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
</dependencies> </dependencies>

@ -0,0 +1,106 @@
/*
* 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.
*/
package org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
import org.wso2.carbon.device.mgt.iot.common.transport.mqtt.MqttPublisher;
import java.util.ArrayList;
import java.util.List;
import static java.nio.charset.StandardCharsets.UTF_8;
public class FireAlarmPolicyMonitoringService implements PolicyMonitoringService {
private static Log log = LogFactory.getLog(FireAlarmPolicyMonitoringService.class);
@Override
public void notifyDevices(List<Device> list) throws PolicyComplianceException {
String userName = CarbonContext.getThreadLocalCarbonContext().getUsername();
MqttPublisher mqttPublisher = new MqttPublisher();
try {
mqttPublisher.initControlQueue();
mqttPublisher.publish("Raspberry-Policy-sender",
"/iot/policymgt/govern/" + FireAlarmConstants.DEVICE_TYPE + "/"
+ userName, userName.getBytes(UTF_8));
} catch (DeviceControllerException e) {
log.error("Error on notifying "+FireAlarmConstants.DEVICE_TYPE+" devices, message not sent.");
}
}
@Override
public ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy,
Object compliancePayload) throws PolicyComplianceException {
if (log.isDebugEnabled()) {
log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'");
}
ComplianceData complianceData = new ComplianceData();
if (compliancePayload == null || policy == null) {
return complianceData;
}
List<ComplianceFeature> complianceFeatures = new ArrayList<ComplianceFeature>();
// Parsing json string to get compliance features.
JsonElement jsonElement;
if (compliancePayload instanceof String) {
jsonElement = new JsonParser().parse((String) compliancePayload);
} else {
throw new PolicyComplianceException("Invalid policy compliance payload");
}
JsonArray jsonArray = jsonElement.getAsJsonArray();
Gson gson = new Gson();
ComplianceFeature complianceFeature;
for (JsonElement element : jsonArray) {
complianceFeature = gson.fromJson(element, ComplianceFeature.class);
complianceFeatures.add(complianceFeature);
}
complianceData.setComplianceFeatures(complianceFeatures);
for (ComplianceFeature cf : complianceFeatures) {
if (!cf.isCompliant()) {
complianceData.setStatus(false);
break;
}
}
return complianceData;
}
@Override
public String getType() {
return FireAlarmConstants.DEVICE_TYPE;
}
}

@ -96,6 +96,18 @@
<version>${carbon.device.mgt.version}</version> <version>${carbon.device.mgt.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.core</artifactId>
<version>${carbon.device.mgt.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.common</artifactId>
<version>${carbon.device.mgt.version}</version>
<scope>provided</scope>
</dependency>
<!--IOT dependencies--> <!--IOT dependencies-->
@ -211,6 +223,12 @@
<!--<scope>provided</scope>--> <!--<scope>provided</scope>-->
</dependency> </dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${google.gson.version}</version>
</dependency>
<!--Analytics dependencies--> <!--Analytics dependencies-->
<dependency> <dependency>
@ -417,11 +435,10 @@
<orbit.tomcat.jdbc.pooling.version>7.0.34.wso2v2</orbit.tomcat.jdbc.pooling.version> <orbit.tomcat.jdbc.pooling.version>7.0.34.wso2v2</orbit.tomcat.jdbc.pooling.version>
<eclipse.paho.version>0.4.0</eclipse.paho.version> <eclipse.paho.version>0.4.0</eclipse.paho.version>
<google.gson.version>2.2.4</google.gson.version>
<!-- CXF version --> <cxf.version>2.6.1</cxf.version>
<!-- CXF version -->
<cxf.version>2.6.1</cxf.version>
<jackson.version>1.9.0</jackson.version> <jackson.version>1.9.0</jackson.version>
<javax.ws.rs.version>1.1.1</javax.ws.rs.version> <javax.ws.rs.version>1.1.1</javax.ws.rs.version>

@ -99,6 +99,13 @@
<scope>system</scope> <scope>system</scope>
<systemPath>${basedir}/../../../../repository/components/plugins/org.wso2.carbon.device.mgt.analytics_0.9.2.SNAPSHOT.jar</systemPath> <systemPath>${basedir}/../../../../repository/components/plugins/org.wso2.carbon.device.mgt.analytics_0.9.2.SNAPSHOT.jar</systemPath>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.common</artifactId>
<version>${carbon.device.mgt.version}</version>
<scope>system</scope>
<systemPath>${basedir}/../../../../repository/components/plugins/org.wso2.carbon.policy.mgt.common_0.9.2.SNAPSHOT.jar</systemPath>
</dependency>
<!--IOT dependencies--> <!--IOT dependencies-->

@ -412,6 +412,16 @@
<artifactId>org.wso2.carbon.device.mgt.analytics</artifactId> <artifactId>org.wso2.carbon.device.mgt.analytics</artifactId>
<version>${carbon.device.mgt.version}</version> <version>${carbon.device.mgt.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.core</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.mgt.common</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<!--IOT dependencies--> <!--IOT dependencies-->

Loading…
Cancel
Save