Merge "Support of selecting RIC in dashboard"
[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.annotations.SerializedName;
43 import com.google.gson.reflect.TypeToken;
44
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.immutables.gson.Gson;
48 import org.immutables.value.Value;
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     private PolicyType toPolicyType(PolicyTypeInfo i) {
83         return new PolicyType(i.name(), i.schema());
84     }
85
86     @Override
87     public PolicyTypes getAllPolicyTypes() throws RestClientException {
88         String url = baseUrl() + "/policy_types";
89         String rsp = this.restTemplate.getForObject(url, String.class);
90
91         Type listType = new TypeToken<List<ImmutablePolicyTypeInfo>>() {
92         }.getType();
93         List<PolicyTypeInfo> rspParsed = gson.fromJson(rsp, listType);
94
95         PolicyTypes result = new PolicyTypes();
96         for (PolicyTypeInfo i : rspParsed) {
97             result.add(toPolicyType(i));
98         }
99         return result;
100     }
101
102     @Override
103     public PolicyInstances getPolicyInstancesForType(String type) {
104         String url = baseUrl() + "/policies?type={type}";
105         Map<String, ?> uriVariables = Map.of("type", type);
106         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
107
108         Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
109         }.getType();
110         List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
111
112         PolicyInstances result = new PolicyInstances();
113         for (PolicyInfo p : rspParsed) {
114             result.add(p);
115         }
116         return result;
117     }
118
119     @Override
120     public String getPolicyInstance(String id) throws RestClientException {
121         String url = baseUrl() + "/policy?instance={id}";
122         Map<String, ?> uriVariables = Map.of("id", id);
123
124         return this.restTemplate.getForObject(url, String.class, uriVariables);
125     }
126
127     @Override
128     public void putPolicy(String policyTypeIdString, String policyInstanceId, String json, String ric)
129             throws RestClientException {
130         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
131         Map<String, ?> uriVariables = Map.of( //
132                 "type", policyTypeIdString, //
133                 "instance", policyInstanceId, //
134                 "ric", ric, //
135                 "service", "dashboard");
136
137         this.restTemplate.put(url, json, uriVariables);
138     }
139
140     @Override
141     public void deletePolicy(String policyInstanceId) throws RestClientException {
142         String url = baseUrl() + "/policy?instance={instance}";
143         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
144         this.restTemplate.delete(url, uriVariables);
145     }
146
147     @Value.Immutable
148     @Gson.TypeAdapters
149     interface RicInfo {
150         public String name();
151
152         public Collection<String> nodeNames();
153
154         public Collection<String> policyTypes();
155     }
156
157     @Override
158     public Collection<String> getRicsSupportingType(String typeName) {
159         String url = baseUrl() + "/rics?policyType={typeName}";
160         Map<String, ?> uriVariables = Map.of("typeName", typeName);
161         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
162
163         Type listType = new TypeToken<List<ImmutableRicInfo>>() {
164         }.getType();
165         List<RicInfo> rspParsed = gson.fromJson(rsp, listType);
166
167         Collection<String> result = new Vector<>(rspParsed.size());
168         for (RicInfo ric : rspParsed) {
169             result.add(ric.name());
170         }
171         return result;
172     }
173
174 }