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