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