Remove SDC query
[clamp.git] / src / main / resources / META-INF / resources / designer / scripts / CldsModelService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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
24 .service(
25 'cldsModelService',
26 [
27     'alertService',
28     '$http',
29     '$q',
30     '$rootScope',
31     function(alertService, $http, $q, $rootScope) {
32
33             function checkIfElementType(name, isSimple) {
34
35         //This will open the methods located in the app.js
36                   if (isSimple){
37                           if (undefined == name) {
38                                   return;
39                           }else if (name.toLowerCase().indexOf("policy") >= 0){
40                                           PolicyWindow();
41                           } else {
42                                   $rootScope.selectedBoxName = name.toLowerCase();
43                                   ToscaModelWindow();
44                           }
45                   } else {
46                           if (undefined == name) {
47                                   return;
48                           }
49                         mapping = {
50                                 'tca' : TCAWindow,
51                                 'policy' : PolicyWindow,
52                                 'vescollector' : VesCollectorWindow,
53                                 'holmes' : HolmesWindow,
54                             };
55                             key = name.split('_')[0].toLowerCase()
56                             if (key in mapping) {
57                                     mapping[key]();
58                             }
59                   };
60             }
61             function handleQueryToBackend(def, svcAction, svcUrl, svcPayload) {
62
63                     $http.put(svcUrl, svcPayload).success(
64                     function(data) {
65
66                             def.resolve(data);
67                             if (typeof data.statusCodeValue === 'undefined'
68                             || data.statusCodeValue === 200) {
69                                     alertService.alertMessage(
70                                     "Action Successful: " + svcAction, 1)
71                             } else {
72                                     if (typeof data.body !== 'undefined') {
73                                             alertService.alertMessage("Action Failure: "
74                                             + svcAction + ", " + data.body.errorMessageForUi, 2);
75                                     } else {
76                                             alertService.alertMessage("Action Failure: "
77                                             + svcAction, 2);
78                                     }
79                                     def.reject(svcAction + " not successful");
80                             }
81                     }).error(
82                     function(data) {
83
84                             def.resolve(data);
85                             if (typeof data.body !== 'undefined') {
86                                     alertService.alertMessage("Action Failure: " + svcAction
87                                     + ", " + data.body.errorMessageForUi, 2);
88                             } else {
89                                     alertService
90                                     .alertMessage("Action Failure: " + svcAction, 2);
91                             }
92                             def.reject(svcAction + " not successful");
93                     });
94             }
95             this.toggleDeploy = function(uiAction, modelName, controlNamePrefixIn,
96                                          bpmnTextIn, propTextIn, svgXmlIn,
97                                          templateName, typeID, controlNameUuid,
98                                          modelEventService, deploymentId) {
99
100                     var def = $q.defer();
101                     var sets = [];
102                     var action = uiAction.toLowerCase();
103                     var deployUrl = "/restservices/clds/v1/clds/" + action + "/"
104                     + modelName;
105                     var requestData = {
106                         name : modelName,
107                         controlNamePrefix : controlNamePrefixIn,
108                         bpmnText : bpmnTextIn,
109                         propText : propTextIn,
110                         imageText : svgXmlIn,
111                         templateName : templateName,
112                         typeId : typeID,
113                         controlNameUuid : controlNameUuid,
114                         event : modelEventService,
115                         deploymentId : deploymentId
116                     };
117                     handleQueryToBackend(def, action, deployUrl, requestData);
118                     return def.promise;
119             };
120             this.getModel = function(modelName) {
121
122                     var def = $q.defer();
123                     var sets = [];
124                     var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;
125                     $http.get(svcUrl).success(function(data) {
126
127                             def.resolve(data);
128                     }).error(function(data) {
129
130                             def.reject("Open Model not successful");
131                     });
132                     return def.promise;
133             };
134             this.getSavedModel = function() {
135
136                     var def = $q.defer();
137                     var sets = [];
138                     var svcUrl = "/restservices/clds/v1/clds/model-names";
139                     $http.get(svcUrl).success(function(data) {
140
141                             def.resolve(data);
142                     }).error(function(data) {
143
144                             def.reject("Open Model not successful");
145                     });
146                     return def.promise;
147             };
148             this.setModel = function(modelName, controlNamePrefixIn, bpmnTextIn,
149                                      propTextIn) {
150
151                     var def = $q.defer();
152                     var sets = [];
153                     var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;
154                     var svcRequest = {
155                         name : modelName,
156                         controlNamePrefix : controlNamePrefixIn,
157                         bpmnText : bpmnTextIn,
158                         propText : propTextIn
159                     };
160                     $http.put(svcUrl, svcRequest).success(function(data) {
161
162                             def.resolve(data);
163                     }).error(function(data) {
164
165                             def.reject("Save Model not successful");
166                     });
167                     return def.promise;
168             };
169             this.processAction = function(uiAction, modelName, controlNamePrefixIn,
170                                           bpmnTextIn, propTextIn, svgXmlIn,
171                                           templateName, typeID, deploymentId) {
172
173                     var def = $q.defer();
174                     var sets = [];
175                     // console.log(svgXmlIn);
176                     var svcUrl = "/restservices/clds/v1/clds/";
177                     var svcAction = uiAction.toLowerCase();
178                     if (svcAction == "save" || svcAction == "refresh") {
179                             svcUrl = svcUrl + "model/" + modelName;
180                     } else if (svcAction == "test") {
181                             svcUrl = svcUrl + "action/submit/" + modelName + "?test=true";
182                     } else {
183                             svcUrl = svcUrl + "action/" + svcAction + "/" + modelName;
184                     }
185                     var svcRequest = {
186                         name : modelName,
187                         controlNamePrefix : controlNamePrefixIn,
188                         bpmnText : bpmnTextIn,
189                         propText : propTextIn,
190                         imageText : svgXmlIn,
191                         templateName : templateName,
192                         typeId : typeID,
193                         deploymentId : deploymentId
194                     };
195                     handleQueryToBackend(def, svcAction, svcUrl, svcRequest);
196                     return def.promise;
197             };
198             this.manageAction = function(modelName, typeId, typeName) {
199
200                     var def = $q.defer();
201                     var sets = [];
202                     var config = {
203                         url : "/restservices/clds/v1/clds/getDispatcherInfo",
204                         method : "GET",
205                         params : {
206                             model : modelName,
207                             typeId : typeId,
208                             typeName : typeName
209                         }
210                     };
211                     $http(config).success(function(data) {
212
213                             def.resolve(data);
214                     }).error(function(data) {
215
216                             def.reject("manage action not successful");
217                     });
218                     return def.promise;
219             };
220             this.checkPermittedActionCd = function(permittedActionCd, menuText,
221                                                    actionCd) {
222
223                     if (permittedActionCd.indexOf(actionCd) > -1) {
224                             document.getElementById(menuText).classList.remove('ThisLink');
225                     } else {
226                             document.getElementById(menuText).classList.add('ThisLink');
227                     }
228             };
229             this.processActionResponse = function(modelName, pars, simple) {
230
231                     // populate control name (prefix and uuid here)
232                     var controlNamePrefix = pars.controlNamePrefix;
233                     var controlNameUuid = pars.controlNameUuid;
234                     deploymentId = pars.deploymentId;
235                     var modelEventService = pars.modelEventService;
236                     typeID = pars.typeId;
237                     var headerText = "Closed Loop Modeler - " + modelName;
238                     if (controlNameUuid != null) {
239                             var actionCd = pars.event.actionCd;
240                             var actionStateCd = pars.event.actionStateCd;
241                             headerText = headerText + " [" + controlNamePrefix
242                             + controlNameUuid + "]";
243                     }
244                     document.getElementById("modeler_name").textContent = headerText;
245                     document.getElementById("templa_name").textContent = ("Template Used - " + selected_template);
246                     setStatus(pars)
247                     disableBPMNAddSVG(pars, simple);
248                     this.enableDisableMenuOptions(pars);
249             };
250             this.processRefresh = function(pars) {
251
252                     var newPars = pars;
253                     if (typeof pars.body !== 'undefined') {
254                             newPars = pars.body;
255                     }
256                     typeID = newPars.typeId;
257                     deploymentId = newPars.deploymentId;
258                     setStatus(newPars);
259                     this.enableDisableMenuOptions(newPars);
260                     elementMap = JSON.parse(newPars.propText);
261             }
262             function setStatus(pars) {
263
264                     var status = pars.status;
265                     // apply color to status
266                     var statusColor = 'white';
267                     if (status.trim() === "DESIGN") {
268                             statusColor = 'gray'
269                     } else if (status.trim() === "DISTRIBUTED") {
270                             statusColor = 'blue'
271                     } else if (status.trim() === "ACTIVE") {
272                             statusColor = 'green'
273                     } else if (status.trim() === "STOPPED") {
274                             statusColor = 'red'
275                     } else if (status.trim() === "DELETING") {
276                             statusColor = 'pink'
277                     } else if (status.trim() === "ERROR") {
278                             statusColor = 'orange'
279                     } else if (status.trim() === "UNKNOWN") {
280                             statusColor = 'blue'
281                     } else {
282                             statusColor = null;
283                     }
284                     var statusMsg = '<span style="background-color:'
285                     + statusColor
286                     + ';-moz-border-radius: 50px;  -webkit-border-radius: 50px;  border-radius: 50px;">&nbsp;&nbsp;&nbsp;'
287                     + status + '&nbsp;&nbsp;&nbsp;</span>';
288                     // display status
289                     if ($("#status_clds").length >= 1)
290                             $("#status_clds").remove();
291                     $("#activity_modeler")
292                     .append(
293                     '<span id="status_clds" style="position: absolute;  left: 61%;top: 151px; font-size:20px;">Status: '
294                     + statusMsg + '</span>');
295             }
296             function disableBPMNAddSVG(pars, simple) {
297
298                     var svg = pars.imageText.substring(pars.imageText.indexOf("<svg"))
299                     if ($("#svgContainer").length > 0)
300                             $("#svgContainer").remove();
301                     $("#js-canvas").append(
302                     "<span id=\"svgContainer\">" + svg + "</span>");
303                     /* added code for height width viewBox */
304                     $("#svgContainer svg").removeAttr("height");
305                     $("#svgContainer svg").removeAttr('viewBox');
306                     $("#svgContainer svg").removeAttr('width');
307                     $("#svgContainer svg").attr('width', '100%');
308                     $("#svgContainer svg").attr('height', '100%');
309                     $("#svgContainer").click(
310                     function(event) {
311
312                             // console.log($(event.target).parent().html())
313                             // console.log($($(event.target).parent()).attr("data-element-id"))
314                             var name = $($(event.target).parent()).attr("data-element-id")
315                             lastElementSelected = $($(event.target).parent()).attr(
316                             "data-element-id")
317                             checkIfElementType(name, simple)
318                     });
319             }
320             this.enableDisableMenuOptions = function(pars) {
321
322                     var permittedActionCd = pars.permittedActionCd;
323                     // dropdown options - always true
324                     document.getElementById('Open CL').classList.remove('ThisLink');
325                     if (readMOnly) {
326                             // enable model options
327                             document.getElementById('Properties CL').classList
328                             .remove('ThisLink');
329                             document.getElementById('Close Model').classList
330                             .remove('ThisLink');
331                             // disable models options
332                             document.getElementById('Save CL').classList.add('ThisLink');
333                             document.getElementById('Revert Model Changes').classList
334                             .add('ThisLink');
335                     } else {
336                             // enable menu options
337                             document.getElementById('Save CL').classList.remove('ThisLink');
338                             document.getElementById('Properties CL').classList
339                             .remove('ThisLink');
340                             document.getElementById('Revert Model Changes').classList
341                             .remove('ThisLink');
342                             document.getElementById('Close Model').classList
343                             .remove('ThisLink');
344                             document.getElementById('Validation Test').classList
345                             .remove('ThisLink');
346                             document.getElementById('Refresh Status').classList
347                             .remove('ThisLink');
348                     }
349                     // enable/disable menu options based on permittedActionCd
350                     // list
351                     this.checkPermittedActionCd(permittedActionCd, 'Validation Test',
352                     'TEST');
353                     this.checkPermittedActionCd(permittedActionCd, 'Submit', 'SUBMIT');
354                     this.checkPermittedActionCd(permittedActionCd, 'Resubmit',
355                     'RESUBMIT');
356                     this.checkPermittedActionCd(permittedActionCd, 'Update', 'UPDATE');
357                     this.checkPermittedActionCd(permittedActionCd, 'Stop', 'STOP');
358                     this
359                     .checkPermittedActionCd(permittedActionCd, 'Restart', 'RESTART');
360                     this.checkPermittedActionCd(permittedActionCd, 'Delete', 'DELETE');
361                     this.checkPermittedActionCd(permittedActionCd, 'Deploy', 'DEPLOY');
362                     this.checkPermittedActionCd(permittedActionCd, 'UnDeploy',
363                     'UNDEPLOY');
364             }
365     } ]);