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 @@

Policy creation is successful

Please click "Finish" to complete the process and go back to the policy list
- @@ -92,7 +92,7 @@
- +

@@ -103,7 +103,7 @@
- +

@@ -134,7 +134,7 @@
- +

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 index a4521326..5705de52 100755 --- 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 @@ -57,9 +57,11 @@ function savePolicy(){ profile: { profileName: policy.policyName, deviceType: { - id: policy.devicetypeId + id: policy.devicetypeId, + name: policy.devicetype }, - policyDefinition: policy.policyDefinition + policyDefinition: policy.policyDefinition, + policyDescription: policy.policyDescription } }; @@ -102,8 +104,6 @@ $(document).ready(function(){ savePolicy(); }; stepperRegistry['policy-profile'] = function (actionButton){ - var deviceType = policy.devicetype; - console.log(window.queryEditor.getValue()); policy.policyDefinition = window.queryEditor.getValue(); }; stepperRegistry['policy-devicetype'] = function (actionButton){ diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.hbs b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.hbs index 4ec0213e..c59bf826 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.hbs +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.hbs @@ -4,41 +4,48 @@

Policies

+

{{listPolicyStatus}}

+
- {{#if policies}} - {{#each policies}} - - - -
-

{{policyName}}

- {{policyName}} -
- - + - + - Invite - - - - - + Remove + - Remove - +
-
- - {{/each}} - {{else}} + {{/each}} + {{else}}

You don't have any Policies added at the moment.

+

@@ -51,7 +58,7 @@

- {{/if}} + {{/if}}
@@ -59,13 +66,114 @@ +
+
+ +
+
+ +
+
+
+
+

Policy was successfully removed.

+ + +
+
+
+
+ +
+
+
+
+

Exception at backend. Try Later.

+ + +
+
+
+
+ +
+
+
+
+

Action not permitted.

+ + +
+
+
+
+ +
+
+
+
+

Policy does not exist.

+ + +
+
+
+
+ +
+
+
+
+

Policy does not exist.

+ + +
+
+
+
+ + {{/zone}} {{#zone "common-navigation"}} {{/zone}} {{#zone "action-bar"}} - {{#if permissions.ADD_POLICY}} + {{#if permissions.ADD_POLICY}} @@ -73,7 +181,7 @@ Add Policy - {{/if}} + {{/if}} {{/zone}} {{#zone "bottomJs"}} diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.js index 238b8130..5da8aee7 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/policy-listing.js @@ -2,6 +2,7 @@ function onRequest(context) { // var log = new Log("policy-listing"); var policyModule = require("/modules/policy.js").policyModule; var allPolicies = policyModule.getPolicies(); + //log.info((allPolicies)); if (!allPolicies || allPolicies.length == 0) { context.policies = []; context.listPolicyStatus = "Oops, Sorry, No other Policies found."; @@ -10,6 +11,7 @@ function onRequest(context) { for (i = 0; i < allPolicies.length; i++) { filteredPoliciesList.push(allPolicies[i]); } + //log.info(filteredPoliciesList.length); context.policies = filteredPoliciesList; context.listPolicyStatus = "Total number of Policies found : " + filteredPoliciesList.length; } diff --git a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/public/js/policy-listing.js b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/public/js/policy-listing.js index c28963b2..4ecb748f 100644 --- a/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/public/js/policy-listing.js +++ b/modules/distribution/src/repository/jaggeryapps/iotserver/units/policy-listing/public/js/policy-listing.js @@ -5,7 +5,7 @@ $(function () { var sortableElem = '.wr-sortable'; $(sortableElem).sortable({ - beforeStop : function () { + beforeStop: function () { var sortedIDs = $(this).sortable('toArray'); console.log(sortedIDs); } @@ -22,8 +22,8 @@ var body = "body"; * set popup maximum height function. */ function setPopupMaxHeight() { - $(modalPopupContent).css('max-height', ($(body).height() - ($(body).height()/100 * 30))); - $(modalPopupContainer).css('margin-top', (-($(modalPopupContainer).height()/2))); + $(modalPopupContent).css('max-height', ($(body).height() - ($(body).height() / 100 * 30))); + $(modalPopupContainer).css('margin-top', (-($(modalPopupContainer).height() / 2))); } /* @@ -42,90 +42,74 @@ function hidePopup() { $(modalPopup).hide(); } -/** - * Following click function would execute - * when a user clicks on "Invite" link - * on User Management page in WSO2 MDM Console. - */ -$("a.invite-user-link").click(function () { - var username = $(this).data("username"); - var inviteUserAPI = "/iotserver/api/users/" + username + "/invite"; - - $(modalPopupContent).html($('#invite-user-modal-content').html()); - showPopup(); - - $("a#invite-user-yes-link").click(function () { - invokerUtil.get( - inviteUserAPI, - function () { - $(modalPopupContent).html($('#invite-user-success-content').html()); - $("a#invite-user-success-link").click(function () { - hidePopup(); - }); - }, - function () { - $(modalPopupContent).html($('#invite-user-error-content').html()); - $("a#invite-user-error-link").click(function () { - hidePopup(); - }); - } - ); - }); +$(document).ready(function () { + formatDates(); +}); - $("a#invite-user-cancel-link").click(function () { - hidePopup(); +function formatDates() { + $(".formatDate").each(function () { + var timeStamp = $(this).html(); + $(this).html(new Date(parseFloat(timeStamp)).toUTCString()); }); -}); +} /** * Following click function would execute * when a user clicks on "Remove" link * on User Management page in WSO2 MDM Console. */ -$("a.remove-user-link").click(function () { - var username = $(this).data("username"); - var removeUserAPI = "/iotserver/api/users/" + username + "/remove"; +$("a.remove-policy-link").click(function () { + var deviceType = $(this).data("devicetype"); + var policyName = $(this).data("policyname"); + var policyUUID = $(this).data("policyuuid"); + ///{context}/api/policies/{deviceType}/{policyName}/remove + var removePolicyAPI = "/iotserver/api/policies/" + deviceType + "/" + policyName + "/remove"; - $(modalPopupContent).html($('#remove-user-modal-content').html()); + $(modalPopupContent).html($('#remove-policy-modal-content').html()); showPopup(); - $("a#remove-user-yes-link").click(function () { + $("a#remove-policy-yes-link").click(function () { invokerUtil.get( - removeUserAPI, + removePolicyAPI, function (data) { - if (data == 200) { - $("#" + username).addClass("hide"); - $(modalPopupContent).html($('#remove-user-200-content').html()); - $("a#remove-user-200-link").click(function () { + if (data == 200 || data == "true") { + $(modalPopupContent).html($('#remove-policy-200-content').html()); + $('#' + policyUUID).remove(); + $("a#remove-policy-200-link").click(function () { hidePopup(); }); } else if (data == 400) { - $(modalPopupContent).html($('#remove-user-400-content').html()); - $("a#remove-user-400-link").click(function () { + $(modalPopupContent).html($('#remove-policy-400-content').html()); + $("a#remove-policy-400-link").click(function () { hidePopup(); }); } else if (data == 403) { - $(modalPopupContent).html($('#remove-user-403-content').html()); - $("a#remove-user-403-link").click(function () { + $(modalPopupContent).html($('#remove-policy-403-content').html()); + $("a#remove-policy-403-link").click(function () { + hidePopup(); + }); + } else if (data == 409 || data == "false") { + $(modalPopupContent).html($('#remove-policy-409-content').html()); + $("a#remove-policy-409-link").click(function () { hidePopup(); }); - } else if (data == 409) { - $(modalPopupContent).html($('#remove-user-409-content').html()); - $("a#remove-user-409-link").click(function () { + } else if (data == 500) { + $(modalPopupContent).html($('#remove-policy-unexpected-content').html()); + $("a#remove-policy-unexpected-link").click(function () { hidePopup(); }); } }, function () { - $(modalPopupContent).html($('#remove-user-unexpected-error-content').html()); - $("a#remove-user-unexpected-error-link").click(function () { + $(modalPopupContent).html($('#remove-policy-unexpected-error-content').html()); + $("a#remove-policy-unexpected-error-link").click(function () { hidePopup(); }); } ); }); - $("a#remove-user-cancel-link").click(function () { + $("a#remove-policy-cancel-link").click(function () { hidePopup(); }); }); \ No newline at end of file