adding policies

merge-requests/1/head
Rasika Perera 9 years ago
parent 2b9ebf1e2f
commit 43a756d851

@ -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;
}
%>

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

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

@ -68,7 +68,7 @@
<h1 class="wr-title">Policy creation is successful</h1>
Please click <b>"Finish"</b> to complete the process and go back to the policy list
<hr />
<button class="wr-btn wizard-stepper" data-current="policy-message" data-direct="/mdm/policies/" onclick="window.location.href=''">
<button class="wr-btn wizard-stepper" data-current="policy-message" data-direct="/iotserver/policies/" onclick="window.location.href='/iotserver/policies/'">
&nbsp;&nbsp;&nbsp;&nbsp;Finish&nbsp;&nbsp;&nbsp;&nbsp;
</button>
@ -92,7 +92,7 @@
</label>
<div class="wr-input-control">
<div class="cus-col-50">
<input id="policy-name-input" type="text" value="" placeholder="input text"/>
<input id="policy-name-input" type="text" value="" placeholder="Policy name here" required/>
</div>
<br class="c-both" />
</div>
@ -103,7 +103,7 @@
</label>
<div class="wr-input-control">
<div class="cus-col-50">
<textarea id="policy-description-input" placeholder="description"></textarea>
<textarea id="policy-description-input" placeholder="Policy description here" required></textarea>
</div>
<br class="c-both" />
</div>
@ -134,7 +134,7 @@
</label>
<div class="wr-input-control">
<div class="cus-col-100">
<textarea id="policy-definition-input" placeholder="Enter the policy" style="width: 100%;"></textarea>
<textarea id="policy-definition-input" placeholder="Enter the policy" style="width: 100%;" required></textarea>
</div>
<br class="c-both" />
</div>

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

@ -4,41 +4,48 @@
<!-- content -->
<div class="wr-content">
<p class="page-sub-title">Policies</p>
<p>{{listPolicyStatus}}</p>
<div class="wr-list-group wr-sortable">
{{#if policies}}
{{#each policies}}
<span id="{{id}}" class="list-group-item">
<i class="wr-sortable-icon fw fw-sort hide"></i>
<i class="wr-list-icon fw fw-user"></i>
<div class="wr-list-desc">
<h3 class="wr-list-name">{{policyName}}</h3>
<span class="wr-list-username">{{policyName}}</span>
</div>
<span class="list-group-item-actions">
<!--a href="#" class="invite-user-link cu-btn-inner" data-policyname="{{policyName}}">
{{#if policies}}
{{#each policies}}
<span id="{{id}}" class="list-group-item">
<i class="wr-sortable-icon fw fw-sort hide"></i>
<i class="wr-list-icon fw fw-user"></i>
<div class="wr-list-desc">
<h3 class="wr-list-name">{{policyName}}</h3>
<span class="wr-list-username">{{deviceType}} | Last Updated: <span
class="formatDate">{{updated}}</span></span>
</div>
<span class="list-group-item-actions">
<!--a href="#" class="invite-user-link cu-btn-inner" data-policyname="{{policyName}}">
<span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i>
<i class="fw fw-invitation fw-stack-1x"></i>
</span>
Invite
</a-->
<a href="#" class="remove-policy-link cu-btn-inner"
data-devicetype="{{deviceType}}"
data-policyname="{{policyName}}"
data-policyuuid="{{id}}">
<span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i>
<i class="fw fw-invitation fw-stack-1x"></i>
<i class="fw fw-delete fw-stack-1x"></i>
</span>
Invite
</a-->
<a href="#" class="remove-user-link cu-btn-inner" data-policyname="{{policyName}}">
<span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i>
<i class="fw fw-delete fw-stack-1x"></i>
Remove
</a>
</span>
Remove
</a>
<div class="clearfix"></div>
</span>
<div class="clearfix"></div>
</span>
{{/each}}
{{else}}
{{/each}}
{{else}}
<!-- no policies found -->
<div class="container-fluid wr-content-alt">
<div class="ctrl-info-panel col-md-6 col-centered">
<h2>You don't have any Policies added at the moment.</h2>
<p>
</a><a href="/iotserver/policies/add-policy" class="cu-btn">
<span class="fw-stack">
@ -51,7 +58,7 @@
</div>
</div>
<!-- /no policies found -->
{{/if}}
{{/if}}
</div>
</div>
<!-- /content -->
@ -59,13 +66,114 @@
</div>
<div id="remove-policy-modal-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Do you really want to remove this policy from IoT Server?</h3>
<div class="buttons">
<a href="#" id="remove-policy-yes-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Yes&nbsp;&nbsp;&nbsp;&nbsp;
</a>
&nbsp;&nbsp;
<a href="#" id="remove-policy-cancel-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Cancel&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-200-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy was successfully removed.</h3>
<div class="buttons">
<a href="#" id="remove-policy-200-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-400-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Exception at backend. Try Later.</h3>
<div class="buttons">
<a href="#" id="remove-policy-400-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-403-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Action not permitted.</h3>
<div class="buttons">
<a href="#" id="remove-policy-403-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-409-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy does not exist.</h3>
<div class="buttons">
<a href="#" id="remove-policy-409-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
<div id="remove-policy-unexpected-error-content" class="hide">
<div class="content">
<div class="row">
<div class="col-lg-5 col-md-6 col-centered">
<h3>Policy does not exist.</h3>
<div class="buttons">
<a href="#" id="remove-policy-unexpected-error-link" class="btn-operations">
&nbsp;&nbsp;&nbsp;&nbsp;Ok&nbsp;&nbsp;&nbsp;&nbsp;
</a>
</div>
</div>
</div>
</div>
</div>
{{/zone}}
{{#zone "common-navigation"}}
<!--Later add the navigation menu from here-->
{{/zone}}
{{#zone "action-bar"}}
{{#if permissions.ADD_POLICY}}
{{#if permissions.ADD_POLICY}}
<a href="/iotserver/policies/add-policy" class="cu-btn">
<span class="fw-stack">
<i class="fw fw-ring fw-stack-2x"></i>
@ -73,7 +181,7 @@
</span>
Add Policy
</a>
{{/if}}
{{/if}}
{{/zone}}
{{#zone "bottomJs"}}
<script src="{{self.publicURL}}/js/policy-listing.js"></script>

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

@ -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();
});
});
Loading…
Cancel
Save