Merge "Removed the duplicate dependency"
[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 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;
29
30 import org.springframework.stereotype.Component;
31 import org.springframework.web.client.RestClientException;
32 import org.springframework.web.client.RestTemplate;
33
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Vector;
38 import java.lang.invoke.MethodHandles;
39 import java.lang.reflect.Type;
40
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.JsonArray;
43 import com.google.gson.JsonElement;
44 import com.google.gson.JsonObject;
45 import com.google.gson.JsonParser;
46 import com.google.gson.annotations.SerializedName;
47 import com.google.gson.reflect.TypeToken;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.immutables.gson.Gson;
52 import org.immutables.value.Value;
53
54 @Component("PolicyAgentApi")
55 public class PolicyAgentApiImpl implements PolicyAgentApi {
56     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
57
58     RestTemplate restTemplate = new RestTemplate();
59
60     private static com.google.gson.Gson gson = new GsonBuilder() //
61             .serializeNulls() //
62             .create(); //
63
64     private final String urlPrefix;
65
66     @Autowired
67     public PolicyAgentApiImpl(
68             @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
69         logger.debug("ctor prefix '{}'", urlPrefix);
70         this.urlPrefix = urlPrefix;
71     }
72
73     private String baseUrl() {
74         return urlPrefix;
75     }
76
77     @Value.Immutable
78     @Gson.TypeAdapters
79     interface PolicyTypeInfo {
80
81         public String name();
82
83         public String schema();
84     }
85
86     @Override
87     public PolicyTypes getAllPolicyTypes() throws RestClientException {
88         String url = baseUrl() + "/policy_schemas";
89         String rsp = this.restTemplate.getForObject(url, String.class);
90
91         PolicyTypes result = new PolicyTypes();
92         JsonParser jsonParser = new JsonParser();
93         JsonArray schemas = jsonParser.parse(rsp).getAsJsonArray();
94         for (JsonElement schema : schemas) {
95             JsonObject schemaObj = schema.getAsJsonObject();
96             String title = schemaObj.get("title").getAsString();
97             String schemaAsStr = schemaObj.toString();
98             PolicyType pt = new PolicyType(title, schemaAsStr);
99             result.add(pt);
100         }
101         return result;
102     }
103
104     @Override
105     public PolicyInstances getPolicyInstancesForType(String type) {
106         String url = baseUrl() + "/policies?type={type}";
107         Map<String, ?> uriVariables = Map.of("type", type);
108         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
109
110         Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
111         }.getType();
112         List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
113
114         PolicyInstances result = new PolicyInstances();
115         for (PolicyInfo p : rspParsed) {
116             result.add(p);
117         }
118         return result;
119     }
120
121     @Override
122     public String getPolicyInstance(String id) throws RestClientException {
123         String url = baseUrl() + "/policy?instance={id}";
124         Map<String, ?> uriVariables = Map.of("id", id);
125
126         return this.restTemplate.getForObject(url, String.class, uriVariables);
127     }
128
129     @Override
130     public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
131             throws RestClientException {
132         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
133         Map<String, ?> uriVariables = Map.of( //
134                 "type", policyTypeIdString, //
135                 "instance", policyInstanceId, //
136                 "ric", ric, //
137                 "service", "dashboard");
138
139         this.restTemplate.put(url, json, uriVariables);
140     }
141
142     @Override
143     public void deletePolicy(String policyInstanceId) throws RestClientException {
144         String url = baseUrl() + "/policy?instance={instance}";
145         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
146         this.restTemplate.delete(url, uriVariables);
147     }
148
149     @Value.Immutable
150     @Gson.TypeAdapters
151     interface RicInfo {
152         public String name();
153
154         public Collection<String> nodeNames();
155
156         public Collection<String> policyTypes();
157     }
158
159     @Override
160     public Collection<String> getRicsSupportingType(String typeName) {
161         String url = baseUrl() + "/rics?policyType={typeName}";
162         Map<String, ?> uriVariables = Map.of("typeName", typeName);
163         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
164
165         Type listType = new TypeToken<List<ImmutableRicInfo>>() {
166         }.getType();
167         List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
168
169         Collection<String> result = new Vector<>(rspParsed.size());
170         for (RicInfo ric : rspParsed) {
171             result.add(ric.name());
172         }
173         return result;
174     }
175
176 }