NBI documentation using swagger UI
[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.clients.A1ClientFactory;
35 import org.oransc.policyagent.configuration.ApplicationConfig;
36 import org.oransc.policyagent.exceptions.ServiceException;
37 import org.oransc.policyagent.repository.ImmutablePolicy;
38 import org.oransc.policyagent.repository.Policies;
39 import org.oransc.policyagent.repository.Policy;
40 import org.oransc.policyagent.repository.PolicyType;
41 import org.oransc.policyagent.repository.PolicyTypes;
42 import org.oransc.policyagent.repository.Ric;
43 import org.oransc.policyagent.repository.Rics;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.DeleteMapping;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestParam;
52 import org.springframework.web.bind.annotation.RestController;
53 import reactor.core.publisher.Mono;
54
55 @RestController
56 @Api(value = "Policy Management API")
57 public class PolicyController {
58
59     private final Rics rics;
60     private final PolicyTypes policyTypes;
61     private final Policies policies;
62     private final A1ClientFactory a1ClientFactory;
63
64     private static Gson gson = new GsonBuilder() //
65         .serializeNulls() //
66         .create(); //
67
68     @Autowired
69     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics,
70         A1ClientFactory a1ClientFactory) {
71         this.policyTypes = types;
72         this.policies = policies;
73         this.rics = rics;
74         this.a1ClientFactory = a1ClientFactory;
75     }
76
77     @GetMapping("/policy_schemas")
78     @ApiOperation(value = "Returns policy type schema definitions")
79     @ApiResponses(
80         value = {
81             @ApiResponse(code = 200, message = "Policy schemas", response = String.class, responseContainer = "List")})
82     public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
83         synchronized (this.policyTypes) {
84             if (ricName == null) {
85                 Collection<PolicyType> types = this.policyTypes.getAll();
86                 return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
87             } else {
88                 try {
89                     Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
90                     return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
91                 } catch (ServiceException e) {
92                     return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
93                 }
94             }
95         }
96     }
97
98     @GetMapping("/policy_schema")
99     @ApiOperation(value = "Returns one policy type schema definition")
100     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy schema", response = Object.class)})
101     public ResponseEntity<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
102         try {
103             PolicyType type = policyTypes.getType(id);
104             return new ResponseEntity<String>(type.schema(), HttpStatus.OK);
105         } catch (ServiceException e) {
106             return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
107         }
108     }
109
110     @GetMapping("/policy_types")
111     @ApiOperation(value = "Returns policy types")
112     @ApiResponses(
113         value = {@ApiResponse(
114             code = 200,
115             message = "Policy type names",
116             response = String.class,
117             responseContainer = "List")})
118     public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
119         synchronized (this.policyTypes) {
120             if (ricName == null) {
121                 Collection<PolicyType> types = this.policyTypes.getAll();
122                 return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
123             } else {
124                 try {
125                     Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
126                     return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
127                 } catch (ServiceException e) {
128                     return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
129                 }
130             }
131         }
132     }
133
134     @GetMapping("/policy")
135     @ApiOperation(value = "Returns a policy configuration") //
136     @ApiResponses(
137         value = { //
138             @ApiResponse(code = 200, message = "Policy found", response = Object.class), //
139             @ApiResponse(code = 204, message = "Policy is not found")} //
140     )
141
142     public ResponseEntity<String> getPolicy( //
143         @RequestParam(name = "instance", required = true) String instance) {
144         try {
145             Policy p = policies.getPolicy(instance);
146             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
147         } catch (ServiceException e) {
148             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
149         }
150     }
151
152     @DeleteMapping("/policy")
153     @ApiOperation(value = "Deletes the policy", response = Object.class)
154     @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted", response = Object.class)})
155     public Mono<ResponseEntity<Void>> deletePolicy( //
156         @RequestParam(name = "instance", required = true) String id) {
157         Policy policy = policies.get(id);
158         if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) {
159             policies.remove(policy);
160             return a1ClientFactory.createA1Client(policy.ric()) //
161                 .flatMap(client -> client.deletePolicy(policy)) //
162                 .flatMap(notUsed -> {
163                     return Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT));
164                 });
165         } else {
166             return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
167         }
168     }
169
170     @PutMapping(path = "/policy")
171     @ApiOperation(value = "Put a policy", response = String.class)
172     @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy created or updated")})
173     public Mono<ResponseEntity<String>> putPolicy( //
174         @RequestParam(name = "type", required = true) String typeName, //
175         @RequestParam(name = "instance", required = true) String instanceId, //
176         @RequestParam(name = "ric", required = true) String ricName, //
177         @RequestParam(name = "service", required = true) String service, //
178         @RequestBody Object jsonBody) {
179
180         String jsonString = gson.toJson(jsonBody);
181
182         Ric ric = rics.get(ricName);
183         PolicyType type = policyTypes.get(typeName);
184         if (ric != null && type != null && ric.getState() == Ric.RicState.IDLE) {
185             Policy policy = ImmutablePolicy.builder() //
186                 .id(instanceId) //
187                 .json(jsonString) //
188                 .type(type) //
189                 .ric(ric) //
190                 .ownerServiceName(service) //
191                 .lastModified(getTimeStampUTC()) //
192                 .build();
193             return a1ClientFactory.createA1Client(ric) //
194                 .flatMap(client -> client.putPolicy(policy)) //
195                 .doOnNext(notUsed -> policies.put(policy)) //
196                 .flatMap(notUsed -> {
197                     return Mono.just(new ResponseEntity<>(HttpStatus.OK));
198                 });
199         }
200         return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
201     }
202
203     @GetMapping("/policies")
204     @ApiOperation(value = "Returns the policies")
205     @ApiResponses(
206         value = {
207             @ApiResponse(code = 200, message = "Policies", response = PolicyInfo.class, responseContainer = "List")})
208     public ResponseEntity<String> getPolicies( //
209         @RequestParam(name = "type", required = false) String type, //
210         @RequestParam(name = "ric", required = false) String ric, //
211         @RequestParam(name = "service", required = false) String service) //
212     {
213         synchronized (policies) {
214             Collection<Policy> result = null;
215
216             if (type != null) {
217                 result = policies.getForType(type);
218                 result = filter(result, null, ric, service);
219             } else if (service != null) {
220                 result = policies.getForService(service);
221                 result = filter(result, type, ric, null);
222             } else if (ric != null) {
223                 result = policies.getForRic(ric);
224                 result = filter(result, type, null, service);
225             } else {
226                 result = policies.getAll();
227             }
228
229             return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
230         }
231     }
232
233     private boolean include(String filter, String value) {
234         return filter == null || value.equals(filter);
235     }
236
237     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
238         if (type == null && ric == null && service == null) {
239             return collection;
240         }
241         Vector<Policy> filtered = new Vector<>();
242         for (Policy p : collection) {
243             if (include(type, p.type().name()) && include(ric, p.ric().name())
244                 && include(service, p.ownerServiceName())) {
245                 filtered.add(p);
246             }
247         }
248         return filtered;
249     }
250
251     private String policiesToJson(Collection<Policy> policies) {
252         Vector<PolicyInfo> v = new Vector<>(policies.size());
253         for (Policy p : policies) {
254             PolicyInfo policyInfo = new PolicyInfo();
255             policyInfo.id = p.id();
256             policyInfo.json = p.json();
257             policyInfo.ric = p.ric().name();
258             policyInfo.type = p.type().name();
259             policyInfo.service = p.ownerServiceName();
260             policyInfo.lastModified = p.lastModified();
261             if (!policyInfo.validate()) {
262                 throw new RuntimeException("BUG, all fields must be set");
263             }
264             v.add(policyInfo);
265         }
266         return gson.toJson(v);
267     }
268
269     private String toPolicyTypeSchemasJson(Collection<PolicyType> types) {
270         StringBuilder result = new StringBuilder();
271         result.append("[");
272         boolean first = true;
273         for (PolicyType t : types) {
274             if (!first) {
275                 result.append(",");
276             }
277             first = false;
278             result.append(t.schema());
279         }
280         result.append("]");
281         return result.toString();
282     }
283
284     private String toPolicyTypeIdsJson(Collection<PolicyType> types) {
285         Vector<String> v = new Vector<>(types.size());
286         for (PolicyType t : types) {
287             v.add(t.name());
288         }
289         return gson.toJson(v);
290     }
291
292     private String getTimeStampUTC() {
293         return java.time.Instant.now().toString();
294     }
295
296 }