Automatic Config Policy Ui generation
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / ToscaModelCtrl.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23 app.controller('ToscaModelCtrl',
24     ['$scope', '$rootScope', '$modalInstance', '$location', 'dialogs', 'toscaModelService',
25     function($scope, $rootScope, $modalInstance, $location, dialogs, toscaModelService) {
26
27         $scope.jsonByPolicyType = function(selectedPolicy, oldSelectedPolicy, editorData){
28                 if (selectedPolicy && selectedPolicy != '') {
29                         toscaModelService.getHpModelJsonByPolicyType(selectedPolicy).then(function(response) {
30                                 $('#editor').empty();
31                                 // get the list of available policies
32                                 $scope.getPolicyList();
33                                 var toscaModel = JSON.parse(response.body.toscaModelJson);
34                                 if($scope.policyList && toscaModel.schema.properties && toscaModel.schema.properties.policyList){
35                                         toscaModel.schema.properties.policyList.enum = $scope.policyList;
36                                 }
37
38                                 JSONEditor.defaults.options.theme = 'bootstrap3';
39                                 JSONEditor.defaults.options.iconlib = 'bootstrap2';
40                                 JSONEditor.defaults.options.object_layout = 'grid';
41                                 JSONEditor.defaults.options.disable_properties = true;
42                                 JSONEditor.defaults.options.disable_edit_json = true;
43                                 JSONEditor.defaults.options.disable_array_reorder = true;
44                                 JSONEditor.defaults.options.disable_array_delete_last_row = true;
45                                 JSONEditor.defaults.options.disable_array_delete_all_rows = false;
46                                 JSONEditor.defaults.options.show_errors = 'always';
47
48                                 if($scope.editor) { $scope.editor.destroy(); }
49                                 $scope.editor = new JSONEditor(document.getElementById("editor"),
50                                                       { schema: toscaModel.schema, startval: editorData });
51                                 $scope.editor.watch('root.policy.recipe',function() {
52
53                                 });
54                                 $('#form1').show();
55                         });
56                 } else {
57                                 $('#editor').empty();
58                                 $('#form1').hide();
59                         }
60         }
61
62         $scope.getPolicyList = function(){
63                         var policyNameList = [];
64                         if (typeof elementMap !== 'undefined'){
65                                 for (key in elementMap){
66                                         if (key.indexOf('Policy')>-1){
67                                                 angular.forEach(Object.keys(elementMap[key]), function(text, val){
68                                                         for (policyKey in elementMap[key][text]){
69                                                                 if(elementMap[key][text][policyKey].name == 'pname'){
70                                                                         policyNameList.push(elementMap[key][text][policyKey].value);
71                                                                 }
72                                                         }
73                                                 });
74                                         }
75                                 }
76                         };
77                         $scope.policyList = policyNameList;
78                 }
79
80         if($rootScope.selectedBoxName) {
81                 var policyType = $rootScope.selectedBoxName.split('_')[0].toLowerCase();
82                 $scope.toscaModelName = policyType.toUpperCase() + " Microservice";
83                 if(elementMap[lastElementSelected]) {
84                         $scope.jsonByPolicyType(policyType, '', elementMap[lastElementSelected][policyType]);
85                 }else{
86                         $scope.jsonByPolicyType(policyType, '', '');
87                 }
88             }
89
90         $scope.getEditorData = function(){
91                 if(!$scope.editor){
92                         return null;
93                 }
94                 var errors = $scope.editor.validate();
95                 var editorData = $scope.editor.getValue();
96
97                 if(errors.length) {
98                         $scope.displayErrorMessage(errors);
99                         return null;
100                 }
101                 else{
102                         console.log("there are NO validation errors........");
103                 }
104                 return editorData;
105         }
106
107         $scope.saveToscaProps = function(){
108                 var policyType = $rootScope.selectedBoxName.split('_')[0].toLowerCase();
109                 var data = $scope.getEditorData();
110
111             if(data !== null) {
112                 data = {[policyType]: data};
113                         saveProperties(data);
114                         if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
115                         $modalInstance.close('closed');
116                 }
117         }
118
119         $scope.displayErrorMessage = function(errors){
120                 console.log("there are validation errors.....");
121                 var all_errs = "Please address the following issues before selecting 'Done' or 'Policy Types':\n";
122                 for (var i = 0; i < errors.length; i++) {
123                   if(all_errs.indexOf(errors[i].message) < 0) {
124                         all_errs += '\n' + errors[i].message;
125                   }
126                 }
127             window.alert(all_errs);
128         };
129
130         $scope.close = function(){
131                 angular.copy(elementMap[lastElementSelected], $scope.hpPolicyList);
132                         $modalInstance.close('closed');
133                         if($scope.editor) { $scope.editor.destroy(); $scope.editor = null; }
134         }
135
136         $scope.checkDuplicateInObject = function(propertyName, inputArray) {
137                   var seenDuplicate = false,
138                       testObject = {};
139
140                   inputArray.map(function(item) {
141                     var itemPropertyName = item[propertyName];
142                     if (itemPropertyName in testObject) {
143                       testObject[itemPropertyName].duplicate = true;
144                       item.duplicate = true;
145                       seenDuplicate = true;
146                     }
147                     else {
148                       testObject[itemPropertyName] = item;
149                       delete item.duplicate;
150                     }
151                   });
152
153                   return seenDuplicate;
154                 }
155 }
156 ]);