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