2b27a7fd522c9316c5de0cb9b753748867e19ce7
[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.getModel = function(modelName) {
45                     var def = $q.defer();
46                     var svcUrl = "/restservices/clds/v2/loop/" + modelName;
47                     $http.get(svcUrl).success(function(data) {
48                         cl_props = data;
49                         setStatus(data.lastComputedState);
50                             def.resolve(data);
51                     }).error(function(data) {
52                             def.reject("Open Model not successful");
53                     });
54                     return def.promise;
55             };
56             this.getSavedModel = function() {
57                     var def = $q.defer();
58                     var svcUrl = "/restservices/clds/v2/loop/getAllNames";
59                     $http.get(svcUrl).success(function(data) {
60
61                             def.resolve(data);
62                     }).error(function(data) {
63
64                             def.reject("Open Model not successful");
65                     });
66                     return def.promise;
67             };
68             this.processAction = function(uiAction, modelName) {
69                     var def = $q.defer();
70                     var svcAction = uiAction.toLowerCase();
71                     var svcUrl = "/restservices/clds/v2/loop/" + svcAction + "/" + modelName;
72
73                         $http.put(svcUrl).success(
74                                 function(data) {
75                                         def.resolve(data);
76                                         alertService.alertMessage("Action Successful: " + svcAction, 1)
77                         }).error(
78                                 function(data) {
79                                         def.resolve(data);
80                                         alertService.alertMessage("Action Failure: " + svcAction, 2);
81                                         def.reject(svcAction + " not successful");
82                         });
83                     
84                     return def.promise;
85             };
86             this.manageAction = function(modelName, typeId, typeName) {
87
88                     var def = $q.defer();
89                     var config = {
90                         url : "/restservices/clds/v1/clds/getDispatcherInfo",
91                         method : "GET",
92                         params : {
93                             model : modelName,
94                             typeId : typeId,
95                             typeName : typeName
96                         }
97                     };
98                     $http(config).success(function(data) {
99
100                             def.resolve(data);
101                     }).error(function(data) {
102
103                             def.reject("manage action not successful");
104                     });
105                     return def.promise;
106             };
107             this.processActionResponse = function(modelName) {
108                 // populate control name (prefix and uuid here)
109                 $("#loop_name").text(getLoopName());
110                     manageCLImage(modelName);
111                     enableDisableMenuOptions();
112             };
113             this.refreshStatus = function(modelName) {
114                     var def = $q.defer();
115                     var svcUrl = "/restservices/clds/v2/loop/getstatus/" + modelName;
116                     $http.get(svcUrl).success(function(data) {
117                         cl_props = data;
118                         setStatus(data.lastComputedState);
119                             def.resolve(data);
120                     }).error(function(data) {
121                             def.reject("Refresh Status not successful");
122                     });
123                     return def.promise;
124             }
125             function setStatus(status) {
126                     // apply color to status
127                     var statusColor = 'white';
128                     if (status.trim() === "DESIGN") {
129                             statusColor = 'gray'
130                     } else if (status.trim() === "SUBMITTED") {
131                             statusColor = 'blue'
132                     } else if (status.trim() === "DEPLOYED") {
133                             statusColor = 'blue'
134                     } else if (status.trim() === "RUNNING") {
135                             statusColor = 'green'
136                     } else if (status.trim() === "STOPPED") {
137                             statusColor = 'orange'
138                     } else if (status.trim() === "IN_ERROR") {
139                             statusColor = 'red'
140                     } else if (status.trim() === "WAITING") {
141                             statusColor = 'greenyellow'
142                     } 
143                     var statusMsg = '<span style="background-color:'
144                     + statusColor
145                     + ';-moz-border-radius: 50px;  -webkit-border-radius: 50px;  border-radius: 50px;">&nbsp;&nbsp;&nbsp;'
146                     + status + '&nbsp;&nbsp;&nbsp;</span>';
147                     // display status
148                     if ($("#status_clds").length >= 1)
149                             $("#status_clds").remove();
150                     $("#activity_modeler")
151                     .append(
152                     '<span id="status_clds" style="position: absolute;  left: 61%;top: 151px; font-size:20px;">Status: '
153                     + statusMsg + '</span>');
154                     
155                     var statusTable = '<table id="status_components_table" style="width:100%"><tr><th><span align="left" class="text">Component</span></th><th><span align="center" class="text">State</span></th><th><span align="right" class="text">Description</span></th></tr>';
156                     
157                     $.each(cl_props['components'], function(componentIndex, componentValue) {
158                    statusTable+='<tr><td>'+componentIndex+'</td>';
159                    statusTable+='<td>'+componentValue['componentState']['stateName']+'</td>';
160                    statusTable+='<td>'+componentValue['componentState']['description']+'</td></tr>';
161                     });
162                     statusTable+= '</table>';
163                     if ($("#status_components").length >= 1)
164                             $("#status_components").remove();
165                     $("#activity_modeler")
166                     .append(
167                     '<span id="status_components" style="position: absolute;  left: 61%;top: 191px; font-size:10px;">'
168                     + statusTable + '</span>');
169                     
170             }
171             function manageCLImage(modelName) {
172                 getModelImage(modelName).then(function(pars) {
173                         var svg = pars;
174                             if ($("#svgContainer").length > 0)
175                                     $("#svgContainer").remove();
176                             $("#loop-content").append(
177                             "<span id=\"svgContainer\">" + svg + "</span>");
178                             $("#svgContainer svg").attr('width', '100%');
179                             $("#svgContainer svg").attr('height', '100%');
180                             $("#svgContainer").click(
181                             function(event) {
182                                     var name = $($($($(event.target).parent()).parent()).parent()).attr("data-element-id")
183                                     checkIfElementType(name)
184                             });
185                         }, function(data) {
186                         });
187             }
188             function enableDisableMenuOptions () {
189                     enableDefaultMenu();
190                 enableAllActionMenu();
191             }
192             function getModelImage(modelName) {
193                     var def = $q.defer();
194                     var svcUrl = "/restservices/clds/v2/loop/svgRepresentation/" + modelName;
195                     $http.get(svcUrl).success(function(data) {
196                             def.resolve(data);
197                     }).error(function(data) {
198                             def.reject("Not able to get the model image");
199                     });
200                     return def.promise;
201             };
202     } ]);