b80389cf3e549916a8831824ddf3a36615d98754
[nonrtric.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / policyagentapi / PolicyAgentApiImpl.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.ric.portal.dashboard.policyagentapi;
21
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;
28 import java.lang.invoke.MethodHandles;
29 import java.lang.reflect.Type;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Vector;
34 import org.immutables.gson.Gson;
35 import org.immutables.value.Value;
36 import org.oransc.ric.portal.dashboard.model.ImmutablePolicyInfo;
37 import org.oransc.ric.portal.dashboard.model.PolicyInfo;
38 import org.oransc.ric.portal.dashboard.model.PolicyInstances;
39 import org.oransc.ric.portal.dashboard.model.PolicyType;
40 import org.oransc.ric.portal.dashboard.model.PolicyTypes;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.stereotype.Component;
47 import org.springframework.web.client.RestClientException;
48 import org.springframework.web.client.RestTemplate;
49
50 @Component("PolicyAgentApi")
51 public class PolicyAgentApiImpl implements PolicyAgentApi {
52     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54     RestTemplate restTemplate = new RestTemplate();
55
56     private static com.google.gson.Gson gson = new GsonBuilder() //
57         .serializeNulls() //
58         .create(); //
59
60     private final String urlPrefix;
61
62     @Autowired
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;
67     }
68
69     private String baseUrl() {
70         return urlPrefix;
71     }
72
73     @Value.Immutable
74     @Gson.TypeAdapters
75     interface PolicyTypeInfo {
76
77         public String name();
78
79         public String schema();
80     }
81
82     @Override
83     public ResponseEntity<PolicyTypes> getAllPolicyTypes() throws RestClientException {
84         String url = baseUrl() + "/policy_schemas";
85         ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
86         if (!rsp.getStatusCode().is2xxSuccessful()) {
87             return new ResponseEntity<>(rsp.getStatusCode());
88         }
89
90         PolicyTypes result = new PolicyTypes();
91         JsonParser jsonParser = new JsonParser();
92         JsonArray schemas = jsonParser.parse(rsp.getBody()).getAsJsonArray();
93         for (JsonElement schema : schemas) {
94             JsonObject schemaObj = schema.getAsJsonObject();
95             String title = schemaObj.get("title").getAsString();
96             String schemaAsStr = schemaObj.toString();
97             PolicyType pt = new PolicyType(title, schemaAsStr);
98             result.add(pt);
99         }
100         return new ResponseEntity<>(result, rsp.getStatusCode());
101     }
102
103     @Override
104     public ResponseEntity<PolicyInstances> getPolicyInstancesForType(String type) {
105         String url = baseUrl() + "/policies?type={type}";
106         Map<String, ?> uriVariables = Map.of("type", type);
107         ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class, uriVariables);
108         if (!rsp.getStatusCode().is2xxSuccessful()) {
109             return new ResponseEntity<>(rsp.getStatusCode());
110         }
111
112         Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {}.getType();
113         List<PolicyInfo> rspParsed = gson.fromJson(rsp.getBody(), listType);
114
115         PolicyInstances result = new PolicyInstances();
116         for (PolicyInfo p : rspParsed) {
117             result.add(p);
118         }
119         return new ResponseEntity<>(result, rsp.getStatusCode());
120     }
121
122     @Override
123     public ResponseEntity<String> getPolicyInstance(String id) throws RestClientException {
124         String url = baseUrl() + "/policy?instance={id}";
125         Map<String, ?> uriVariables = Map.of("id", id);
126
127         return this.restTemplate.getForEntity(url, String.class, uriVariables);
128     }
129
130     @Override
131     public ResponseEntity<String> putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
132         throws RestClientException {
133         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
134         Map<String, ?> uriVariables = Map.of( //
135             "type", policyTypeIdString, //
136             "instance", policyInstanceId, //
137             "ric", ric, //
138             "service", "dashboard");
139
140         try {
141             this.restTemplate.put(url, json, uriVariables);
142             return new ResponseEntity<>(HttpStatus.OK);
143         } catch (Exception e) {
144             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
145         }
146     }
147
148     @Override
149     public void deletePolicy(String policyInstanceId) throws RestClientException {
150         String url = baseUrl() + "/policy?instance={instance}";
151         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
152         this.restTemplate.delete(url, uriVariables);
153     }
154
155     @Value.Immutable
156     @Gson.TypeAdapters
157     interface RicInfo {
158         public String name();
159
160         public Collection<String> nodeNames();
161
162         public Collection<String> policyTypes();
163     }
164
165     @Override
166     public ResponseEntity<Collection<String>> getRicsSupportingType(String typeName) {
167         String url = baseUrl() + "/rics?policyType={typeName}";
168         Map<String, ?> uriVariables = Map.of("typeName", typeName);
169         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
170
171         Type listType = new TypeToken<List<ImmutableRicInfo>>() {}.getType();
172         List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
173
174         Collection<String> result = new Vector<>(rspParsed.size());
175         for (RicInfo ric : rspParsed) {
176             result.add(ric.name());
177         }
178         return new ResponseEntity<>(result, HttpStatus.OK);
179     }
180
181 }