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.annotations.SerializedName;
43 import com.google.gson.reflect.TypeToken;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.immutables.gson.Gson;
48 import org.immutables.value.Value;
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();
82 private PolicyType toPolicyType(PolicyTypeInfo i) {
83 return new PolicyType(i.name(), i.schema());
87 public PolicyTypes getAllPolicyTypes() throws RestClientException {
88 String url = baseUrl() + "/policy_types";
89 String rsp = this.restTemplate.getForObject(url, String.class);
91 Type listType = new TypeToken<List<ImmutablePolicyTypeInfo>>() {
93 List<PolicyTypeInfo> rspParsed = gson.fromJson(rsp, listType);
95 PolicyTypes result = new PolicyTypes();
96 for (PolicyTypeInfo i : rspParsed) {
97 result.add(toPolicyType(i));
103 public PolicyInstances getPolicyInstancesForType(String type) {
104 String url = baseUrl() + "/policies?type={type}";
105 Map<String, ?> uriVariables = Map.of("type", type);
106 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
108 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
110 List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
112 PolicyInstances result = new PolicyInstances();
113 for (PolicyInfo p : rspParsed) {
120 public String getPolicyInstance(String id) throws RestClientException {
121 String url = baseUrl() + "/policy?instance={id}";
122 Map<String, ?> uriVariables = Map.of("id", id);
124 return this.restTemplate.getForObject(url, String.class, uriVariables);
128 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
129 throws RestClientException {
130 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
131 Map<String, ?> uriVariables = Map.of( //
132 "type", policyTypeIdString, //
133 "instance", policyInstanceId, //
135 "service", "dashboard");
137 this.restTemplate.put(url, json, uriVariables);
141 public void deletePolicy(String policyInstanceId) throws RestClientException {
142 String url = baseUrl() + "/policy?instance={instance}";
143 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
144 this.restTemplate.delete(url, uriVariables);
150 public String name();
152 public Collection<String> nodeNames();
154 public Collection<String> policyTypes();
158 public Collection<String> getRicsSupportingType(String typeName) {
159 String url = baseUrl() + "/rics?policyType={typeName}";
160 Map<String, ?> uriVariables = Map.of("typeName", typeName);
161 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
163 Type listType = new TypeToken<List<ImmutableRicInfo>>() {
165 List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
167 Collection<String> result = new Vector<>(rspParsed.size());
168 for (RicInfo ric : rspParsed) {
169 result.add(ric.name());