diff --git a/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/pom.xml b/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/pom.xml index 765e8edf..cbe014c7 100644 --- a/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/pom.xml +++ b/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/pom.xml @@ -67,9 +67,12 @@ org.w3c.dom, 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.ndatasource.core, org.wso2.carbon.device.mgt.iot.common.*, + com.google.gson.* @@ -97,6 +100,17 @@ org.wso2.carbon.logging + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.common + provided + + org.wso2.carbon.devicemgt org.wso2.carbon.device.mgt.common @@ -111,6 +125,10 @@ org.wso2.carbon.device.mgt.iot.common + + com.google.code.gson + gson + diff --git a/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/plugin/impl/FireAlarmPolicyMonitoringService.java b/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/plugin/impl/FireAlarmPolicyMonitoringService.java new file mode 100644 index 00000000..4ee496e1 --- /dev/null +++ b/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/plugin/impl/FireAlarmPolicyMonitoringService.java @@ -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 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 complianceFeatures = new ArrayList(); + + // 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; + } +} \ No newline at end of file diff --git a/modules/samples/pom.xml b/modules/samples/pom.xml index 92ccda50..2fcdc9ae 100644 --- a/modules/samples/pom.xml +++ b/modules/samples/pom.xml @@ -96,6 +96,18 @@ ${carbon.device.mgt.version} provided + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + ${carbon.device.mgt.version} + provided + + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.common + ${carbon.device.mgt.version} + provided + @@ -211,6 +223,12 @@ + + com.google.code.gson + gson + ${google.gson.version} + + @@ -424,9 +442,10 @@ 7.0.34.wso2v2 0.4.0 + 2.2.4 + - - 2.6.1 + 2.6.1 1.9.0 1.1.1 diff --git a/modules/samples/sample_pom.xml b/modules/samples/sample_pom.xml index 76e1dc33..20b641a5 100644 --- a/modules/samples/sample_pom.xml +++ b/modules/samples/sample_pom.xml @@ -99,6 +99,13 @@ system ${basedir}/../../../../repository/components/plugins/org.wso2.carbon.device.mgt.analytics_0.9.2.SNAPSHOT.jar + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.common + ${carbon.device.mgt.version} + system + ${basedir}/../../../../repository/components/plugins/org.wso2.carbon.policy.mgt.common_0.9.2.SNAPSHOT.jar + diff --git a/pom.xml b/pom.xml index cd191614..2e0e8f13 100644 --- a/pom.xml +++ b/pom.xml @@ -412,6 +412,16 @@ org.wso2.carbon.device.mgt.analytics ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.core + ${carbon.device.mgt.version} + + + org.wso2.carbon.devicemgt + org.wso2.carbon.policy.mgt.common + ${carbon.device.mgt.version} +