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 org.oransc.policyagent.Beans;
24 import com.google.gson.GsonBuilder;
26 import java.util.Collection;
27 import java.util.Vector;
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.PolicyTypes;
35 import org.oransc.policyagent.repository.Ric;
36 import org.oransc.policyagent.repository.Rics;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.web.bind.annotation.GetMapping;
41 import org.springframework.web.bind.annotation.PutMapping;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestParam;
44 import org.springframework.web.bind.annotation.RestController;
47 public class PolicyController {
49 private final Beans beans;
50 private final ApplicationConfig appConfig;
51 private final Rics rics;
52 private final PolicyTypes types;
53 private final Policies policies;
54 private static Gson gson = new GsonBuilder() //
59 PolicyController(Beans beans) {
61 this.appConfig = beans.getApplicationConfig();
62 this.rics = beans.getRics();
63 this.types = beans.getPolicyTypes();
64 this.policies = beans.getPolicies();
67 @GetMapping("/policy")
68 public ResponseEntity<String> getPolicy( //
69 @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
71 Policy p = policies.get(instance);
72 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
74 } catch (ServiceException e) {
75 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
79 @GetMapping("/policies")
80 public ResponseEntity<String> getPolicies( //
81 @RequestParam(name = "type", required = false) String type, //
82 @RequestParam(name = "ric", required = false) String ric, //
83 @RequestParam(name = "service", required = false) String service) //
85 Collection<Policy> result = null;
88 result = policies.getForType(type);
89 result = filter(result, null, ric, service);
90 } else if (service != null) {
91 result = policies.getForService(service);
92 result = filter(result, type, ric, null);
93 } else if (ric != null) {
94 result = policies.getForRic(ric);
95 result = filter(result, type, null, service);
97 result = policies.getAll();
100 return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
103 private boolean include(String filter, String value) {
104 return filter == null || value.equals(filter);
107 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
108 if (type == null && ric == null && service == null) {
111 Vector<Policy> filtered = new Vector<>();
112 for (Policy p : collection) {
113 if (include(type, p.type().name()) && include(ric, p.ric().name())
114 && include(service, p.ownerServiceName())) {
121 private String toJson(Collection<Policy> policies) {
122 Vector<PolicyInfo> v = new Vector<>(policies.size());
123 for (Policy p : policies) {
124 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
127 .ric(p.ric().name()) //
128 .type(p.type().name()).build();
131 return gson.toJson(v);
134 @PutMapping(path = "/policy")
135 public ResponseEntity<String> putPolicy( //
136 @RequestParam(name = "type", required = true) String type, //
137 @RequestParam(name = "instance", required = true) String instanceId, //
138 @RequestParam(name = "ric", required = true) String ric, //
139 @RequestParam(name = "service", required = true) String service, //
140 @RequestBody String jsonBody) {
143 Ric ricObj = rics.getRic(ric);
144 Policy policy = ImmutablePolicy.builder() //
147 .type(types.getType(type)) //
149 .ownerServiceName(service) //
151 policies.put(policy);
152 return new ResponseEntity<String>(HttpStatus.OK);
153 } catch (ServiceException e) {
154 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);