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