Merge "NBI documentation using swagger UI"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / ServiceController.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 io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29
30 import java.time.Duration;
31 import java.util.Collection;
32 import java.util.Vector;
33
34 import org.oransc.policyagent.exceptions.ServiceException;
35 import org.oransc.policyagent.repository.Policies;
36 import org.oransc.policyagent.repository.Policy;
37 import org.oransc.policyagent.repository.Service;
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.PostMapping;
45 import org.springframework.web.bind.annotation.PutMapping;
46 import org.springframework.web.bind.annotation.RequestBody;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
49
50 @RestController
51 public class ServiceController {
52
53     private final Services services;
54     private final Policies policies;
55
56     private static Gson gson = new GsonBuilder() //
57         .serializeNulls() //
58         .create(); //
59
60     @Autowired
61     ServiceController(Services services, Policies policies) {
62         this.services = services;
63         this.policies = policies;
64     }
65
66     @GetMapping("/services")
67     @ApiOperation(value = "Returns service information")
68     @ApiResponses(
69         value = {@ApiResponse(code = 200, message = "OK", response = ServiceStatus.class, responseContainer = "List")})
70     public ResponseEntity<String> getServices( //
71         @RequestParam(name = "name", required = false) String name) {
72
73         Collection<ServiceStatus> servicesStatus = new Vector<>();
74         synchronized (this.services) {
75             for (Service s : this.services.getAll()) {
76                 if (name == null || name.equals(s.getName())) {
77                     servicesStatus.add(toServiceStatus(s));
78                 }
79             }
80         }
81
82         String res = gson.toJson(servicesStatus);
83         return new ResponseEntity<String>(res, HttpStatus.OK);
84     }
85
86     private ServiceStatus toServiceStatus(Service s) {
87         return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds());
88     }
89
90     @ApiOperation(value = "Register a service")
91     @ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
92     @PutMapping("/service")
93     public ResponseEntity<String> putService( //
94         @RequestBody ServiceRegistrationInfo registrationInfo) {
95         try {
96             this.services.put(toService(registrationInfo));
97             return new ResponseEntity<String>("OK", HttpStatus.OK);
98         } catch (Exception e) {
99             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
100         }
101     }
102
103     @ApiOperation(value = "Delete a service")
104     @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
105     @DeleteMapping("/services")
106     public ResponseEntity<String> deleteService( //
107         @RequestParam(name = "name", required = true) String name) {
108         try {
109             Service service = removeService(name);
110             // Remove the policies from the repo and let the consistency monitoring
111             // do the rest.
112             removePolicies(service);
113             return new ResponseEntity<String>("OK", HttpStatus.NO_CONTENT);
114         } catch (Exception e) {
115             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
116         }
117     }
118
119     @ApiOperation(value = "Keep the poilicies alive for a service")
120     @ApiResponses(
121         value = {@ApiResponse(code = 200, message = "Policies timeout supervision refreshed"),
122             @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
123     @PostMapping("/services/keepalive")
124     public ResponseEntity<String> keepAliveService( //
125         @RequestParam(name = "name", required = true) String name) {
126         try {
127             services.getService(name).ping();
128             return new ResponseEntity<String>("OK", HttpStatus.OK);
129         } catch (Exception e) {
130             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
131         }
132     }
133
134     private Service removeService(String name) throws ServiceException {
135         synchronized (this.services) {
136             Service service = this.services.getService(name);
137             this.services.remove(service.getName());
138             return service;
139         }
140     }
141
142     private void removePolicies(Service service) {
143         synchronized (this.policies) {
144             Vector<Policy> policyList = new Vector<>(this.policies.getForService(service.getName()));
145             for (Policy policy : policyList) {
146                 this.policies.remove(policy);
147             }
148         }
149     }
150
151     private Service toService(ServiceRegistrationInfo s) {
152         return new Service(s.name, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);
153     }
154
155 }