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