4b3c9cf7088bac864cef902d5b073c95a1d69b51
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / PolicyController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20 package org.oransc.policyagent.controllers;
21
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24
25 import java.util.Collection;
26 import java.util.Vector;
27
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;
42
43 @RestController
44 public class PolicyController {
45
46     private final ApplicationConfig appConfig;
47     private final PolicyTypes types;
48     private final Policies policies;
49     private static Gson gson = new GsonBuilder() //
50         .serializeNulls() //
51         .create(); //
52
53     @Autowired
54     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies) {
55         this.appConfig = config;
56         this.types = types;
57         this.policies = policies;
58     }
59
60     @GetMapping("/policy")
61     public ResponseEntity<String> getPolicy( //
62         @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
63         try {
64             Policy p = policies.get(instance);
65             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
66
67         } catch (ServiceException e) {
68             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
69         }
70     }
71
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) //
77     {
78         Collection<Policy> result = null;
79
80         if (type != 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);
89         } else {
90             result = policies.getAll();
91         }
92
93         return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
94     }
95
96     private boolean include(String filter, String value) {
97         return filter == null || value.equals(filter);
98     }
99
100     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
101         if (type == null && ric == null && service == null) {
102             return collection;
103         }
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())) {
108                 filtered.add(p);
109             }
110         }
111         return filtered;
112     }
113
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() //
118                 .json(p.json()) //
119                 .name(p.id()) //
120                 .ric(p.ric().name()) //
121                 .type(p.type().name()).build();
122             v.add(policyInfo);
123         }
124         return gson.toJson(v);
125     }
126
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) {
134
135         try {
136             Policy policy = ImmutablePolicy.builder() //
137                 .id(instanceId) //
138                 .json(jsonBody) //
139                 .type(types.getType(type)) //
140                 .ric(appConfig.getRic(ric)) //
141                 .ownerServiceName(service) //
142                 .build();
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);
147         }
148     }
149
150 }