Merge "Removed the duplicate dependency"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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
21 package org.oransc.policyagent.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
30
31 import java.util.Collection;
32 import java.util.Vector;
33
34 import org.oransc.policyagent.configuration.ApplicationConfig;
35 import org.oransc.policyagent.exceptions.ServiceException;
36 import org.oransc.policyagent.repository.ImmutablePolicy;
37 import org.oransc.policyagent.repository.Policies;
38 import org.oransc.policyagent.repository.Policy;
39 import org.oransc.policyagent.repository.PolicyType;
40 import org.oransc.policyagent.repository.PolicyTypes;
41 import org.oransc.policyagent.repository.Ric;
42 import org.oransc.policyagent.repository.Rics;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.DeleteMapping;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
52
53 @RestController
54 @Api(value = "Policy Management API")
55 public class PolicyController {
56
57     private final Rics rics;
58     private final PolicyTypes policyTypes;
59     private final Policies policies;
60     private static Gson gson = new GsonBuilder() //
61         .serializeNulls() //
62         .create(); //
63
64     @Autowired
65     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics) {
66         this.policyTypes = types;
67         this.policies = policies;
68         this.rics = rics;
69     }
70
71     @GetMapping("/policy_schemas")
72     @ApiOperation(value = "Returns policy type schema definitions")
73     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
74     public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
75         if (ricName == null) {
76             Collection<PolicyType> types = this.policyTypes.getAll();
77             return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
78         } else {
79             try {
80                 Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
81                 return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
82             } catch (ServiceException e) {
83                 return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
84             }
85         }
86     }
87
88     @GetMapping("/policy_schema")
89     @ApiOperation(value = "Returns one policy type schema definition")
90     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Type found")})
91     public ResponseEntity<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
92         try {
93             PolicyType type = policyTypes.getType(id);
94             return new ResponseEntity<String>(type.schema(), HttpStatus.OK);
95         } catch (ServiceException e) {
96             return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
97         }
98     }
99
100     @GetMapping("/policy_types")
101     @ApiOperation(value = "Returns policy types")
102     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy Types found")})
103     public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
104         if (ricName == null) {
105             Collection<PolicyType> types = this.policyTypes.getAll();
106             return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
107         } else {
108             try {
109                 Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
110                 return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
111             } catch (ServiceException e) {
112                 return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
113             }
114         }
115     }
116
117     @GetMapping("/policy")
118     @ApiOperation(value = "Returns the policy")
119     @ApiResponses(
120         value = {@ApiResponse(code = 200, message = "Policy found"),
121             @ApiResponse(code = 204, message = "Policy is not found")})
122     public ResponseEntity<String> getPolicy( //
123         @RequestParam(name = "instance", required = true) String instance) {
124         try {
125             Policy p = policies.getPolicy(instance);
126             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
127         } catch (ServiceException e) {
128             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
129         }
130     }
131
132     @DeleteMapping("/policy")
133     @ApiOperation(value = "Deletes the policy")
134     @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted")})
135     public ResponseEntity<Void> deletePolicy( //
136         @RequestParam(name = "instance", required = true) String instance) {
137
138         policies.removeId(instance);
139         return new ResponseEntity<>(HttpStatus.NO_CONTENT);
140     }
141
142     @GetMapping("/policies")
143     @ApiOperation(value = "Returns the policies")
144     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policies found")})
145     public ResponseEntity<String> getPolicies( //
146         @RequestParam(name = "type", required = false) String type, //
147         @RequestParam(name = "ric", required = false) String ric, //
148         @RequestParam(name = "service", required = false) String service) //
149     {
150         Collection<Policy> result = null;
151
152         if (type != null) {
153             result = policies.getForType(type);
154             result = filter(result, null, ric, service);
155         } else if (service != null) {
156             result = policies.getForService(service);
157             result = filter(result, type, ric, null);
158         } else if (ric != null) {
159             result = policies.getForRic(ric);
160             result = filter(result, type, null, service);
161         } else {
162             result = policies.getAll();
163         }
164
165         return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
166     }
167
168     private boolean include(String filter, String value) {
169         return filter == null || value.equals(filter);
170     }
171
172     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
173         if (type == null && ric == null && service == null) {
174             return collection;
175         }
176         Vector<Policy> filtered = new Vector<>();
177         for (Policy p : collection) {
178             if (include(type, p.type().name()) && include(ric, p.ric().name())
179                 && include(service, p.ownerServiceName())) {
180                 filtered.add(p);
181             }
182         }
183         return filtered;
184     }
185
186     private String policiesToJson(Collection<Policy> policies) {
187         Vector<PolicyInfo> v = new Vector<>(policies.size());
188         for (Policy p : policies) {
189             PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
190                 .json(p.json()) //
191                 .id(p.id()) //
192                 .ric(p.ric().name()) //
193                 .type(p.type().name()) //
194                 .service(p.ownerServiceName()) //
195                 .lastModified(p.lastModified()) //
196                 .build();
197             v.add(policyInfo);
198         }
199         return gson.toJson(v);
200     }
201
202     private String toPolicyTypeSchemasJson(Collection<PolicyType> types) {
203         StringBuilder result = new StringBuilder();
204         result.append("[");
205         boolean first = true;
206         for (PolicyType t : types) {
207             if (!first) {
208                 result.append(",");
209             }
210             first = false;
211             result.append(t.schema());
212         }
213         result.append("]");
214         return result.toString();
215     }
216
217     private String toPolicyTypeIdsJson(Collection<PolicyType> types) {
218         Vector<String> v = new Vector<>(types.size());
219         for (PolicyType t : types) {
220             v.add(t.name());
221         }
222         return gson.toJson(v);
223     }
224
225     private String getTimeStampUTC() {
226         return java.time.Instant.now().toString();
227     }
228
229     @PutMapping(path = "/policy")
230     @ApiOperation(value = "Create the policy")
231     @ApiResponses(value = {@ApiResponse(code = 201, message = "Policy created")})
232     public ResponseEntity<String> putPolicy( //
233         @RequestParam(name = "type", required = true) String type, //
234         @RequestParam(name = "instance", required = true) String instanceId, //
235         @RequestParam(name = "ric", required = true) String ric, //
236         @RequestParam(name = "service", required = true) String service, //
237         @RequestBody String jsonBody) {
238
239         try {
240             // services.getService(service).ping();
241             Ric ricObj = rics.getRic(ric);
242             Policy policy = ImmutablePolicy.builder() //
243                 .id(instanceId) //
244                 .json(jsonBody) //
245                 .type(policyTypes.getType(type)) //
246                 .ric(ricObj) //
247                 .ownerServiceName(service) //
248                 .lastModified(getTimeStampUTC()) //
249                 .build();
250             policies.put(policy);
251             return new ResponseEntity<String>(HttpStatus.CREATED);
252         } catch (ServiceException e) {
253             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
254         }
255     }
256
257 }