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.oransc.policyagent.repository.Ric;
35 import org.oransc.policyagent.repository.Rics;
36 import org.oransc.policyagent.repository.Services;
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 ApplicationConfig appConfig;
50 private final Rics rics;
51 private final PolicyTypes types;
52 private final Policies policies;
53 private final Services services;
54 private static Gson gson = new GsonBuilder() //
59 PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
60 this.appConfig = config;
62 this.policies = policies;
64 this.services = services;
67 @GetMapping("/policy")
68 public ResponseEntity<String> getPolicy( //
69 @RequestParam(name = "instance", required = true) String instance) {
71 Policy p = policies.get(instance);
72 return new ResponseEntity<String>(p.json(), HttpStatus.OK);
73 } catch (ServiceException e) {
74 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
78 @GetMapping("/policies")
79 public ResponseEntity<String> getPolicies( //
80 @RequestParam(name = "type", required = false) String type, //
81 @RequestParam(name = "ric", required = false) String ric, //
82 @RequestParam(name = "service", required = false) String service) //
84 Collection<Policy> result = null;
87 result = policies.getForType(type);
88 result = filter(result, null, ric, service);
89 } else if (service != null) {
90 result = policies.getForService(service);
91 result = filter(result, type, ric, null);
92 } else if (ric != null) {
93 result = policies.getForRic(ric);
94 result = filter(result, type, null, service);
96 result = policies.getAll();
99 return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
102 private boolean include(String filter, String value) {
103 return filter == null || value.equals(filter);
106 private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
107 if (type == null && ric == null && service == null) {
110 Vector<Policy> filtered = new Vector<>();
111 for (Policy p : collection) {
112 if (include(type, p.type().name()) && include(ric, p.ric().name())
113 && include(service, p.ownerServiceName())) {
120 private String toJson(Collection<Policy> policies) {
121 Vector<PolicyInfo> v = new Vector<>(policies.size());
122 for (Policy p : policies) {
123 PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
126 .ric(p.ric().name()) //
127 .type(p.type().name()).build();
130 return gson.toJson(v);
133 @PutMapping(path = "/policy")
134 public ResponseEntity<String> putPolicy( //
135 @RequestParam(name = "type", required = true) String type, //
136 @RequestParam(name = "instance", required = true) String instanceId, //
137 @RequestParam(name = "ric", required = true) String ric, //
138 @RequestParam(name = "service", required = true) String service, //
139 @RequestBody String jsonBody) {
142 services.getService(service).ping();
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);