2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent.controllers;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiResponse;
29 import io.swagger.annotations.ApiResponses;
31 import java.time.Duration;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
36 import org.oransc.policyagent.exceptions.ServiceException;
37 import org.oransc.policyagent.repository.Policies;
38 import org.oransc.policyagent.repository.Policy;
39 import org.oransc.policyagent.repository.Service;
40 import org.oransc.policyagent.repository.Services;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.web.bind.annotation.DeleteMapping;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.PutMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestParam;
50 import org.springframework.web.bind.annotation.RestController;
53 @Api(tags = "Service registry and supervision")
54 public class ServiceController {
56 private final Services services;
57 private final Policies policies;
59 private static Gson gson = new GsonBuilder() //
63 ServiceController(Services services, Policies policies) {
64 this.services = services;
65 this.policies = policies;
68 @GetMapping("/services")
69 @ApiOperation(value = "Returns service information")
72 @ApiResponse(code = 200, message = "OK", response = ServiceStatus.class, responseContainer = "List"), //
73 @ApiResponse(code = 404, message = "Service is not found", response = String.class)})
74 public ResponseEntity<String> getServices(//
75 @RequestParam(name = "name", required = false) String name) {
77 if (name != null && this.services.get(name) == null) {
78 return new ResponseEntity<>("Service not found", HttpStatus.NOT_FOUND);
81 Collection<ServiceStatus> servicesStatus = new ArrayList<>();
82 synchronized (this.services) {
83 for (Service s : this.services.getAll()) {
84 if (name == null || name.equals(s.getName())) {
85 servicesStatus.add(toServiceStatus(s));
90 String res = gson.toJson(servicesStatus);
91 return new ResponseEntity<>(res, HttpStatus.OK);
94 private ServiceStatus toServiceStatus(Service s) {
95 return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds(),
99 private void validateRegistrationInfo(ServiceRegistrationInfo registrationInfo) throws ServiceException {
100 if (registrationInfo.serviceName.isEmpty()) {
101 throw new ServiceException("Missing mandatory parameter 'serviceName'");
105 @ApiOperation(value = "Register a service")
108 @ApiResponse(code = 200, message = "OK", response = String.class),
109 @ApiResponse(code = 400, message = "Cannot parse the ServiceRegistrationInfo", response = String.class)})
110 @PutMapping("/service")
111 public ResponseEntity<String> putService(//
112 @RequestBody ServiceRegistrationInfo registrationInfo) {
114 validateRegistrationInfo(registrationInfo);
115 this.services.put(toService(registrationInfo));
116 return new ResponseEntity<>("OK", HttpStatus.OK);
117 } catch (Exception e) {
118 return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
122 @ApiOperation(value = "Delete a service")
125 @ApiResponse(code = 204, message = "OK"),
126 @ApiResponse(code = 404, message = "Service not found", response = String.class)})
127 @DeleteMapping("/services")
128 public ResponseEntity<String> deleteService(//
129 @RequestParam(name = "name", required = true) String serviceName) {
131 Service service = removeService(serviceName);
132 // Remove the policies from the repo and let the consistency monitoring
134 removePolicies(service);
135 return new ResponseEntity<>("OK", HttpStatus.NO_CONTENT);
136 } catch (Exception e) {
137 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
141 @ApiOperation(value = "Heartbeat from a serice")
144 @ApiResponse(code = 200, message = "Service supervision timer refreshed, OK"),
145 @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
146 @PostMapping("/services/keepalive")
147 public ResponseEntity<String> keepAliveService(//
148 @RequestParam(name = "name", required = true) String serviceName) {
150 services.getService(serviceName).keepAlive();
151 return new ResponseEntity<>("OK", HttpStatus.OK);
152 } catch (ServiceException e) {
153 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
157 private Service removeService(String name) throws ServiceException {
158 synchronized (this.services) {
159 Service service = this.services.getService(name);
160 this.services.remove(service.getName());
165 private void removePolicies(Service service) {
166 synchronized (this.policies) {
167 List<Policy> policyList = new ArrayList<>(this.policies.getForService(service.getName()));
168 for (Policy policy : policyList) {
169 this.policies.remove(policy);
174 private Service toService(ServiceRegistrationInfo s) {
175 return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);