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;
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;
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;
52 @Component("PolicyAgentApi")
53 public class PolicyAgentApiImpl implements PolicyAgentApi {
54 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 private final AsyncRestClient webClient;
58 private static com.google.gson.Gson gson = new GsonBuilder() //
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);
69 public PolicyAgentApiImpl(AsyncRestClient webClient) {
70 this.webClient = webClient;
74 public ResponseEntity<String> getAllPolicyTypes() {
75 final String TITLE = "title";
77 final String url = "/policy_schemas";
78 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
79 if (!rsp.getStatusCode().is2xxSuccessful()) {
83 PolicyTypes result = new PolicyTypes();
84 JsonArray schemas = JsonParser.parseString(rsp.getBody()).getAsJsonArray();
85 for (JsonElement schema : schemas) {
86 JsonObject schemaObj = schema.getAsJsonObject();
88 if (schemaObj.get(TITLE) != null) {
89 title = schemaObj.get(TITLE).getAsString();
91 PolicyType pt = new PolicyType(title, schemaObj.toString());
94 return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
95 } catch (Exception e) {
96 return ErrorResponseHandler.handleException(e);
101 public ResponseEntity<String> getPolicyInstancesForType(String type) {
103 String url = "/policies?type=" + type;
104 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
105 if (!rsp.getStatusCode().is2xxSuccessful()) {
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) {
115 return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
116 } catch (Exception e) {
117 return ErrorResponseHandler.handleException(e);
122 public ResponseEntity<Object> getPolicyInstance(String id) {
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());
136 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, Object json,
139 "/policy?type=" + policyTypeIdString + "&id=" + policyInstanceId + "&ric=" + ric + "&service=controlpanel";
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);
151 public ResponseEntity<String> deletePolicy(String policyInstanceId) {
152 String url = "/policy?id=" + policyInstanceId;
154 webClient.deleteForEntity(url).block();
155 return new ResponseEntity<>(HttpStatus.OK);
156 } catch (Exception e) {
157 return ErrorResponseHandler.handleException(e);
164 public String ricName();
166 public Collection<String> nodeNames();
168 public Collection<String> policyTypes();
172 public ResponseEntity<String> getRicsSupportingType(String typeName) {
174 String url = "/rics?policyType=" + typeName;
175 ResponseEntity<String> rsp = webClient.getForEntity(url).block();
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());
183 String json = gson.toJson(result);
184 return new ResponseEntity<>(json, HttpStatus.OK);
185 } catch (Exception e) {
186 return ErrorResponseHandler.handleException(e);