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