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