From 6ea6551adabdda79fe18ed9534bff8a6be7dc454 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Mon, 27 Jul 2015 17:29:52 +0530 Subject: [PATCH] fixing adding new policy not shown error --- .../jaggeryapps/iotserver/api/policy-api.jag | 4 +- .../jaggeryapps/iotserver/modules/policy.js | 65 ++++++++++--------- 2 files changed, 36 insertions(+), 33 deletions(-) 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 8dcf5808..fadc4869 100755 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag @@ -32,7 +32,7 @@ if (uriMatcher.match("/{context}/api/policies/update")) { policyModule.updatePolicyPriorities(payload); } else if (uriMatcher.match("/{context}/api/policies/add")) { var content = request.getContent(); - var id = content.policyName; + var policyName = content.policyName; var policyDefinition = content.profile.policyDefinition; var policyDescription = content.profile.policyDescription; var deviceType = content.profile.deviceType.name; @@ -40,7 +40,7 @@ if (uriMatcher.match("/{context}/api/policies/update")) { log.info("@@@@@ Policy Declaration : " + stringify(content)); try { - result = policyModule.addPolicy(id, deviceType, policyDefinition, policyDescription); + result = policyModule.addPolicy(policyName, 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. diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js index 3470266b..949455a1 100755 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js @@ -27,39 +27,42 @@ policyModule = function () { var privateMethods = {}; publicMethods.addPolicy = function (policyName, deviceType, policyDefinition, policyDescription) { - if(policyName && deviceType){ - return false; - } + if (policyName && deviceType) { + + var carbonModule = require("carbon"); + var carbonServer = application.get("carbonServer"); + var options = {system: true}; + var carbonUser = session.get(constants.USER_SESSION_KEY); + + var resource = { + name: policyName, + mediaType: 'text/plain', + content: policyDefinition, + description: policyDescription + }; + + if (carbonUser) { + options.tenantId = carbonUser.tenantId; + var registry = new carbonModule.registry.Registry(carbonServer, options); + log.info("########### Policy name : " + policyName); + log.info("########### Policy type : " + deviceType); + log.info("########### Policy Declaration : " + policyDefinition); + log.info("########### Policy policyDescription: " + policyDescription); + registry.put(constants.POLICY_REGISTRY_PATH + deviceType + "/" + policyName, resource); + } - var carbonModule = require("carbon"); - var carbonServer = application.get("carbonServer"); - var options = {system: true}; - var carbonUser = session.get(constants.USER_SESSION_KEY); + var mqttsenderClass = Packages.org.wso2.device.mgt.mqtt.policy.push.MqttPush; + var mqttsender = new mqttsenderClass(); - var resource = { - name: policyName, - mediaType: 'text/plain', - content: policyDefinition, - description: policyDescription - }; + var result = mqttsender.pushToMQTT("/iot/policymgt/govern", policyDefinition, "tcp://10.100.0.104:1883", "Raspberry-Policy-sender"); - if (carbonUser) { - options.tenantId = carbonUser.tenantId; - var registry = new carbonModule.registry.Registry(carbonServer, options); - log.info("########### Policy name : " + policyName); - log.info("########### Policy type : " + deviceType); - log.info("########### Policy Declaration : " + policyDefinition); - log.info("########### Policy policyDescription: " + policyDescription); - registry.put(constants.POLICY_REGISTRY_PATH + deviceType + "/" + policyName, resource); - } + mqttsender = null; - var mqttsenderClass = Packages.org.wso2.device.mgt.mqtt.policy.push.MqttPush; - var mqttsender = new mqttsenderClass(); + return true; - var result = mqttsender.pushToMQTT("/iot/policymgt/govern", policyDefinition, "tcp://10.100.0.104:1883", "Raspberry-Policy-sender"); - - mqttsender = null; - return true; + } else { + return false; + } }; publicMethods.getPolicies = function () { @@ -82,7 +85,7 @@ policyModule = function () { log.info("Policies for device types: " + allPolicies.content); log.info(""); var deviceType = allPolicies.content[i].replace(constants.POLICY_REGISTRY_PATH, ""); - log.info("##### deviceType:"+deviceType); + log.info("##### deviceType:" + deviceType); var deviceTypePolicies = registry.get(allPolicies.content[i]); //loop through policies @@ -96,7 +99,7 @@ policyModule = function () { //"profile": {}, // Profile "policyName": deviceTypePolicy.name, // Name of the policy. "updated": deviceTypePolicy.updated.time, - "deviceType":deviceType + "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) @@ -139,7 +142,7 @@ policyModule = function () { try { registry.remove(constants.POLICY_REGISTRY_PATH + deviceType + "/" + name); bool = true; - }catch(err){ + } catch (err) { log.error("Error while trying to remove policy :" + name); } }