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