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