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===================================
20 package org.oransc.policyagent.controllers;
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;
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;
52 @Api(value = "Policy Management API")
53 public class PolicyController {
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() //
65 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
66 this.appConfig = config;
67 this.policyTypes = types;
68 this.policies = policies;
70 this.services = services;
73 @GetMapping("/policy_types")
74 @ApiOperation(value = "Returns all the policy types")
77 @ApiResponse(code = 200, message = "Policy Types found")
79 public ResponseEntity<String> getPolicyTypes() {
81 Collection<PolicyType> types = this.policyTypes.getAll();
82 return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
85 @GetMapping("/policy")
86 @ApiOperation(value = "Returns the policy")
89 @ApiResponse(code = 200, message = "Policy found"),
90 @ApiResponse(code = 204, message = "Policy is not found")
92 public ResponseEntity<String> getPolicy( //
93 @RequestParam(name = "instance", required = true) String instance) {
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);
102 @DeleteMapping("/policy")
103 @ApiOperation(value = "Deletes the policy")
106 @ApiResponse(code = 204, message = "Policy deleted")
108 public ResponseEntity<Void> deletePolicy( //
109 @RequestParam(name = "instance", required = true) String instance) {
111 Policy p = policies.removeId(instance);
112 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
115 @GetMapping("/policies")
116 @ApiOperation(value = "Returns the policies")
119 @ApiResponse(code = 200, message = "Polcies found")
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) //
126 Collection<Policy> result = 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);
138 result = policies.getAll();
141 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
144 private boolean include(String filter, String value) {
145 return filter == null || value.equals(filter);
148 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
149 if (type == null && ric == null && service == null) {
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())) {
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() //
168 .ric(p.ric().name()) //
169 .type(p.type().name()) //
170 .service(p.ownerServiceName()) //
171 .lastModified(p.lastModified()) //
175 return gson.toJson(v);
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()) //
187 return gson.toJson(v);
190 private String getTimeStampUTC() {
191 return java.time.Instant.now().toString();
194 @PutMapping(path = "/policy")
195 @ApiOperation(value = "Create the policy")
198 @ApiResponse(code = 201, message = "Policy created")
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) {
208 // services.getService(service).ping();
209 Ric ricObj = rics.getRic(ric);
210 Policy policy = ImmutablePolicy.builder() //
213 .type(policyTypes.getType(type)) //
215 .ownerServiceName(service) //
216 .lastModified(getTimeStampUTC()) //
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);