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;
30 if(!$scope.temp.policy.editPolicy && !$scope.temp.policy.readOnly){
31 $scope.temp.policy = {
32 policyType : "Config",
33 configPolicyType : "Micro Service"
37 $scope.refresh = function(){
38 if($scope.refreshCheck){
39 $scope.policyNavigator.refresh();
41 $scope.modal('createNewPolicy', true);
42 $scope.temp.policy = "";
45 $scope.modal = function(id, hide) {
46 return $('#' + id).modal(hide ? 'hide' : 'show');
49 $('#ttlDate').datepicker({
50 dateFormat: 'dd/mm/yy',
53 onSelect: function(date) {
54 angular.element($('#ttlDate')).triggerHandler('input');
58 if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly != undefined){
59 if ($scope.temp.policy.configName == undefined){
60 $scope.isCheck = false;
62 $scope.isCheck = true;
65 $scope.isCheck = false;
68 PolicyAppService.getData('getDictionary/get_OnapNameDataByName').then(function (data) {
70 $scope.data = JSON.parse(j.data);
71 console.log($scope.data);
72 $scope.onapNameDictionaryDatas = JSON.parse($scope.data.onapNameDictionaryDatas);
73 console.log($scope.onapNameDictionaryDatas);
75 console.log("failed");
78 PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
80 $scope.data = JSON.parse(j.data);
81 console.log($scope.data);
82 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
83 console.log($scope.priorityDatas);
85 console.log("failed");
88 PolicyAppService.getData('getDictionary/get_GroupPolicyScopeDataByName').then(function (data) {
90 $scope.data = JSON.parse(j.data);
91 console.log($scope.data);
92 $scope.groupPolicyScopeListDatas = JSON.parse($scope.data.groupPolicyScopeListDatas);
93 console.log($scope.groupPolicyScopeListDatas);
95 console.log("failed");
98 PolicyAppService.getData('getDictionary/get_MicroServiceConfigNameDataByName').then(function (data) {
100 $scope.data = JSON.parse(j.data);
101 console.log($scope.data);
102 console.log("$scope.data.microServiceCongigNameDictionaryDatas : " + $scope.data.microServiceCongigNameDictionaryDatas);
103 if($scope.data.microServiceCongigNameDictionaryDatas){
104 $scope.microServiceCongigNameDictionaryDatas = JSON.parse($scope.data.microServiceCongigNameDictionaryDatas);
106 console.log($scope.microServiceCongigNameDictionaryDatas);
107 }, function (error) {
108 console.log("failed");
111 PolicyAppService.getData('getDictionary/get_MicroServiceLocationDataByName').then(function (data) {
113 $scope.data = JSON.parse(j.data);
114 console.log($scope.data);
115 $scope.microServiceLocationDictionaryDatas = JSON.parse($scope.data.microServiceLocationDictionaryDatas);
116 console.log($scope.microServiceLocationDictionaryDatas);
117 }, function (error) {
118 console.log("failed");
121 PolicyAppService.getData('getDictionary/get_MicroServiceModelsDataByName').then(function (data) {
123 $scope.data = JSON.parse(j.data);
124 console.log($scope.data);
125 var inputModelList = JSON.parse($scope.data.microServiceModelsDictionaryDatas);
128 for(var i = 0; i < inputModelList.length; i++){
129 if(typeof unique[inputModelList[i]] == "undefined"){
130 unique[inputModelList[i]] = "";
131 uniqueList.push(inputModelList[i]);
134 $scope.microServiceModelsDictionaryDatas = uniqueList;
135 console.log($scope.microServiceModelsDictionaryDatas);
136 }, function (error) {
137 console.log("failed");
140 PolicyAppService.getData('getDictionary/get_DCAEUUIDDataByName').then(function (data) {
142 $scope.data = JSON.parse(j.data);
143 console.log($scope.data);
144 $scope.dcaeUUIDDictionaryDatas = JSON.parse($scope.data.dcaeUUIDDictionaryDatas);
145 console.log($scope.dcaeUUIDDictionaryDatas);
146 }, function (error) {
147 console.log("failed");
150 PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
152 $scope.data = JSON.parse(j.data);
153 console.log($scope.data);
154 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
155 console.log($scope.riskTypeDictionaryDatas);
156 }, function (error) {
157 console.log("failed");
160 PolicyAppService.getData('getDictionary/get_MicroServiceAttributeData').then(function (data) {
162 $scope.data = JSON.parse(j.data);
163 console.log($scope.data);
164 $scope.microServiceAttributeDictionaryDatas = JSON.parse($scope.data.microServiceAttributeDictionaryDatas);
165 console.log($scope.microServiceAttributeDictionaryDatas);
166 }, function (error) {
167 console.log("failed");
172 $scope.attributeDatas = [{"attributes" : $scope.choices}];
173 $scope.isInitEditTemplate = true; //just initially create the edit template, didn't click add button yet.
174 addNewChoice = function(value) {
175 console.log("input value : " + value);
176 if(value != undefined){
177 if (value.startsWith('div.')){
178 value = value.replace('div.','');
181 console.log(" document.getElementById : div."+value);
182 var parentElement = document.getElementById("div."+value);
183 console.log("parentElement : " + parentElement);
184 var div = document.getElementById(value+"@0");
186 var clone = div.cloneNode(true);
187 var addElement = parentElement.childElementCount;
188 clone.id = ''+value+'@'+addElement;
190 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ //if it's view or edit
191 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
192 if($scope.temp.policy.ruleData[clone.id]){
193 clone.value = $scope.temp.policy.ruleData[clone.id];
195 if(!clone.className.includes("child_single")){
196 clone.className += ' child_single'; //here cloned is single element
198 document.getElementById("div."+value).appendChild(clone);
199 plainAttributeKeys.push(''+value+'@'+addElement);
201 }else{ //not view or edit
202 if(!clone.className.includes("child_single")){
203 clone.className += ' child_single'; //here cloned is single element
205 document.getElementById("div."+value).appendChild(clone);
206 plainAttributeKeys.push(''+value+'@'+addElement);
210 if(parentElement == null){
213 div = document.getElementById("div."+value+"@0");
216 if(!div.className.includes('children_group border')){
221 div.className += ' children_group border' + layer; //here is div with a group of children.
224 var childElement = parentElement.firstElementChild;
225 var countParent = parentElement.childElementCount;
226 var childElementString = childElement.innerHTML;
227 var find = value+"@0";
228 var re = new RegExp(find, 'g');
229 childElementString = childElementString.replace(re,value+'@' + countParent);
230 var clone = childElement.cloneNode(true);
231 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
232 var childId = parentElement.childNodes[ii].id;
233 if(ii = parentElement.childNodes.length){
234 var childnewId = childId.slice(0, -1);
235 var count = childId.slice(-1);
238 var countvalue = parseInt(count) + 1;
239 clone.id = childnewId+countvalue;
241 clone.innerHTML=childElementString;
242 document.getElementById("div."+value).appendChild(clone);
243 var selects = clone.getElementsByTagName("select");
244 var inputs = clone.getElementsByTagName("input");
245 var removeValues = [];
246 for(var i=0; i<inputs.length; i++){
247 if ($scope.temp.policy.ruleData!=undefined){
248 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
249 if (checkValue!=undefined && checkValue != "undefined"){
250 document.getElementById(inputs[i].id).value = checkValue;
251 plainAttributeKeys.push(inputs[i].id);
253 plainAttributeKeys.push(inputs[i].id);
256 plainAttributeKeys.push(inputs[i].id);
260 for(var i=0; i<selects.length; i++){
261 if ($scope.temp.policy.ruleData!=undefined){
262 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
263 if (checkValue!=undefined && checkValue!="undefined"){
264 if($scope.temp.policy.ruleData != null){
265 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
266 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
267 plainAttributeKeys.push(selects[i].id);
270 plainAttributeKeys.push(selects[i].id);
273 plainAttributeKeys.push(selects[i].id);
277 for (var k=0; k<removeValues.length; k++){
278 var elem = document.getElementById(removeValues[k]);
279 elem.parentNode.removeChild(elem);
281 var ele = angular.element(document.getElementById("div."+value));
282 $compile(ele.contents())($scope)
288 removeChoice = function(value) {
290 if(value != undefined){
291 var c = document.getElementById("div."+value).childElementCount;
294 Notification.error("The original one is not removable.");
297 document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);
301 function findVal(object, key) {
303 Object.keys(object).some(function(k) {
308 if (object[k] && typeof object[k] === 'object') {
309 value = findVal(object[k], key);
310 return value !== undefined;
316 $scope.pullVersion = function(serviceName) {
317 console.log(serviceName);
318 if(serviceName != undefined){
319 var uuu = "policyController/getModelServiceVersioneData.htm";
320 var postData={policyData: serviceName};
325 contentType: 'application/json',
326 data: JSON.stringify(postData),
327 success : function(data){
328 $scope.$apply(function(){
329 $scope.microServiceModelsDictionaryVersionDatas = data[0].dcaeModelVersionData;
332 error : function(data){
333 alert("Error While Retriving the Template Layout Pattren.");
340 var splitEqual = '=';
341 var splitComma = ',';
342 var splitcolon = ':';
343 var splitsemicolon = ";";
344 var splitEnum = "],";
345 var plainAttributeKeys = [];
347 var enumKeyList = [];
348 var dictionaryList = [];
349 var dictionaryNameList = [];
350 $scope.addDataToFields = function(serviceName, version){
351 if(serviceName != null && version !=null){
352 var service=serviceName+"-v"+version;
353 var myNode = document.getElementById("DynamicTemplate");
354 myNode.innerHTML = '';
355 var uuu = "policyController/getDCAEMSTemplateData.htm";
356 var postData={policyData: service};
358 console.log("service: " +service);
360 var dataOrderInfo = "";
366 contentType: 'application/json',
367 data: JSON.stringify(postData),
368 success : function(data){
369 $scope.$apply(function(){
370 var plainAttributeKeys = [];
371 $scope.dcaeModelData = data[0].dcaeModelData;
372 $scope.dcaeJsonDate = data[0].jsonValue;
373 $scope.dataOrderInfo = null;
374 $scope.dataOrderInfo = data[0].dataOrderInfo;
375 console.log("data[0].dataOrderInfo: " + data[0].dataOrderInfo);
376 console.log("$scope.dataOrderInfo: " + $scope.dataOrderInfo);
378 if(data[0].allManyTrueKeys){
379 console.log("$scope.allManyTrueKeys: " + $scope.allManyTrueKeys);
381 console.log("$scope.dcaeJsonDate: " + $scope.dcaeJsonDate);
382 var attributes = $scope.dcaeModelData.attributes;
383 console.log("attributes: " +attributes);
384 var refAttributes = $scope.dcaeModelData.ref_attributes;
385 var subAttributes = $scope.dcaeModelData.sub_attributes;
386 console.log("subAttributes: " + subAttributes);
387 console.log("refAttributes: " + refAttributes);
388 var headDefautlsData = data[0].headDefautlsData;
389 if(headDefautlsData != null){
390 $scope.temp.policy.onapName = headDefautlsData.onapName;
391 $scope.temp.policy.guard = headDefautlsData.guard;
392 $scope.temp.policy.riskType = headDefautlsData.riskType;
393 $scope.temp.policy.riskLevel = headDefautlsData.riskLevel;
394 $scope.temp.policy.priority = headDefautlsData.priority;
397 var enumAttributes = $scope.dcaeModelData.enumValues;
398 var annotation = $scope.dcaeModelData.annotation;
399 var dictionary = $scope.microServiceAttributeDictionaryDatas;
401 if (annotation == null || annotation.length<1){
402 $scope.isCheck = true;
404 $scope.isCheck = false;
405 var annoationList = annotation.split(splitComma);
406 for (k = 0; k < annoationList.length; k++){
407 var splitAnnotation = annoationList[k].split(splitEqual);
408 if (splitAnnotation[1].includes("matching-true")){
409 matching.push(splitAnnotation[0].trim());
415 if (dictionary!= null && dictionary.length>1){
416 for (m=0; m < dictionary.length; m += 1){
417 var keyCompare = dictionary[m].name;
418 var valueCompare = dictionary[m].value;
419 var valueModel = dictionary[m].modelName;
420 var conpairService = serviceName;
421 if (valueModel.includes('-v')){
422 conpairService = service;
424 if(valueModel.localeCompare(conpairService) == 0){
425 console.log(valueCompare);
426 dictionaryList.push(dictionary[m]);
427 if (!dictionaryNameList.includes(dictionary[m].name)){
428 dictionaryNameList.push(dictionary[m].name)
434 $scope.temp.policy.ruleGridData = [];
436 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
437 dataOrderInfo = $scope.dataOrderInfo;
440 $scope.jsonLayout($scope.dcaeJsonDate);
444 if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){ // If it's veiw or edit
446 if($scope.temp.policy.editPolicy){
447 $scope.isInitEditTemplate = true;
452 // If ruleData contains extra elements created by clicked add button
453 if($scope.temp.policy.ruleData != null){
454 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
455 propNames.forEach(function(name) {
459 var extraElements = data;
461 if(plainAttributeKeys != null){
462 for(var b = 0; b < plainAttributeKeys.length; b++){ // Remove already populated elements from data array
463 var newValue = plainAttributeKeys[b].split("*");
464 for(var a = 0; a < data.length; a++){
465 if(data[a] === newValue[0] || data[a] === (newValue[0]+"@0")){
466 extraElements.splice(a, 1);
471 //--- Populate these extra elements created by clicked add button
472 for(var a = 0; a < extraElements.length; a++){
473 if(extraElements[a].includes("@")){
474 var index = extraElements[a].lastIndexOf("@");
476 // Get the number after @
477 var n = getNumOfDigits(extraElements[a], index+1);
479 var key = extraElements[a].substring(0, index+n+1); //include @x in key also by n+2 since x can be 1,12, etc
480 console.log("key: " + key);
485 var unique = checkData.filter(onlyUnique);
486 var parentLevelElements = [];
488 //--- get all root level exta elments first (only contains one "@")
489 for(var i =0; i < unique.length; i++){
490 var firstIndex = unique[i].indexOf("@");
491 var lastIndex = unique[i].lastIndexOf("@");
492 if(firstIndex == lastIndex){
493 var newKey = unique[i].substring(0, firstIndex);
494 console.log("root element: " + newKey);
495 parentLevelElements.push(newKey);
496 unique[i] = "*processed*";
500 //if no layout order info, keep the process as before
502 for(var i =0; i < unique.length; i++){
503 //remove @x and let addNewChoice add @1 or @2...
504 //var newKey = unique[i].substring(0, unique[i].length-2);
505 var index = unique[i].lastIndexOf("@");
506 var newKey = unique[i].substring(0, index);
507 console.log("newKey: " + newKey);
508 addNewChoice(newKey);
512 for (var i = 0; i < $scope.labelManyKeys.length; i++) {
513 //console.log("dataOrderInfo["+i+"]"+ dataOrderInfo[i]);
514 var label = $scope.labelManyKeys[i];
516 if(parentLevelElements){
517 for (var k = 0; k < parentLevelElements.length; k++){
518 if(label == parentLevelElements[k]){
527 //---reset to default
529 $scope.labelManyKeys = [];
531 //---process none labels
532 for (var j = 0; j < unique.length; j++){
533 if(unique[j] != "*processed*"){
534 // if not created yet
535 if(!document.getElementById(unique[j])){
536 var index = unique[j].lastIndexOf("@");
537 var newKey = unique[j].substring(0, index);
539 var newElement = document.getElementById("div."+unique[j]);
541 //check weather it has been created already
542 if(newElement != null){
545 newElement = document.getElementById(unique[j]);
546 if(newElement != null){
550 //if not created yet, then create it.
551 addNewChoice(newKey);
561 var ele = angular.element(document.getElementById("DynamicTemplate"));
562 $compile(ele.contents())($scope);
566 error : function(data){
567 alert("Error While Retriving the Template Layout Pattren.");
574 function getNumOfDigits(str_value, index){
575 // Get the number after @
576 var str = str_value.substring(index, str_value.length);
579 for (var x = 0; x < str.length; x++){
590 function getDictionary(attribute){
591 var dicName = attribute;
593 if(attribute.includes(":")){
594 dicName = attribute.split(":")[0];
597 var dictionaryRegExp = new RegExp(dicName);
598 listemunerateValues = [];
599 if (dictionaryRegExp.test(dictionaryNameList)) {
600 for (p=0; p < dictionaryList.length; p += 1) {
601 if (dicName == dictionaryList[p].name) {
602 listemunerateValues.push(dictionaryList[p].value);
606 return listemunerateValues;
609 function getList(attribute) {
610 var enumName = attribute;
611 console.log("In getList: attribute => " + attribute);
613 if(attribute.includes(":")){
614 enumName = attribute.split(":")[0];
617 var baseEnum = $scope.dcaeModelData.enumValues;
619 if(baseEnum != null){
620 enumList = baseEnum.split(splitEnum);
623 var patternTest = new RegExp(enumName);
624 for (k=0; k < enumList.length; k += 1){
625 if(patternTest.test(enumList[k]) == true){
626 enumAttributes = enumList[k].trim();
631 enumAttributes = enumAttributes.replace("[", "");
632 enumAttributes = enumAttributes.replace("]", "");
633 enumAttributes = enumAttributes.replace(/ /g, '');
634 var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
635 listemunerateValues = dropListAfterCommaSplit[1].split(splitComma);
636 //enumKeyList.push(attribute);
637 return listemunerateValues;
643 function getBooleanList(){
644 var booleanList = [];
645 booleanList.push(true);
646 booleanList.push(false);
651 function isArray(arrayTest) {
652 return Object.prototype.toString.call(arrayTest) === '[object Array]';
656 $scope.layOutElementList = [];
657 $scope.layOutOnlyLableList = [];
659 var elementOrderNum = 0;
661 function deconstructJSON(layOutData, level , name) {
665 var stringValue = "java.lang.String";
666 var string = "string";
667 var intValue = "int";
668 var integerValue = "integer";
669 var double = "double";
670 var boolean = "boolean";
671 var baseLevel = level;
673 var String = "String";
675 var attributekey = "";
677 if (name.length > 1){
678 label = label + name + '.';
681 for (key in layOutData) {
682 array = isArray(layOutData[key]);
683 console.log("key: " + key , "value: " + layOutData[key]);
685 if (!!layOutData[key] && typeof(layOutData[key])=="object") {
687 if (array==false && key!=="0"){
689 if($scope.dataOrderInfo){
690 var labelObject = {"label" : key, "level" : label, "array" : array};
691 //save it to the list
692 $scope.layOutOnlyLableList.push(labelObject);
696 $scope.labelLayout(label, key, array );
701 if (array == true && key!=0){
705 if (lableList.length > 0){
709 var newKey = lableList.pop();
711 if($scope.dataOrderInfo){
713 var labelObject = {"label" : newKey, "level" : baseLevel, "array" : array};
714 //save it to the list
715 $scope.layOutOnlyLableList.push(labelObject);
719 $scope.labelLayout(baseLevel, newKey, array );
723 label = baseLevel + newKey + '@0.';
725 label = baseLevel + newKey + '.';
728 deconstructJSON(layOutData[key] , label, key);
730 var attirbuteLabel = label;
732 var isRequired = false;
733 if (layOutData[key].includes('defaultValue-')){
734 defaultValue = layOutData[key].split('defaultValue-')[1];
739 attributekey = lableList.pop();
740 attirbuteLabel = baseLevel;
742 attributekey = key.split();
745 if (layOutData[key].includes('required-true')){
749 var subAttributes = $scope.dcaeModelData.sub_attributes;
752 var jsonObject = JSON.parse(subAttributes);
753 var allkeys = Object.keys(jsonObject);
755 for (var k = 0; k < allkeys.length; k++) {
756 var keyValue = allkeys[k];
757 console.log(" keyValue:jsonObject["+keyValue+ "]: " + jsonObject[keyValue]);
758 if(jsonObject[keyValue]){
759 var tempObject = jsonObject[keyValue];
760 if(tempObject && tempObject[key]){
761 if (tempObject[key].includes('required-true')){
770 var elementObject = {};
771 switch (layOutData[key].split(splitcolon)[0]){
775 if($scope.dataOrderInfo){
777 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue, "isRequired": isRequired, "type":"text"};
778 $scope.layOutElementList.push(elementObject);
780 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
785 if($scope.dataOrderInfo){
787 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"number"};
788 $scope.layOutElementList.push(elementObject);
790 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "number");
794 if($scope.dataOrderInfo){
796 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"double"};
797 $scope.layOutElementList.push(elementObject);
799 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "double");
803 if($scope.dataOrderInfo){
805 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key], "list": getBooleanList, "isRequired": isRequired, "type":"dropBox"};
806 $scope.layOutElementList.push(elementObject);
808 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], getBooleanList());
812 if (layOutData[key].includes('dictionary-')){
813 var list = getDictionary(layOutData[key].split('dictionary-')[1]);
815 //--- get dropdown values from enumValues
816 var list = getList(layOutData[key]);
818 if (list.length===0){ //not dropdown element
819 if($scope.dataOrderInfo){
821 elementObject = {"id": elementOrderNum,"attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": defaultValue,"isRequired": isRequired, "type":"text"};
822 $scope.layOutElementList.push(elementObject);
825 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired, "text");
828 if($scope.dataOrderInfo){
830 elementObject = {"id": elementOrderNum, "attributekey" : attributekey, "array": array, "attirbuteLabel" : attirbuteLabel, "defaultValue": layOutData[key],"isRequired": isRequired, "list":list, "type":"dropBox"};
831 $scope.layOutElementList.push(elementObject);
833 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, layOutData[key], list, isRequired);
843 $scope.validContionalRequired = function(parentId) {
844 console.log("ng-blur event: parentId : " + parentId);
845 var c = document.getElementById(parentId).children;
847 var hasValue = false;
848 for (i = 0; i < c.length; i++) {
849 if(c[i].getAttribute("data-conditional")){
850 console.log(c[i].getAttribute("data-conditional"));
851 console.log(c[i].value);
852 if(c[i].value != null && c[i].value.trim() != ""){
858 for (i = 0; i < c.length; i++) {
859 if(c[i].getAttribute("data-conditional")){
861 c[i].setAttribute("required", true);
863 c[i].removeAttribute("required");
869 $scope.jsonLayout = function(layOutData){
871 deconstructJSON(layOutData , "", "");
873 var orderValue = $scope.dataOrderInfo;
874 var layOutElementList = $scope.layOutElementList;
875 var labelList = $scope.layOutOnlyLableList;
879 $scope.layOutElementList = [];
880 $scope.layOutOnlyLableList = [];
882 // Only layout in order if order info provided
885 if(orderValue.includes("[")){
886 orderValue = orderValue.replace("[", "") ;
887 orderValue = orderValue.replace("]", "") ;
890 orderValue = orderValue.split(',') ;
892 for (var i = 0; i < orderValue.length; i++) {
893 console.log("orderValue["+i+"]"+ orderValue[i]);
894 var key = orderValue[i].trim();
896 //--- Create labels first {"label" : newKey, "level" : baseLevel, "array" : array};
898 for (var k = 0; k < labelList.length; k++){
900 var label = labelList[k].label.toString().trim();
901 var level = labelList[k].level.toString().trim();
902 var array = labelList[k].array;
905 $scope.labelLayout(level, label, array);
906 //in case to have duplicate label names
907 labelList[k].label = "*processed*";
912 //--- then layout each element based on its order defined in YAML file
913 for (var j = 0; j < layOutElementList.length; j++) {
915 var attributekey = layOutElementList[j].attributekey.toString().trim();
917 if(key == attributekey){
919 var attirbuteLabel = layOutElementList[j].attirbuteLabel.toString().trim();
920 var defaultValue = layOutElementList[j].defaultValue.toString().trim();
921 var isRequired = layOutElementList[j].isRequired;
923 console.log("layOutElementList[" +j+ "]: id:" + layOutElementList[j].id + ", attributekey:"+ layOutElementList[j].attributekey + ", attirbuteLabel:" + layOutElementList[j].attirbuteLabel);
924 console.log("layOutElementList[" +j+ "]: type:" + layOutElementList[j].type);
925 if (layOutElementList[j].type == "dropBox"){
926 $scope.dropBoxLayout(attirbuteLabel, attributekey, layOutElementList[j].array, defaultValue, layOutElementList[j].list, isRequired);
929 $scope.attributeBox(attributekey, layOutElementList[j].array, attirbuteLabel, defaultValue, isRequired, layOutElementList[j].type);
933 //in case to have duplicate attribute names
934 layOutElementList[j].attributekey = "*processed*";
943 $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired, dataType ){
944 $scope.temp.policy.ruleGridData.push(attibuteKey);
945 var br = document.createElement("BR");
947 var label = document.createElement("Label");
948 var labeltext = null;
949 var requiredLabName = "";
950 if (matching.includes(attibuteKey)){
951 labeltext = document.createTextNode(attibuteKey + "*!");
952 isRequired = true; //set required as true for matching element
955 requiredLabName = attibuteKey + " * ";
956 labeltext = document.createTextNode(requiredLabName);
958 labeltext = document.createTextNode(attibuteKey);
963 var divID = labelValue;
965 if (labelValue.length < 1){
966 divID = "DynamicTemplate";
967 }else if (labelValue.endsWith('.')){
968 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
971 label.appendChild(labeltext);
973 var textField = document.createElement("INPUT");
975 textField.setAttribute("class" , "form-control");
977 if(dataType == "double"){
978 textField.setAttribute("type" , "number");
979 textField.setAttribute("step" , "any");
981 textField.setAttribute("type" , dataType);
984 textField.setAttribute("style" , "width:300px;");
985 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
988 if(attributeManyKey){
989 checkKey = labelValue + attibuteKey+'@0';
990 textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
991 var divTag = document.createElement("div");
992 divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
993 var addButton = document.createElement("BUTTON");
994 var buttonaddLabel = document.createTextNode("+");
995 addButton.appendChild(buttonaddLabel);
996 addButton.setAttribute("id", labelValue + attibuteKey);
997 addButton.setAttribute("class", "btn btn-add-remove");
998 addButton.setAttribute("onclick" , 'addNewChoice("'+labelValue + attibuteKey+'");');
999 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1000 var removeButton = document.createElement("BUTTON");
1001 var buttonremoveLabel = document.createTextNode("-");
1002 removeButton.appendChild(buttonremoveLabel);
1003 removeButton.setAttribute("class", "btn btn-add-remove");
1004 removeButton.setAttribute("onclick" , 'removeChoice("'+labelValue + attibuteKey+'");');
1005 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1006 document.getElementById(divID).appendChild(addButton);
1007 document.getElementById(divID).appendChild(removeButton);
1008 document.getElementById(divID).appendChild(label);
1009 id = "div."+labelValue+attibuteKey;
1010 //var divTag = document.createElement("div");
1011 divTag.setAttribute("id", id);
1012 document.getElementById(divID).appendChild(divTag);
1013 textField.className += ' first_child';
1015 textField.setAttribute("required", "true");
1017 divTag.appendChild(textField);
1018 document.getElementById(divID).appendChild(divTag);
1021 checkKey = labelValue + attibuteKey;
1022 textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
1023 if(document.getElementById(divID).hasAttribute('required') || !document.getElementById(divID).hasAttribute('data-conditional')){
1024 if(requiredLabName.includes("*") || isRequired){
1025 textField.setAttribute("required", "true");
1027 }else if (document.getElementById(divID).hasAttribute('data-conditional')){
1028 if(requiredLabName.includes("*")){
1029 var requiredNode = document.createElement('span');
1030 requiredNode.setAttribute("class", "mstooltip");
1031 requiredNode.textContent = "?";
1032 label.appendChild(requiredNode);
1034 var requiredNodeToolTip = document.createElement('span');
1035 requiredNodeToolTip.setAttribute("class", "tooltiptext");
1036 requiredNodeToolTip.textContent = "Conditional Required";
1037 requiredNode.appendChild(requiredNodeToolTip);
1039 textField.setAttribute("data-conditional", divID);
1040 textField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
1044 document.getElementById(divID).appendChild(label);
1045 document.getElementById(divID).appendChild(textField);
1046 document.getElementById(divID).appendChild(br);
1050 if(divID.includes("@0") && divID.includes("div.")){
1051 var firstChild_Id = divID.split("@0")[0];
1052 var firstChild_element = document.getElementById(firstChild_Id);
1053 if(firstChild_element){
1055 if(!firstChild_element.className.includes('children_group border')){
1060 firstChild_element.className += ' children_group border' + layer; //here is div with a group of children.
1064 console.log('firstChild_Id: ' + firstChild_Id);
1065 console.log('divID: ' + divID);
1067 if (defaultValue.length > 0){
1068 if(defaultValue.includes(":")){
1069 defaultValue = defaultValue.split(":")[0];
1070 if(defaultValue === "NA") {
1074 if(defaultValue != "undefined" && defaultValue != undefined && defaultValue != "null"){
1075 document.getElementById(checkKey).value = defaultValue;
1079 if($scope.temp.policy.ruleData != null){
1080 //document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
1081 if (attributeManyKey){
1082 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
1083 if($scope.temp.policy.ruleData[newCheckKey +'@0'] != undefined && $scope.temp.policy.ruleData[newCheckKey +'@0'] != "undefined"){
1084 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
1087 if($scope.temp.policy.ruleData[checkKey] != undefined && $scope.temp.policy.ruleData[checkKey] != "undefined"){
1088 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
1092 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey);
1095 $scope.labelManyKeys = [];
1096 $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
1097 var label = document.createElement("Label")
1098 var divID = labelValue;
1099 if (labelValue.endsWith('.')){
1100 var workingLabel = labelValue.substring(0, labelValue.length-1);
1102 var workingLabel = labelValue;
1105 if (labelValue.length < 1){
1106 divID = "DynamicTemplate";
1107 } else if (labelValue.endsWith('.')){
1108 var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
1111 var subAttributes = $scope.dcaeModelData.sub_attributes;
1112 var jsonObject = JSON.parse(subAttributes);
1113 var lablInfo = findVal(jsonObject, lableName);
1114 console.log("findValue : " + lableName +": "+ lablInfo);
1116 var required = null;
1118 if(lablInfo.includes("required-true")){
1121 }else if (lablInfo.includes("required-false")){
1126 var labeltext = document.createTextNode(lableName + star);
1128 label.appendChild(labeltext);
1132 var addButton = document.createElement("BUTTON");
1133 var buttonLabel = document.createTextNode("+");
1134 addButton.appendChild(buttonLabel);
1135 addButton.setAttribute("class", "btn btn-add-remove");
1136 addButton.setAttribute("onclick" , 'addNewChoice("'+labelValue + lableName+'");');
1137 addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1138 var removeButton = document.createElement("BUTTON");
1139 var buttonremoveLabel = document.createTextNode("-");
1140 removeButton.appendChild(buttonremoveLabel);
1141 removeButton.setAttribute("class", "btn btn-add-remove");
1142 removeButton.setAttribute("onclick" , 'removeChoice("'+labelValue +lableName+'");');
1143 removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
1144 document.getElementById(divID).appendChild(addButton);
1145 document.getElementById(divID).appendChild(removeButton);
1146 document.getElementById(divID).appendChild(label);
1147 var id = "div."+labelValue+lableName;
1148 var divTag = document.createElement("div");
1149 divTag.setAttribute("id", id);
1150 document.getElementById(divID).appendChild(divTag);
1152 var divTag = document.createElement("div");
1153 divTag.setAttribute("id", id +'@0');
1155 if(!divTag.className.includes('children_group border')){
1160 divTag.className += ' children_group border' + layer; //here is div with a group of children.
1163 divTag.setAttribute("required", required);
1164 }else if(required == false){
1165 divTag.setAttribute("data-conditional", "yes");
1168 document.getElementById(id).appendChild(divTag);
1170 $scope.labelManyKeys.push(lableName);
1173 var divTag = document.createElement("div");
1174 divTag.setAttribute("id", "div."+labelValue+lableName);
1176 if(!divTag.className.includes('children_group border')){
1181 divTag.className += ' children_group border' + layer; //here is div with a group of children.
1184 divTag.setAttribute("required", required);
1185 }else if(required == false){
1186 divTag.setAttribute("data-conditional", "yes");
1188 document.getElementById(divID).appendChild(label);
1189 document.getElementById(divID).appendChild(divTag);
1193 $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues, isRequired){
1194 var br = document.createElement("BR");
1196 if (labelLevel.length < 1){
1197 var divID = "DynamicTemplate";
1198 } else if (labelLevel.endsWith('.')){
1199 var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
1203 var label = document.createElement("Label")
1205 var refAttributes = $scope.dcaeModelData.ref_attributes;
1206 if(isRequired != true && refAttributes){ //check refAttributes also
1207 var refAttributesList = refAttributes.split(splitComma);
1208 for (k = 0; k < refAttributesList.length; k++){
1209 var refAttribute = refAttributesList[k].split(splitEqual);
1210 if (attributeName == refAttribute[0].trim() && refAttribute[1].includes("required-true")){
1216 if (matching.includes(attributeName)){
1217 var labeltext = document.createTextNode(attributeName + "*!");
1218 label.appendChild(labeltext);
1219 isRequired = true; //set required as true for matching element
1221 var labeltext = document.createTextNode(attributeName);
1223 var requiredLabName = attributeName+ " * ";
1224 labeltext = document.createTextNode(requiredLabName);
1226 labeltext = document.createTextNode(attributeName);
1229 label.appendChild(labeltext);
1231 label.appendChild(labeltext);
1232 // if this field is required, but its parent is not required
1233 if(isRequired && document.getElementById(divID).hasAttribute('data-conditional')){
1234 var requiredNode = document.createElement('span');
1235 requiredNode.setAttribute("class", "mstooltip");
1236 requiredNode.textContent = "?";
1237 label.appendChild(requiredNode);
1239 var requiredNodeToolTip = document.createElement('span');
1240 requiredNodeToolTip.setAttribute("class", "tooltiptext");
1241 requiredNodeToolTip.textContent = "Conditional Required";
1242 requiredNode.appendChild(requiredNodeToolTip);
1246 var listField = document.createElement("SELECT");
1247 listField.setAttribute("class" , "form-control");
1248 listField.setAttribute("style" , "width:300px;");
1249 listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
1252 if(document.getElementById(divID).hasAttribute('data-conditional')){
1253 listField.setAttribute("data-conditional", divID);
1254 listField.setAttribute("ng-blur", "validContionalRequired('"+divID+"')");
1256 listField.setAttribute("required", true);
1259 if( many != true || isRequired != true){ // add an empty option for not required or not multiple select element
1260 var optionFirst = document.createElement('option');
1261 optionFirst.setAttribute('value', "");
1262 listField.appendChild(optionFirst);
1265 for (i=0; i < listemunerateValues.length; i += 1) {
1266 if(listemunerateValues[i].includes("equal-sign")){
1267 listemunerateValues[i] = listemunerateValues[i].replace('equal-sign','=');
1269 option = document.createElement('option');
1270 option.setAttribute('value', listemunerateValues[i]);
1271 option.appendChild(document.createTextNode(listemunerateValues[i]));
1272 option.setAttribute('value', listemunerateValues[i]);
1273 listField.appendChild(option);
1275 listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
1277 enumKeyList.push(attributeName);
1279 document.getElementById(divID).appendChild(label);
1280 document.getElementById(divID).appendChild(br);
1283 document.getElementById(divID).appendChild(listField).multiple = true;
1284 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
1286 document.getElementById(divID).appendChild(listField).multiple = false;
1287 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
1290 if($scope.temp.policy.ruleData != null){
1292 document.getElementById(labelLevel +attributeName).options[0].selected = false;
1293 for (i=0; i < listemunerateValues.length; i += 1) {
1294 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
1295 if (testValue === undefined){
1296 testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
1298 var location = listemunerateValues.indexOf(testValue);
1300 document.getElementById(labelLevel +attributeName).options[location].selected = true;
1304 if($scope.temp.policy.ruleData[labelLevel + attributeName] != undefined && $scope.temp.policy.ruleData[labelLevel + attributeName] != "undefined"){
1305 document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];
1311 function onlyUnique(value, index, self) {
1312 return self.indexOf(value) === index;
1316 function checkDictionary(value){
1317 for (i = 0; i < $scope.microServiceAttributeDictionaryDatas.length; i++) {
1318 if ($scope.microServiceAttributeDictionaryDatas[i].name.localeCompare(value)){
1324 $scope.savePolicy = function(policy){
1325 if(policy.itemContent != undefined){
1326 $scope.refreshCheck = true;
1327 $scope.policyNavigator = policy.itemContent;
1328 policy.itemContent = "";
1330 $scope.savebutton = false;
1333 var jsonPolicy = {};
1334 if(plainAttributeKeys != null){
1335 for(a = 0; a < plainAttributeKeys.length; a++){
1336 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1337 console.log("splitPlainAttributeKey: " + splitPlainAttributeKey);
1338 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1339 var key = splitPlainAttributeKey[0];
1340 if(searchElement == null){
1341 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1342 key = splitPlainAttributeKey[0]+'@0';
1343 }else if (searchElement.nodeName == 'BUTTON'){
1344 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1345 key = splitPlainAttributeKey[0]+'@0';
1347 if(searchElement != null){
1348 var keySplit = key.split(dot);
1349 var elumentLocation = keySplit.length;
1351 if (elumentLocation > 1){
1352 enumKey = keySplit[keySplit.length - 1];
1354 //check it is undefined or not
1355 if (enumKeyList != undefined && enumKeyList.indexOf(enumKey) != -1){
1356 if (splitPlainAttributeKey[1]!= undefined && splitPlainAttributeKey[1].indexOf("true") !== -1){
1357 var multiSlect = [];
1358 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1359 multiSlect.push(searchElement.selectedOptions[i].value);
1361 jsonPolicy[key]= multiSlect;
1363 console.log(" searchElement.value = > " + searchElement.value);
1364 jsonPolicy[key]= searchElement.value;
1367 if(searchElement.value != null){
1368 console.log(" searchElement.value = > " + searchElement.value);
1369 jsonPolicy[key]= searchElement.value;
1375 var uuu = "policycreation/save_policy";
1376 var postData={policyData: policy, policyJSON : jsonPolicy};
1381 contentType: 'application/json',
1382 data: JSON.stringify(postData),
1383 success : function(data){
1384 $scope.$apply(function(){
1385 $scope.data=data.policyData;
1386 if($scope.data == 'success'){
1387 $scope.temp.policy.readOnly = 'true';
1388 $scope.safetyChecker = data.policyData.split("#")[2];
1389 if ($scope.safetyChecker!=undefined) {
1390 Notification.success($scope.safetyChecker);
1392 $scope.pushStatus=data.policyData.split("&")[1];
1393 if($scope.pushStatus=="successPush"){
1394 Notification.success("Policy pushed successfully");
1396 Notification.success("Policy Saved Successfully.");
1397 }else if ($scope.data == 'PolicyExists'){
1398 $scope.savebutton = true;
1399 Notification.error("Policy Already Exists with Same Name in Scope.");
1402 console.log($scope.data);
1404 error : function(data){
1405 Notification.error("Error Occured while saving Policy.");
1410 $scope.validatePolicy = function(policy){
1411 document.getElementById("validate").innerHTML = "";
1414 var jsonPolicy = {};
1415 if(plainAttributeKeys != null){
1416 for(a = 0; a < plainAttributeKeys.length; a++){
1417 var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
1418 console.log(splitPlainAttributeKey[1]);
1419 var searchElement = document.getElementById(splitPlainAttributeKey[0]);
1420 var key = splitPlainAttributeKey[0];
1421 if(searchElement == null){
1422 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1423 key = splitPlainAttributeKey[0]+'@0';
1424 }else if (searchElement.nodeName == 'BUTTON'){
1425 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
1426 key = splitPlainAttributeKey[0]+'@0';
1428 if(searchElement != null){
1429 if (enumKeyList.indexOf(key) != -1){
1430 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
1431 var multiSlect = [];
1432 for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
1433 multiSlect.push(searchElement.selectedOptions[i].value);
1435 jsonPolicy[key]= multiSlect;
1437 jsonPolicy[key]= searchElement.value;
1439 if(searchElement.getAttribute("required")){
1440 if(!searchElement.value){
1445 if(searchElement.value != null){
1446 jsonPolicy[key]= searchElement.value;
1447 if(searchElement.getAttribute("required")){
1448 if(!searchElement.value){
1457 var uuu = "policyController/validate_policy.htm";
1459 console.log("$scope.isCheck:" + $scope.isCheck);
1461 if($scope.isCheck == true){
1462 if(("configName" in policy) == false){
1463 Notification.error("Validation Failed: configName is required");
1464 $scope.savebutton = true;
1467 if(("location" in policy) == false){
1468 Notification.error("Validation Failed: location is required");
1469 $scope.savebutton = true;
1472 if(("uuid" in policy) == false){
1473 Notification.error("Validation Failed: uuid is required");
1474 $scope.savebutton = true;
1477 if(("policyScope" in policy) == false){
1478 Notification.error("Validation Failed: policyScope is required");
1479 $scope.savebutton = true;
1484 var postData={policyData: policy, policyJSON : jsonPolicy};
1489 contentType: 'application/json',
1490 data: JSON.stringify(postData),
1491 success : function(data){
1492 $scope.$apply(function(){
1493 $scope.validateData = data.data.replace(/\"/g, "");
1494 $scope.data=data.data.substring(1,8);
1495 var size = data.data.length;
1496 if($scope.data == 'success'){
1497 Notification.success("Validation Success.");
1498 $scope.savebutton = false;
1500 var displayWarning = data.data.substring(19,size - 1);
1501 document.getElementById("validate").innerHTML = "Safe Policy Warning Message : "+displayWarning;
1502 document.getElementById("validate").style.color = "white";
1503 document.getElementById("validate").style.backgroundColor = "skyblue";
1506 Notification.error("Validation Failed.");
1507 document.getElementById("validate").innerHTML = $scope.validateData;
1508 document.getElementById("validate").style.color = "white";
1509 document.getElementById("validate").style.backgroundColor = "red";
1510 $scope.savebutton = true;
1514 console.log($scope.data);
1516 error : function(data){
1517 Notification.error("Validation Failed.");
1518 $scope.savebutton = true;
1523 function extend(obj, src) {
1524 for (var key in src) {
1525 if (src.hasOwnProperty(key)) obj[key] = src[key];