[VID-6] Initial rebase push
[vid.git] / vid-app-common / src / main / webapp / app / vid / scripts / services / msoService.js
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 "use strict";\r
22 \r
23 var MsoService = function($http, $log, PropertyService, UtilityService, COMPONENT, FIELD) {\r
24 \r
25         var _this = this;\r
26 \r
27         /*\r
28          * Common function to handle both create and delete instance requests\r
29          */\r
30         var requestInstanceUpdate = function(request, successCallbackFunction) {\r
31                 $log.debug("MsoService:requestInstanceUpdate: request:");\r
32                 $log.debug(request);\r
33                 $http.post( "mso/" + request.url, {\r
34                         requestDetails : request.requestDetails\r
35                 }, {\r
36                         timeout : PropertyService.getServerResponseTimeoutMsec()\r
37                 }).then(successCallbackFunction)["catch"]\r
38                                 (UtilityService.runHttpErrorHandler);\r
39         }\r
40 \r
41         var checkValidStatus = function(response) {\r
42                 if (response.data.status < 200 || response.data.status > 202) {\r
43                         throw {\r
44                                 type : FIELD.ID.MSO_FAILURE\r
45                         }\r
46                 }\r
47         }\r
48 \r
49         var addListEntry = function(name, value) {\r
50                 var entry = '"' + name + '": ';\r
51                 if (value === undefined) {\r
52                         return entry + "undefined";\r
53                 } else {\r
54                         return entry + '"' + value + '"';\r
55                 }\r
56         }\r
57 \r
58         return {\r
59                 createInstance : requestInstanceUpdate,\r
60                 deleteInstance : requestInstanceUpdate,\r
61                 getOrchestrationRequest : function(requestId, successCallbackFunction) {\r
62                         $log.debug("MsoService:getOrchestrationRequest: requestId: "\r
63                                         + requestId);\r
64                         $http.get(\r
65                                         "mso/mso_get_orch_req/"\r
66                                                         + requestId + "?r=" + Math.random(),\r
67                                         {\r
68                                                 timeout : PropertyService\r
69                                                                 .getServerResponseTimeoutMsec()\r
70                                         }).then(successCallbackFunction)["catch"]\r
71                                         (UtilityService.runHttpErrorHandler);\r
72                 },\r
73                 getOrchestrationRequests : function(filterString,\r
74                                 successCallbackFunction) {\r
75                         $log.debug("MsoService:getOrchestrationRequests: filterString: "\r
76                                         + filterString);\r
77                         $http.get(\r
78                                         "mso/mso_get_orch_reqs/"\r
79                                                         + encodeURIComponent(filterString) + "?r="\r
80                                                         + Math.random(),\r
81                                         {\r
82                                                 timeout : PropertyService\r
83                                                                 .getServerResponseTimeoutMsec()\r
84                                         }).then(successCallbackFunction)["catch"]\r
85                                         (UtilityService.runHttpErrorHandler);\r
86                 },\r
87                 getFormattedCommonResponse : function(response) {\r
88                                 return UtilityService.getCurrentTime() + " HTTP Status: "\r
89                                                 + UtilityService.getHttpStatusText(response.data.status)\r
90                                                 + "\n" + angular.toJson(response.data.entity, true)\r
91                         \r
92                 },\r
93                 checkValidStatus : checkValidStatus,\r
94                 getFormattedGetOrchestrationRequestsResponse : function(response) {\r
95                         UtilityService.checkUndefined(COMPONENT.ENTITY, response.data.entity);\r
96                         UtilityService.checkUndefined(COMPONENT.STATUS, response.data.status);\r
97                         checkValidStatus(response);\r
98 \r
99                         var list = response.data.entity.requestList\r
100                         UtilityService.checkUndefined(FIELD.ID.REQUEST_LIST, list);\r
101 \r
102                         var message = "";\r
103 \r
104                         for (var i = 0; i < list.length; i++) {\r
105                                 var request = list[i].request;\r
106                                 message += addListEntry(FIELD.ID.REQUEST_ID, request.requestId) + ",\n";\r
107                                 message += addListEntry(FIELD.ID.REQUEST_TYPE, request.requestType)\r
108                                                 + ",\n";\r
109                                 var status = request.requestStatus;\r
110                                 if (status === undefined) {\r
111                                         message += addListEntry(FIELD.ID.REQUEST_STATUS, undefined) + "\n";\r
112                                 } else {\r
113                                         message += addListEntry(FIELD.ID.TIMESTAMP, status.timestamp)\r
114                                                         + ",\n";\r
115                                         message += addListEntry(FIELD.ID.REQUEST_STATE, status.requestState)\r
116                                                         + ",\n";\r
117                                         message += addListEntry(FIELD.ID.REQUEST_STATUS,\r
118                                                         status.statusMessage)\r
119                                                         + ",\n";\r
120                                         message += addListEntry(FIELD.ID.PERCENT_PROGRESS,\r
121                                                         status.percentProgress)\r
122                                                         + "\n";\r
123                                 }\r
124                                 if (i < (list.length - 1)) {\r
125                                         message += "\n";\r
126                                 }\r
127                         }\r
128                         return message;\r
129                 },\r
130                 getFormattedSingleGetOrchestrationRequestResponse : function (response) {\r
131                         UtilityService.checkUndefined(COMPONENT.ENTITY, response.data.entity);\r
132                         UtilityService.checkUndefined(COMPONENT.STATUS, response.data.status);\r
133                         checkValidStatus(response);\r
134 \r
135                         var message = "";\r
136                         if ( UtilityService.hasContents (response.data.entity.request) ) {\r
137                                 var request = response.data.entity.request;\r
138                                 message += addListEntry(FIELD.ID.REQUEST_ID, request.requestId) + ",\n";\r
139                                 message += addListEntry(FIELD.ID.REQUEST_TYPE, request.requestType)\r
140                                                 + ",\n";\r
141                                 var status = request.requestStatus;\r
142                                 if (status === undefined) {\r
143                                         message += addListEntry(FIELD.ID.REQUEST_STATUS, undefined) + "\n";\r
144                                 } else {\r
145                                         message += addListEntry(FIELD.ID.TIMESTAMP, status.timestamp)\r
146                                                         + ",\n";\r
147                                         message += addListEntry(FIELD.ID.REQUEST_STATE, status.requestState)\r
148                                                         + ",\n";\r
149                                         message += addListEntry(FIELD.ID.REQUEST_STATUS,\r
150                                                         status.statusMessage)\r
151                                                         + ",\n";\r
152                                         message += addListEntry(FIELD.ID.PERCENT_PROGRESS,\r
153                                                         status.percentProgress)\r
154                                                         + "\n\n";\r
155                                 }\r
156                         }\r
157                         return message;\r
158                 },\r
159                 showResponseContentError : function(error, showFunction) {\r
160                         switch (error.type) {\r
161                         case "undefinedObject":\r
162                                 showFunction(FIELD.ERROR.SYSTEM_FAILURE, error.message);\r
163                                 break;\r
164                         case "msoFailure":\r
165                                 showFunction(FIELD.ERROR.MSO, "")\r
166                                 break;\r
167                         default:\r
168                                 showFunction(FIELD.ERROR.SYSTEM_FAILURE);\r
169                         }\r
170                 }\r
171         }\r
172 }\r
173 \r
174 appDS2.factory("MsoService", [ "$http", "$log", "PropertyService",\r
175                 "UtilityService", "COMPONENT", "FIELD", MsoService ]);\r