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;
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;
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;
53 @Api(value = "Policy Management API")
54 public class PolicyController {
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() //
66 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
67 this.appConfig = config;
68 this.policyTypes = types;
69 this.policies = policies;
71 this.services = services;
74 @GetMapping("/policy_types")
75 @ApiOperation(value = "Returns all the policy types")
78 @ApiResponse(code = 200, message = "Policy Types found")
80 public ResponseEntity<String> getPolicyTypes() {
82 Collection<PolicyType> types = this.policyTypes.getAll();
83 return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
86 @GetMapping("/policy")
87 @ApiOperation(value = "Returns the policy")
90 @ApiResponse(code = 200, message = "Policy found"),
91 @ApiResponse(code = 204, message = "Policy is not found")
93 public ResponseEntity<String> getPolicy( //
94 @RequestParam(name = "instance", required = true) String instance) {
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);
103 @DeleteMapping("/policy")
104 @ApiOperation(value = "Deletes the policy")
107 @ApiResponse(code = 204, message = "Policy deleted")
109 public ResponseEntity<Void> deletePolicy( //
110 @RequestParam(name = "instance", required = true) String instance) {
112 Policy p = policies.removeId(instance);
113 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
116 @GetMapping("/policies")
117 @ApiOperation(value = "Returns the policies")
120 @ApiResponse(code = 200, message = "Polcies found")
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) //
127 Collection<Policy> result = 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);
139 result = policies.getAll();
142 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
145 private boolean include(String filter, String value) {
146 return filter == null || value.equals(filter);
149 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
150 if (type == null && ric == null && service == null) {
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())) {
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() //
169 .ric(p.ric().name()) //
170 .type(p.type().name()) //
171 .service(p.ownerServiceName()) //
172 .lastModified(p.lastModified()) //
176 return gson.toJson(v);
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() //
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);