Added ServiceController and Service supervision
[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.oransc.policyagent.repository.Ric;
35 import org.oransc.policyagent.repository.Rics;
36 import org.oransc.policyagent.repository.Services;
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 ApplicationConfig appConfig;
50     private final Rics rics;
51     private final PolicyTypes types;
52     private final Policies policies;
53     private final Services services;
54     private static Gson gson = new GsonBuilder() //
55         .serializeNulls() //
56         .create(); //
57
58     @Autowired
59     PolicyController(ApplicationConfig config, PolicyTypes types, Policies policies, Rics rics, Services services) {
60         this.appConfig = config;
61         this.types = types;
62         this.policies = policies;
63         this.rics = rics;
64         this.services = services;
65     }
66
67     @GetMapping("/policy")
68     public ResponseEntity<String> getPolicy( //
69         @RequestParam(name = "instance", required = true) String instance) {
70         try {
71             Policy p = policies.get(instance);
72             return new ResponseEntity<String>(p.json(), HttpStatus.OK);
73         } catch (ServiceException e) {
74             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
75         }
76     }
77
78     @GetMapping("/policies")
79     public ResponseEntity<String> getPolicies( //
80         @RequestParam(name = "type", required = false) String type, //
81         @RequestParam(name = "ric", required = false) String ric, //
82         @RequestParam(name = "service", required = false) String service) //
83     {
84         Collection<Policy> result = null;
85
86         if (type != null) {
87             result = policies.getForType(type);
88             result = filter(result, null, ric, service);
89         } else if (service != null) {
90             result = policies.getForService(service);
91             result = filter(result, type, ric, null);
92         } else if (ric != null) {
93             result = policies.getForRic(ric);
94             result = filter(result, type, null, service);
95         } else {
96             result = policies.getAll();
97         }
98
99         return new ResponseEntity<String>(toJson(result), HttpStatus.OK);
100     }
101
102     private boolean include(String filter, String value) {
103         return filter == null || value.equals(filter);
104     }
105
106     private Collection<Policy> filter(Collection<Policy> collection, String type, String ric, String service) {
107         if (type == null && ric == null && service == null) {
108             return collection;
109         }
110         Vector<Policy> filtered = new Vector<>();
111         for (Policy p : collection) {
112             if (include(type, p.type().name()) && include(ric, p.ric().name())
113                 && include(service, p.ownerServiceName())) {
114                 filtered.add(p);
115             }
116         }
117         return filtered;
118     }
119
120     private String toJson(Collection<Policy> policies) {
121         Vector<PolicyInfo> v = new Vector<>(policies.size());
122         for (Policy p : policies) {
123             PolicyInfo policyInfo = ImmutablePolicyInfo.builder() //
124                 .json(p.json()) //
125                 .name(p.id()) //
126                 .ric(p.ric().name()) //
127                 .type(p.type().name()).build();
128             v.add(policyInfo);
129         }
130         return gson.toJson(v);
131     }
132
133     @PutMapping(path = "/policy")
134     public ResponseEntity<String> putPolicy( //
135         @RequestParam(name = "type", required = true) String type, //
136         @RequestParam(name = "instance", required = true) String instanceId, //
137         @RequestParam(name = "ric", required = true) String ric, //
138         @RequestParam(name = "service", required = true) String service, //
139         @RequestBody String jsonBody) {
140
141         try {
142             services.getService(service).ping();
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 }