2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
6 * Modifications Copyright (C) 2020 Nordix Foundation
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.portal.nonrtric.controlpanel.policyagentapi;
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;
50 @Component("PolicyAgentApi")
51 public class PolicyAgentApiImpl implements PolicyAgentApi {
52 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54 private final AsyncRestClient webClient;
56 private static com.google.gson.Gson gson = new GsonBuilder() //
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);
67 public PolicyAgentApiImpl(AsyncRestClient webClient) {
68 this.webClient = webClient;
72 public ResponseEntity<String> getAllPolicyTypes() {
73 final String TITLE = "title";
75 final String url = "/policy_schemas";
76 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
77 if (!rsp.getStatusCode().is2xxSuccessful()) {
81 PolicyTypes result = new PolicyTypes();
82 JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
83 for (JsonElement schema : schemas) {
84 JsonObject schemaObj = schema.getAsJsonObject();
86 if (schemaObj.get(TITLE) != null) {
87 title = schemaObj.get(TITLE).getAsString();
89 PolicyType pt = new PolicyType(title, schemaObj.toString());
92 return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
93 } catch (Exception e) {
94 return ErrorResponseHandler.handleException(e);
99 public ResponseEntity<String> getPolicyInstancesForType(String type) {
101 String url = "/policies?type=" + type;
102 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
103 if (!rsp.getStatusCode().is2xxSuccessful()) {
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) {
113 return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
114 } catch (Exception e) {
115 return ErrorResponseHandler.handleException(e);
120 public ResponseEntity<Object> getPolicyInstance(String id) {
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());
134 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
137 "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel";
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);
149 public ResponseEntity<String> deletePolicy(String policyInstanceId) {
150 String url = "/policy?id=" + policyInstanceId;
152 webClient.deleteForEntity(url).block();
153 return new ResponseEntity<>(HttpStatus.OK);
154 } catch (Exception e) {
155 return ErrorResponseHandler.handleException(e);
162 public String ricName();
164 public Collection<String> nodeNames();
166 public Collection<String> policyTypes();
170 public ResponseEntity<String> getRicsSupportingType(String typeName) {
172 String url = "/rics?policyType=" + typeName;
173 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
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());
181 String json = gson.toJson(result);
182 return new ResponseEntity<>(json, HttpStatus.OK);
183 } catch (Exception e) {
184 return ErrorResponseHandler.handleException(e);