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