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.List;
36 import java.lang.invoke.MethodHandles;
37 import java.lang.reflect.Type;
39 import com.google.gson.GsonBuilder;
40 import com.google.gson.annotations.SerializedName;
41 import com.google.gson.reflect.TypeToken;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.immutables.gson.Gson;
46 import org.immutables.value.Value;
48 @Component("PolicyAgentApi")
49 public class PolicyAgentApiImpl implements PolicyAgentApi {
50 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52 RestTemplate restTemplate = new RestTemplate();
54 private static com.google.gson.Gson gson = new GsonBuilder() //
58 private final String urlPrefix;
61 public PolicyAgentApiImpl(
62 @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
63 logger.debug("ctor prefix '{}'", urlPrefix);
64 this.urlPrefix = urlPrefix;
67 private String baseUrl() {
73 interface PolicyTypeInfo {
77 public String schema();
80 private PolicyType toPolicyType(PolicyTypeInfo i) {
81 return new PolicyType(i.name(), i.schema());
85 public PolicyTypes getAllPolicyTypes() throws RestClientException {
86 String url = baseUrl() + "/policy_types";
87 String rsp = this.restTemplate.getForObject(url, String.class);
89 Type listType = new TypeToken<List<ImmutablePolicyTypeInfo>>() {
91 List<PolicyTypeInfo> rspParsed = gson.fromJson(rsp, listType);
93 PolicyTypes result = new PolicyTypes();
94 for (PolicyTypeInfo i : rspParsed) {
95 result.add(toPolicyType(i));
101 public PolicyInstances getPolicyInstancesForType(String type) {
102 String url = baseUrl() + "/policies?type={type}";
103 Map<String, ?> uriVariables = Map.of("type", type);
104 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
106 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
108 List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
110 PolicyInstances result = new PolicyInstances();
111 for (PolicyInfo p : rspParsed) {
118 public String getPolicyInstance(String id) throws RestClientException {
119 String url = baseUrl() + "/policy?instance={id}";
120 Map<String, ?> uriVariables = Map.of("id", id);
122 return this.restTemplate.getForObject(url, String.class, uriVariables);
126 public void putPolicy(String policyTypeIdString, String policyInstanceId, String json) throws RestClientException {
127 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
128 Map<String, ?> uriVariables = Map.of( //
129 "type", policyTypeIdString, //
130 "instance", policyInstanceId, //
131 "ric", "ric1", // TODO
132 "service", "dashboard");
134 this.restTemplate.put(url, json, uriVariables);
138 public void deletePolicy(String policyInstanceId) throws RestClientException {
139 String url = baseUrl() + "/policy?instance={instance}";
140 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
141 this.restTemplate.delete(url, uriVariables);