Adding validation of updated policies
[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.HttpEntity;
47 import org.springframework.http.HttpHeaders;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.MediaType;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.stereotype.Component;
52 import org.springframework.web.client.RestTemplate;
53
54 @Component("PolicyAgentApi")
55 public class PolicyAgentApiImpl implements PolicyAgentApi {
56     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58     RestTemplate restTemplate = new RestTemplate();
59
60     private static com.google.gson.Gson gson = new GsonBuilder() //
61             .serializeNulls() //
62             .create(); //
63
64     private final String urlPrefix;
65
66     @Autowired
67     public PolicyAgentApiImpl(
68             @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
69         logger.debug("ctor prefix '{}'", urlPrefix);
70         this.urlPrefix = urlPrefix;
71     }
72
73     private String baseUrl() {
74         return urlPrefix;
75     }
76
77     @Value.Immutable
78     @Gson.TypeAdapters
79     interface PolicyTypeInfo {
80
81         public String name();
82
83         public String schema();
84     }
85
86     @Override
87     public ResponseEntity<String> getAllPolicyTypes() {
88         try {
89             String url = baseUrl() + "/policy_schemas";
90             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
91             if (!rsp.getStatusCode().is2xxSuccessful()) {
92                 return rsp;
93             }
94
95             PolicyTypes result = new PolicyTypes();
96             JsonParser jsonParser = new JsonParser();
97
98             JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
99             for (JsonElement schema : schemas) {
100                 JsonObject schemaObj = schema.getAsJsonObject();
101                 String title = schemaObj.get("title").getAsString();
102                 String schemaAsStr = schemaObj.toString();
103                 PolicyType pt = new PolicyType(title, schemaAsStr);
104                 result.add(pt);
105             }
106             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
107         } catch (Exception e) {
108             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
109         }
110     }
111
112     @Override
113     public ResponseEntity<String> getPolicyInstancesForType(String type) {
114         String url = baseUrl() + "/policies?type={type}";
115         Map<String, ?> uriVariables = Map.of("type", type);
116         ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
117         if (!rsp.getStatusCode().is2xxSuccessful()) {
118             return rsp;
119         }
120
121         try {
122             Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
123             }.getType();
124             List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
125             PolicyInstances result = new PolicyInstances();
126             for (PolicyInfo p : rspParsed) {
127                 result.add(p);
128             }
129             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
130         } catch (Exception e) {
131             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
132         }
133     }
134
135     @Override
136     public ResponseEntity<Object> getPolicyInstance(String id) {
137         String url = baseUrl() + "/policy?instance={id}";
138         Map<String, ?> uriVariables = Map.of("id", id);
139
140         return this.restTemplate.getForEntity(url, Object.class, uriVariables);
141     }
142
143     @Override
144     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
145             String ric) {
146         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
147         Map<String, ?> uriVariables = Map.of( //
148                 "type", policyTypeIdString, //
149                 "instance", policyInstanceId, //
150                 "ric", ric, //
151                 "service", "dashboard");
152
153         try {
154             this.restTemplate.put(url, createJsonHttpEntity(json), uriVariables);
155             return new ResponseEntity<>(HttpStatus.OK);
156         } catch (Exception e) {
157             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
158         }
159     }
160
161     @Override
162     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
163         String url = baseUrl() + "/policy?instance={instance}";
164         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
165         try {
166             this.restTemplate.delete(url, uriVariables);
167             return new ResponseEntity<>(HttpStatus.OK);
168         } catch (Exception e) {
169             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
170         }
171
172     }
173
174     @Value.Immutable
175     @Gson.TypeAdapters
176     interface RicInfo {
177         public String ricName();
178
179         public Collection<String> nodeNames();
180
181         public Collection<String> policyTypes();
182     }
183
184     @Override
185     public ResponseEntity<String> getRicsSupportingType(String typeName) {
186         String url = baseUrl() + "/rics?policyType={typeName}";
187         Map<String, ?> uriVariables = Map.of("typeName", typeName);
188         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
189
190         try {
191             Type listType = new TypeToken<List<ImmutableRicInfo>>() {
192             }.getType();
193             List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
194             Collection<String> result = new Vector<>(rspParsed.size());
195             for (RicInfo ric : rspParsed) {
196                 result.add(ric.ricName());
197             }
198             return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
199         } catch (Exception e) {
200             return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
201         }
202     }
203
204     private HttpEntity<Object> createJsonHttpEntity(Object content) {
205         HttpHeaders headers = new HttpHeaders();
206         headers.setContentType(MediaType.APPLICATION_JSON);
207         return new HttpEntity<Object>(content, headers);
208     }
209
210 }