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