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.clients.A1ClientFactory;
35 import org.oransc.policyagent.configuration.ApplicationConfig;
36 import org.oransc.policyagent.exceptions.ServiceException;
37 import org.oransc.policyagent.repository.ImmutablePolicy;
38 import org.oransc.policyagent.repository.Policies;
39 import org.oransc.policyagent.repository.Policy;
40 import org.oransc.policyagent.repository.PolicyType;
41 import org.oransc.policyagent.repository.PolicyTypes;
42 import org.oransc.policyagent.repository.Ric;
43 import org.oransc.policyagent.repository.Rics;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.DeleteMapping;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestParam;
52 import org.springframework.web.bind.annotation.RestController;
53 import reactor.core.publisher.Mono;
56 @Api(value = "Policy Management API")
57 public class PolicyController {
59 private final Rics rics;
60 private final PolicyTypes policyTypes;
61 private final Policies policies;
62 private final A1ClientFactory a1ClientFactory;
64 private static Gson gson = new GsonBuilder() //
69 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics,
70 A1ClientFactory a1ClientFactory) {
71 this.policyTypes = types;
72 this.policies = policies;
74 this.a1ClientFactory = a1ClientFactory;
77 @GetMapping("/policy_schemas")
78 @ApiOperation(value = "Returns policy type schema definitions")
81 @ApiResponse(code = 200, message = "Policy schemas", response = String.class, responseContainer = "List")})
82 public ResponseEntity<String> getPolicySchemas(@RequestParam(name = "ric", required = false) String ricName) {
83 synchronized (this.policyTypes) {
84 if (ricName == null) {
85 Collection<PolicyType> types = this.policyTypes.getAll();
86 return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
89 Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
90 return new ResponseEntity<String>(toPolicyTypeSchemasJson(types), HttpStatus.OK);
91 } catch (ServiceException e) {
92 return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
98 @GetMapping("/policy_schema")
99 @ApiOperation(value = "Returns one policy type schema definition")
100 @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy schema", response = Object.class)})
101 public ResponseEntity<String> getPolicySchema(@RequestParam(name = "id", required = true) String id) {
103 PolicyType type = policyTypes.getType(id);
104 return new ResponseEntity<String>(type.schema(), HttpStatus.OK);
105 } catch (ServiceException e) {
106 return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
110 @GetMapping("/policy_types")
111 @ApiOperation(value = "Returns policy types")
113 value = {@ApiResponse(
115 message = "Policy type names",
116 response = String.class,
117 responseContainer = "List")})
118 public ResponseEntity<String> getPolicyTypes(@RequestParam(name = "ric", required = false) String ricName) {
119 synchronized (this.policyTypes) {
120 if (ricName == null) {
121 Collection<PolicyType> types = this.policyTypes.getAll();
122 return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
125 Collection<PolicyType> types = rics.getRic(ricName).getSupportedPolicyTypes();
126 return new ResponseEntity<String>(toPolicyTypeIdsJson(types), HttpStatus.OK);
127 } catch (ServiceException e) {
128 return new ResponseEntity<String>(e.toString(), HttpStatus.NOT_FOUND);
134 @GetMapping("/policy")
135 @ApiOperation(value = "Returns a policy configuration") //
138 @ApiResponse(code = 200, message = "Policy found", response = Object.class), //
139 @ApiResponse(code = 204, message = "Policy is not found")} //
142 public ResponseEntity<String> getPolicy( //
143 @RequestParam(name = "instance", required = true) String instance) {
145 Policy p = policies.getPolicy(instance);
146 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
147 } catch (ServiceException e) {
148 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
152 @DeleteMapping("/policy")
153 @ApiOperation(value = "Deletes the policy", response = Object.class)
154 @ApiResponses(value = {@ApiResponse(code = 204, message = "Policy deleted", response = Object.class)})
155 public Mono<ResponseEntity<Void>> deletePolicy( //
156 @RequestParam(name = "instance", required = true) String id) {
157 Policy policy = policies.get(id);
158 if (policy != null && policy.ric().getState() == Ric.RicState.IDLE) {
159 policies.remove(policy);
160 return a1ClientFactory.createA1Client(policy.ric()) //
161 .flatMap(client -> client.deletePolicy(policy)) //
162 .flatMap(notUsed -> {
163 return Mono.just(new ResponseEntity<>(HttpStatus.NO_CONTENT));
166 return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
170 @PutMapping(path = "/policy")
171 @ApiOperation(value = "Put a policy", response = String.class)
172 @ApiResponses(value = {@ApiResponse(code = 200, message = "Policy created or updated")})
173 public Mono<ResponseEntity<String>> putPolicy( //
174 @RequestParam(name = "type", required = true) String typeName, //
175 @RequestParam(name = "instance", required = true) String instanceId, //
176 @RequestParam(name = "ric", required = true) String ricName, //
177 @RequestParam(name = "service", required = true) String service, //
178 @RequestBody Object jsonBody) {
180 String jsonString = gson.toJson(jsonBody);
182 Ric ric = rics.get(ricName);
183 PolicyType type = policyTypes.get(typeName);
184 if (ric != null && type != null && ric.getState() == Ric.RicState.IDLE) {
185 Policy policy = ImmutablePolicy.builder() //
190 .ownerServiceName(service) //
191 .lastModified(getTimeStampUTC()) //
193 return a1ClientFactory.createA1Client(ric) //
194 .flatMap(client -> client.putPolicy(policy)) //
195 .doOnNext(notUsed -> policies.put(policy)) //
196 .flatMap(notUsed -> {
197 return Mono.just(new ResponseEntity<>(HttpStatus.OK));
200 return Mono.just(new ResponseEntity<>(HttpStatus.NOT_FOUND));
203 @GetMapping("/policies")
204 @ApiOperation(value = "Returns the policies")
207 @ApiResponse(code = 200, message = "Policies", response = PolicyInfo.class, responseContainer = "List")})
208 public ResponseEntity<String> getPolicies( //
209 @RequestParam(name = "type", required = false) String type, //
210 @RequestParam(name = "ric", required = false) String ric, //
211 @RequestParam(name = "service", required = false) String service) //
213 synchronized (policies) {
214 Collection<Policy> result = null;
217 result = policies.getForType(type);
218 result = filter(result, null, ric, service);
219 } else if (service != null) {
220 result = policies.getForService(service);
221 result = filter(result, type, ric, null);
222 } else if (ric != null) {
223 result = policies.getForRic(ric);
224 result = filter(result, type, null, service);
226 result = policies.getAll();
229 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
233 private boolean include(String filter, String value) {
234 return filter == null || value.equals(filter);
237 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
238 if (type == null && ric == null && service == null) {
241 Vector<Policy> filtered = new Vector<>();
242 for (Policy p : collection) {
243 if (include(type, p.type().name()) && include(ric, p.ric().name())
244 && include(service, p.ownerServiceName())) {
251 private String policiesToJson(Collection<Policy> policies) {
252 Vector<PolicyInfo> v = new Vector<>(policies.size());
253 for (Policy p : policies) {
254 PolicyInfo policyInfo = new PolicyInfo();
255 policyInfo.id = p.id();
256 policyInfo.json = p.json();
257 policyInfo.ric = p.ric().name();
258 policyInfo.type = p.type().name();
259 policyInfo.service = p.ownerServiceName();
260 policyInfo.lastModified = p.lastModified();
261 if (!policyInfo.validate()) {
262 throw new RuntimeException("BUG, all fields must be set");
266 return gson.toJson(v);
269 private String toPolicyTypeSchemasJson(Collection<PolicyType> types) {
270 StringBuilder result = new StringBuilder();
272 boolean first = true;
273 for (PolicyType t : types) {
278 result.append(t.schema());
281 return result.toString();
284 private String toPolicyTypeIdsJson(Collection<PolicyType> types) {
285 Vector<String> v = new Vector<>(types.size());
286 for (PolicyType t : types) {
289 return gson.toJson(v);
292 private String getTimeStampUTC() {
293 return java.time.Instant.now().toString();