[CLAMP-1] Initial ONAP CLAMP seed code commit
[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         console.log("/////////////cldsModelService");
26         function checkIfElementType(name){
27
28                 //This will open the methods located in the app.js
29                 if(name.toLowerCase().indexOf("stringmatch")>=0)
30                         StringMatchWindow();
31                 else if(name.toLowerCase().indexOf("tca")>=0)
32                         TCAWindow();
33                 else if(name.toLowerCase().indexOf("collector")>=0)
34                         CollectorsWindow();
35                 else if(name.toLowerCase().indexOf("policy")>=0)
36                         PolicyWindow();
37                 
38         }
39         this.getASDCServices = function(){
40         
41         console.log("getASDCServices");
42         var def = $q.defer();
43         var sets = [];
44         
45         var svcUrl = "/restservices/clds/v1/clds/asdc/services/";                
46         
47         $http.get(svcUrl)
48         .success(function(data){                
49                 def.resolve(data);   
50                 
51                 
52         })
53         .error(function(data){                        
54                 def.reject("Asdc Services not found");
55                 
56         });
57         
58         return def.promise;
59     };
60     
61     this.getRunningInstances=function(resouseInput){
62       console.log("getRunningInstances");
63         var def = $q.defer();
64         var sets = [];
65         
66         //Look in scripts/common_variables.html to get utmModel name
67         
68     // var svcUrl = "/restservices/clds/v1/clds/model/roncl003";                
69         var svcUrl = "/restservices/clds/v1/clds/model/" + resouseInput;                
70         
71        $http.get(svcUrl)
72        .success(function(data){         
73         def.resolve(data);   
74         
75         
76        })
77        .error(function(data){                         
78                 def.reject("Asdc Services not found");
79                 
80        });
81         
82        return def.promise;
83         
84         // return [{"name":"asbg0003vm001","location":"SNANTXCA","status":"Running","view":"KPI"},{"name":"asbg0003vm002","location":"SNANTXCA","status":"Running","view":"KPI"},{"name":"asbg0003vm003","location":"SNANTXCA","status":"Running","view":"KPI"},{"name":"asbg0003vm004","location":"SNANTXCA","status":"Stopped","view":"KPI"}]
85     }
86     
87 this.getASDCService = function(uuid){
88         console.log("getASDCService");
89         
90         var def = $q.defer();
91         var sets = [];
92         
93         var svcUrl = "/restservices/clds/v1/clds/asdc/services/" + uuid;                
94         
95         $http.get(svcUrl)
96         .success(function(data){   
97                 def.resolve(data);              
98                 
99         })
100         .error(function(data){                        
101                 def.reject("ASDC service not found");
102         });
103         
104         return def.promise;
105     };
106     this.getModel = function(modelName){
107         
108         console.log("getModel");
109         var def = $q.defer();
110         var sets = [];
111         
112         var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;                
113         
114         $http.get(svcUrl)
115         .success(function(data){                
116                 def.resolve(data);              
117                 
118         })
119         .error(function(data){                        
120                 def.reject("Open Model not successful");
121         });
122         
123         return def.promise;
124     };
125     this.getSavedModel=function(){
126       console.log("getSavedModel");
127         var def = $q.defer();
128         var sets = [];
129         
130         var svcUrl = "/restservices/clds/v1/clds/model-names";                
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.setModel = function(modelName, controlNamePrefixIn, bpmnTextIn, propTextIn){
144         
145         console.log("setModel");
146         var def = $q.defer();
147         var sets = [];
148
149         var svcUrl = "/restservices/clds/v1/clds/model/" + modelName;        
150         var svcRequest = {name: modelName, controlNamePrefix: controlNamePrefixIn, bpmnText: bpmnTextIn, propText: propTextIn};
151         
152         
153         $http.put(svcUrl, svcRequest)
154         .success(function(data){                
155                 def.resolve(data);              
156                 
157         })
158         .error(function(data){                        
159                 def.reject("Save Model not successful");
160         });
161         
162         return def.promise;
163     };
164     this.processAction = function(uiAction, modelName, controlNamePrefixIn, bpmnTextIn, propTextIn,svgXmlIn,templateName){
165         
166         console.log("processAction");
167         var def = $q.defer();
168         var sets = [];
169         console.log("Generated SVG xml File...");
170         //console.log(svgXmlIn);
171         var svcUrl = "/restservices/clds/v1/clds/";        
172         var svcAction = uiAction.toLowerCase();
173         if ( svcAction == "save" || svcAction == "refresh" ) {
174                 svcUrl = svcUrl + "model/" + modelName;
175         } else if ( svcAction == "test" ) {
176                 svcUrl = svcUrl + "action/submit/" + modelName + "?test=true";
177         } else {
178                 svcUrl = svcUrl + "action/" + svcAction + "/" + modelName;
179         }
180         
181         var svcRequest = {name: modelName, controlNamePrefix: controlNamePrefixIn, bpmnText: bpmnTextIn, propText: propTextIn, imageText:svgXmlIn, templateName:templateName};      
182         console.log(svcRequest)
183         $http.put(svcUrl, svcRequest)
184         .success(function(data){
185                 def.resolve(data);   
186           alertService.alertMessage("Action Successful:"+uiAction,1)            
187                 
188         })
189         .error(function(data){  
190                 alertService.alertMessage("Action Failure:"+uiAction,2);
191                 //def           alertService.alertMessage("Action Successful:"+uiAction,1);
192                 def.reject(svcAction + " not successful");
193         });
194         
195         return def.promise;
196     };
197     this.checkPermittedActionCd = function(permittedActionCd, menuText, actionCd){
198       console.log("checkPermittedActionCd");
199         if ( permittedActionCd.indexOf(actionCd) > -1 ) {
200                 document.getElementById(menuText).classList.remove('ThisLink');
201         } else {
202                 document.getElementById(menuText).classList.add('ThisLink');
203         }
204     };        
205     this.processActionResponse = function(modelName, pars){
206       console.log("processActionResponse");
207         // populate control name (prefix and uuid here)
208         var controlNamePrefix = pars.controlNamePrefix;
209         var controlNameUuid = pars.controlNameUuid;
210         
211        
212         
213         var headerText = "Closed Loop Modeler - " + modelName;
214         if ( controlNameUuid != null ) {
215                 var actionCd = pars.event.actionCd;
216                 var actionStateCd = pars.event.actionStateCd;
217                 //headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "] [" + actionCd + ":" + actionStateCd + "]";
218                 headerText = headerText + " [" + controlNamePrefix + controlNameUuid + "]";
219                 console.log("MINA PARS TEST " + headerText + " ----- " + controlNamePrefix + " ----- " + controlNameUuid + "  ----- " + pars);
220         }
221                 
222         document.getElementById("modeler_name").textContent = headerText;
223         setStatus(pars)
224         disableBPMNAddSVG(pars);
225         this.enableDisableMenuOptions(pars);
226                 
227         
228     };
229     
230     this.processRefresh=function(pars){
231       console.log("processRefresh");
232         setStatus(pars);
233     }
234     function setStatus(pars){
235       console.log("setStatus");
236         var status = pars.status;
237         // apply color to status
238                 var statusColor='white';
239         if(status.trim()==="DESIGN"){
240                 statusColor='gray'
241         } else if(status.trim()==="DISTRIBUTED"){
242                 statusColor='blue'
243         } else if(status.trim()==="ACTIVE"){
244                 statusColor='green'
245         } else if(status.trim()==="STOPPED"){
246                 statusColor='red'
247         } else if(status.trim()==="DELETING"){
248                 statusColor='pink'
249         } else if(status.trim()==="ERROR"){
250                 statusColor='orange'
251         } else if(status.trim()==="UNKNOWN"){
252                 statusColor='blue'
253         } else{
254                 statusColor=null;
255         }
256         
257
258         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>';
259         // display status
260         if ($("#status_clds").length>=1)
261                 $("#status_clds").remove();
262         $("#activity_modeler").append('<span id="status_clds" style="position: absolute;  left: 61%;top: 51px; font-size:20px;">Status: '+statusMsg+'</span>');
263
264         
265     }
266     function disableBPMNAddSVG(pars){
267       console.log("disableBPMNAddSVG");
268         var svg=pars.imageText.substring(pars.imageText.indexOf("<svg"))
269         if($("#svgContainer").length>0)
270                 $("#svgContainer").remove();
271         $("#js-canvas").append("<span id=\"svgContainer\">"+svg+"</span>");
272         /* added code for height width viewBox */
273         $("#svgContainer svg").removeAttr("height");
274         $("#svgContainer svg").removeAttr('viewBox');
275         $("#svgContainer svg").removeAttr('width');
276         
277         $("#svgContainer svg").attr('width','100%');
278         $("#svgContainer svg").attr('height','100%');
279
280         $("#svgContainer").click(function(event){
281                 console.log($(event.target).parent().html())
282                 console.log($($(event.target).parent()).attr("data-element-id"))
283                 var name=$($(event.target).parent()).attr("data-element-id")
284                 lastElementSelected=$($(event.target).parent()).attr("data-element-id")
285                 checkIfElementType(name)
286                 
287         })
288         $(".bjs-container").attr("hidden","");
289     }
290     this.enableDisableMenuOptions=function(pars){
291       console.log("enableDisableMenuOptions");
292         var permittedActionCd = pars.permittedActionCd;
293         // enable menu options
294         document.getElementById('Save CL').classList.remove('ThisLink');
295         document.getElementById('Test').classList.remove('ThisLink');
296         document.getElementById('Properties CL').classList.remove('ThisLink');
297         document.getElementById('Refresh Status').classList.remove('ThisLink');
298         document.getElementById('Revert Model Changes').classList.remove('ThisLink');
299         document.getElementById('Close Model').classList.remove('ThisLink');
300         document.getElementById('Refresh ASDC').classList.remove('ThisLink');
301         document.getElementById('Running Instances').classList.remove('ThisLink');
302         
303         //disable template options for save/properties
304         document.getElementById('Save Template').classList.add('ThisLink');
305         document.getElementById('Template Properties').classList.add('ThisLink');
306         document.getElementById('Revert Template Changes').classList.add('ThisLink');
307         document.getElementById('Close Template').classList.add('ThisLink');
308
309         
310         // enable/disable menu options based on permittedActionCd list
311         this.checkPermittedActionCd(permittedActionCd, 'Submit', 'SUBMIT');
312         this.checkPermittedActionCd(permittedActionCd, 'Resubmit', 'RESUBMIT');
313         this.checkPermittedActionCd(permittedActionCd, 'Update', 'UPDATE');
314         this.checkPermittedActionCd(permittedActionCd, 'Delete', 'DELETE');
315         this.checkPermittedActionCd(permittedActionCd, 'Stop', 'STOP');
316         this.checkPermittedActionCd(permittedActionCd, 'Restart', 'RESTART');
317     }
318     
319    
320     this.getASDCServices().then(function(pars){
321       console.log("getASDCServices");
322         var services=pars.service;
323         asdc_Services=services
324     });
325     
326  }]);