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