Add first version of startup
[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 org.oransc.policyagent.Beans;
24 import com.google.gson.GsonBuilder;
25
26 import java.util.Collection;
27 import java.util.Vector;
28
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.PolicyTypes;
35 import org.oransc.policyagent.repository.Ric;
36 import org.oransc.policyagent.repository.Rics;
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;
45
46 @RestController
47 public class PolicyController {
48
49     private final Beans beans;
50     private final ApplicationConfig appConfig;
51     private final Rics rics;
52     private final PolicyTypes types;
53     private final Policies policies;
54     private static Gson gson = new GsonBuilder() //
55         .serializeNulls() //
56         .create(); //
57
58     @Autowired
59     PolicyController(Beans beans) {
60         this.beans = beans;
61         this.appConfig = beans.getApplicationConfig();
62         this.rics = beans.getRics();
63         this.types = beans.getPolicyTypes();
64         this.policies = beans.getPolicies();
65     }
66
67     @GetMapping("/policy")
68     public ResponseEntity<String> getPolicy( //
69         @RequestParam(name = "instance", required = false, defaultValue = "new") String instance) {
70         try {
71             Policy p = policies.get(instance);
72             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
73
74         } catch (ServiceException e) {
75             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
76         }
77     }
78
79     @GetMapping("/policies")
80     public ResponseEntity<String> getPolicies( //
81         @RequestParam(name = "type", required = false) String type, //
82         @RequestParam(name = "ric", required = false) String ric, //
83         @RequestParam(name = "service", required = false) String service) //
84     {
85         Collection<Policy> result = null;
86
87         if (type != null) {
88             result = policies.getForType(type);
89             result = filter(result, null, ric, service);
90         } else if (service != null) {
91             result = policies.getForService(service);
92             result = filter(result, type, ric, null);
93         } else if (ric != null) {
94             result = policies.getForRic(ric);
95             result = filter(result, type, null, service);
96         } else {
97             result = policies.getAll();
98         }
99
100         return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
101     }
102
103     private boolean include(String filter, String value) {
104         return filter == null || value.equals(filter);
105     }
106
107     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
108         if (type == null && ric == null && service == null) {
109             return collection;
110         }
111         Vector<Policy> filtered = new Vector<>();
112         for (Policy p : collection) {
113             if (include(type, p.type().name()) && include(ric, p.ric().name())
114                 && include(service, p.ownerServiceName())) {
115                 filtered.add(p);
116             }
117         }
118         return filtered;
119     }
120
121     private String toJson(Collection<Policy> policies) {
122         Vector<PolicyInfo> v = new Vector<>(policies.size());
123         for (Policy p : policies) {
124             PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
125                 .json(p.json()) //
126                 .name(p.id()) //
127                 .ric(p.ric().name()) //
128                 .type(p.type().name()).build();
129             v.add(policyInfo);
130         }
131         return gson.toJson(v);
132     }
133
134     @PutMapping(path = "/policy")
135     public ResponseEntity<String> putPolicy( //
136         @RequestParam(name = "type", required = true) String type, //
137         @RequestParam(name = "instance", required = true) String instanceId, //
138         @RequestParam(name = "ric", required = true) String ric, //
139         @RequestParam(name = "service", required = true) String service, //
140         @RequestBody String jsonBody) {
141
142         try {
143             Ric ricObj = rics.getRic(ric);
144             Policy policy = ImmutablePolicy.builder() //
145                 .id(instanceId) //
146                 .json(jsonBody) //
147                 .type(types.getType(type)) //
148                 .ric(ricObj) //
149                 .ownerServiceName(service) //
150                 .build();
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);
155         }
156     }
157
158 }