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 java.util.Collection;
24 import java.util.Vector;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
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;
48 import io.swagger.annotations.Api;
49 import io.swagger.annotations.ApiOperation;
50 import io.swagger.annotations.ApiResponse;
51 import io.swagger.annotations.ApiResponses;
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_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);
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);
88 @GetMapping("/policy")
89 @ApiOperation(value = "Returns the policy")
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) {
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);
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) {
109 policies.removeId(instance);
110 return new ResponseEntity<>(HttpStatus.NO_CONTENT);
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) //
121 Collection<Policy> result = 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);
133 result = policies.getAll();
136 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
139 private boolean include(String filter, String value) {
140 return filter == null || value.equals(filter);
143 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
144 if (type == null && ric == null && service == null) {
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())) {
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() //
163 .ric(p.ric().name()) //
164 .type(p.type().name()) //
165 .service(p.ownerServiceName()) //
166 .lastModified(p.lastModified()) //
170 return gson.toJson(v);
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() //
181 return gson.toJson(v);
184 private String getTimeStampUTC() {
185 return java.time.Instant.now().toString();
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) {
199 // services.getService(service).ping();
200 Ric ricObj = rics.getRic(ric);
201 Policy policy = ImmutablePolicy.builder() //
204 .type(policyTypes.getType(type)) //
206 .ownerServiceName(service) //
207 .lastModified(getTimeStampUTC()) //
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);