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 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.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.oransc.policyagent.repository.Services;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.DeleteMapping;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
50 public class PolicyController {
52 private final ApplicationConfig appConfig;
53 private final Rics rics;
54 private final PolicyTypes policyTypes;
55 private final Policies policies;
56 private final Services services;
57 private static Gson gson = new GsonBuilder() //
62 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
63 this.appConfig = config;
64 this.policyTypes = types;
65 this.policies = policies;
67 this.services = services;
70 @GetMapping("/policy_types")
71 public ResponseEntity<String> getPolicyTypes() {
73 Collection<PolicyType> types = this.policyTypes.getAll();
74 return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
77 @GetMapping("/policy")
78 public ResponseEntity<String> getPolicy( //
79 @RequestParam(name = "instance", required = true) String instance) {
81 Policy p = policies.get(instance);
82 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
83 } catch (ServiceException e) {
84 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
88 @DeleteMapping("/policy")
89 public ResponseEntity<String> deletePolicy( //
90 @RequestParam(name = "instance", required = true) String instance) {
92 Policy p = policies.removeId(instance);
93 return new ResponseEntity<String>("OK", HttpStatus.OK);
96 @GetMapping("/policies")
97 public ResponseEntity<String> getPolicies( //
98 @RequestParam(name = "type", required = false) String type, //
99 @RequestParam(name = "ric", required = false) String ric, //
100 @RequestParam(name = "service", required = false) String service) //
102 Collection<Policy> result = null;
105 result = policies.getForType(type);
106 result = filter(result, null, ric, service);
107 } else if (service != null) {
108 result = policies.getForService(service);
109 result = filter(result, type, ric, null);
110 } else if (ric != null) {
111 result = policies.getForRic(ric);
112 result = filter(result, type, null, service);
114 result = policies.getAll();
117 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
120 private boolean include(String filter, String value) {
121 return filter == null || value.equals(filter);
124 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
125 if (type == null && ric == null && service == null) {
128 Vector<Policy> filtered = new Vector<>();
129 for (Policy p : collection) {
130 if (include(type, p.type().name()) && include(ric, p.ric().name())
131 && include(service, p.ownerServiceName())) {
138 private String policiesToJson(Collection<Policy> policies) {
139 Vector<PolicyInfo> v = new Vector<>(policies.size());
140 for (Policy p : policies) {
141 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
144 .ric(p.ric().name()) //
145 .type(p.type().name()) //
146 .service(p.ownerServiceName()) //
147 .lastModified(p.lastModified()) //
151 return gson.toJson(v);
154 private String policyTypesToJson(Collection<PolicyType> types) {
155 Vector<PolicyTypeInfo> v = new Vector<>(types.size());
156 for (PolicyType t : types) {
157 PolicyTypeInfo policyInfo = ImmutablePolicyTypeInfo.builder() //
162 return gson.toJson(v);
165 private String getTimeStampUTC() {
166 return java.time.Instant.now().toString();
169 @PutMapping(path = "/policy")
170 public ResponseEntity<String> putPolicy( //
171 @RequestParam(name = "type", required = true) String type, //
172 @RequestParam(name = "instance", required = true) String instanceId, //
173 @RequestParam(name = "ric", required = true) String ric, //
174 @RequestParam(name = "service", required = true) String service, //
175 @RequestBody String jsonBody) {
178 // services.getService(service).ping();
179 Ric ricObj = rics.getRic(ric);
180 Policy policy = ImmutablePolicy.builder() //
183 .type(policyTypes.getType(type)) //
185 .ownerServiceName(service) //
186 .lastModified(getTimeStampUTC()) //
188 policies.put(policy);
189 return new ResponseEntity<String>(HttpStatus.OK);
190 } catch (ServiceException e) {
191 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);