2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
31 import java.util.Collection;
32 import java.util.Vector;
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;
54 @Api(value = "Policy Management API")
55 public class PolicyController {
57 private final Rics rics;
58 private final PolicyTypes policyTypes;
59 private final Policies policies;
60 private static Gson gson = new GsonBuilder() //
65 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics) {
66 this.policyTypes = types;
67 this.policies = policies;
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);
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);
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) {
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);
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);
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);
117 @GetMapping("/policy")
118 @ApiOperation(value = "Returns the policy")
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) {
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);
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) {
138 policies.removeId(instance);
139 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
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) //
150 Collection<Policy> result = 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);
162 result = policies.getAll();
165 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
168 private boolean include(String filter, String value) {
169 return filter == null || value.equals(filter);
172 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
173 if (type == null && ric == null && service == null) {
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())) {
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() //
192 .ric(p.ric().name()) //
193 .type(p.type().name()) //
194 .service(p.ownerServiceName()) //
195 .lastModified(p.lastModified()) //
199 return gson.toJson(v);
202 private String toPolicyTypeSchemasJson(Collection<PolicyType> types) {
203 StringBuilder result = new StringBuilder();
205 boolean first = true;
206 for (PolicyType t : types) {
211 result.append(t.schema());
214 return result.toString();
217 private String toPolicyTypeIdsJson(Collection<PolicyType> types) {
218 Vector<String> v = new Vector<>(types.size());
219 for (PolicyType t : types) {
222 return gson.toJson(v);
225 private String getTimeStampUTC() {
226 return java.time.Instant.now().toString();
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) {
240 // services.getService(service).ping();
241 Ric ricObj = rics.getRic(ric);
242 Policy policy = ImmutablePolicy.builder() //
245 .type(policyTypes.getType(type)) //
247 .ownerServiceName(service) //
248 .lastModified(getTimeStampUTC()) //
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);