Dashboard using policy agent NBI
[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.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;
47
48 @RestController
49 public class PolicyController {
50
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() //
57         .serializeNulls() //
58         .create(); //
59
60     @Autowired
61     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
62         this.appConfig = config;
63         this.policyTypes = types;
64         this.policies = policies;
65         this.rics = rics;
66         this.services = services;
67     }
68
69     @GetMapping("/policy_types")
70     public ResponseEntity<String> getPolicyTypes() {
71
72         Collection<PolicyType> types = this.policyTypes.getAll();
73         return new ResponseEntity<String>(policyTypesToJson(types), HttpStatus.OK);
74     }
75
76     @GetMapping("/policy")
77     public ResponseEntity<String> getPolicy( //
78         @RequestParam(name = "instance", required = true) String instance) {
79         try {
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);
84         }
85     }
86
87     @DeleteMapping("/policy")
88     public ResponseEntity<String> deletePolicy( //
89         @RequestParam(name = "instance", required = true) String instance) {
90
91         Policy p = policies.removeId(instance);
92         return new ResponseEntity<String>("OK", HttpStatus.OK);
93     }
94
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) //
100     {
101         Collection<Policy> result = null;
102
103         if (type != 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);
112         } else {
113             result = policies.getAll();
114         }
115
116         return new ResponseEntity<String>(policiesToJson(result), HttpStatus.OK);
117     }
118
119     private boolean include(String filter, String value) {
120         return filter == null || value.equals(filter);
121     }
122
123     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
124         if (type == null && ric == null && service == null) {
125             return collection;
126         }
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())) {
131                 filtered.add(p);
132             }
133         }
134         return filtered;
135     }
136
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() //
141                 .json(p.json()) //
142                 .id(p.id()) //
143                 .ric(p.ric().name()) //
144                 .type(p.type().name()) //
145                 .service(p.ownerServiceName()) //
146                 .lastModified(p.lastModified()) //
147                 .build();
148             v.add(policyInfo);
149         }
150         return gson.toJson(v);
151     }
152
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()) //
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 }