Merge "Added STD sim 2.0.0 tests"
[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.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.time.Duration;
35 import java.util.ArrayList;
36 import java.util.Collection;
37
38 import org.oransc.policyagent.exceptions.ServiceException;
39 import org.oransc.policyagent.repository.Policies;
40 import org.oransc.policyagent.repository.Policy;
41 import org.oransc.policyagent.repository.Service;
42 import org.oransc.policyagent.repository.Services;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.bind.annotation.DeleteMapping;
47 import org.springframework.web.bind.annotation.GetMapping;
48 import org.springframework.web.bind.annotation.PutMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestParam;
51 import org.springframework.web.bind.annotation.RestController;
52
53 @RestController
54 @Api(tags = "Service registry and supervision")
55 public class ServiceController {
56
57     private final Services services;
58     private final Policies policies;
59
60     private static Gson gson = new GsonBuilder() //
61         .create(); //
62
63     @Autowired
64     ServiceController(Services services, Policies policies) {
65         this.services = services;
66         this.policies = policies;
67     }
68
69     @GetMapping("/services")
70     @ApiOperation(value = "Returns service information")
71     @ApiResponses(
72         value = { //
73             @ApiResponse(code = 200, message = "OK", response = ServiceStatus.class, responseContainer = "List"), //
74             @ApiResponse(code = 404, message = "Service is not found", response = String.class)})
75     public ResponseEntity<String> getServices(//
76         @ApiParam(name = "name", required = false, value = "The name of the service") //
77         @RequestParam(name = "name", required = false) String name) {
78         if (name != null && this.services.get(name) == null) {
79             return new ResponseEntity<>("Service not found", HttpStatus.NOT_FOUND);
80         }
81
82         Collection<ServiceStatus> servicesStatus = new ArrayList<>();
83         for (Service s : this.services.getAll()) {
84             if (name == null || name.equals(s.getName())) {
85                 servicesStatus.add(toServiceStatus(s));
86             }
87         }
88
89         String res = gson.toJson(servicesStatus);
90         return new ResponseEntity<>(res, HttpStatus.OK);
91     }
92
93     private ServiceStatus toServiceStatus(Service s) {
94         return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds(),
95             s.getCallbackUrl());
96     }
97
98     private void validateRegistrationInfo(ServiceRegistrationInfo registrationInfo)
99         throws ServiceException, MalformedURLException {
100         if (registrationInfo.serviceName.isEmpty()) {
101             throw new ServiceException("Missing mandatory parameter 'serviceName'");
102         }
103         if (registrationInfo.keepAliveIntervalSeconds < 0) {
104             throw new ServiceException("Keepalive interval shoul be greater or equal to 0");
105         }
106         if (!registrationInfo.callbackUrl.isEmpty()) {
107             new URL(registrationInfo.callbackUrl);
108         }
109     }
110
111     @ApiOperation(value = "Register a service")
112     @ApiResponses(
113         value = { //
114             @ApiResponse(code = 200, message = "Service updated", response = String.class),
115             @ApiResponse(code = 201, message = "Service created", response = String.class), //
116             @ApiResponse(code = 400, message = "The ServiceRegistrationInfo is not accepted", response = String.class)})
117     @PutMapping("/service")
118     public ResponseEntity<String> putService(//
119         @RequestBody ServiceRegistrationInfo registrationInfo) {
120         try {
121             validateRegistrationInfo(registrationInfo);
122             final boolean isCreate = this.services.get(registrationInfo.serviceName) == null;
123             this.services.put(toService(registrationInfo));
124             return new ResponseEntity<>("OK", isCreate ? HttpStatus.CREATED : HttpStatus.OK);
125         } catch (Exception e) {
126             return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
127         }
128     }
129
130     @ApiOperation(value = "Delete a service")
131     @ApiResponses(
132         value = { //
133             @ApiResponse(code = 204, message = "OK"),
134             @ApiResponse(code = 404, message = "Service not found", response = String.class)})
135     @DeleteMapping("/services")
136     public ResponseEntity<String> deleteService(//
137         @ApiParam(name = "name", required = true, value = "The name of the service") //
138         @RequestParam(name = "name", required = true) String serviceName) {
139         try {
140             Service service = removeService(serviceName);
141             // Remove the policies from the repo and let the consistency monitoring
142             // do the rest.
143             removePolicies(service);
144             return new ResponseEntity<>("OK", HttpStatus.NO_CONTENT);
145         } catch (Exception e) {
146             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
147         }
148     }
149
150     @ApiOperation(value = "Heartbeat from a serice")
151     @ApiResponses(
152         value = { //
153             @ApiResponse(code = 200, message = "Service supervision timer refreshed, OK"),
154             @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
155     @PutMapping("/services/keepalive")
156     public ResponseEntity<String> keepAliveService(//
157         @ApiParam(name = "name", required = true, value = "The name of the service") //
158         @RequestParam(name = "name", required = true) String serviceName) {
159         try {
160             services.getService(serviceName).keepAlive();
161             return new ResponseEntity<>("OK", HttpStatus.OK);
162         } catch (ServiceException e) {
163             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
164         }
165     }
166
167     private Service removeService(String name) throws ServiceException {
168         Service service = this.services.getService(name); // Just to verify that it exists
169         this.services.remove(service.getName());
170         return service;
171     }
172
173     private void removePolicies(Service service) {
174         Collection<Policy> policyList = this.policies.getForService(service.getName());
175         for (Policy policy : policyList) {
176             this.policies.remove(policy);
177         }
178     }
179
180     private Service toService(ServiceRegistrationInfo s) {
181         return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);
182     }
183
184 }