Clamp react ui to use url pathname dynamically instead of hardcoded prefix for callin...
[clamp.git] / ui-react / src / api / PolicyToscaService.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 PolicyToscaService {
24   static getToscaPolicyModels() {
25     return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels', { method: 'GET', credentials: 'same-origin' })
26       .then(function (response) {
27         console.debug("getToscaPolicyModels response received: ", response.status);
28         if (response.ok) {
29           return response.json();
30         } else {
31           console.error("getToscaPolicyModels query failed");
32           return {};
33         }
34       })
35       .catch(function (error) {
36         console.error("getToscaPolicyModels error received", error);
37         return {};
38       });
39   }
40
41   static getToscaPolicyModelYaml(policyModelType, policyModelVersion) {
42                 return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/yaml/' + policyModelType + "/" + policyModelVersion, {
43                         method: 'GET',
44                         credentials: 'same-origin'
45                 })
46                         .then(function (response) {
47                                 console.debug("getToscaPolicyModelYaml response received: ", response.status);
48                                 if (response.ok) {
49                                         return response.json();
50                                 } else {
51                                         console.error("getToscaPolicyModelYaml query failed");
52                                         return "";
53                                 }
54                         })
55                         .catch(function (error) {
56                                 console.error("getToscaPolicyModelYaml error received", error);
57                                 return "";
58                         });
59  }
60
61  static getToscaPolicyModel(policyModelType, policyModelVersion) {
62                 return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/' + policyModelType + "/" + policyModelVersion, {
63                         method: 'GET',
64                         credentials: 'same-origin'
65                 })
66                         .then(function (response) {
67                                 console.debug("getToscaPolicyModel response received: ", response.status);
68                                 if (response.ok) {
69                                         return response.json();
70                                 } else {
71                                         console.error("getToscaPolicyModel query failed");
72                                         return "";
73                                 }
74                         })
75                         .catch(function (error) {
76                                 console.error("getToscaPolicyModel error received", error);
77                                 return "";
78                         });
79   }
80
81   static createPolicyModelFromToscaModel(jsonData) {
82        return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels', {
83            method: 'POST',
84            credentials: 'same-origin',
85            headers: {
86              "Content-Type": "a",
87            },
88            body: JSON.stringify(jsonData)
89          })
90          .then(function(response) {
91            console.debug("createPolicyModelFromToscaModel response received: ", response.status);
92            if (response.ok) {
93              var message = {
94                status: response.status,
95                message: 'Tosca Policy Model successfully uploaded'
96              };
97              return message;
98            } else {
99              console.error("createPolicyModelFromToscaModel failed");
100              return response.text();
101            }
102          })
103          .catch(function(error) {
104            console.error("createPolicyModelFromToscaModel error received", error);
105            return "";
106          });
107      }
108
109      static updatePolicyModelTosca(policyModelType, policyModelVersion, jsonData) {
110          return fetch(window.location.pathname + 'restservices/clds/v2/policyToscaModels/' + policyModelType + '/' + policyModelVersion, {
111              method: 'PUT',
112              credentials: 'same-origin',
113              headers: {
114                "Content-Type": "a",
115              },
116              body: JSON.stringify(jsonData)
117            })
118            .then(function(response) {
119              console.debug("updatePolicyModelTosca response received: ", response.status);
120              if (response.ok) {
121                var message = {
122                  status: response.status,
123                  message: 'Tosca Policy Model successfully uploaded'
124                };
125                return message;
126              } else {
127                console.error("updatePolicyModelTosca failed");
128                return response.text();
129              }
130            })
131            .catch(function(error) {
132              console.error("updatePolicyModelTosca error received", error);
133              return "";
134            });
135        }
136 }