Merge "Add Error Handling in A1 Client"
[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.List;
35 import java.util.Map;
36 import java.lang.invoke.MethodHandles;
37 import java.lang.reflect.Type;
38
39 import com.google.gson.GsonBuilder;
40 import com.google.gson.annotations.SerializedName;
41 import com.google.gson.reflect.TypeToken;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45 import org.immutables.gson.Gson;
46 import org.immutables.value.Value;
47
48 @Component("PolicyAgentApi")
49 public class PolicyAgentApiImpl implements PolicyAgentApi {
50     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
52     RestTemplate restTemplate = new RestTemplate();
53
54     private static com.google.gson.Gson gson = new GsonBuilder() //
55             .serializeNulls() //
56             .create(); //
57
58     private final String urlPrefix;
59
60     @Autowired
61     public PolicyAgentApiImpl(
62             @org.springframework.beans.factory.annotation.Value("${policycontroller.url.prefix}") final String urlPrefix) {
63         logger.debug("ctor prefix '{}'", urlPrefix);
64         this.urlPrefix = urlPrefix;
65     }
66
67     private String baseUrl() {
68         return urlPrefix;
69     }
70
71     @Value.Immutable
72     @Gson.TypeAdapters
73     interface PolicyTypeInfo {
74
75         public String name();
76
77         public String schema();
78     }
79
80     private PolicyType toPolicyType(PolicyTypeInfo i) {
81         return new PolicyType(i.name(), i.schema());
82     }
83
84     @Override
85     public PolicyTypes getAllPolicyTypes() throws RestClientException {
86         String url = baseUrl() + "/policy_types";
87         String rsp = this.restTemplate.getForObject(url, String.class);
88
89         Type listType = new TypeToken<List<ImmutablePolicyTypeInfo>>() {
90         }.getType();
91         List<PolicyTypeInfo> rspParsed = gson.fromJson(rsp, listType);
92
93         PolicyTypes result = new PolicyTypes();
94         for (PolicyTypeInfo i : rspParsed) {
95             result.add(toPolicyType(i));
96         }
97         return result;
98     }
99
100     @Override
101     public PolicyInstances getPolicyInstancesForType(String type) {
102         String url = baseUrl() + "/policies?type={type}";
103         Map<String, ?> uriVariables = Map.of("type", type);
104         String rsp = this.restTemplate.getForObject(url, String.class, uriVariables);
105
106         Type listType = new TypeToken<List<ImmutablePolicyInfo>>() {
107         }.getType();
108         List<PolicyInfo> rspParsed = gson.fromJson(rsp, listType);
109
110         PolicyInstances result = new PolicyInstances();
111         for (PolicyInfo p : rspParsed) {
112             result.add(p);
113         }
114         return result;
115
116     }
117
118     @Override
119     public String getPolicyInstance(String id) throws RestClientException {
120         String url = baseUrl() + "/policy?instance={id}";
121         Map<String, ?> uriVariables = Map.of("id", id);
122
123         return this.restTemplate.getForObject(url, String.class, uriVariables);
124     }
125
126     @Override
127     public void putPolicy(String policyTypeIdString, String policyInstanceId, String json) throws RestClientException {
128         String url = baseUrl() + "/policy?type={type}&instance={instance}&ric={ric}&service={service}";
129         Map<String, ?> uriVariables = Map.of( //
130                 "type", policyTypeIdString, //
131                 "instance", policyInstanceId, //
132                 "ric", "ric1", // TODO
133                 "service", "dashboard");
134
135         this.restTemplate.put(url, json, uriVariables);
136     }
137
138     @Override
139     public void deletePolicy(String policyInstanceId) throws RestClientException {
140         String url = baseUrl() + "/policy?instance={instance}";
141         Map<String, ?> uriVariables = Map.of("instance", policyInstanceId);
142         this.restTemplate.delete(url, uriVariables);
143     }
144
145 }