Fix the get loop state logic
[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) {
34                 //This will open the methods located in the app.js
35                         if (undefined == name) {
36                                 return;
37                         }else if (name.toLowerCase().indexOf("policy") >= 0){
38                                 PolicyWindow();
39                         } else {
40                                 $rootScope.selectedBoxName = name;
41                                 ToscaModelWindow();
42                         }
43                 }
44                 this.toggleDeploy = function(uiAction, modelName) {
45                         var svcAction = uiAction.toLowerCase();
46                         var deployUrl = "/restservices/clds/v2/loop/" + svcAction + "Loop/" + modelName;
47                         var def = $q.defer();
48                         var sets = [];
49                         $http.put(deployUrl).success(
50                                 function(data) {
51                                         def.resolve(data);
52                                         alertService.alertMessage("Action Successful: " + svcAction, 1)
53                                         // update deploymentID, lastUpdatedStatus
54                                         setLastUpdatedStatus(data.lastUpdatedStatus);
55                                         setDeploymentStatusURL(data.dcaeDeploymentStatusUrl);
56                                         setDeploymentID(data.dcaeDeploymentId);
57                                         setStatus();
58                                         enableDisableMenuOptions();
59                         }).error(
60                                 function(data) {
61                                         def.resolve(data);
62                                         alertService.alertMessage("Action Failure: " + svcAction, 2);
63                                         def.reject(svcAction + " not successful");
64                         });
65                         return def.promise;
66                 }
67                 this.getModel = function(modelName) {
68                     var def = $q.defer();
69                     var sets = [];
70                     var svcUrl = "/restservices/clds/v2/loop/" + modelName;
71                     $http.get(svcUrl).success(function(data) {
72                         cl_props = data;
73                             def.resolve(data);
74                     }).error(function(data) {
75                             def.reject("Open Model not successful");
76                     });
77                     return def.promise;
78             };
79             this.getSavedModel = function() {
80                     var def = $q.defer();
81                     var sets = [];
82                     var svcUrl = "/restservices/clds/v2/loop/getAllNames";
83                     $http.get(svcUrl).success(function(data) {
84
85                             def.resolve(data);
86                     }).error(function(data) {
87
88                             def.reject("Open Model not successful");
89                     });
90                     return def.promise;
91             };
92             this.processAction = function(uiAction, modelName) {
93                     var def = $q.defer();
94                     var sets = [];
95                     var svcAction = uiAction.toLowerCase();
96                     var svcUrl = "/restservices/clds/v2/loop/" + svcAction + "/" + modelName;
97
98                         $http.put(svcUrl).success(
99                                 function(data) {
100                                         def.resolve(data);
101                                         alertService.alertMessage("Action Successful: " + svcAction, 1)
102                                         // update deploymentID, lastUpdatedStatus
103                                         setLastUpdatedStatus(data.lastUpdatedStatus);
104                                         setDeploymentStatusURL(data.dcaeDeploymentStatusUrl);
105                                         setDeploymentID(data.dcaeDeploymentId);
106                                         setStatus();
107                                         enableDisableMenuOptions();
108                         }).error(
109                                 function(data) {
110                                         def.resolve(data);
111                                         alertService.alertMessage("Action Failure: " + svcAction, 2);
112                                         def.reject(svcAction + " not successful");
113                         });
114                     
115                     return def.promise;
116             };
117             this.manageAction = function(modelName, typeId, typeName) {
118
119                     var def = $q.defer();
120                     var sets = [];
121                     var config = {
122                         url : "/restservices/clds/v1/clds/getDispatcherInfo",
123                         method : "GET",
124                         params : {
125                             model : modelName,
126                             typeId : typeId,
127                             typeName : typeName
128                         }
129                     };
130                     $http(config).success(function(data) {
131
132                             def.resolve(data);
133                     }).error(function(data) {
134
135                             def.reject("manage action not successful");
136                     });
137                     return def.promise;
138             };
139             this.processActionResponse = function(modelName) {
140                 // populate control name (prefix and uuid here)
141                 $("#loop_name").text(getLoopName());
142                     setStatus();
143                     manageCLImage(modelName);
144                     enableDisableMenuOptions();
145             };
146             this.processRefresh = function() {
147                     setStatus();
148                     enableDisableMenuOptions();
149             }
150             function setStatus() {
151                     var status = getLastUpdatedStatus();
152                     // apply color to status
153                     var statusColor = 'white';
154                     if (status.trim() === "DESIGN") {
155                             statusColor = 'gray'
156                     } else if (status.trim() === "DISTRIBUTED") {
157                             statusColor = 'blue'
158                     } else if (status.trim() === "ACTIVE") {
159                             statusColor = 'green'
160                     } else if (status.trim() === "STOPPED") {
161                             statusColor = 'red'
162                     } else if (status.trim() === "DELETING") {
163                             statusColor = 'pink'
164                     } else if (status.trim() === "ERROR") {
165                             statusColor = 'orange'
166                     } else if (status.trim() === "UNKNOWN") {
167                             statusColor = 'blue'
168                     } else {
169                             statusColor = null;
170                     }
171                     var statusMsg = '<span style="background-color:'
172                     + statusColor
173                     + ';-moz-border-radius: 50px;  -webkit-border-radius: 50px;  border-radius: 50px;">&nbsp;&nbsp;&nbsp;'
174                     + status + '&nbsp;&nbsp;&nbsp;</span>';
175                     // display status
176                     if ($("#status_clds").length >= 1)
177                             $("#status_clds").remove();
178                     $("#activity_modeler")
179                     .append(
180                     '<span id="status_clds" style="position: absolute;  left: 61%;top: 151px; font-size:20px;">Status: '
181                     + statusMsg + '</span>');
182             }
183             
184             function manageCLImage(modelName) {
185                 getModelImage(modelName).then(function(pars) {
186                         var svg = pars;
187                             if ($("#svgContainer").length > 0)
188                                     $("#svgContainer").remove();
189                             $("#js-canvas").append(
190                             "<span id=\"svgContainer\">" + svg + "</span>");
191                             $("#svgContainer svg").attr('width', '100%');
192                             $("#svgContainer svg").attr('height', '100%');
193                             $("#svgContainer").click(
194                             function(event) {
195                                     var name = $($($($(event.target).parent()).parent()).parent()).attr("data-element-id")
196                                     checkIfElementType(name)
197                             });
198                         }, function(data) {
199                         });
200             }
201             enableDisableMenuOptions = function() {
202                     enableDefaultMenu();
203                     //var status = getStatus();
204                 //enableActionMenu(status);
205                 enableAllActionMenu();
206             }
207             getModelImage = function(modelName) {
208                     var def = $q.defer();
209                     var sets = [];
210                     var svcUrl = "/restservices/clds/v2/loop/svgRepresentation/" + modelName;
211                     $http.get(svcUrl).success(function(data) {
212                             def.resolve(data);
213                     }).error(function(data) {
214                             def.reject("Not able to get the model image");
215                     });
216                     return def.promise;
217             };
218     } ]);