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.RestTemplate;
51 @Component("PolicyAgentApi")
52 public class PolicyAgentApiImpl implements PolicyAgentApi {
53 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
55 RestTemplate restTemplate = new RestTemplate();
57 private static com.google.gson.Gson gson = new GsonBuilder() //
61 private final String urlPrefix;
64 public PolicyAgentApiImpl(
65 @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
66 logger.debug("ctor prefix '{}'", urlPrefix);
67 this.urlPrefix = urlPrefix;
70 private String baseUrl() {
76 interface PolicyTypeInfo {
80 public String schema();
84 public ResponseEntity<String> getAllPolicyTypes() {
85 String url = baseUrl() + "/policy_schemas";
86 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
87 if (!rsp.getStatusCode().is2xxSuccessful()) {
91 PolicyTypes result = new PolicyTypes();
92 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<>(gson.toJson(result), rsp.getStatusCode());
103 } catch (Exception e) {
104 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
109 public ResponseEntity<String> getPolicyInstancesForType(String type) {
110 String url = baseUrl() + "/policies?type={type}";
111 Map<String, ?> uriVariables = Map.of("type", type);
112 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
113 if (!rsp.getStatusCode().is2xxSuccessful()) {
118 Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
120 List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
121 PolicyInstances result = new PolicyInstances();
122 for (PolicyInfo p : rspParsed) {
125 return new ResponseEntity<>(gson.toJson(result), rsp.getStatusCode());
126 } catch (Exception e) {
127 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
132 public ResponseEntity<String> getPolicyInstance(String id) {
133 String url = baseUrl() + "/policy?instance={id}";
134 Map<String, ?> uriVariables = Map.of("id", id);
136 return this.restTemplate.getForEntity(url, String.class, uriVariables);
140 public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json,
142 String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
143 Map<String, ?> uriVariables = Map.of( //
144 "type", policyTypeIdString, //
145 "instance", policyInstanceId, //
147 "service", "dashboard");
150 this.restTemplate.put(url, json, uriVariables);
151 return new ResponseEntity<>(HttpStatus.OK);
152 } catch (Exception e) {
153 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
158 public ResponseEntity<String> deletePolicy(String policyInstanceId) {
159 String url = baseUrl() + "/policy?instance={instance}";
160 Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
162 this.restTemplate.delete(url, uriVariables);
163 return new ResponseEntity<>(HttpStatus.OK);
164 } catch (Exception e) {
165 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
173 public String name();
175 public Collection<String> nodeNames();
177 public Collection<String> policyTypes();
181 public ResponseEntity<String> getRicsSupportingType(String typeName) {
182 String url = baseUrl() + "/rics?policyType={typeName}";
183 Map<String, ?> uriVariables = Map.of("typeName", typeName);
184 String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
187 Type listType = new TypeToken<List<ImmutableRicInfo>>() {
189 List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
190 Collection<String> result = new Vector<>(rspParsed.size());
191 for (RicInfo ric : rspParsed) {
192 result.add(ric.name());
194 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
195 } catch (Exception e) {
196 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);