Merge "Error in JS logs when click in the background"
[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 = {name: modelName, 
55     controlNamePrefix: controlNamePrefixIn, 
56     bpmnText: bpmnTextIn, 
57    propText: propTextIn, 
58    imageText:svgXmlIn, 
59    templateName:templateName,
60    typeId:typeID,
61    controlNameUuid: controlNameUuid,
62    event:modelEventService,
63    deploymentId:deploymentId};
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 = {name: modelName, controlNamePrefix: controlNamePrefixIn, bpmnText: bpmnTextIn, propText: propTextIn};
164         
165       
166         $http.put(svcUrl, svcRequest)
167         .success(function(data){          
168           def.resolve(data);          
169           
170         })
171         .error(function(data){                
172           def.reject("Save Model not successful");
173         });
174         
175         return def.promise;
176     };
177     this.processAction = function(uiAction, modelName, controlNamePrefixIn, bpmnTextIn, propTextIn,svgXmlIn,templateName,typeID,deploymentId){
178       
179       
180       var def = $q.defer();
181       var sets = [];
182       
183       //console.log(svgXmlIn);
184       var svcUrl = "/restservices/clds/v1/clds/";        
185       var svcAction = uiAction.toLowerCase();
186       if ( svcAction == "save" || svcAction == "refresh" ) {
187         svcUrl = svcUrl + "model/" + modelName;
188       } else if ( svcAction == "test" ) {
189         svcUrl = svcUrl + "action/submit/" + modelName + "?test=true";
190       } else {
191           svcUrl = svcUrl + "action/" + svcAction + "/" + modelName;
192         }
193       
194       var svcRequest = {name: modelName, controlNamePrefix: controlNamePrefixIn, bpmnText: bpmnTextIn, propText: propTextIn, imageText:svgXmlIn, templateName:templateName, typeId:typeID, deploymentId:deploymentId};      
195       
196         $http.put(svcUrl, svcRequest)
197         .success(function(data){
198           def.resolve(data);   
199           alertService.alertMessage("Action Successful:"+uiAction,1)        
200           
201         })
202         .error(function(data){  
203           alertService.alertMessage("Action Failure:"+uiAction,2);
204           //def         alertService.alertMessage("Action Successful:"+uiAction,1);
205           def.reject(svcAction + " not successful");
206         });
207         
208         return def.promise;
209     };
210     this.manageAction = function(modelName,typeId,typeName){
211       var def = $q.defer();
212       var sets = [];
213
214       var config = {
215        url: "/restservices/clds/v1/clds/getDispatcherInfo",
216        method: "GET",
217        params:{model:modelName,typeId:typeId,typeName:typeName}
218      };
219        $http(config)
220         .success(function(data){          
221           def.resolve(data);          
222           
223         })
224         .error(function(data){                
225           def.reject("manage action not successful");
226         });
227         
228         return def.promise;
229
230     };
231     this.checkPermittedActionCd = function(permittedActionCd, menuText, actionCd){
232         if ( permittedActionCd.indexOf(actionCd) > -1 ) {
233           document.getElementById(menuText).classList.remove('ThisLink');
234         } else {
235           document.getElementById(menuText).classList.add('ThisLink');
236         }
237     };        
238     this.processActionResponse = function(modelName, pars){
239       
240       // populate control name (prefix and uuid here)
241         var controlNamePrefix = pars.controlNamePrefix;
242         var controlNameUuid = pars.controlNameUuid;
243         deploymentId = pars.deploymentId;
244         var modelEventService = pars.modelEventService;
245         typeID = pars.typeId;
246         
247         var headerText = "Closed Loop Modeler - " +  modelName;
248         if ( controlNameUuid != null ) {
249           var actionCd = pars.event.actionCd;
250           var actionStateCd = pars.event.actionStateCd;
251           //headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "] [" + actionCd + ":" + actionStateCd + "]";
252           headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "]";
253         }
254           
255       document.getElementById("modeler_name").textContent = headerText;
256       document.getElementById("templa_name").textContent = ("Template Used - " + selected_template);
257       setStatus(pars)
258       disableBPMNAddSVG(pars);
259         this.enableDisableMenuOptions(pars);
260     
261         
262     };
263     
264     this.processRefresh=function(pars){
265       typeID = pars.typeId;
266       deploymentId = pars.deploymentId;
267       setStatus(pars);
268       this.enableDisableMenuOptions(pars);
269     }
270     function setStatus(pars){
271       
272       var status = pars.status;
273       // apply color to status
274     var statusColor='white';
275         if(status.trim()==="DESIGN"){
276           statusColor='gray'
277         } else if(status.trim()==="DISTRIBUTED"){
278           statusColor='blue'
279         } else if(status.trim()==="ACTIVE"){
280           statusColor='green'
281         } else if(status.trim()==="STOPPED"){
282           statusColor='red'
283         } else if(status.trim()==="DELETING"){
284           statusColor='pink'
285         } else if(status.trim()==="ERROR"){
286           statusColor='orange'
287         } else if(status.trim()==="UNKNOWN"){
288           statusColor='blue'
289         } else{
290           statusColor=null;
291         }
292         
293
294         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>';
295         // display status
296         if ($("#status_clds").length>=1)
297           $("#status_clds").remove();
298         $("#activity_modeler").append('<span id="status_clds" style="position: absolute;  left: 61%;top: 51px; font-size:20px;">Status: '+statusMsg+'</span>');
299
300         
301     }
302     function disableBPMNAddSVG(pars){
303       
304       var svg=pars.imageText.substring(pars.imageText.indexOf("<svg"))
305       if($("#svgContainer").length>0)
306           $("#svgContainer").remove();
307         $("#js-canvas").append("<span id=\"svgContainer\">"+svg+"</span>");
308         /* added code for height width viewBox */
309         $("#svgContainer svg").removeAttr("height");
310         $("#svgContainer svg").removeAttr('viewBox');
311         $("#svgContainer svg").removeAttr('width');
312         
313         $("#svgContainer svg").attr('width','100%');
314         $("#svgContainer svg").attr('height','100%');
315
316         $("#svgContainer").click(function(event){
317           //console.log($(event.target).parent().html())
318           //console.log($($(event.target).parent()).attr("data-element-id"))
319           var name=$($(event.target).parent()).attr("data-element-id")
320           lastElementSelected=$($(event.target).parent()).attr("data-element-id")
321           checkIfElementType(name)
322           
323         })
324         $(".bjs-container").attr("hidden","");
325     }
326     this.enableDisableMenuOptions=function(pars){
327       
328       var permittedActionCd = pars.permittedActionCd;
329       
330       //dropdown options - always true
331       document.getElementById('Open Template').classList.remove('ThisLink');
332           document.getElementById('Open CL').classList.remove('ThisLink');
333           document.getElementById('Save Template').classList.add('ThisLink');
334           document.getElementById('Template Properties').classList.add('ThisLink');
335           document.getElementById('Revert Template Changes').classList.add('ThisLink');
336           document.getElementById('Close Template').classList.add('ThisLink'); 
337                 
338       if (readOnly||readMOnly){
339         //enable model options
340                 document.getElementById('Properties CL').classList.remove('ThisLink');
341                 document.getElementById('Close Model').classList.remove('ThisLink');
342                 
343                 //disable models options
344                 document.getElementById('Create CL').classList.add('ThisLink');
345                 document.getElementById('Save CL').classList.add('ThisLink');
346         document.getElementById('Revert Model Changes').classList.add('ThisLink');                      
347           } else {
348               // enable menu options
349                   document.getElementById('Create CL').classList.remove('ThisLink');
350               document.getElementById('Save CL').classList.remove('ThisLink');
351               document.getElementById('Properties CL').classList.remove('ThisLink');
352               document.getElementById('Revert Model Changes').classList.remove('ThisLink');
353               document.getElementById('Close Model').classList.remove('ThisLink');
354               
355               
356               document.getElementById('Validation Test').classList.remove('ThisLink');
357               document.getElementById('Refresh Status').classList.remove('ThisLink');      
358               document.getElementById('Refresh ASDC').classList.remove('ThisLink');     
359           } 
360       if (readTOnly){
361                 document.getElementById('Create Template').classList.add('ThisLink');
362           } else {
363                 document.getElementById('Create Template').classList.remove('ThisLink');
364           }
365       
366                 // enable/disable menu options based on permittedActionCd list
367                 this.checkPermittedActionCd(permittedActionCd, 'Validation Test', 'TEST');
368                 this.checkPermittedActionCd(permittedActionCd, 'Submit', 'SUBMIT');
369                 this.checkPermittedActionCd(permittedActionCd, 'Resubmit', 'RESUBMIT');
370                 this.checkPermittedActionCd(permittedActionCd, 'Update', 'UPDATE');
371                 this.checkPermittedActionCd(permittedActionCd, 'Stop', 'STOP');
372                 this.checkPermittedActionCd(permittedActionCd, 'Restart', 'RESTART');
373                 this.checkPermittedActionCd(permittedActionCd, 'Delete', 'DELETE');
374                 this.checkPermittedActionCd(permittedActionCd, 'Deploy', 'DEPLOY');
375                 this.checkPermittedActionCd(permittedActionCd, 'UnDeploy', 'UNDEPLOY');
376                 
377         }
378     
379    
380     this.getASDCServices().then(function(pars){
381       
382       var services=pars.service;
383       asdc_Services=services
384     });
385     
386  }]);