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 java.util.Collection;
25 import java.util.Vector;
26 import org.oransc.policyagent.Beans;
27 import org.oransc.policyagent.configuration.ApplicationConfig;
28 import org.oransc.policyagent.exceptions.ServiceException;
29 import org.oransc.policyagent.repository.ImmutablePolicy;
30 import org.oransc.policyagent.repository.Policies;
31 import org.oransc.policyagent.repository.Policy;
32 import org.oransc.policyagent.repository.PolicyTypes;
33 import org.oransc.policyagent.repository.Ric;
34 import org.oransc.policyagent.repository.Rics;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.http.HttpStatus;
37 import org.springframework.http.ResponseEntity;
38 import org.springframework.web.bind.annotation.GetMapping;
39 import org.springframework.web.bind.annotation.PutMapping;
40 import org.springframework.web.bind.annotation.RequestBody;
41 import org.springframework.web.bind.annotation.RequestParam;
42 import org.springframework.web.bind.annotation.RestController;
45 public class PolicyController {
47 private final ApplicationConfig appConfig;
48 private final Rics rics;
49 private final PolicyTypes types;
50 private final Policies policies;
51 private static Gson gson = new GsonBuilder() //
56 PolicyController(Beans beans) {
57 this.appConfig = beans.getApplicationConfig();
58 this.rics = beans.getRics();
59 this.types = beans.getPolicyTypes();
60 this.policies = beans.getPolicies();
63 @GetMapping("/policy")
64 public ResponseEntity<String> getPolicy( //
65 @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
67 Policy p = policies.get(instance);
68 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
70 } catch (ServiceException e) {
71 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
75 @GetMapping("/policies")
76 public ResponseEntity<String> getPolicies( //
77 @RequestParam(name = "type", required = false) String type, //
78 @RequestParam(name = "ric", required = false) String ric, //
79 @RequestParam(name = "service", required = false) String service) //
81 Collection<Policy> result = null;
84 result = policies.getForType(type);
85 result = filter(result, null, ric, service);
86 } else if (service != null) {
87 result = policies.getForService(service);
88 result = filter(result, type, ric, null);
89 } else if (ric != null) {
90 result = policies.getForRic(ric);
91 result = filter(result, type, null, service);
93 result = policies.getAll();
96 return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
99 private boolean include(String filter, String value) {
100 return filter == null || value.equals(filter);
103 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
104 if (type == null && ric == null && service == null) {
107 Vector<Policy> filtered = new Vector<>();
108 for (Policy p : collection) {
109 if (include(type, p.type().name()) && include(ric, p.ric().name())
110 && include(service, p.ownerServiceName())) {
117 private String toJson(Collection<Policy> policies) {
118 Vector<PolicyInfo> v = new Vector<>(policies.size());
119 for (Policy p : policies) {
120 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
123 .ric(p.ric().name()) //
124 .type(p.type().name()).build();
127 return gson.toJson(v);
130 @PutMapping(path = "/policy")
131 public ResponseEntity<String> putPolicy( //
132 @RequestParam(name = "type", required = true) String type, //
133 @RequestParam(name = "instance", required = true) String instanceId, //
134 @RequestParam(name = "ric", required = true) String ric, //
135 @RequestParam(name = "service", required = true) String service, //
136 @RequestBody String jsonBody) {
139 Ric ricObj = rics.getRic(ric);
140 Policy policy = ImmutablePolicy.builder() //
143 .type(types.getType(type)) //
145 .ownerServiceName(service) //
147 policies.put(policy);
148 return new ResponseEntity<String>(HttpStatus.OK);
149 } catch (ServiceException e) {
150 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);