Merge "Adapted to latest STD A1 API spec"
[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>>() {
119             }.getType();
120             List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
121             PolicyInstances result = new PolicyInstances();
122             for (PolicyInfo p : rspParsed) {
123                 result.add(p);
124             }
125             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
126         } catch (Exception e) {
127             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
128         }
129     }
130
131     @Override
132     public ResponseEntity<String> getPolicyInstance(String id) {
133         String url = baseUrl() + "/policy?instance={id}";
134         Map<String, ?> uriVariables = Map.of("id", id);
135
136         return this.restTemplate.getForEntity(url, String.class, uriVariables);
137     }
138
139     @Override
140     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json,
141             String ric) {
142         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
143         Map<String, ?> uriVariables = Map.of( //
144                 "type", policyTypeIdString, //
145                 "instance", policyInstanceId, //
146                 "ric", ric, //
147                 "service", "dashboard");
148
149         try {
150             this.restTemplate.put(url, json, uriVariables);
151             return new ResponseEntity<>(HttpStatus.OK);
152         } catch (Exception e) {
153             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
154         }
155     }
156
157     @Override
158     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
159         String url = baseUrl() + "/policy?instance={instance}";
160         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
161         try {
162             this.restTemplate.delete(url, uriVariables);
163             return new ResponseEntity<>(HttpStatus.OK);
164         } catch (Exception e) {
165             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
166         }
167
168     }
169
170     @Value.Immutable
171     @Gson.TypeAdapters
172     interface RicInfo {
173         public String name();
174
175         public Collection<String> nodeNames();
176
177         public Collection<String> policyTypes();
178     }
179
180     @Override
181     public ResponseEntity<String> getRicsSupportingType(String typeName) {
182         String url = baseUrl() + "/rics?policyType={typeName}";
183         Map<String, ?> uriVariables = Map.of("typeName", typeName);
184         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
185
186         try {
187             Type listType = new TypeToken<List<ImmutableRicInfo>>() {
188             }.getType();
189             List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
190             Collection<String> result = new Vector<>(rspParsed.size());
191             for (RicInfo ric : rspParsed) {
192                 result.add(ric.name());
193             }
194             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
195         } catch (Exception e) {
196             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
197         }
198     }
199
200 }