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