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