Add getJobs and adapt getProducers
[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
30 import java.lang.invoke.MethodHandles;
31 import java.lang.reflect.Type;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36 import org.immutables.gson.Gson;
37 import org.immutables.value.Value;
38 import org.oransc.portal.nonrtric.controlpanel.model.ImmutablePolicyInfo;
39 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInfo;
40 import org.oransc.portal.nonrtric.controlpanel.model.PolicyInstances;
41 import org.oransc.portal.nonrtric.controlpanel.model.PolicyType;
42 import org.oransc.portal.nonrtric.controlpanel.model.PolicyTypes;
43 import org.oransc.portal.nonrtric.controlpanel.util.AsyncRestClient;
44 import org.oransc.portal.nonrtric.controlpanel.util.ErrorResponseHandler;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.stereotype.Component;
51
52 @Component("PolicyAgentApi")
53 public class PolicyAgentApiImpl implements PolicyAgentApi {
54     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55
56     private final AsyncRestClient webClient;
57
58     private static com.google.gson.Gson gson = new GsonBuilder() //
59         .serializeNulls() //
60         .create(); //
61
62     @Autowired
63     public PolicyAgentApiImpl(
64         @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
65         this(new AsyncRestClient(urlPrefix));
66         logger.debug("ctor prefix '{}'", urlPrefix);
67     }
68
69     public PolicyAgentApiImpl(AsyncRestClient webClient) {
70         this.webClient = webClient;
71     }
72
73     @Override
74     public ResponseEntity<String> getAllPolicyTypes() {
75         final String TITLE = "title";
76         try {
77             final String url = "/policy_schemas";
78             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
79             if (!rsp.getStatusCode().is2xxSuccessful()) {
80                 return rsp;
81             }
82
83             PolicyTypes result = new PolicyTypes();
84             JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
85             for (JsonElement schema : schemas) {
86                 JsonObject schemaObj = schema.getAsJsonObject();
87                 String title = "";
88                 if (schemaObj.get(TITLE) != null) {
89                     title = schemaObj.get(TITLE).getAsString();
90                 }
91                 PolicyType pt = new PolicyType(title, schemaObj.toString());
92                 result.add(pt);
93             }
94             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
95         } catch (Exception e) {
96             return ErrorResponseHandler.handleException(e);
97         }
98     }
99
100     @Override
101     public ResponseEntity<String> getPolicyInstancesForType(String type) {
102         try {
103             String url = "/policies?type=" + type;
104             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
105             if (!rsp.getStatusCode().is2xxSuccessful()) {
106                 return rsp;
107             }
108
109             Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
110             List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
111             PolicyInstances result = new PolicyInstances();
112             for (PolicyInfo p : rspParsed) {
113                 result.add(p);
114             }
115             return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
116         } catch (Exception e) {
117             return ErrorResponseHandler.handleException(e);
118         }
119     }
120
121     @Override
122     public ResponseEntity<Object> getPolicyInstance(String id) {
123         try {
124             String url = "/policy?id=" + id;
125             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
126             JsonObject obj = JsonParser.parseString(rsp.getBody()).getAsJsonObject();
127             String str = obj.toString();
128             return new ResponseEntity<>(str, rsp.getStatusCode());
129         } catch (Exception e) {
130             ResponseEntity<String> rsp = ErrorResponseHandler.handleException(e);
131             return new ResponseEntity<>(rsp.getBody(), rsp.getStatusCode());
132         }
133     }
134
135     @Override
136     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
137         String ric) {
138         String url =
139             "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel";
140
141         try {
142             String jsonStr = json.toString();
143             webClient.putForEntity(url, jsonStr).block();
144             return new ResponseEntity<>(HttpStatus.OK);
145         } catch (Exception e) {
146             return ErrorResponseHandler.handleException(e);
147         }
148     }
149
150     @Override
151     public ResponseEntity<String> deletePolicy(String policyInstanceId) {
152         String url = "/policy?id=" + policyInstanceId;
153         try {
154             webClient.deleteForEntity(url).block();
155             return new ResponseEntity<>(HttpStatus.OK);
156         } catch (Exception e) {
157             return ErrorResponseHandler.handleException(e);
158         }
159     }
160
161     @Value.Immutable
162     @Gson.TypeAdapters
163     interface RicInfo {
164         public String ricName();
165
166         public Collection<String> nodeNames();
167
168         public Collection<String> policyTypes();
169     }
170
171     @Override
172     public ResponseEntity<String> getRicsSupportingType(String typeName) {
173         try {
174             String url = "/rics?policyType=" + typeName;
175             ResponseEntity<String> rsp = webClient.getForEntity(url).block();
176
177             Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
178             List<RicInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
179             Collection<String> result = new ArrayList<>(rspParsed.size());
180             for (RicInfo ric : rspParsed) {
181                 result.add(ric.ricName());
182             }
183             String json = gson.toJson(result);
184             return new ResponseEntity<>(json, HttpStatus.OK);
185         } catch (Exception e) {
186             return ErrorResponseHandler.handleException(e);
187         }
188     }
189 }