From c7b3161231d736de5288311d161304a7e25b1602 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Mon, 27 Jul 2015 10:42:23 +0530 Subject: [PATCH] adding policies --- .../jaggeryapps/iotserver/api/policy-api.jag | 31 ++++++++++++++++ .../jaggeryapps/iotserver/modules/policy.js | 35 +++++++++++++++++++ .../policy-create/public/js/policy-create.js | 15 ++++---- 3 files changed, 73 insertions(+), 8 deletions(-) mode change 100644 => 100755 modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag mode change 100644 => 100755 modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js mode change 100644 => 100755 modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/public/js/policy-create.js diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag b/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag old mode 100644 new mode 100755 index 5615c877..bf46c198 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/api/policy-api.jag @@ -40,6 +40,37 @@ if (uriMatcher.match("/{context}/api/policies/update")) { // 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); +} 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; + + 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"); + + 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; + } } // returning the result. diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js old mode 100644 new mode 100755 index c7501239..fd065d70 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/modules/policy.js @@ -27,10 +27,45 @@ policyModule = function () { var privateMethods = {}; publicMethods.addPolicy = function (name, deviceType, policyDefinition) { + var carbonModule = require("carbon"); + var carbonServer = application.get("carbonServer"); + var options = {system: true}; + var carbonUser = session.get(constants.USER_SESSION_KEY); + resource = { + name : name, + mediaType: 'text/plain', + content : policyDefinition + }; + + 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); + } + + 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"); + + mqttsender = null; }; publicMethods.getPolicies = function () { + var carbonModule = require("carbon"); + var carbonServer = application.get("carbonServer"); + var options = {system: true}; + var carbonUser = session.get(constants.USER_SESSION_KEY); + + if (carbonUser) { + options.tenantId = carbonUser.tenantId; + var registry = new carbonModule.registry.Registry(carbonServer, options); + log.info(registry.get("/_system/governance/policy_declarations/firealarm/")); + } //TODO-This method returns includes dummy policy data diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/public/js/policy-create.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/public/js/policy-create.js old mode 100644 new mode 100755 index f4e75296..a4521326 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/public/js/policy-create.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-create/public/js/policy-create.js @@ -63,14 +63,12 @@ function savePolicy(){ } }; - console.log(payload); + invokerUtil.post("/iotserver/api/policies/add", payload, function(){ + $(".policy-message").removeClass("hidden"); + $(".add-policy").addClass("hidden"); + }, function(){ - //invokerUtil.post("/iotserver/policy-api/policy/add", payload, function(){ - // $(".policy-message").removeClass("hidden"); - // $(".add-policy").addClass("hidden"); - //}, function(){ - // - //}); + }); } $(document).ready(function(){ @@ -105,7 +103,8 @@ $(document).ready(function(){ }; stepperRegistry['policy-profile'] = function (actionButton){ var deviceType = policy.devicetype; - policy.policyDefinition = $("#policy-definition-input").val(); + console.log(window.queryEditor.getValue()); + policy.policyDefinition = window.queryEditor.getValue(); }; stepperRegistry['policy-devicetype'] = function (actionButton){ policy.devicetype = $(actionButton).data("devicetype");