diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag index bf46c198..8dcf5808 100755 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag @@ -28,53 +28,40 @@ var policyModule = require("/modules/policy.js").policyModule; var result; if (uriMatcher.match("/{context}/api/policies/update")) { - payload = request.getContent(); - policyModule.updatePolicyPriorities(payload); -} else if (uriMatcher.match("/{context}/api/policies/{id}/delete")) { - elements = uriMatcher.elements(); - policyId = elements.id; - try { - result = policyModule.deletePolicy(policyId); - } catch (e) { - log.error("Exception occurred while trying to delete policy for id:" + policyId, e); - // http status code 500 refers to - Internal Server Error. - result = 500; - } -}else if (uriMatcher.match("/{context}/api/policies/update")) { - payload = request.getContent(); - policyModule.updatePolicyPriorities(payload); + payload = request.getContent(); + policyModule.updatePolicyPriorities(payload); } else if (uriMatcher.match("/{context}/api/policies/add")) { - var content = request.getContent(); - var id = content.policyName; - var policyDefinition = content.profile.policyDefinition; - var deviceType = content.profile.deviceType.id; + var content = request.getContent(); + var id = content.policyName; + var policyDefinition = content.profile.policyDefinition; + var policyDescription = content.profile.policyDescription; + var deviceType = content.profile.deviceType.name; - log.info("@@@@@ Policy Declaration : "+stringify(content)); + log.info("@@@@@ Policy Declaration : " + stringify(content)); - try { - result = policyModule.addPolicy(id,deviceType,policyDefinition); - } catch (e) { - log.error("Exception occurred while trying to add new policy under name:" + id, e); - // http status code 500 refers to - Internal Server Error. - result = 500; - } -} else if (uriMatcher.match("/{context}/api/policies/get")) { - var id = request.getParameter("policyName"); - var deviceType = request.getParameter("deviceTypeId"); + try { + result = policyModule.addPolicy(id, deviceType, policyDefinition, policyDescription); + } catch (e) { + log.error("Exception occurred while trying to add new policy under name:" + id, e); + // http status code 500 refers to - Internal Server Error. + result = 500; + } +} else if (uriMatcher.match("/{context}/api/policies/{deviceType}/{policyName}/remove")) { + elements = uriMatcher.elements(); + var id = elements.policyName; + var deviceType = elements.deviceType; - log.info("@@@@@ Policy Declaration : "+stringify(content)); - - try { - result = policyModule.getPolicy(id,deviceType); - } catch (e) { - log.error("Exception occurred while trying to add new policy under name:" + id, e); - // http status code 500 refers to - Internal Server Error. - result = 500; - } + try { + result = policyModule.removePolicy(id, deviceType); + } catch (e) { + log.error("Exception occurred while trying to remove policy under name:" + id, e); + // http status code 500 refers to - Internal Server Error. + result = 500; + } } // returning the result. if (result) { - response.content = result; + response.content = result; } %> \ No newline at end of file diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/constants.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/constants.js index d0cc6283..0e68d24b 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/constants.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/constants.js @@ -21,6 +21,7 @@ var WEB_APP_CONTEXT = "/iotserver"; var USER_SESSION_KEY = "USER"; var UNSPECIFIED = "Unspecified"; var DEVICES_UNIT_PATH="/units/"; +var POLICY_REGISTRY_PATH="/_system/governance/policy_declarations/"; var DEVICE_IDENTIFIER = "deviceIdentifier"; var DEVICE_NAME = "name"; diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js index fd065d70..e828e070 100755 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js @@ -26,31 +26,33 @@ policyModule = function () { var publicMethods = {}; var privateMethods = {}; - publicMethods.addPolicy = function (name, deviceType, policyDefinition) { + publicMethods.addPolicy = function (name, deviceType, policyDefinition, policyDescription) { var carbonModule = require("carbon"); var carbonServer = application.get("carbonServer"); var options = {system: true}; var carbonUser = session.get(constants.USER_SESSION_KEY); resource = { - name : name, + name: name, mediaType: 'text/plain', - content : policyDefinition + content: policyDefinition, + description: policyDescription }; if (carbonUser) { options.tenantId = carbonUser.tenantId; var registry = new carbonModule.registry.Registry(carbonServer, options); - log.info("########### Policy name : "+name); - log.info("########### Policy type : "+deviceType); - log.info("########### Policy Declarationsss : "+policyDefinition); - registry.put("/_system/governance/policy_declarations/" + deviceType + "/" + name, resource); + log.info("########### Policy name : " + name); + log.info("########### Policy type : " + deviceType); + log.info("########### Policy Declaration : " + policyDefinition); + log.info("########### Policy policyDescription: " + policyDescription); + registry.put(constants.POLICY_REGISTRY_PATH + deviceType + "/" + name, resource); } var mqttsenderClass = Packages.org.wso2.device.mgt.mqtt.policy.push.MqttPush; var mqttsender = new mqttsenderClass(); - var result = mqttsender.pushToMQTT("/iot/policymgt/govern",policyDefinition,"tcp://10.100.0.104:1883","Raspberry-Policy-sender"); + var result = mqttsender.pushToMQTT("/iot/policymgt/govern", policyDefinition, "tcp://10.100.0.104:1883", "Raspberry-Policy-sender"); mqttsender = null; }; @@ -61,59 +63,83 @@ policyModule = function () { var options = {system: true}; var carbonUser = session.get(constants.USER_SESSION_KEY); + var policies = []; + if (carbonUser) { options.tenantId = carbonUser.tenantId; var registry = new carbonModule.registry.Registry(carbonServer, options); - log.info(registry.get("/_system/governance/policy_declarations/firealarm/")); + var allPolicies = registry.get(constants.POLICY_REGISTRY_PATH); + + if (allPolicies) { + + //loop through all device types + for (var i = 0; i < allPolicies.content.length; i++) { + log.info("Policies for device types: " + allPolicies.content); + log.info(""); + var deviceType = allPolicies.content[i].replace(constants.POLICY_REGISTRY_PATH, ""); + log.info("##### deviceType:"+deviceType); + var deviceTypePolicies = registry.get(allPolicies.content[i]); + + //loop through policies + for (var j = 0; j < deviceTypePolicies.content.length; j++) { + log.info("Policies:" + deviceTypePolicies.content); + var deviceTypePolicy = registry.get(deviceTypePolicies.content[j]); + log.info(deviceTypePolicy); + var policyObj = { + "id": deviceTypePolicy.uuid, // Identifier of the policy. + //"priorityId": 1, // Priority of the policies. This will be used only for simple evaluation. + //"profile": {}, // Profile + "policyName": deviceTypePolicy.name, // Name of the policy. + "updated": deviceTypePolicy.updated.time, + "deviceType":deviceType + //"generic": true, // If true, this should be applied to all related device. + //"roles": {}, // Roles which this policy should be applied. + //"ownershipType": {}, // Ownership type (COPE, BYOD, CPE) + //"devices": {}, // Individual devices this policy should be applied + //"users": {}, // Individual users this policy should be applied + //"Compliance": {}, + //"policyCriterias": {}, + //"startTime": 283468236, // Start time to apply the policy. + //"endTime": 283468236, // After this time policy will not be applied + //"startDate": "", // Start date to apply the policy + //"endDate": "", // After this date policy will not be applied. + //"tenantId": -1234, + //"profileId": 1 + }; + + policies.push(policyObj); + log.info(""); + }//end of policy loop + log.info(""); + }//end of device type policy loop + } } - //TODO-This method returns includes dummy policy data + return policies; - var policies = []; - var policyObj = { - "id":1, // Identifier of the policy. - "priorityId":1, // Priority of the policies. This will be used only for simple evaluation. - "profile":{}, // Profile - "policyName":"Turn off light", // Name of the policy. - "generic":true, // If true, this should be applied to all related device. - "roles":{}, // Roles which this policy should be applied. - "ownershipType":{}, // Ownership type (COPE, BYOD, CPE) - "devices":{}, // Individual devices this policy should be applied - "users":{}, // Individual users this policy should be applied - "Compliance":{}, - "policyCriterias":{}, - "startTime":283468236, // Start time to apply the policy. - "endTime":283468236, // After this time policy will not be applied - "startDate":"", // Start date to apply the policy - "endDate":"", // After this date policy will not be applied. - "tenantId":-1234, - "profileId":1 - }; + }; - policies.push(policyObj); - - policyObj = { - "id":2, // Identifier of the policy. - "priorityId":1, // Priority of the policies. This will be used only for simple evaluation. - "profile":{}, // Profile - "policyName":"Turn on Buzzer", // Name of the policy. - "generic":false, // If true, this should be applied to all related device. - "roles":{}, // Roles which this policy should be applied. - "ownershipType":{}, // Ownership type (COPE, BYOD, CPE) - "devices":{}, // Individual devices this policy should be applied - "users":{}, // Individual users this policy should be applied - "Compliance":{}, - "policyCriterias":{}, - "startTime":283468236, // Start time to apply the policy. - "endTime":283468236, // After this time policy will not be applied - "startDate":"", // Start date to apply the policy - "endDate":"", // After this date policy will not be applied. - "tenantId":-1234, - "profileId":2 - }; + publicMethods.removePolicy = function (name, deviceType) { + var carbonModule = require("carbon"); + var carbonServer = application.get("carbonServer"); + var options = {system: true}; + var carbonUser = session.get(constants.USER_SESSION_KEY); + var bool = false; - policies.push(policyObj); - return policies; + if (carbonUser) { + options.tenantId = carbonUser.tenantId; + var registry = new carbonModule.registry.Registry(carbonServer, options); + log.info("########### Policy name : " + name); + log.info("########### Policy type : " + deviceType); + try { + registry.remove(constants.POLICY_REGISTRY_PATH + deviceType + "/" + name); + bool = true; + }catch(err){ + log.error("Error while trying to remove policy :" + name); + } + } + + return bool; }; return publicMethods; diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/policy-create.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/policy-create.hbs index b7fb40b0..2b217e3f 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/policy-create.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/policy-create.hbs @@ -68,7 +68,7 @@