2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 AT&T Intellectual Property
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
20 package org.oransc.ric.portal.dashboard.policyagentapi;
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 import java.lang.invoke.MethodHandles;
29 import java.lang.reflect.Type;
30 import java.util.Collection;
31 import java.util.List;
33 import java.util.Vector;
34 import org.immutables.gson.Gson;
35 import org.immutables.value.Value;
36 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
37 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
38 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
39 import org.oransc.ric.portal.dashboard.model.PolicyType;
40 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.stereotype.Component;
47 import org.springframework.web.client.RestClientException;
48 import org.springframework.web.client.RestTemplate;
50 @Component("PolicyAgentApi")
51 public class PolicyAgentApiImpl implements PolicyAgentApi {
52 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
54 RestTemplate restTemplate = new RestTemplate();
56 private static com.google.gson.Gson gson = new GsonBuilder() //
60 private final String urlPrefix;
63 public PolicyAgentApiImpl(
64 @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
65 logger.debug("ctor prefix '{}'", urlPrefix);
66 this.urlPrefix = urlPrefix;
69 private String baseUrl() {
75 interface PolicyTypeInfo {
79 public String schema();
83 public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
84 String url = baseUrl() + "/policy_schemas";
85 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
86 if (!rsp.getStatusCode().is2xxSuccessful()) {
87 return new ResponseEntity<>(rsp.getStatusCode());
90 PolicyTypes result = new PolicyTypes();
91 JsonParser jsonParser = new JsonParser();
92 JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
93 for (JsonElement schema : schemas) {
94 JsonObject schemaObj = schema.getAsJsonObject();
95 String title = schemaObj.get("title").getAsString();
96 String schemaAsStr = schemaObj.toString();
97 PolicyType pt = new PolicyType(title, schemaAsStr);
100 return new ResponseEntity<>(result, rsp.getStatusCode());
104 public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
105 String url = baseUrl() + "/policies?type={type}";
106 Map<String, ?> uriVariables = Map.of("type", type);
107 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
108 if (!rsp.getStatusCode().is2xxSuccessful()) {
109 return new ResponseEntity<>(rsp.getStatusCode());
112 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
113 List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
115 PolicyInstances result = new PolicyInstances();
116 for (PolicyInfo p : rspParsed) {
119 return new ResponseEntity<>(result, rsp.getStatusCode());
123 public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
124 String url = baseUrl() + "/policy?instance={id}";
125 Map<String, ?> uriVariables = Map.of("id", id);
127 return this.restTemplate.getForEntity(url, String.class, uriVariables);
131 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
132 throws RestClientException {
133 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
134 Map<String, ?> uriVariables = Map.of( //
135 "type", policyTypeIdString, //
136 "instance", policyInstanceId, //
138 "service", "dashboard");
141 this.restTemplate.put(url, json, uriVariables);
142 return new ResponseEntity<>(HttpStatus.OK);
143 } catch (Exception e) {
144 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
149 public void deletePolicy(String policyInstanceId) throws RestClientException {
150 String url = baseUrl() + "/policy?instance={instance}";
151 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
152 this.restTemplate.delete(url, uriVariables);
158 public String name();
160 public Collection<String> nodeNames();
162 public Collection<String> policyTypes();
166 public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
167 String url = baseUrl() + "/rics?policyType={typeName}";
168 Map<String, ?> uriVariables = Map.of("typeName", typeName);
169 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
171 Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
172 List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
174 Collection<String> result = new Vector<>(rspParsed.size());
175 for (RicInfo ric : rspParsed) {
176 result.add(ric.name());
178 return new ResponseEntity<>(result, HttpStatus.OK);