8f9805e33939b51b6e0b7e0c9da0a7def20b450b
[clamp.git] / ui-react / src / api / LoopService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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  */
22
23 export default class LoopService {
24         static getLoopNames() {
25                 return fetch('/restservices/clds/v2/loop/getAllNames', { method: 'GET', credentials: 'same-origin' })
26                         .then(function (response) {
27                                 console.debug("GetLoopNames response received: ", response.status);
28                                 if (response.ok) {
29                                         return response.json();
30                                 } else {
31                                         console.error("GetLoopNames query failed");
32                                         return {};
33                                 }
34                         })
35                         .catch(function (error) {
36                                 console.error("GetLoopNames error received", error);
37                                 return {};
38                         });
39         }
40
41         static createLoop(loopName, templateName) {
42                 return fetch('/restservices/clds/v2/loop/create/' + loopName + '?templateName=' + templateName, {
43                         method: 'POST',
44                         headers: {
45                                 "Content-Type": "application/json"
46                         },
47                         credentials: 'same-origin'
48                 })
49                         .then(function (response) {
50                                 console.debug("CreateLoop response received: ", response.status);
51                                 return response.json();
52                         })
53                         .catch(function (error) {
54                                 console.error("CreateLoop error received", error);
55                                 return "";
56                         });
57         }
58
59         static getLoop(loopName) {
60                 return fetch('/restservices/clds/v2/loop/' + loopName, {
61                         method: 'GET',
62                         headers: {
63                                 "Content-Type": "application/json"
64                         },
65                         credentials: 'same-origin'
66                 })
67                         .then(function (response) {
68                                 console.debug("GetLoop response received: ", response.status);
69                                 if (response.ok) {
70                                         return response.json();
71                                 } else {
72                                         console.error("GetLoop query failed");
73                                         return {};
74                                 }
75                         })
76                         .catch(function (error) {
77                                 console.error("GetLoop error received", error);
78                                 return {};
79                         });
80         }
81
82         static getSvg(loopName) {
83                 return fetch('/restservices/clds/v2/loop/svgRepresentation/' + loopName, {
84                         method: 'GET',
85                         credentials: 'same-origin'
86                 })
87                         .then(function (response) {
88                                 console.debug("svgRepresentation response received: ", response.status);
89                                 if (response.ok) {
90                                         return response.text();
91                                 } else {
92                                         console.error("svgRepresentation query failed");
93                                         return "";
94                                 }
95                         })
96                         .catch(function (error) {
97                                 console.error("svgRepresentation error received", error);
98                                 return "";
99                         });
100         }
101
102         static setMicroServiceProperties(loopName, jsonData) {
103                 return fetch('/restservices/clds/v2/loop/updateMicroservicePolicy/' + loopName, {
104                         method: 'POST',
105                         credentials: 'same-origin',
106                         headers: {
107                                 "Content-Type": "application/json"
108                         },
109                         body: JSON.stringify(jsonData)
110                 })
111                         .then(function (response) {
112                                 console.debug("updateMicroservicePolicy response received: ", response.status);
113                                 if (response.ok) {
114                                         return response.text();
115                                 } else {
116                                         console.error("updateMicroservicePolicy query failed");
117                                         return "";
118                                 }
119                         })
120                         .catch(function (error) {
121                                 console.error("updateMicroservicePolicy error received", error);
122                                 return "";
123                         });
124         }
125         
126         static setOperationalPolicyProperties(loopName, jsonData) {
127                 return fetch('/restservices/clds/v2/loop/updateOperationalPolicies/' + loopName, {
128                         method: 'POST',
129                         credentials: 'same-origin',
130                         headers: {
131                                 "Content-Type": "application/json"
132                         },
133                         body: JSON.stringify(jsonData)
134                 })
135                         .then(function (response) {
136                                 console.debug("updateOperationalPolicies response received: ", response.status);
137                                 if (response.ok) {
138                                         return response.text();
139                                 } else {
140                                         console.error("updateOperationalPolicies query failed");
141                                         return "";
142                                 }
143                         })
144                         .catch(function (error) {
145                                 console.error("updateOperationalPolicies error received", error);
146                                 return "";
147                         });
148         }
149
150         static updateGlobalProperties(loopName, jsonData) {
151                 return fetch('/restservices/clds/v2/loop/updateGlobalProperties/' + loopName, {
152                         method: 'POST',
153                         credentials: 'same-origin',
154                         headers: {
155                                 "Content-Type": "application/json"
156                         },
157                         body: JSON.stringify(jsonData)
158                 })
159                         .then(function (response) {
160                                 console.debug("updateGlobalProperties response received: ", response.status);
161                                 if (response.ok) {
162                                         return response.text();
163                                 } else {
164                                         console.error("updateGlobalProperties query failed");
165                                         return "";
166                                 }
167                         })
168                         .catch(function (error) {
169                                 console.error("updateGlobalProperties error received", error);
170                                 return "";
171                         });
172         }
173
174         static refreshOperationalPolicyJson(loopName,operationalPolicyName) {
175                 return fetch('/restservices/clds/v2/loop/refreshOperationalPolicyJsonSchema/' + loopName + '/' + operationalPolicyName, {
176                         method: 'PUT',
177                         headers: {
178                                 "Content-Type": "application/json"
179                         },
180                         credentials: 'same-origin'
181                 })
182                         .then(function (response) {
183                                 console.debug("Refresh Operational Policy Json Schema response received: ", response.status);
184                                 if (response.ok) {
185                                         return response.json();
186                                 } else {
187                                         console.error("Refresh Operational Policy Json Schema query failed");
188                                         return {};
189                                 }
190                         })
191                         .catch(function (error) {
192                                 console.error("Refresh Operational Policy Json Schema error received", error);
193                                 return {};
194                         });
195         }
196
197                 static refreshMicroServicePolicyJson(loopName,microServicePolicyName) {
198                 return fetch('/restservices/clds/v2/loop/refreshMicroServicePolicyJsonSchema/' + loopName + '/' + microServicePolicyName, {
199                         method: 'PUT',
200                         headers: {
201                                 "Content-Type": "application/json"
202                         },
203                         credentials: 'same-origin'
204                 })
205                         .then(function (response) {
206                                 console.debug("Refresh Operational Policy Json Schema response received: ", response.status);
207                                 if (response.ok) {
208                                         return response.json();
209                                 } else {
210                                         console.error("Refresh Operational Policy Json Schema query failed");
211                                         return {};
212                                 }
213                         })
214                         .catch(function (error) {
215                                 console.error("Refresh Operational Policy Json Schema error received", error);
216                                 return {};
217                         });
218     }
219
220         static addOperationalPolicyType(loopName, policyType, policyVersion) {
221                 return fetch('/restservices/clds/v2/loop/addOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion , {
222                         method: 'PUT',
223                         headers: {
224                                 "Content-Type": "application/json"
225                         },
226                         credentials: 'same-origin'
227                 })
228                                 .then(function (response) {
229                                 console.debug("Add Operational Policy response received: ", response.status);
230                                 if (response.ok) {
231                                         return response.json();
232                                 } else {
233                                         return response.text();
234                                 }
235                         })
236                         .catch(function (error) {
237                                 console.error("Add Operational Policy query failed");
238                                 throw new Error(error);
239                         })
240         }
241
242         static removeOperationalPolicyType(loopName, policyType, policyVersion, policyName) {
243                 return fetch('/restservices/clds/v2/loop/removeOperationaPolicy/' + loopName + '/policyModel/' + policyType +'/' + policyVersion + '/' + policyName , {
244                         method: 'PUT',
245                         headers: {
246                                 "Content-Type": "application/json"
247                         },
248                         credentials: 'same-origin'
249                 })
250                                 .then(function (response) {
251                                         console.debug("Remove Operational Policy response received: ", response.status);
252                                 if (response.ok) {
253                                         return response.json();
254                                 } else {
255                                         console.error("Remove Operational Policy query failed");
256                                         return {};
257                                 }
258                         })
259                         .catch(function (error) {
260                                 console.error("Remove Operational Policy error received", error);
261                                 return {};
262                         });
263         }
264 }