Return response codes from policy controller
[nonrtric.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / policyagentapi / PolicyAgentApiImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
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 package org.oransc.ric.portal.dashboard.policyagentapi;
21
22 import com.google.gson.GsonBuilder;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonObject;
26 import com.google.gson.JsonParser;
27 import com.google.gson.reflect.TypeToken;
28
29 import java.lang.invoke.MethodHandles;
30 import java.lang.reflect.Type;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Vector;
35
36 import org.immutables.gson.Gson;
37 import org.immutables.value.Value;
38 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
39 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
40 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
41 import org.oransc.ric.portal.dashboard.model.PolicyType;
42 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.stereotype.Component;
49 import org.springframework.web.client.RestTemplate;
50
51 @Component("PolicyAgentApi")
52 public class PolicyAgentApiImpl implements PolicyAgentApi {
53     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54
55     RestTemplate restTemplate = new RestTemplate();
56
57     private static com.google.gson.Gson gson = new GsonBuilder() //
58         .serializeNulls() //
59         .create(); //
60
61     private final String urlPrefix;
62
63     @Autowired
64     public PolicyAgentApiImpl(
65         @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
66         logger.debug("ctor prefix '{}'", urlPrefix);
67         this.urlPrefix = urlPrefix;
68     }
69
70     private String baseUrl() {
71         return urlPrefix;
72     }
73
74     @Value.Immutable
75     @Gson.TypeAdapters
76     interface PolicyTypeInfo {
77
78         public String name();
79
80         public String schema();
81     }
82
83     @Override
84     public ResponseEntity<String> getAllPolicyTypes() {
85         String url = baseUrl() + "/policy_schemas";
86         ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
87         if (!rsp.getStatusCode().is2xxSuccessful()) {
88             return rsp;
89         }
90
91         PolicyTypes result = new PolicyTypes();
92         JsonParser jsonParser = new JsonParser();
93         try {
94             JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
95             for (JsonElement schema : schemas) {
96                 JsonObject schemaObj = schema.getAsJsonObject();
97                 String title = schemaObj.get("title").getAsString();
98                 String schemaAsStr = schemaObj.toString();
99                 PolicyType pt = new PolicyType(title, schemaAsStr);
100                 result.add(pt);
101             }
102             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
103         } catch (Exception e) {
104             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
105         }
106     }
107
108     @Override
109     public ResponseEntity<String> getPolicyInstancesForType(String type) {
110         String url = baseUrl() + "/policies?type={type}";
111         Map<String, ?> uriVariables = Map.of("type", type);
112         ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
113         if (!rsp.getStatusCode().is2xxSuccessful()) {
114             return rsp;
115         }
116
117         try {
118             Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
119             List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
120             PolicyInstances result = new PolicyInstances();
121             for (PolicyInfo p : rspParsed) {
122                 result.add(p);
123             }
124             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
125         } catch (Exception e) {
126             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
127         }
128     }
129
130     @Override
131     public ResponseEntity<String> getPolicyInstance(String id) {
132         String url = baseUrl() + "/policy?instance={id}";
133         Map<String, ?> uriVariables = Map.of("id", id);
134
135         return this.restTemplate.getForEntity(url, String.class, uriVariables);
136     }
137
138     @Override
139     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json,
140         String ric) {
141         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
142         Map<String, ?> uriVariables = Map.of( //
143             "type", policyTypeIdString, //
144             "instance", policyInstanceId, //
145             "ric", ric, //
146             "service", "dashboard");
147
148         try {
149             this.restTemplate.put(url, json, uriVariables);
150             return new ResponseEntity<>("Policy was put successfully", HttpStatus.OK);
151         } catch (Exception e) {
152             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
153         }
154     }
155
156     @Override
157     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
158         String url = baseUrl() + "/policy?instance={instance}";
159         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
160         try {
161             this.restTemplate.delete(url, uriVariables);
162             return new ResponseEntity<>("Policy was deleted successfully", HttpStatus.NO_CONTENT);
163         } catch (Exception e) {
164             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
165         }
166
167     }
168
169     @Value.Immutable
170     @Gson.TypeAdapters
171     interface RicInfo {
172         public String name();
173
174         public Collection<String> nodeNames();
175
176         public Collection<String> policyTypes();
177     }
178
179     @Override
180     public ResponseEntity<String> getRicsSupportingType(String typeName) {
181         String url = baseUrl() + "/rics?policyType={typeName}";
182         Map<String, ?> uriVariables = Map.of("typeName", typeName);
183         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
184
185         try {
186             Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
187             List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
188             Collection<String> result = new Vector<>(rspParsed.size());
189             for (RicInfo ric : rspParsed) {
190                 result.add(ric.name());
191             }
192             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
193         } catch (Exception e) {
194             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
195         }
196     }
197
198 }