[Policy-52, Policy-92, Policy-93] Policy Enhancements and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / policy-models / Editor / PolicyTemplateController / DCAEMicroServicePolicyController.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 angular.module('abs').controller('dcaeMicroServiceController', ['$scope', '$window', '$compile', 'PolicyAppService', 'policyNavigator', 'modalService', '$modal', 'Notification', function ($scope, $window, $compile, PolicyAppService, PolicyNavigator, modalService, $modal, Notification) {
21     $("#dialog").hide();
22     
23     $scope.policyNavigator;
24     $scope.isCheck = false;
25     $scope.savebutton = true;
26     $scope.refreshCheck = false;
27     
28     $scope.refresh = function(){
29         if($scope.refreshCheck){
30                 $scope.policyNavigator.refresh();
31         }
32         $scope.modal('createNewPolicy', true);
33         $scope.temp.policy = "";
34     };
35     
36     $scope.modal = function(id, hide) {
37         return $('#' + id).modal(hide ? 'hide' : 'show');
38     };
39     
40         if ($scope.temp.policy.editPolicy != undefined|| $scope.temp.policy.readOnly  != undefined){
41                 if ($scope.temp.policy.configName == undefined){
42                         $scope.isCheck = false;
43                 }else{
44                         $scope.isCheck = true;
45                 }
46         }else {
47                 $scope.isCheck = false;
48         }
49                 
50         PolicyAppService.getData('getDictionary/get_EcompNameDataByName').then(function (data) {
51                 var j = data;
52                 $scope.data = JSON.parse(j.data);
53                 console.log($scope.data);
54                 $scope.ecompNameDictionaryDatas = JSON.parse($scope.data.ecompNameDictionaryDatas);
55                 console.log($scope.ecompNameDictionaryDatas);
56         }, function (error) {
57                 console.log("failed");
58         });
59
60         PolicyAppService.getData('get_DCAEPriorityValues').then(function (data) {
61                 var j = data;
62                 $scope.data = JSON.parse(j.data);
63                 console.log($scope.data);
64                 $scope.priorityDatas = JSON.parse($scope.data.priorityDatas);
65                 console.log($scope.priorityDatas);
66         }, function (error) {
67                 console.log("failed");
68         });
69
70         PolicyAppService.getData('getDictionary/get_GroupPolicyScopeDataByName').then(function (data) {
71                 var j = data;
72                 $scope.data = JSON.parse(j.data);
73                 console.log($scope.data);
74                 $scope.groupPolicyScopeListDatas = JSON.parse($scope.data.groupPolicyScopeListDatas);
75                 console.log($scope.groupPolicyScopeListDatas);
76         }, function (error) {
77                 console.log("failed");
78         });
79
80         PolicyAppService.getData('getDictionary/get_MicroServiceConfigNameDataByName').then(function (data) {
81                 var j = data;
82                 $scope.data = JSON.parse(j.data);
83                 console.log($scope.data);
84                 $scope.microServiceCongigNameDictionaryDatas = JSON.parse($scope.data.microServiceCongigNameDictionaryDatas);
85                 console.log($scope.microServiceCongigNameDictionaryDatas);
86         }, function (error) {
87                 console.log("failed");
88         });
89
90         PolicyAppService.getData('getDictionary/get_MicroServiceLocationDataByName').then(function (data) {
91                 var j = data;
92                 $scope.data = JSON.parse(j.data);
93                 console.log($scope.data);
94                 $scope.microServiceLocationDictionaryDatas = JSON.parse($scope.data.microServiceLocationDictionaryDatas);
95                 console.log($scope.microServiceLocationDictionaryDatas);
96         }, function (error) {
97                 console.log("failed");
98         });
99
100         PolicyAppService.getData('getDictionary/get_MicroServiceModelsDataByName').then(function (data) {
101                 var j = data;
102                 $scope.data = JSON.parse(j.data);
103                 console.log($scope.data);
104                 var inputModelList = JSON.parse($scope.data.microServiceModelsDictionaryDatas);
105                 var unique = {};
106                 var uniqueList = [];
107                 for(var i = 0; i < inputModelList.length; i++){
108                         if(typeof unique[inputModelList[i]] == "undefined"){
109                                 unique[inputModelList[i]] = "";
110                                 uniqueList.push(inputModelList[i]);
111                         }
112                 }
113                 $scope.microServiceModelsDictionaryDatas = uniqueList;
114                 console.log($scope.microServiceModelsDictionaryDatas);
115         }, function (error) {
116                 console.log("failed");
117         });
118
119         PolicyAppService.getData('getDictionary/get_DCAEUUIDDataByName').then(function (data) {
120                 var j = data;
121                 $scope.data = JSON.parse(j.data);
122                 console.log($scope.data);
123                 $scope.dcaeUUIDDictionaryDatas = JSON.parse($scope.data.dcaeUUIDDictionaryDatas);
124                 console.log($scope.dcaeUUIDDictionaryDatas);
125         }, function (error) {
126                 console.log("failed");
127         });
128
129         PolicyAppService.getData('getDictionary/get_RiskTypeDataByName').then(function (data) {
130                 var j = data;
131                 $scope.data = JSON.parse(j.data);
132                 console.log($scope.data);
133                 $scope.riskTypeDictionaryDatas = JSON.parse($scope.data.riskTypeDictionaryDatas);
134                 console.log($scope.riskTypeDictionaryDatas);
135         }, function (error) {
136                 console.log("failed");
137         });
138
139         PolicyAppService.getData('getDictionary/get_MicroServiceAttributeData').then(function (data) {
140                 var j = data;
141                 $scope.data = JSON.parse(j.data);
142                 console.log($scope.data);
143                 $scope.microServiceAttributeDictionaryDatas = JSON.parse($scope.data.microServiceAttributeDictionaryDatas);
144                 console.log($scope.microServiceAttributeDictionaryDatas);
145         }, function (error) {
146                 console.log("failed");
147         });
148             
149
150          $scope.choices = [];
151          $scope.attributeDatas = [{"attributes" : $scope.choices}];
152          
153          $scope.addNewChoice = function(value) {
154                  console.log(value);
155                  if(value != undefined){
156                         if (value.startsWith('div.')){
157                                 value = value.replace('div.','');
158                         }
159                         var parentElement = document.getElementById("div."+value);
160                         var div = document.getElementById(value+"@0");
161                         if(div != null){
162                                 var clone = div.cloneNode(true); 
163                                 var addElement = parentElement.childElementCount + 1;
164                                 clone.id = ''+value+'@'+addElement;
165                                 clone.value = '';
166                                 clone.className += ' child_single'; //here cloned is single element
167                                 document.getElementById("div."+value).appendChild(clone);
168                                 plainAttributeKeys.push(''+value+'@'+addElement);
169                         }else{
170                                 div = document.getElementById("div."+value+"@0");
171                                 
172                                 div.className += ' children_group'; //here is div with a group of children.
173                                 
174                                 var childElement = parentElement.firstElementChild;
175                                 var countParent = parentElement.childElementCount;
176                                 var childElementString = childElement.innerHTML;
177                                 var find = value+"@0";
178                                 var re = new RegExp(find, 'g');
179                                 childElementString = childElementString.replace(re,value+'@' + countParent);
180                                 var clone = childElement.cloneNode(true);
181                                 for (var ii = 0; ii < parentElement.childNodes.length; ii++){
182                             var childId = parentElement.childNodes[ii].id;
183                             if(ii = parentElement.childNodes.length){
184                                 var childnewId = childId.slice(0, -1);
185                                 var count = childId.slice(-1);
186                             }
187                         }
188                                 var countvalue = parseInt(count) + 1;
189                                 clone.id = childnewId+countvalue;
190                                 clone.value = '';
191                                 clone.innerHTML=childElementString;
192                                 document.getElementById("div."+value).appendChild(clone);
193                                 var selects = clone.getElementsByTagName("select");
194                                 var inputs = clone.getElementsByTagName("input");
195                                 var removeValues = [];
196                                 for(var i=0; i<inputs.length; i++){
197                                         if ($scope.temp.policy.ruleData!=undefined){
198                                                 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
199                                                 if (checkValue!=undefined){
200                                                         if($scope.temp.policy.ruleData != null){
201                                                                 var checkValue = $scope.temp.policy.ruleData[inputs[i].id];
202                                                                 document.getElementById(inputs[i].id).value = $scope.temp.policy.ruleData[inputs[i].id];
203                                                                 plainAttributeKeys.push(inputs[i].id);
204                                                         }
205                                                 } else {
206 //                                                      removeValues.push(inputs[i].id);
207                                                         plainAttributeKeys.push(inputs[i].id);
208                                                 }
209                                         }else {
210                                                 plainAttributeKeys.push(inputs[i].id);
211                                         }
212                                 }
213                                 
214                                 for(var i=0; i<selects.length; i++){
215                                         if ($scope.temp.policy.ruleData!=undefined){
216                                                 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
217                                                 if (checkValue!=undefined){
218                                                         if($scope.temp.policy.ruleData != null){
219                                                                 var checkValue = $scope.temp.policy.ruleData[selects[i].id];
220                                                                 document.getElementById(selects[i].id).value = $scope.temp.policy.ruleData[selects[i].id];
221                                                                 plainAttributeKeys.push(selects[i].id);
222                                                         }
223                                                 } else {
224                                                         plainAttributeKeys.push(selects[i].id);
225                                                 }
226                                         }else {
227                                                 plainAttributeKeys.push(selects[i].id);
228                                         }
229                                 }
230                                 
231                                 for (var k=0; k<removeValues.length; k++){
232                                         var elem = document.getElementById(removeValues[k]);
233                                         elem.parentNode.removeChild(elem);
234                                 }
235                                 var ele = angular.element(document.getElementById("div."+value));
236                         $compile(ele.contents())($scope);
237                     $scope.$apply();
238                         }
239                  }
240          };
241          
242          $scope.removeChoice = function(value) {
243                  console.log(value);
244                  if(value != undefined){
245                          document.getElementById("div."+value).removeChild(document.getElementById("div."+value).lastChild);     
246                  }
247          };
248          
249          $scope.pullVersion = function(serviceName) {
250                  console.log(serviceName);
251                  if(serviceName != undefined){
252                          var uuu = "policyController/getModelServiceVersioneData.htm";
253                          var postData={policyData: serviceName};
254                          $.ajax({
255                                  type : 'POST',
256                                  url : uuu,
257                                  dataType: 'json',
258                                  contentType: 'application/json',
259                                  data: JSON.stringify(postData),
260                                  success : function(data){
261                                          $scope.$apply(function(){
262                                                  $scope.microServiceModelsDictionaryVersionDatas = data[0].dcaeModelVersionData;
263                                          });
264                                  },
265                                  error : function(data){
266                                          alert("Error While Retriving the Template Layout Pattren.");
267                                  }
268                          });             
269                  }
270          };
271     
272     var splitDash = '-';
273         var splitEqual = '=';
274         var splitComma = ',';
275         var splitcolon = ':';
276         var splitsemicolon = ";";
277         var splitEnum = "],";
278         var plainAttributeKeys = [];
279         var matching = [];
280         var enumKeyList = [];
281         var dictionaryList = [];
282         var dictionaryNameList = [];
283     $scope.addDataToFields = function(serviceName, version){
284         if(serviceName != null && version !=null){
285                 var service=serviceName+"-v"+version;
286                 var myNode = document.getElementById("DynamicTemplate");
287                 myNode.innerHTML = '';
288             var uuu = "policyController/getDCAEMSTemplateData.htm";
289             var postData={policyData: service};
290             $.ajax({
291                 type : 'POST',
292                 url : uuu,
293                 dataType: 'json',
294                 contentType: 'application/json',
295                 data: JSON.stringify(postData),
296                 success : function(data){
297                     $scope.$apply(function(){
298                         $scope.addNewChoice();
299                         var plainAttributeKeys = [];
300                         $scope.dcaeModelData = data[0].dcaeModelData;
301                         $scope.dcaeJsonDate = data[0].jsonValue;
302                         console.log("$scope.dcaeJsonDate: " + $scope.dcaeJsonDate);     
303                         var attributes = $scope.dcaeModelData.attributes;
304                         var refAttributes = $scope.dcaeModelData.ref_attributes;
305                         var subAttributes =     $scope.dcaeModelData.sub_attributes;                            
306                         console.log("subAttributes: " + subAttributes); 
307                         
308                         var enumAttributes = $scope.dcaeModelData.enumValues;
309                         var annotation = $scope.dcaeModelData.annotation;
310                         var dictionary = $scope.microServiceAttributeDictionaryDatas;
311
312                         if (annotation == null || annotation.length<1){
313                                 $scope.isCheck = true;
314                         }else {
315                                 $scope.isCheck = false;
316                                 var annoationList = annotation.split(splitComma);
317                                 for (k = 0; k < annoationList.length; k++){
318                                         var splitAnnotation = annoationList[k].split(splitEqual);
319                                         if (splitAnnotation[1].includes("matching-true")){
320                                                 matching.push(splitAnnotation[0].trim());
321                                         }
322                                 }
323
324                         }
325
326                         if (dictionary!= null || dictionary>1){
327                                 if (dictionary!= null || dictionary.length>1){
328                                         for (m=0; m < dictionary.length; m += 1){
329                                                 var keyCompare = dictionary[m].name;
330                                                 var valueCompare = dictionary[m].value;
331                                                 var valueModel = dictionary[m].modelName;
332                                                 var conpairService = serviceName;
333                                                 if (valueModel.includes('-v')){
334                                                         conpairService = service;
335                                                 }
336                                                 if(valueModel.localeCompare(conpairService) == 0){
337                                                         console.log(valueCompare);      
338                                                         dictionaryList.push(dictionary[m]);
339                                                         if (!dictionaryNameList.includes(dictionary[m].name)){
340                                                                 dictionaryNameList.push(dictionary[m].name)
341                                                         }
342                                                 }
343                                         }
344                                 }
345                         }
346
347                                 $scope.temp.policy.ruleGridData = [];
348                                 
349                                 $scope.jsonLayout($scope.dcaeJsonDate);
350                                 
351                     });
352                     if($scope.temp.policy.editPolicy || $scope.temp.policy.readOnly){
353                         var checkData = [];
354                         var data = [];
355                         if($scope.temp.policy.ruleData != null){
356                                 var propNames = Object.getOwnPropertyNames($scope.temp.policy.ruleData);
357                                 propNames.forEach(function(name) {
358                                         data.push(name);
359                                 });
360                                 for(a = 0; a < data.length; a++){
361                                         var splitPlainAttributeKey1 = data[a].split(".");
362                                         if(splitPlainAttributeKey1[0].indexOf("@") != -1){
363                                                 var key = splitPlainAttributeKey1[0];
364                                                 checkData.push(key);
365                                         }
366                                 }
367                                 var unique = checkData.filter(onlyUnique);
368                                 for(i =0; i < unique.length; i++){
369                                         if(unique[i].indexOf("@0") == -1){
370                                                 var finalkey = unique[i].split("@")[0];
371                                                 $scope.addNewChoice(finalkey);
372                                         }       
373                                 }
374                         }
375                     }
376                     var ele = angular.element(document.getElementById("DynamicTemplate"));
377                         $compile(ele.contents())($scope);
378                     $scope.$apply();
379                 },
380                 error : function(data){
381                     alert("Error While Retriving the Template Layout Pattren.");
382                 }
383             });
384         }
385     };
386     
387     function getDictionary(attribute){
388         var dicName = attribute;
389         if(attribute){
390            if(attribute.includes(":")){
391                    dicName = attribute.split(":")[0];
392            }
393         }
394             var dictionaryRegExp = new RegExp(dicName);
395             listemunerateValues = [];
396             if (dictionaryRegExp.test(dictionaryNameList)) {
397                         for (p=0; p < dictionaryList.length; p += 1) {
398                                 if (dicName == dictionaryList[p].name) {
399                                         listemunerateValues.push(dictionaryList[p].value);
400                                 }
401                         }
402             }
403             return listemunerateValues;
404     }
405     
406     function getList(attribute) {
407                 var enumName = attribute;
408                 console.log("In getList: attribute => " + attribute);
409                 if(attribute){
410                    if(attribute.includes(":")){
411                            enumName = attribute.split(":")[0];
412                    }
413                 }   
414             var baseEnum = $scope.dcaeModelData.enumValues;
415             var enumList = [];
416             if(baseEnum != null){
417                 enumList = baseEnum.split(splitEnum);
418             }
419                 var enumAttributes;
420                 var patternTest = new RegExp(enumName);
421                 for (k=0; k < enumList.length; k += 1){
422                         if(patternTest.test(enumList[k]) == true){
423                                 enumAttributes = enumList[k].trim();
424                 }
425         }
426
427          if(enumAttributes){
428                 enumAttributes = enumAttributes.replace("[", "");
429                 enumAttributes = enumAttributes.replace("]", "");
430                 enumAttributes = enumAttributes.replace(/ /g, '');
431                         var dropListAfterCommaSplit = enumAttributes.split(splitEqual);
432                         listemunerateValues  = dropListAfterCommaSplit[1].split(splitComma);
433                         //enumKeyList.push(attribute); 
434                 return listemunerateValues;
435          }
436          
437             return [];
438             }
439             
440             function getBooleanList(){
441                 var booleanList = [];
442                 booleanList.push(true);
443                 booleanList.push(false);
444                 return booleanList;
445             }
446             
447
448             function isArray(arrayTest) {
449                 return Object.prototype.toString.call(arrayTest) === '[object Array]';
450             }
451             var lableList = [];
452                 function deconstructJSON(dataTest, level , name) {
453                         var array = false;
454                         var label = level;
455                          var stringValue = "java.lang.String";
456                          var string = "string";
457                          var intValue = "int";
458                          var double = "double";
459                          var boolean = "boolean";
460                          var baseLevel = level;
461                          
462                         if (name.length > 1){
463                                 label = label + name + '.';
464                         }
465                         
466                     for (key in dataTest) {
467                         array = isArray(dataTest[key]);
468                         console.log(key , dataTest[key]);
469                 
470                         if (!!dataTest[key] && typeof(dataTest[key])=="object") {
471                                 if (array==false && key!=="0"){
472                                         $scope.labelLayout(label, key, array );                         
473                                 }
474                                 
475                                 if (array == true && key!=0){
476                                         lableList.push(key);
477                                 }
478                                 
479                                 if (lableList.length > 0){
480                                         array = true;
481                                 }
482                                 if ( key=="0"){
483                                         var newKey = lableList.pop();
484                                         $scope.labelLayout(baseLevel, newKey, array );
485                                         if (array){
486                                                 label = baseLevel + newKey + '@0.';
487                                         } else {
488                                                 label = baseLevel + newKey + '.';
489                                         }
490                                 }
491                                 deconstructJSON(dataTest[key] , label, key);
492                         } else {
493                                 var attirbuteLabel = label;
494                                 var defaultValue='';
495                                 var isRequired = false;
496                                 if (dataTest[key].includes('defaultValue-')){
497                                         defaultValue = dataTest[key].split('defaultValue-')[1];
498                                 }
499
500                                 if (key==="0"){
501                                         array = true;
502                                         attributekey = lableList.pop();
503                                         attirbuteLabel = baseLevel;
504                                 } else {
505                                         attributekey = key.split();
506                                 }
507                                 
508                                 if (dataTest[key].includes('required-true')){
509                                         isRequired = true;
510                                 }
511                                 
512                                 var subAttributes = $scope.dcaeModelData.sub_attributes;
513                                 
514                                 if(subAttributes){                                      
515                                         var jsonObject = JSON.parse(subAttributes);                                     
516                                         var allkeys = Object.keys(jsonObject);
517                                         if(allkeys){
518                                                 for (var k = 0; k < allkeys.length; k++) {
519                                                         var keyValue = allkeys[k];
520                                                         console.log(" keyValue:jsonObject["+keyValue+ "]: " + jsonObject[keyValue]);
521                                                         if(jsonObject[keyValue]){
522                                                                 var tempObject = jsonObject[keyValue];
523                                                                 if(tempObject && tempObject[key]){
524                                                                 if (tempObject[key].includes('required-true')){
525                                                                         isRequired = true;
526                                                                 }       
527                                                                 }
528                                                         }
529                                                 }                                               
530                                         }                                       
531                                 }
532                                 
533                                 switch (dataTest[key].split(splitcolon)[0]){
534                                         case stringValue:
535                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
536                                                 break;
537                                         case string:
538                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
539                                                 break;                                          
540                                         case intValue: 
541                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
542                                                 break;
543                                         case double:
544                                                 $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
545                                                 break;
546                                         case boolean:
547                                                 $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], getBooleanList());
548                                                 break;
549                                         default:
550                                                 if (dataTest[key].includes('dictionary-')){
551                                                         var list = getDictionary(dataTest[key].split('dictionary-')[1]);
552                                                 }else{
553                                                         var list = getList(dataTest[key]);
554                                                 }
555                                                 if (list.length===0){
556                                                         $scope.attributeBox(attributekey, array, attirbuteLabel, defaultValue, isRequired);
557                                                 }else{
558                                                         $scope.dropBoxLayout(attirbuteLabel, attributekey, array, dataTest[key], list);
559                                                 }
560                                                 break;
561                                 }
562                         }
563                     }
564                 }  
565
566             $scope.jsonLayout = function(dataTest){
567                 deconstructJSON(dataTest , "", "");
568             }
569             
570             
571             $scope.attributeBox = function(attibuteKey, attributeManyKey, labelValue, defaultValue, isRequired){
572                         $scope.temp.policy.ruleGridData.push(attibuteKey);                      
573                 var br = document.createElement("BR");
574                 
575                 var label = document.createElement("Label");
576                 var labeltext = null;
577                 var requiredLabName = "";
578                 if (matching.includes(attibuteKey)){
579                         labeltext = document.createTextNode(attibuteKey + "*!");                
580                 }else {
581                         //var labeltext = document.createTextNode(refattributeLabel);
582                         if(isRequired){
583                                 requiredLabName = attibuteKey + " *";
584                                 labeltext = document.createTextNode(requiredLabName);
585                         }else{
586                             labeltext = document.createTextNode(attibuteKey);   
587                         }
588                 }
589
590                 
591                 var divID = labelValue;
592                 
593                 if (labelValue.length  < 1){
594                         divID = "DynamicTemplate";
595                 }else if (labelValue.endsWith('.')){
596                         var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
597                 }
598                 
599                 label.appendChild(labeltext);
600                 
601                 var textField = document.createElement("INPUT");
602                 
603                 textField.setAttribute("class" , "form-control");
604                 textField.setAttribute("type" , "text");
605                 textField.setAttribute("style" , "width:300px;");
606                 textField.setAttribute("ng-disabled" , "temp.policy.readOnly");
607                 var checkKey;
608                 if(attributeManyKey){
609                         checkKey = labelValue + attibuteKey+'@0';
610                         textField.setAttribute("id" , ''+labelValue + attibuteKey+'@0'+'');
611                         var divTag = document.createElement("div");
612                         divTag.setAttribute("id", "div."+ labelValue +attibuteKey);
613                         var addButton = document.createElement("BUTTON");
614                         var buttonaddLabel = document.createTextNode("+");       
615                         addButton.appendChild(buttonaddLabel); 
616                         addButton.setAttribute("id", labelValue + attibuteKey);
617                         addButton.setAttribute("class", "btn btn-add-remove");
618                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+labelValue + attibuteKey+'");');
619                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
620                         var removeButton = document.createElement("BUTTON");
621                         var buttonremoveLabel = document.createTextNode("-");       
622                         removeButton.appendChild(buttonremoveLabel); 
623                         removeButton.setAttribute("class", "btn btn-add-remove");
624                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+labelValue + attibuteKey+'");');
625                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
626                         document.getElementById(divID).appendChild(addButton); 
627                         document.getElementById(divID).appendChild(removeButton); 
628                         document.getElementById(divID).appendChild(label);  
629                         document.getElementById(divID).appendChild(textField); 
630                         document.getElementById(divID).appendChild(br); 
631                         document.getElementById(divID).appendChild(divTag); 
632                         
633                 }else{
634                         checkKey = labelValue + attibuteKey;
635                         textField.setAttribute("id" , ''+labelValue +attibuteKey+'');
636                         if(requiredLabName.includes("*")){
637                                 textField.setAttribute("required", "true");
638                         }
639                         document.getElementById(divID).appendChild(label);  
640                         document.getElementById(divID).appendChild(textField);  
641                         document.getElementById(divID).appendChild(br); 
642
643                 }
644                 
645                 if(divID.includes("@0") && divID.includes("div.")){
646                         var firstChild_Id = divID.split("@0")[0];
647                         var firstChild_element = document.getElementById(firstChild_Id);
648                         if(firstChild_element){
649                                 firstChild_element.className += ' children_group';  //here is a div with a group of children.
650                         }
651                 }
652                 console.log('firstChild_Id: ' + firstChild_Id);
653                 console.log('divID: ' + divID);
654                 
655                 if (defaultValue.length > 0){   
656                         if(defaultValue.includes(":")){
657                                 defaultValue = defaultValue.split(":")[0];
658                                 if(defaultValue === "NA") {
659                                         defaultValue = "";
660                                 }                               
661                         }
662                         document.getElementById(checkKey).value = defaultValue;
663                 }
664                 
665                 if($scope.temp.policy.ruleData != null){
666                         //document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
667                         if (attributeManyKey){
668                                 var newCheckKey = checkKey.replace(attibuteKey + '@0',attibuteKey);
669                                 document.getElementById(newCheckKey +'@0').value = $scope.temp.policy.ruleData[newCheckKey +'@0'];
670                                 var i =1 ;
671                                 while(true){
672                                         var newValue = $scope.temp.policy.ruleData[newCheckKey+'@'+i];
673                                         if(newValue==null){
674                                                 break;
675                                         }else{
676                                                 $scope.addNewChoice(newCheckKey);
677                                                 document.getElementById(newCheckKey+'@'+i).value = newValue;
678                                                 i = i+1;
679                                         }
680                                 }               
681                 
682                         }else{
683                                 document.getElementById(checkKey).value = $scope.temp.policy.ruleData[checkKey];
684                         }
685                 } 
686                 plainAttributeKeys.push(labelValue + attibuteKey+'*'+attributeManyKey); 
687     };
688   
689     $scope.labelLayout = function(labelValue, lableName, labelManyKey ){
690                 var label = document.createElement("Label")
691                 var divID = labelValue;
692                 if (labelValue.endsWith('.')){
693                         var workingLabel = labelValue.substring(0, labelValue.length-1);
694                 }else {
695                         var workingLabel = labelValue;
696                 }
697                 
698                 if (labelValue.length  < 1){
699                         divID = "DynamicTemplate";
700                 } else if (labelValue.endsWith('.')){
701                         var divID = 'div.'+ labelValue.substring(0, labelValue.length-1);
702                 }
703                 
704                 var labeltext = document.createTextNode(lableName);
705         
706                 label.appendChild(labeltext);
707                 
708                 var subAttributes = $scope.dcaeModelData.sub_attributes;
709
710                 if(labelManyKey){
711                         var addButton = document.createElement("BUTTON");
712                         var buttonLabel = document.createTextNode("+");       
713                         addButton.appendChild(buttonLabel); 
714                         addButton.setAttribute("class", "btn btn-add-remove");
715                         addButton.setAttribute("ng-click" ,  'addNewChoice("'+labelValue + lableName+'");');
716                         addButton.setAttribute("ng-disabled" , "temp.policy.readOnly");
717                         var removeButton = document.createElement("BUTTON");
718                         var buttonremoveLabel = document.createTextNode("-");       
719                         removeButton.appendChild(buttonremoveLabel); 
720                         removeButton.setAttribute("class", "btn btn-add-remove");
721                         removeButton.setAttribute("ng-click" ,  'removeChoice("'+labelValue +lableName+'");');
722                         removeButton.setAttribute("ng-disabled" , "temp.policy.readOnly"); 
723                         document.getElementById(divID).appendChild(addButton); 
724                         document.getElementById(divID).appendChild(removeButton);
725                         document.getElementById(divID).appendChild(label);
726                         var id = "div."+labelValue+lableName;
727                         var divTag = document.createElement("div");
728                         divTag.setAttribute("id", id); 
729                         document.getElementById(divID).appendChild(divTag);
730                         
731                         var divTag = document.createElement("div");
732                         divTag.setAttribute("id", id +'@0');
733                         
734                         divTag.className += ' children_group'; //here is div with a group of children.
735                         
736                         document.getElementById(id).appendChild(divTag);
737                 }else{
738                         var divTag = document.createElement("div");
739                         divTag.setAttribute("id", "div."+labelValue+lableName);
740                         divTag.className += ' children_group'; //here is div with a group of children.
741                         document.getElementById(divID).appendChild(label);  
742                         document.getElementById(divID).appendChild(divTag);                     
743                 }
744     };
745
746     $scope.dropBoxLayout = function(labelLevel, attributeName, many , refValue, listemunerateValues){
747                 var br = document.createElement("BR");
748
749                 if (labelLevel.length  < 1){
750                         var divID = "DynamicTemplate";
751                 } else if (labelLevel.endsWith('.')){
752                         var divID = 'div.'+ labelLevel.substring(0, labelLevel.length-1);
753                 }       
754
755
756         var label = document.createElement("Label")
757         if (matching.includes(attributeName)){
758                 var labeltext = document.createTextNode(attributeName + "*!");
759         }else {
760                 var labeltext = document.createTextNode(attributeName);
761         }
762         label.appendChild(labeltext);
763
764         var listField = document.createElement("SELECT");
765         listField.setAttribute("class" , "form-control");
766         listField.setAttribute("style" , "width:300px;");
767         listField.setAttribute("ng-disabled" , "temp.policy.readOnly");
768         
769         for (i=0; i < listemunerateValues.length; i += 1) {
770             option = document.createElement('option');
771             option.setAttribute('value', listemunerateValues[i]);
772             option.appendChild(document.createTextNode(listemunerateValues[i]));
773             listField.appendChild(option);
774         }
775         listField.setAttribute("id" , ''+ labelLevel + attributeName + '');
776         
777         enumKeyList.push(attributeName);
778         
779         document.getElementById(divID).appendChild(label);  
780         document.getElementById(divID).appendChild(br); 
781                         
782         if(many == true){
783                 document.getElementById(divID).appendChild(listField).multiple = true;
784                 plainAttributeKeys.push(labelLevel + attributeName+'*'+true);
785         }else {
786                 document.getElementById(divID).appendChild(listField).multiple = false;
787                 plainAttributeKeys.push(labelLevel + attributeName+'*'+false);
788         }
789
790         if($scope.temp.policy.ruleData != null){
791                 if (many == true){
792                         document.getElementById(labelLevel +attributeName).options[0].selected = false;
793                         for (i=0; i < listemunerateValues.length; i += 1) {
794                                 var testValue = $scope.temp.policy.ruleData[labelLevel +attributeName+'@' + i];
795                                 if (testValue === undefined){
796                                         testValue = $scope.temp.policy.ruleData[labelLevel +attributeName];
797                                         }
798                                 var location = listemunerateValues.indexOf(testValue);
799                                 if (location!=-1){
800                                         document.getElementById(labelLevel +attributeName).options[location].selected = true;
801                                         }
802                                 }                       
803                         }else {
804                         document.getElementById(labelLevel + attributeName).value = $scope.temp.policy.ruleData[labelLevel + attributeName];    
805                                 
806                         }
807                 }
808     };
809     
810     function onlyUnique(value, index, self) { 
811         return self.indexOf(value) === index;
812     };
813     
814     
815     function checkDictionary(value){
816         for (i = 0; i < $scope.microServiceAttributeDictionaryDatas.length; i++) {
817                 if ($scope.microServiceAttributeDictionaryDatas[i].name.localeCompare(value)){
818                         return true;
819                 }
820         }
821         
822     }
823     $scope.savePolicy = function(policy){
824         if(policy.itemContent != undefined){
825                 $scope.refreshCheck = true; 
826                 $scope.policyNavigator = policy.itemContent;
827                 policy.itemContent = "";
828         }
829         $scope.savebutton = false;
830         var splitAt = '*';
831         var dot ='.';
832         var jsonPolicy = {};
833         if(plainAttributeKeys != null){
834                 for(a = 0; a < plainAttributeKeys.length; a++){
835                         var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
836                         console.log(splitPlainAttributeKey[1]); 
837                         var searchElement = document.getElementById(splitPlainAttributeKey[0]);
838                         var key = splitPlainAttributeKey[0];
839                 if(searchElement == null){
840                     searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
841                     key = splitPlainAttributeKey[0]+'@0';
842                 }else if (searchElement.nodeName == 'BUTTON'){
843                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
844                                 key = splitPlainAttributeKey[0]+'@0';
845                         }
846                         if(searchElement != null){
847                                 var keySplit = key.split(dot);
848                                 var elumentLocation = keySplit.length;
849                                 var enumKey = key;
850                                 if (elumentLocation > 1){
851                                         enumKey = keySplit[keySplit.length - 1];
852                                 }
853                                 if (enumKeyList.indexOf(enumKey) != -1){
854                                                 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
855                                                         var multiSlect = [];
856                                                         for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
857                                                                 multiSlect.push(searchElement.selectedOptions[i].value);
858                                                                 }
859                                                         jsonPolicy[key]= multiSlect;
860                                                 }else{
861                                                         console.log(" searchElement.value = > " + searchElement.value);
862                                                         jsonPolicy[key]= searchElement.value;
863                                                 }
864                                 } else {
865                                         if(searchElement.value != null){
866                                                         console.log(" searchElement.value = > " + searchElement.value);
867                                                 jsonPolicy[key]= searchElement.value;
868                                         }
869                                 }
870                         }
871                 }
872         }
873         var uuu = "policycreation/save_policy";
874         var postData={policyData: policy, policyJSON : jsonPolicy};
875         $.ajax({
876             type : 'POST',
877             url : uuu,
878             dataType: 'json',
879             contentType: 'application/json',
880             data: JSON.stringify(postData),
881             success : function(data){
882                 $scope.$apply(function(){
883                         $scope.data=data.policyData;
884                         if($scope.data == 'success'){
885                                 $scope.temp.policy.readOnly = 'true';
886                                 $scope.safetyChecker = data.policyData.split("#")[2];
887                                 if ($scope.safetyChecker!=undefined) {
888                                         Notification.success($scope.safetyChecker);
889                                 }
890                                 $scope.pushStatus=data.policyData.split("&")[1];
891                                 if($scope.pushStatus=="successPush"){
892                                         Notification.success("Policy pushed successfully");
893                                 }
894                                 Notification.success("Policy Saved Successfully.");     
895                         }else if ($scope.data == 'PolicyExists'){
896                                                 $scope.savebutton = true;
897                                                 Notification.error("Policy Already Exists with Same Name in Scope.");
898                                         }
899                 });
900                 console.log($scope.data);
901             },
902             error : function(data){
903                 Notification.error("Error Occured while saving Policy.");
904             }
905         });
906     };
907     
908     $scope.validatePolicy = function(policy){
909         document.getElementById("validate").innerHTML = "";
910         var splitAt = '*';
911         var dot ='.';
912         var jsonPolicy = {};
913         if(plainAttributeKeys != null){
914                 for(a = 0; a < plainAttributeKeys.length; a++){
915                         var splitPlainAttributeKey = plainAttributeKeys[a].split(splitAt);
916                         console.log(splitPlainAttributeKey[1]); 
917                         var searchElement = document.getElementById(splitPlainAttributeKey[0]);
918                         var key = splitPlainAttributeKey[0];
919                         if(searchElement == null){
920                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
921                                 key = splitPlainAttributeKey[0]+'@0';
922                         }else if (searchElement.nodeName == 'BUTTON'){
923                                 searchElement = document.getElementById(splitPlainAttributeKey[0]+'@0');
924                                 key = splitPlainAttributeKey[0]+'@0';
925                         }
926                         if(searchElement != null){
927                                 if (enumKeyList.indexOf(key) != -1){
928                                                 if (splitPlainAttributeKey[1].indexOf("true") !== -1){
929                                                         var multiSlect = [];
930                                                         for ( var i = 0; i < searchElement.selectedOptions.length; i++) {
931                                                                 multiSlect.push(searchElement.selectedOptions[i].value);
932                                                                 }
933                                                         jsonPolicy[key]= multiSlect;
934                                                 }else{
935                                                         jsonPolicy[key]= searchElement.value;
936                                                 }
937                                 } else {
938                                         if(searchElement.value != null){
939                                                 jsonPolicy[key]= searchElement.value;
940                                                 if(searchElement.getAttribute("required")){
941                                                         if(!searchElement.value){
942                                                                 return;
943                                                         }
944                                                 }                                               
945                                         }
946                                 }
947                         }
948                 }
949         }
950         var uuu = "policyController/validate_policy.htm";
951         var postData={policyData: policy, policyJSON : jsonPolicy};
952                 $.ajax({
953                         type : 'POST',
954                         url : uuu,
955                         dataType: 'json',
956                         contentType: 'application/json',
957                         data: JSON.stringify(postData),
958                         success : function(data){
959                                 $scope.$apply(function(){
960                                         $scope.validateData = data.data.replace(/\"/g, "");
961                                         $scope.data=data.data.substring(1,8);
962                                                 var size = data.data.length;
963                                                 if($scope.data == 'success'){
964                                                         Notification.success("Validation Success.");
965                                                         $scope.savebutton = false;
966                                                         if (size > 18){
967                                                                 var displayWarning = data.data.substring(19,size  - 1);
968                                                                 document.getElementById("validate").innerHTML = "Safe Policy Warning Message  :  "+displayWarning;
969                                                                 document.getElementById("validate").style.color = "white";
970                                                                 document.getElementById("validate").style.backgroundColor = "skyblue";
971                                                         }
972                                                 }else{
973                                                         Notification.error("Validation Failed.");
974                                                         document.getElementById("validate").innerHTML = $scope.validateData;
975                                                         document.getElementById("validate").style.color = "white";
976                                                         document.getElementById("validate").style.backgroundColor = "red";
977                                                         $scope.savebutton = true;
978                                                 }
979                                                 
980                                 });
981                                 console.log($scope.data);       
982                         },
983                         error : function(data){
984                                 Notification.error("Validation Failed.");
985                                 $scope.savebutton = true;
986                         }
987                 });
988     };
989
990     function extend(obj, src) {
991         for (var key in src) {
992             if (src.hasOwnProperty(key)) obj[key] = src[key];
993         }
994         return obj;
995     }
996 }]);