25e604ad5cbd184b809e96eb44fb3f612ed5eb97
[portal/nonrtric-controlpanel.git] / webapp-backend / src / main / java / org / oransc / portal / nonrtric / controlpanel / policyagentapi / PolicyAgentApiImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * Modifications Copyright (C) 2020 Nordix Foundation
7  * %%
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ========================LICENSE_END===================================
20  */
21 package org.oransc.portal.nonrtric.controlpanel.policyagentapi;
22
23 import com.google.gson.GsonBuilder;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParser;
28 import com.google.gson.reflect.TypeToken;
29 import java.lang.invoke.MethodHandles;
30 import java.lang.reflect.Type;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
34 import org.immutables.gson.Gson;
35 import org.immutables.value.Value;
36 import org.oransc.portal.nonrtric.controlpanel.model.ImmutablePolicyInfo;
37 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo;
38 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
39 import org.oransc.portal.nonrtric.controlpanel.model.PolicyType;
40 import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes;
41 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
42 import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
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
50 @Component("PolicyAgentApi")
51 public class PolicyAgentApiImpl implements PolicyAgentApi {
52     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54     private final AsyncRestClient webClient;
55
56     private static com.google.gson.Gson gson = new GsonBuilder() //
57         .serializeNulls() //
58         .create(); //
59
60     @Autowired
61     public PolicyAgentApiImpl(
62         @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
63         this(new AsyncRestClient(urlPrefix));
64         logger.debug("ctor prefix '{}'", urlPrefix);
65     }
66
67     public PolicyAgentApiImpl(AsyncRestClient webClient) {
68         this.webClient = webClient;
69     }
70
71     @Override
72     public ResponseEntity<String> getAllPolicyTypes() {
73         final String TITLE = "title";
74         try {
75             final String url = "/policy_schemas";
76             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
77             if (!rsp.getStatusCode().is2xxSuccessful()) {
78                 return rsp;
79             }
80
81             PolicyTypes result = new PolicyTypes();
82             JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
83             for (JsonElement schema : schemas) {
84                 JsonObject schemaObj = schema.getAsJsonObject();
85                 String title = "";
86                 if (schemaObj.get(TITLE) != null) {
87                     title = schemaObj.get(TITLE).getAsString();
88                 }
89                 PolicyType pt = new PolicyType(title, schemaObj.toString());
90                 result.add(pt);
91             }
92             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
93         } catch (Exception e) {
94             return ErrorResponseHandler.handleException(e);
95         }
96     }
97
98     @Override
99     public ResponseEntity<String> getPolicyInstancesForType(String type) {
100         try {
101             String url = "/policies?type=" + type;
102             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
103             if (!rsp.getStatusCode().is2xxSuccessful()) {
104                 return rsp;
105             }
106
107             Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
108             List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
109             PolicyInstances result = new PolicyInstances();
110             for (PolicyInfo p : rspParsed) {
111                 result.add(p);
112             }
113             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
114         } catch (Exception e) {
115             return ErrorResponseHandler.handleException(e);
116         }
117     }
118
119     @Override
120     public ResponseEntity<Object> getPolicyInstance(String id) {
121         try {
122             String url = "/policy?id=" + id;
123             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
124             JsonObject obj = JsonParser.parseString(rsp.getBody()).getAsJsonObject();
125             String str = obj.toString();
126             return new ResponseEntity<>(str, rsp.getStatusCode());
127         } catch (Exception e) {
128             ResponseEntity<String> rsp = ErrorResponseHandler.handleException(e);
129             return new ResponseEntity<>(rsp.getBody(), rsp.getStatusCode());
130         }
131     }
132
133     @Override
134     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
135         String ric) {
136         String url =
137             "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel";
138
139         try {
140             String jsonStr = json.toString();
141             webClient.putForEntity(url, jsonStr).block();
142             return new ResponseEntity<>(HttpStatus.OK);
143         } catch (Exception e) {
144             return ErrorResponseHandler.handleException(e);
145         }
146     }
147
148     @Override
149     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
150         String url = "/policy?id=" + policyInstanceId;
151         try {
152             webClient.deleteForEntity(url).block();
153             return new ResponseEntity<>(HttpStatus.OK);
154         } catch (Exception e) {
155             return ErrorResponseHandler.handleException(e);
156         }
157     }
158
159     @Value.Immutable
160     @Gson.TypeAdapters
161     interface RicInfo {
162         public String ricName();
163
164         public Collection<String> nodeNames();
165
166         public Collection<String> policyTypes();
167     }
168
169     @Override
170     public ResponseEntity<String> getRicsSupportingType(String typeName) {
171         try {
172             String url = "/rics?policyType=" + typeName;
173             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
174
175             Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
176             List<RicInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
177             Collection<String> result = new ArrayList<>(rspParsed.size());
178             for (RicInfo ric : rspParsed) {
179                 result.add(ric.ricName());
180             }
181             String json = gson.toJson(result);
182             return new ResponseEntity<>(json, HttpStatus.OK);
183         } catch (Exception e) {
184             return ErrorResponseHandler.handleException(e);
185         }
186     }
187 }