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