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.PolicyType;
34 import org.oransc.policyagent.repository.PolicyTypes;
35 import org.oransc.policyagent.repository.Ric;
36 import org.oransc.policyagent.repository.Rics;
37 import org.oransc.policyagent.repository.Services;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.ResponseEntity;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PutMapping;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
49 public class PolicyController {
51 private final ApplicationConfig appConfig;
52 private final Rics rics;
53 private final PolicyTypes policyTypes;
54 private final Policies policies;
55 private final Services services;
56 private static Gson gson = new GsonBuilder() //
61 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
62 this.appConfig = config;
63 this.policyTypes = types;
64 this.policies = policies;
66 this.services = services;
69 @GetMapping("/policy_types")
70 public ResponseEntity<String> getPolicyTypes() {
72 Collection<PolicyType> types = this.policyTypes.getAll();
73 return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
76 @GetMapping("/policy")
77 public ResponseEntity<String> getPolicy( //
78 @RequestParam(name = "instance", required = true) String instance) {
80 Policy p = policies.get(instance);
81 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
82 } catch (ServiceException e) {
83 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
87 @DeleteMapping("/policy")
88 public ResponseEntity<String> deletePolicy( //
89 @RequestParam(name = "instance", required = true) String instance) {
91 Policy p = policies.removeId(instance);
92 return new ResponseEntity<String>("OK", HttpStatus.OK);
95 @GetMapping("/policies")
96 public ResponseEntity<String> getPolicies( //
97 @RequestParam(name = "type", required = false) String type, //
98 @RequestParam(name = "ric", required = false) String ric, //
99 @RequestParam(name = "service", required = false) String service) //
101 Collection<Policy> result = null;
104 result = policies.getForType(type);
105 result = filter(result, null, ric, service);
106 } else if (service != null) {
107 result = policies.getForService(service);
108 result = filter(result, type, ric, null);
109 } else if (ric != null) {
110 result = policies.getForRic(ric);
111 result = filter(result, type, null, service);
113 result = policies.getAll();
116 return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
119 private boolean include(String filter, String value) {
120 return filter == null || value.equals(filter);
123 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
124 if (type == null && ric == null && service == null) {
127 Vector<Policy> filtered = new Vector<>();
128 for (Policy p : collection) {
129 if (include(type, p.type().name()) && include(ric, p.ric().name())
130 && include(service, p.ownerServiceName())) {
137 private String policiesToJson(Collection<Policy> policies) {
138 Vector<PolicyInfo> v = new Vector<>(policies.size());
139 for (Policy p : policies) {
140 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
143 .ric(p.ric().name()) //
144 .type(p.type().name()) //
145 .service(p.ownerServiceName()) //
146 .lastModified(p.lastModified()) //
150 return gson.toJson(v);
153 private String policyTypesToJson(Collection<PolicyType> types) {
154 Vector<PolicyTypeInfo> v = new Vector<>(types.size());
155 for (PolicyType t : types) {
156 PolicyTypeInfo policyInfo = ImmutablePolicyTypeInfo.builder() //
157 .schema(t.jsonSchema()) //
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);