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;
29 import java.lang.invoke.MethodHandles;
30 import java.lang.reflect.Type;
31 import java.util.Collection;
32 import java.util.List;
34 import java.util.Vector;
36 import org.immutables.gson.Gson;
37 import org.immutables.value.Value;
38 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
39 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
40 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
41 import org.oransc.ric.portal.dashboard.model.PolicyType;
42 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.springframework.beans.factory.annotation.Autowired;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.ResponseEntity;
48 import org.springframework.stereotype.Component;
49 import org.springframework.web.client.RestClientException;
50 import org.springframework.web.client.RestTemplate;
52 @Component("PolicyAgentApi")
53 public class PolicyAgentApiImpl implements PolicyAgentApi {
54 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
56 RestTemplate restTemplate = new RestTemplate();
58 private static com.google.gson.Gson gson = new GsonBuilder() //
62 private final String urlPrefix;
65 public PolicyAgentApiImpl(
66 @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
67 logger.debug("ctor prefix '{}'", urlPrefix);
68 this.urlPrefix = urlPrefix;
71 private String baseUrl() {
77 interface PolicyTypeInfo {
81 public String schema();
85 public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
86 String url = baseUrl() + "/policy_schemas";
87 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
88 if (!rsp.getStatusCode().is2xxSuccessful()) {
89 return new ResponseEntity<>(rsp.getStatusCode());
92 PolicyTypes result = new PolicyTypes();
93 JsonParser jsonParser = new JsonParser();
94 JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
95 for (JsonElement schema : schemas) {
96 JsonObject schemaObj = schema.getAsJsonObject();
97 String title = schemaObj.get("title").getAsString();
98 String schemaAsStr = schemaObj.toString();
99 PolicyType pt = new PolicyType(title, schemaAsStr);
102 return new ResponseEntity<>(result, rsp.getStatusCode());
106 public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
107 String url = baseUrl() + "/policies?type={type}";
108 Map<String, ?> uriVariables = Map.of("type", type);
109 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
110 if (!rsp.getStatusCode().is2xxSuccessful()) {
111 return new ResponseEntity<>(rsp.getStatusCode());
114 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
115 List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
117 PolicyInstances result = new PolicyInstances();
118 for (PolicyInfo p : rspParsed) {
121 return new ResponseEntity<>(result, rsp.getStatusCode());
125 public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
126 String url = baseUrl() + "/policy?instance={id}";
127 Map<String, ?> uriVariables = Map.of("id", id);
129 return this.restTemplate.getForEntity(url, String.class, uriVariables);
133 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
134 throws RestClientException {
135 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
136 Map<String, ?> uriVariables = Map.of( //
137 "type", policyTypeIdString, //
138 "instance", policyInstanceId, //
140 "service", "dashboard");
143 this.restTemplate.put(url, json, uriVariables);
144 return new ResponseEntity<>(HttpStatus.OK);
145 } catch (Exception e) {
146 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
151 public void deletePolicy(String policyInstanceId) throws RestClientException {
152 String url = baseUrl() + "/policy?instance={instance}";
153 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
154 this.restTemplate.delete(url, uriVariables);
160 public String name();
162 public Collection<String> nodeNames();
164 public Collection<String> policyTypes();
168 public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
169 String url = baseUrl() + "/rics?policyType={typeName}";
170 Map<String, ?> uriVariables = Map.of("typeName", typeName);
171 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
173 Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
174 List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
176 Collection<String> result = new Vector<>(rspParsed.size());
177 for (RicInfo ric : rspParsed) {
178 result.add(ric.name());
180 return new ResponseEntity<>(result, HttpStatus.OK);