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() //
64 ServiceController(Services services, Policies policies) {
65 this.services = services;
66 this.policies = policies;
69 @GetMapping("/services")
70 @ApiOperation(value = "Returns service information")
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 @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);
82 Collection<ServiceStatus> servicesStatus = new ArrayList<>();
83 synchronized (this.services) {
84 for (Service s : this.services.getAll()) {
85 if (name == null || name.equals(s.getName())) {
86 servicesStatus.add(toServiceStatus(s));
91 String res = gson.toJson(servicesStatus);
92 return new ResponseEntity<>(res, HttpStatus.OK);
95 private ServiceStatus toServiceStatus(Service s) {
96 return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds(),
100 @ApiOperation(value = "Register a service")
103 @ApiResponse(code = 200, message = "OK", response = String.class),
104 @ApiResponse(code = 400, message = "Cannot parse the ServiceRegistrationInfo", response = String.class)})
105 @PutMapping("/service")
106 public ResponseEntity<String> putService(//
107 @RequestBody ServiceRegistrationInfo registrationInfo) {
109 this.services.put(toService(registrationInfo));
110 return new ResponseEntity<>("OK", HttpStatus.OK);
111 } catch (Exception e) {
112 return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
116 @ApiOperation(value = "Delete a service")
119 @ApiResponse(code = 204, message = "OK"),
120 @ApiResponse(code = 404, message = "Service not found", response = String.class)})
121 @DeleteMapping("/services")
122 public ResponseEntity<String> deleteService(//
123 @RequestParam(name = "name", required = true) String serviceName) {
125 Service service = removeService(serviceName);
126 // Remove the policies from the repo and let the consistency monitoring
128 removePolicies(service);
129 return new ResponseEntity<>("OK", HttpStatus.NO_CONTENT);
130 } catch (Exception e) {
131 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
135 @ApiOperation(value = "Keep the policies alive for a service")
138 @ApiResponse(code = 200, message = "Policies timeout supervision refreshed"),
139 @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
140 @PostMapping("/services/keepalive")
141 public ResponseEntity<String> keepAliveService(//
142 @RequestParam(name = "name", required = true) String serviceName) {
144 services.getService(serviceName).ping();
145 return new ResponseEntity<>("OK", HttpStatus.OK);
146 } catch (Exception e) {
147 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
151 private Service removeService(String name) throws ServiceException {
152 synchronized (this.services) {
153 Service service = this.services.getService(name);
154 this.services.remove(service.getName());
159 private void removePolicies(Service service) {
160 synchronized (this.policies) {
161 List<Policy> policyList = new ArrayList<>(this.policies.getForService(service.getName()));
162 for (Policy policy : policyList) {
163 this.policies.remove(policy);
168 private Service toService(ServiceRegistrationInfo s) {
169 return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);