Adapt to new A1Client interface
[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
21 package org.oransc.policyagent.controllers;
22
23 import com.google.gson.Gson;
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.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;
48
49 @RestController
50 public class PolicyController {
51
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() //
58         .serializeNulls() //
59         .create(); //
60
61     @Autowired
62     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
63         this.appConfig = config;
64         this.policyTypes = types;
65         this.policies = policies;
66         this.rics = rics;
67         this.services = services;
68     }
69
70     @GetMapping("/policy_types")
71     public ResponseEntity<String> getPolicyTypes() {
72
73         Collection<PolicyType> types = this.policyTypes.getAll();
74         return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
75     }
76
77     @GetMapping("/policy")
78     public ResponseEntity<String> getPolicy( //
79         @RequestParam(name = "instance", required = true) String instance) {
80         try {
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);
85         }
86     }
87
88     @DeleteMapping("/policy")
89     public ResponseEntity<String> deletePolicy( //
90         @RequestParam(name = "instance", required = true) String instance) {
91
92         Policy p = policies.removeId(instance);
93         return new ResponseEntity<String>("OK", HttpStatus.OK);
94     }
95
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) //
101     {
102         Collection<Policy> result = null;
103
104         if (type != 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);
113         } else {
114             result = policies.getAll();
115         }
116
117         return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
118     }
119
120     private boolean include(String filter, String value) {
121         return filter == null || value.equals(filter);
122     }
123
124     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
125         if (type == null && ric == null && service == null) {
126             return collection;
127         }
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())) {
132                 filtered.add(p);
133             }
134         }
135         return filtered;
136     }
137
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() //
142                 .json(p.json()) //
143                 .id(p.id()) //
144                 .ric(p.ric().name()) //
145                 .type(p.type().name()) //
146                 .service(p.ownerServiceName()) //
147                 .lastModified(p.lastModified()) //
148                 .build();
149             v.add(policyInfo);
150         }
151         return gson.toJson(v);
152     }
153
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() //
158                 .name(t.name()) //
159                 .build();
160             v.add(policyInfo);
161         }
162         return gson.toJson(v);
163     }
164
165     private String getTimeStampUTC() {
166         return java.time.Instant.now().toString();
167     }
168
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) {
176
177         try {
178             // services.getService(service).ping();
179             Ric ricObj = rics.getRic(ric);
180             Policy policy = ImmutablePolicy.builder() //
181                 .id(instanceId) //
182                 .json(jsonBody) //
183                 .type(policyTypes.getType(type)) //
184                 .ric(ricObj) //
185                 .ownerServiceName(service) //
186                 .lastModified(getTimeStampUTC()) //
187                 .build();
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);
192         }
193     }
194
195 }