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 org.oransc.ric.portal.dashboard.DashboardConstants;
23 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
24 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
25 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
26 import org.oransc.ric.portal.dashboard.model.PolicyType;
27 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
28 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.client.RestClientException;
32 import org.springframework.web.client.RestTemplate;
34 import java.util.Collection;
35 import java.util.List;
37 import java.util.Vector;
38 import java.lang.invoke.MethodHandles;
39 import java.lang.reflect.Type;
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.JsonArray;
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonObject;
45 import com.google.gson.JsonParser;
46 import com.google.gson.annotations.SerializedName;
47 import com.google.gson.reflect.TypeToken;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.immutables.gson.Gson;
52 import org.immutables.value.Value;
54 @Component("PolicyAgentApi")
55 public class PolicyAgentApiImpl implements PolicyAgentApi {
56 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
58 RestTemplate restTemplate = new RestTemplate();
60 private static com.google.gson.Gson gson = new GsonBuilder() //
64 private final String urlPrefix;
67 public PolicyAgentApiImpl(
68 @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
69 logger.debug("ctor prefix '{}'", urlPrefix);
70 this.urlPrefix = urlPrefix;
73 private String baseUrl() {
79 interface PolicyTypeInfo {
83 public String schema();
87 public PolicyTypes getAllPolicyTypes() throws RestClientException {
88 String url = baseUrl() + "/policy_schemas";
89 String rsp = this.restTemplate.getForObject(url, String.class);
91 PolicyTypes result = new PolicyTypes();
92 JsonParser jsonParser = new JsonParser();
93 JsonArray schemas = jsonParser.parse(rsp).getAsJsonArray();
94 for (JsonElement schema : schemas) {
95 JsonObject schemaObj = schema.getAsJsonObject();
96 String title = schemaObj.get("title").getAsString();
97 String schemaAsStr = schemaObj.toString();
98 PolicyType pt = new PolicyType(title, schemaAsStr);
105 public PolicyInstances getPolicyInstancesForType(String type) {
106 String url = baseUrl() + "/policies?type={type}";
107 Map<String, ?> uriVariables = Map.of("type", type);
108 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
110 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
112 List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
114 PolicyInstances result = new PolicyInstances();
115 for (PolicyInfo p : rspParsed) {
122 public String getPolicyInstance(String id) throws RestClientException {
123 String url = baseUrl() + "/policy?instance={id}";
124 Map<String, ?> uriVariables = Map.of("id", id);
126 return this.restTemplate.getForObject(url, String.class, uriVariables);
130 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
131 throws RestClientException {
132 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
133 Map<String, ?> uriVariables = Map.of( //
134 "type", policyTypeIdString, //
135 "instance", policyInstanceId, //
137 "service", "dashboard");
139 this.restTemplate.put(url, json, uriVariables);
143 public void deletePolicy(String policyInstanceId) throws RestClientException {
144 String url = baseUrl() + "/policy?instance={instance}";
145 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
146 this.restTemplate.delete(url, uriVariables);
152 public String name();
154 public Collection<String> nodeNames();
156 public Collection<String> policyTypes();
160 public Collection<String> getRicsSupportingType(String typeName) {
161 String url = baseUrl() + "/rics?policyType={typeName}";
162 Map<String, ?> uriVariables = Map.of("typeName", typeName);
163 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
165 Type listType = new TypeToken<List<ImmutableRicInfo>>() {
167 List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
169 Collection<String> result = new Vector<>(rspParsed.size());
170 for (RicInfo ric : rspParsed) {
171 result.add(ric.name());