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;
25 import java.util.Collection;
26 import java.util.Vector;
28 import org.oransc.policyagent.configuration.ApplicationConfig;
29 import org.oransc.policyagent.exceptions.ServiceException;
30 import org.oransc.policyagent.repository.ImmutablePolicy;
31 import org.oransc.policyagent.repository.Policies;
32 import org.oransc.policyagent.repository.Policy;
33 import org.oransc.policyagent.repository.PolicyTypes;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PutMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestParam;
41 import org.springframework.web.bind.annotation.RestController;
44 public class PolicyController {
46 private final ApplicationConfig appConfig;
47 private final PolicyTypes types;
48 private final Policies policies;
49 private static Gson gson = new GsonBuilder() //
54 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
55 this.appConfig = config;
57 this.policies = policies;
60 @GetMapping("/policy")
61 public ResponseEntity<String> getPolicy( //
62 @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
64 Policy p = policies.get(instance);
65 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
67 } catch (ServiceException e) {
68 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
72 @GetMapping("/policies")
73 public ResponseEntity<String> getPolicies( //
74 @RequestParam(name = "type", required = false) String type, //
75 @RequestParam(name = "ric", required = false) String ric, //
76 @RequestParam(name = "service", required = false) String service) //
78 Collection<Policy> result = null;
81 result = policies.getForType(type);
82 result = filter(result, null, ric, service);
83 } else if (service != null) {
84 result = policies.getForService(service);
85 result = filter(result, type, ric, null);
86 } else if (ric != null) {
87 result = policies.getForRic(ric);
88 result = filter(result, type, null, service);
90 result = policies.getAll();
93 return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
96 private boolean include(String filter, String value) {
97 return filter == null || value.equals(filter);
100 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
101 if (type == null && ric == null && service == null) {
104 Vector<Policy> filtered = new Vector<>();
105 for (Policy p : collection) {
106 if (include(type, p.type().name()) && include(ric, p.ric().name())
107 && include(service, p.ownerServiceName())) {
114 private String toJson(Collection<Policy> policies) {
115 Vector<PolicyInfo> v = new Vector<>(policies.size());
116 for (Policy p : policies) {
117 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
120 .ric(p.ric().name()) //
121 .type(p.type().name()).build();
124 return gson.toJson(v);
127 @PutMapping(path = "/policy")
128 public ResponseEntity<String> putPolicy( //
129 @RequestParam(name = "type", required = true) String type, //
130 @RequestParam(name = "instance", required = true) String instanceId, //
131 @RequestParam(name = "ric", required = true) String ric, //
132 @RequestParam(name = "service", required = true) String service, //
133 @RequestBody String jsonBody) {
136 Policy policy = ImmutablePolicy.builder() //
139 .type(types.getType(type)) //
140 .ric(appConfig.getRic(ric)) //
141 .ownerServiceName(service) //
143 policies.put(policy);
144 return new ResponseEntity<String>(HttpStatus.OK);
145 } catch (ServiceException e) {
146 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);