2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 angular.module('abs').controller('dcaeMicroServiceController', ['$scope', '$window', '$compile', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, $window, $compile, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
23 $scope.policyNavigator;
24 $scope.isCheck = false;
25 $scope.savebutton = true;
26 $scope.refreshCheck = false;
28 if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
29 $scope.temp.policy = {
30 policyType : "Config",
31 configPolicyType : "Micro Service"
35 $scope.refresh = function(){
36 if($scope.refreshCheck){
37 $scope.policyNavigator.refresh();
39 $scope.modal('createNewPolicy', true);
40 $scope.temp.policy = "";
43 $scope.modal = function(id, hide) {
44 return $('#' + id).modal(hide ? 'hide' : 'show');
47 $('#ttlDate').datepicker({
48 dateFormat: 'dd/mm/yy',
51 onSelect: function(date) {
52 angular.element($('#ttlDate')).triggerHandler('input');
56 if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly != undefined){
57 if ($scope.temp.policy.configName == undefined){
58 $scope.isCheck = false;
60 $scope.isCheck = true;
63 $scope.isCheck = false;
66 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function (data) {
68 $scope.data = JSON.parse(j.data);
69 console.log($scope.data);
70 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
71 console.log($scope.onapNameDictionaryDatas);
73 console.log("failed");
76 PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
78 $scope.data = JSON.parse(j.data);
79 console.log($scope.data);
80 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
81 console.log($scope.priorityDatas);
83 console.log("failed");
86 PolicyAppService.getData('getDictionary/get_GroupPolicyScopeDataByName').then(function (data) {
88 $scope.data = JSON.parse(j.data);
89 console.log($scope.data);
90 $scope.groupPolicyScopeListDatas = JSON.parse($scope.data.groupPolicyScopeListDatas);
91 console.log($scope.groupPolicyScopeListDatas);
93 console.log("failed");
96 PolicyAppService.getData('getDictionary/get_MicroServiceConfigNameDataByName').then(function (data) {
98 $scope.data = JSON.parse(j.data);
99 console.log($scope.data);
100 $scope.microServiceCongigNameDictionaryDatas = JSON.parse($scope.data.microServiceCongigNameDictionaryDatas);
101 console.log($scope.microServiceCongigNameDictionaryDatas);
102 }, function (error) {
103 console.log("failed");
106 PolicyAppService.getData('getDictionary/get_MicroServiceLocationDataByName').then(function (data) {
108 $scope.data = JSON.parse(j.data);
109 console.log($scope.data);
110 $scope.microServiceLocationDictionaryDatas = JSON.parse($scope.data.microServiceLocationDictionaryDatas);
111 console.log($scope.microServiceLocationDictionaryDatas);
112 }, function (error) {
113 console.log("failed");
116 PolicyAppService.getData('getDictionary/get_MicroServiceModelsDataByName').then(function (data) {
118 $scope.data = JSON.parse(j.data);
119 console.log($scope.data);
120 var inputModelList = JSON.parse($scope.data.microServiceModelsDictionaryDatas);
123 for(var i = 0; i < inputModelList.length; i++){
124 if(typeof unique[inputModelList[i]] == "undefined"){
125 unique[inputModelList[i]] = "";
126 uniqueList.push(inputModelList[i]);
129 $scope.microServiceModelsDictionaryDatas = uniqueList;
130 console.log($scope.microServiceModelsDictionaryDatas);
131 }, function (error) {
132 console.log("failed");
135 PolicyAppService.getData('getDictionary/get_DCAEUUIDDataByName').then(function (data) {
137 $scope.data = JSON.parse(j.data);
138 console.log($scope.data);
139 $scope.dcaeUUIDDictionaryDatas = JSON.parse($scope.data.dcaeUUIDDictionaryDatas);
140 console.log($scope.dcaeUUIDDictionaryDatas);
141 }, function (error) {
142 console.log("failed");
145 PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
147 $scope.data = JSON.parse(j.data);
148 console.log($scope.data);
149 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
150 console.log($scope.riskTypeDictionaryDatas);
151 }, function (error) {
152 console.log("failed");
155 PolicyAppService.getData('getDictionary/get_MicroServiceAttributeData').then(function (data) {
157 $scope.data = JSON.parse(j.data);
158 console.log($scope.data);
159 $scope.microServiceAttributeDictionaryDatas = JSON.parse($scope.data.microServiceAttributeDictionaryDatas);
160 console.log($scope.microServiceAttributeDictionaryDatas);
161 }, function (error) {
162 console.log("failed");
167 $scope.attributeDatas = [{"attributes" : $scope.choices}];
168 $scope.isInitEditTemplate = true; //just initially create the edit template, didn't click add button yet.
169 $scope.addNewChoice = function(value) {
171 if(value != undefined){
172 if (value.startsWith('div.')){
173 value = value.replace('div.','');
175 var parentElement = document.getElementById("div."+value);
176 var div = document.getElementById(value+"@0");
178 var clone = div.cloneNode(true);
179 var addElement = parentElement.childElementCount;
180 clone.id = ''+value+'@'+addElement;
182 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ //if it's view or edit
183 if($scope.temp.policy.ruleData[clone.id] || ($scope.temp.policy.editPolicy && !$scope.isInitEditTemplate)){ // Only append child if its value found in ruleData or edit mode
184 if($scope.temp.policy.ruleData[clone.id]){
185 clone.value = $scope.temp.policy.ruleData[clone.id];
187 clone.className += ' child_single'; //here cloned is single element
188 document.getElementById("div."+value).appendChild(clone);
189 plainAttributeKeys.push(''+value+'@'+addElement);
191 }else{ //not view or edit
192 clone.className += ' child_single'; //here cloned is single element
193 document.getElementById("div."+value).appendChild(clone);
194 plainAttributeKeys.push(''+value+'@'+addElement);
197 div = document.getElementById("div."+value+"@0");
199 div.className += ' children_group'; //here is div with a group of children.
201 var childElement = parentElement.firstElementChild;
202 var countParent = parentElement.childElementCount;
203 var childElementString = childElement.innerHTML;
204 var find = value+"@0";
205 var re = new RegExp(find, 'g');
206 childElementString = childElementString.replace(re,value+'@' + countParent);
207 var clone = childElement.cloneNode(true);
208 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
209 var childId = parentElement.childNodes[ii].id;
210 if(ii = parentElement.childNodes.length){
211 var childnewId = childId.slice(0, -1);
212 var count = childId.slice(-1);
215 var countvalue = parseInt(count) + 1;
216 clone.id = childnewId+countvalue;
218 clone.innerHTML=childElementString;
219 document.getElementById("div."+value).appendChild(clone);
220 var selects = clone.getElementsByTagName("select");
221 var inputs = clone.getElementsByTagName("input");
222 var removeValues = [];
223 for(var i=0; i<inputs.length; i++){
224 if ($scope.temp.policy.ruleData!=undefined){
225 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
226 if (checkValue!=undefined && checkValue != "undefined"){
227 if($scope.temp.policy.ruleData != null){
228 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
229 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
230 plainAttributeKeys.push(inputs[i].id);
233 // removeValues.push(inputs[i].id);
234 plainAttributeKeys.push(inputs[i].id);
237 plainAttributeKeys.push(inputs[i].id);
241 for(var i=0; i<selects.length; i++){
242 if ($scope.temp.policy.ruleData!=undefined){
243 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
244 if (checkValue!=undefined && checkValue!="undefined"){
245 if($scope.temp.policy.ruleData != null){
246 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
247 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
248 plainAttributeKeys.push(selects[i].id);
251 plainAttributeKeys.push(selects[i].id);
254 plainAttributeKeys.push(selects[i].id);
258 for (var k=0; k<removeValues.length; k++){
259 var elem = document.getElementById(removeValues[k]);
260 elem.parentNode.removeChild(elem);
262 var ele = angular.element(document.getElementById("div."+value));
263 $compile(ele.contents())($scope);
269 $scope.removeChoice = function(value) {
271 if(value != undefined){
272 var c = document.getElementById("div."+value).childElementCount;
275 Notification.error("The original one is not removable.");
278 document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);
282 $scope.pullVersion = function(serviceName) {
283 console.log(serviceName);
284 if(serviceName != undefined){
285 var uuu = "policyController/getModelServiceVersioneData.htm";
286 var postData={policyData: serviceName};
291 contentType: 'application/json',
292 data: JSON.stringify(postData),
293 success : function(data){
294 $scope.$apply(function(){
295 $scope.microServiceModelsDictionaryVersionDatas = data[0].dcaeModelVersionData;
298 error : function(data){
299 alert("Error While Retriving the Template Layout Pattren.");
306 var splitEqual = '=';
307 var splitComma = ',';
308 var splitcolon = ':';
309 var splitsemicolon = ";";
310 var splitEnum = "],";
311 var plainAttributeKeys = [];
313 var enumKeyList = [];
314 var dictionaryList = [];
315 var dictionaryNameList = [];
316 $scope.addDataToFields = function(serviceName, version){
317 if(serviceName != null && version !=null){
318 var service=serviceName+"-v"+version;
319 var myNode = document.getElementById("DynamicTemplate");
320 myNode.innerHTML = '';
321 var uuu = "policyController/getDCAEMSTemplateData.htm";
322 var postData={policyData: service};
327 contentType: 'application/json',
328 data: JSON.stringify(postData),
329 success : function(data){
330 $scope.$apply(function(){
331 $scope.addNewChoice();
332 var plainAttributeKeys = [];
333 $scope.dcaeModelData = data[0].dcaeModelData;
334 $scope.dcaeJsonDate = data[0].jsonValue;
335 if(data[0].allManyTrueKeys){
336 console.log("$scope.allManyTrueKeys: " + $scope.allManyTrueKeys);
338 console.log("$scope.dcaeJsonDate: " + $scope.dcaeJsonDate);
339 var attributes = $scope.dcaeModelData.attributes;
340 var refAttributes = $scope.dcaeModelData.ref_attributes;
341 var subAttributes = $scope.dcaeModelData.sub_attributes;
342 console.log("subAttributes: " + subAttributes);
343 console.log("refAttributes: " + refAttributes);
344 var enumAttributes = $scope.dcaeModelData.enumValues;
345 var annotation = $scope.dcaeModelData.annotation;
346 var dictionary = $scope.microServiceAttributeDictionaryDatas;
348 if (annotation == null || annotation.length<1){
349 $scope.isCheck = true;
351 $scope.isCheck = false;
352 var annoationList = annotation.split(splitComma);
353 for (k = 0; k < annoationList.length; k++){
354 var splitAnnotation = annoationList[k].split(splitEqual);
355 if (splitAnnotation[1].includes("matching-true")){
356 matching.push(splitAnnotation[0].trim());
362 if (dictionary!= null && dictionary.length>1){
363 for (m=0; m < dictionary.length; m += 1){
364 var keyCompare = dictionary[m].name;
365 var valueCompare = dictionary[m].value;
366 var valueModel = dictionary[m].modelName;
367 var conpairService = serviceName;
368 if (valueModel.includes('-v')){
369 conpairService = service;
371 if(valueModel.localeCompare(conpairService) == 0){
372 console.log(valueCompare);
373 dictionaryList.push(dictionary[m]);
374 if (!dictionaryNameList.includes(dictionary[m].name)){
375 dictionaryNameList.push(dictionary[m].name)
381 $scope.temp.policy.ruleGridData = [];
383 $scope.jsonLayout($scope.dcaeJsonDate);
387 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ // If it's veiw or edit
389 if($scope.temp.policy.editPolicy){
390 $scope.isInitEditTemplate = true;
395 // If ruleData contains extra elements created by clicked add button
396 if($scope.temp.policy.ruleData != null){
397 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
398 propNames.forEach(function(name) {
402 var extraElements = data;
404 if(plainAttributeKeys != null){
405 for(b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
406 var newValue = plainAttributeKeys[b].split("*");
407 for(a = 0; a < data.length; a++){
408 if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
409 extraElements.splice(a, 1);
414 //--- Populate these extra elements created by clicked add button
415 for(a = 0; a < extraElements.length; a++){
416 if(extraElements[a].includes("@")){
417 var index = extraElements[a].lastIndexOf("@");
419 // Get the number after @
420 var n = getNumOfDigits(extraElements[a], index+1);
422 var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
423 console.log("key: " + key);
428 var unique = checkData.filter(onlyUnique);
429 for(i =0; i < unique.length; i++){
430 //remove @x and let addNewChoice add @1 or @2...
431 //var newKey = unique[i].substring(0, unique[i].length-2);
432 var index = unique[i].lastIndexOf("@");
433 var newKey = unique[i].substring(0, index);
434 console.log("newKey: " + newKey);
435 $scope.addNewChoice(newKey);
439 //After initially create the edit template, reset it to false.
440 $scope.isInitEditTemplate = false;
442 var ele = angular.element(document.getElementById("DynamicTemplate"));
443 $compile(ele.contents())($scope);
446 error : function(data){
447 alert("Error While Retriving the Template Layout Pattren.");
453 function getNumOfDigits(str_value, index){
454 // Get the number after @
455 var str = str_value.substring(index, str_value.length);
458 for (var x = 0; x < str.length; x++){
469 function getDictionary(attribute){
470 var dicName = attribute;
472 if(attribute.includes(":")){
473 dicName = attribute.split(":")[0];
476 var dictionaryRegExp = new RegExp(dicName);
477 listemunerateValues = [];
478 if (dictionaryRegExp.test(dictionaryNameList)) {
479 for (p=0; p < dictionaryList.length; p += 1) {
480 if (dicName == dictionaryList[p].name) {
481 listemunerateValues.push(dictionaryList[p].value);
485 return listemunerateValues;
488 function getList(attribute) {
489 var enumName = attribute;
490 console.log("In getList: attribute => " + attribute);
492 if(attribute.includes(":")){
493 enumName = attribute.split(":")[0];
496 var baseEnum = $scope.dcaeModelData.enumValues;
498 if(baseEnum != null){
499 enumList = baseEnum.split(splitEnum);
502 var patternTest = new RegExp(enumName);
503 for (k=0; k < enumList.length; k += 1){
504 if(patternTest.test(enumList[k]) == true){
505 enumAttributes = enumList[k].trim();
510 enumAttributes = enumAttributes.replace("[", "");
511 enumAttributes = enumAttributes.replace("]", "");
512 enumAttributes = enumAttributes.replace(/ /g, '');
513 var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
514 listemunerateValues = dropListAfterCommaSplit[1].split(splitComma);
515 //enumKeyList.push(attribute);
516 return listemunerateValues;
522 function getBooleanList(){
523 var booleanList = [];
524 booleanList.push(true);
525 booleanList.push(false);
530 function isArray(arrayTest) {
531 return Object.prototype.toString.call(arrayTest) === '[object Array]';
534 function deconstructJSON(dataTest, level , name) {
537 var stringValue = "java.lang.String";
538 var string = "string";
539 var intValue = "int";
540 var integerValue = "integer";
541 var double = "double";
542 var boolean = "boolean";
543 var baseLevel = level;
545 if (name.length > 1){
546 label = label + name + '.';
549 for (key in dataTest) {
550 array = isArray(dataTest[key]);
551 console.log(key , dataTest[key]);
553 if (!!dataTest[key] && typeof(dataTest[key])=="object") {
554 if (array==false && key!=="0"){
555 $scope.labelLayout(label, key, array );
558 if (array == true && key!=0){
562 if (lableList.length > 0){
566 var newKey = lableList.pop();
567 $scope.labelLayout(baseLevel, newKey, array );
569 label = baseLevel + newKey + '@0.';
571 label = baseLevel + newKey + '.';
574 deconstructJSON(dataTest[key] , label, key);
576 var attirbuteLabel = label;
578 var isRequired = false;
579 if (dataTest[key].includes('defaultValue-')){
580 defaultValue = dataTest[key].split('defaultValue-')[1];
585 attributekey = lableList.pop();
586 attirbuteLabel = baseLevel;
588 attributekey = key.split();
591 if (dataTest[key].includes('required-true')){
595 var subAttributes = $scope.dcaeModelData.sub_attributes;
598 var jsonObject = JSON.parse(subAttributes);
599 var allkeys = Object.keys(jsonObject);
601 for (var k = 0; k < allkeys.length; k++) {
602 var keyValue = allkeys[k];
603 console.log(" keyValue:jsonObject["+keyValue+ "]: " + jsonObject[keyValue]);
604 if(jsonObject[keyValue]){
605 var tempObject = jsonObject[keyValue];
606 if(tempObject && tempObject[key]){
607 if (tempObject[key].includes('required-true')){
616 switch (dataTest[key].split(splitcolon)[0]){
618 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
621 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
624 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
627 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
630 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "double");
633 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], getBooleanList());
636 if (dataTest[key].includes('dictionary-')){
637 var list = getDictionary(dataTest[key].split('dictionary-')[1]);
639 //--- get dropdown values from enumValues
640 var list = getList(dataTest[key]);
642 if (list.length===0){ //not dropdown element
643 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
645 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], list, isRequired);
653 $scope.jsonLayout = function(dataTest){
654 deconstructJSON(dataTest , "", "");
658 $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired, dataType ){
659 $scope.temp.policy.ruleGridData.push(attibuteKey);
660 var br = document.createElement("BR");
662 var label = document.createElement("Label");
663 var labeltext = null;
664 var requiredLabName = "";
665 if (matching.includes(attibuteKey)){
666 labeltext = document.createTextNode(attibuteKey + "*!");
667 isRequired = true; //set required as true for matching element
670 requiredLabName = attibuteKey + " *";
671 labeltext = document.createTextNode(requiredLabName);
673 labeltext = document.createTextNode(attibuteKey);
678 var divID = labelValue;
680 if (labelValue.length < 1){
681 divID = "DynamicTemplate";
682 }else if (labelValue.endsWith('.')){
683 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
686 label.appendChild(labeltext);
688 var textField = document.createElement("INPUT");
690 textField.setAttribute("class" , "form-control");
692 if(dataType == "double"){
693 textField.setAttribute("type" , "number");
694 textField.setAttribute("step" , "any");
696 textField.setAttribute("type" , dataType);
699 textField.setAttribute("style" , "width:300px;");
700 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
702 if(attributeManyKey){
703 checkKey = labelValue + attibuteKey+'@0';
704 textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
705 var divTag = document.createElement("div");
706 divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
707 var addButton = document.createElement("BUTTON");
708 var buttonaddLabel = document.createTextNode("+");
709 addButton.appendChild(buttonaddLabel);
710 addButton.setAttribute("id", labelValue + attibuteKey);
711 addButton.setAttribute("class", "btn btn-add-remove");
712 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + attibuteKey+'");');
713 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
714 var removeButton = document.createElement("BUTTON");
715 var buttonremoveLabel = document.createTextNode("-");
716 removeButton.appendChild(buttonremoveLabel);
717 removeButton.setAttribute("class", "btn btn-add-remove");
718 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue + attibuteKey+'");');
719 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
720 document.getElementById(divID).appendChild(addButton);
721 document.getElementById(divID).appendChild(removeButton);
722 document.getElementById(divID).appendChild(label);
723 var id = "div."+labelValue+attibuteKey;
724 var divTag = document.createElement("div");
725 divTag.setAttribute("id", id);
726 document.getElementById(divID).appendChild(divTag);
727 textField.className += ' first_child';
729 textField.setAttribute("required", "true");
731 divTag.appendChild(textField);
732 document.getElementById(divID).appendChild(divTag);
735 checkKey = labelValue + attibuteKey;
736 textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
737 if(requiredLabName.includes("*")){
738 textField.setAttribute("required", "true");
740 document.getElementById(divID).appendChild(label);
741 document.getElementById(divID).appendChild(textField);
742 document.getElementById(divID).appendChild(br);
746 if(divID.includes("@0") && divID.includes("div.")){
747 var firstChild_Id = divID.split("@0")[0];
748 var firstChild_element = document.getElementById(firstChild_Id);
749 if(firstChild_element){
750 firstChild_element.className += ' children_group'; //here is a div with a group of children.
753 console.log('firstChild_Id: ' + firstChild_Id);
754 console.log('divID: ' + divID);
756 if (defaultValue.length > 0){
757 if(defaultValue.includes(":")){
758 defaultValue = defaultValue.split(":")[0];
759 if(defaultValue === "NA") {
763 if(defaultValue != "undefined" && defaultValue != undefined){
764 document.getElementById(checkKey).value = defaultValue;
768 if($scope.temp.policy.ruleData != null){
769 //document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
770 if (attributeManyKey){
771 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
772 if($scope.temp.policy.ruleData[newCheckKey +'@0'] != undefined && $scope.temp.policy.ruleData[newCheckKey +'@0'] != "undefined"){
773 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
776 if($scope.temp.policy.ruleData[checkKey] != undefined && $scope.temp.policy.ruleData[checkKey] != "undefined"){
777 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
781 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey);
784 $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
785 var label = document.createElement("Label")
786 var divID = labelValue;
787 if (labelValue.endsWith('.')){
788 var workingLabel = labelValue.substring(0, labelValue.length-1);
790 var workingLabel = labelValue;
793 if (labelValue.length < 1){
794 divID = "DynamicTemplate";
795 } else if (labelValue.endsWith('.')){
796 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
799 var labeltext = document.createTextNode(lableName);
801 label.appendChild(labeltext);
803 var subAttributes = $scope.dcaeModelData.sub_attributes;
806 var addButton = document.createElement("BUTTON");
807 var buttonLabel = document.createTextNode("+");
808 addButton.appendChild(buttonLabel);
809 addButton.setAttribute("class", "btn btn-add-remove");
810 addButton.setAttribute("ng-click" , 'addNewChoice("'+labelValue + lableName+'");');
811 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
812 var removeButton = document.createElement("BUTTON");
813 var buttonremoveLabel = document.createTextNode("-");
814 removeButton.appendChild(buttonremoveLabel);
815 removeButton.setAttribute("class", "btn btn-add-remove");
816 removeButton.setAttribute("ng-click" , 'removeChoice("'+labelValue +lableName+'");');
817 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
818 document.getElementById(divID).appendChild(addButton);
819 document.getElementById(divID).appendChild(removeButton);
820 document.getElementById(divID).appendChild(label);
821 var id = "div."+labelValue+lableName;
822 var divTag = document.createElement("div");
823 divTag.setAttribute("id", id);
824 document.getElementById(divID).appendChild(divTag);
826 var divTag = document.createElement("div");
827 divTag.setAttribute("id", id +'@0');
829 divTag.className += ' children_group'; //here is div with a group of children.
831 document.getElementById(id).appendChild(divTag);
833 var divTag = document.createElement("div");
834 divTag.setAttribute("id", "div."+labelValue+lableName);
835 divTag.className += ' children_group'; //here is div with a group of children.
836 document.getElementById(divID).appendChild(label);
837 document.getElementById(divID).appendChild(divTag);
841 $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues, isRequired){
842 var br = document.createElement("BR");
844 if (labelLevel.length < 1){
845 var divID = "DynamicTemplate";
846 } else if (labelLevel.endsWith('.')){
847 var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
851 var label = document.createElement("Label")
853 var refAttributes = $scope.dcaeModelData.ref_attributes;
854 if(isRequired != true && refAttributes){ //check refAttributes also
855 var refAttributesList = refAttributes.split(splitComma);
856 for (k = 0; k < refAttributesList.length; k++){
857 var refAttribute = refAttributesList[k].split(splitEqual);
858 if (attributeName == refAttribute[0].trim() && refAttribute[1].includes("required-true")){
864 if (matching.includes(attributeName)){
865 var labeltext = document.createTextNode(attributeName + "*!");
866 label.appendChild(labeltext);
867 isRequired = true; //set required as true for matching element
869 var labeltext = document.createTextNode(attributeName);
871 requiredLabName = attributeName+ " *";
872 labeltext = document.createTextNode(requiredLabName);
874 labeltext = document.createTextNode(attributeName);
877 label.appendChild(labeltext);
879 label.appendChild(labeltext);
881 var listField = document.createElement("SELECT");
882 listField.setAttribute("class" , "form-control");
883 listField.setAttribute("style" , "width:300px;");
884 listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
887 listField.setAttribute("required", true);
889 if( many != true || isRequired != true){ // add an empty option for not required or not multiple select element
890 var optionFirst = document.createElement('option');
891 optionFirst.setAttribute('value', "");
892 listField.appendChild(optionFirst);
895 for (i=0; i < listemunerateValues.length; i += 1) {
896 option = document.createElement('option');
897 option.setAttribute('value', listemunerateValues[i]);
898 option.appendChild(document.createTextNode(listemunerateValues[i]));
899 option.setAttribute('value', listemunerateValues[i]);
900 listField.appendChild(option);
902 listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
904 enumKeyList.push(attributeName);
906 document.getElementById(divID).appendChild(label);
907 document.getElementById(divID).appendChild(br);
910 document.getElementById(divID).appendChild(listField).multiple = true;
911 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
913 document.getElementById(divID).appendChild(listField).multiple = false;
914 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
917 if($scope.temp.policy.ruleData != null){
919 document.getElementById(labelLevel +attributeName).options[0].selected = false;
920 for (i=0; i < listemunerateValues.length; i += 1) {
921 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
922 if (testValue === undefined){
923 testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
925 var location = listemunerateValues.indexOf(testValue);
927 document.getElementById(labelLevel +attributeName).options[location].selected = true;
931 if($scope.temp.policy.ruleData[labelLevel + attributeName] != undefined && $scope.temp.policy.ruleData[labelLevel + attributeName] != "undefined"){
932 document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];
938 function onlyUnique(value, index, self) {
939 return self.indexOf(value) === index;
943 function checkDictionary(value){
944 for (i = 0; i < $scope.microServiceAttributeDictionaryDatas.length; i++) {
945 if ($scope.microServiceAttributeDictionaryDatas[i].name.localeCompare(value)){
951 $scope.savePolicy = function(policy){
952 if(policy.itemContent != undefined){
953 $scope.refreshCheck = true;
954 $scope.policyNavigator = policy.itemContent;
955 policy.itemContent = "";
957 $scope.savebutton = false;
961 if(plainAttributeKeys != null){
962 for(a = 0; a < plainAttributeKeys.length; a++){
963 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
964 console.log("splitPlainAttributeKey: " + splitPlainAttributeKey);
965 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
966 var key = splitPlainAttributeKey[0];
967 if(searchElement == null){
968 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
969 key = splitPlainAttributeKey[0]+'@0';
970 }else if (searchElement.nodeName == 'BUTTON'){
971 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
972 key = splitPlainAttributeKey[0]+'@0';
974 if(searchElement != null){
975 var keySplit = key.split(dot);
976 var elumentLocation = keySplit.length;
978 if (elumentLocation > 1){
979 enumKey = keySplit[keySplit.length - 1];
981 //check it is undefined or not
982 if (enumKeyList != undefined && enumKeyList.indexOf(enumKey) != -1){
983 if (splitPlainAttributeKey[1]!= undefined && splitPlainAttributeKey[1].indexOf("true") !== -1){
985 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
986 multiSlect.push(searchElement.selectedOptions[i].value);
988 jsonPolicy[key]= multiSlect;
990 console.log(" searchElement.value = > " + searchElement.value);
991 jsonPolicy[key]= searchElement.value;
994 if(searchElement.value != null){
995 console.log(" searchElement.value = > " + searchElement.value);
996 jsonPolicy[key]= searchElement.value;
1002 var uuu = "policycreation/save_policy";
1003 var postData={policyData: policy, policyJSON : jsonPolicy};
1008 contentType: 'application/json',
1009 data: JSON.stringify(postData),
1010 success : function(data){
1011 $scope.$apply(function(){
1012 $scope.data=data.policyData;
1013 if($scope.data == 'success'){
1014 $scope.temp.policy.readOnly = 'true';
1015 $scope.safetyChecker = data.policyData.split("#")[2];
1016 if ($scope.safetyChecker!=undefined) {
1017 Notification.success($scope.safetyChecker);
1019 $scope.pushStatus=data.policyData.split("&")[1];
1020 if($scope.pushStatus=="successPush"){
1021 Notification.success("Policy pushed successfully");
1023 Notification.success("Policy Saved Successfully.");
1024 }else if ($scope.data == 'PolicyExists'){
1025 $scope.savebutton = true;
1026 Notification.error("Policy Already Exists with Same Name in Scope.");
1029 console.log($scope.data);
1031 error : function(data){
1032 Notification.error("Error Occured while saving Policy.");
1037 $scope.validatePolicy = function(policy){
1038 document.getElementById("validate").innerHTML = "";
1041 var jsonPolicy = {};
1042 if(plainAttributeKeys != null){
1043 for(a = 0; a < plainAttributeKeys.length; a++){
1044 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1045 console.log(splitPlainAttributeKey[1]);
1046 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1047 var key = splitPlainAttributeKey[0];
1048 if(searchElement == null){
1049 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1050 key = splitPlainAttributeKey[0]+'@0';
1051 }else if (searchElement.nodeName == 'BUTTON'){
1052 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1053 key = splitPlainAttributeKey[0]+'@0';
1055 if(searchElement != null){
1056 if (enumKeyList.indexOf(key) != -1){
1057 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
1058 var multiSlect = [];
1059 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1060 multiSlect.push(searchElement.selectedOptions[i].value);
1062 jsonPolicy[key]= multiSlect;
1064 jsonPolicy[key]= searchElement.value;
1066 if(searchElement.getAttribute("required")){
1067 if(!searchElement.value){
1072 if(searchElement.value != null){
1073 jsonPolicy[key]= searchElement.value;
1074 if(searchElement.getAttribute("required")){
1075 if(!searchElement.value){
1084 var uuu = "policyController/validate_policy.htm";
1086 console.log("$scope.isCheck:" + $scope.isCheck);
1088 if($scope.isCheck == true){
1089 if(("configName" in policy) == false){
1090 Notification.error("Validation Failed: configName is required");
1091 $scope.savebutton = true;
1094 if(("location" in policy) == false){
1095 Notification.error("Validation Failed: location is required");
1096 $scope.savebutton = true;
1099 if(("uuid" in policy) == false){
1100 Notification.error("Validation Failed: uuid is required");
1101 $scope.savebutton = true;
1104 if(("policyScope" in policy) == false){
1105 Notification.error("Validation Failed: policyScope is required");
1106 $scope.savebutton = true;
1111 var postData={policyData: policy, policyJSON : jsonPolicy};
1116 contentType: 'application/json',
1117 data: JSON.stringify(postData),
1118 success : function(data){
1119 $scope.$apply(function(){
1120 $scope.validateData = data.data.replace(/\"/g, "");
1121 $scope.data=data.data.substring(1,8);
1122 var size = data.data.length;
1123 if($scope.data == 'success'){
1124 Notification.success("Validation Success.");
1125 $scope.savebutton = false;
1127 var displayWarning = data.data.substring(19,size - 1);
1128 document.getElementById("validate").innerHTML = "Safe Policy Warning Message : "+displayWarning;
1129 document.getElementById("validate").style.color = "white";
1130 document.getElementById("validate").style.backgroundColor = "skyblue";
1133 Notification.error("Validation Failed.");
1134 document.getElementById("validate").innerHTML = $scope.validateData;
1135 document.getElementById("validate").style.color = "white";
1136 document.getElementById("validate").style.backgroundColor = "red";
1137 $scope.savebutton = true;
1141 console.log($scope.data);
1143 error : function(data){
1144 Notification.error("Validation Failed.");
1145 $scope.savebutton = true;
1150 function extend(obj, src) {
1151 for (var key in src) {
1152 if (src.hasOwnProperty(key)) obj[key] = src[key];