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.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
30 import java.time.Duration;
31 import java.util.Collection;
32 import java.util.Vector;
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;
51 public class ServiceController {
53 private final Services services;
54 private final Policies policies;
56 private static Gson gson = new GsonBuilder() //
61 ServiceController(Services services, Policies policies) {
62 this.services = services;
63 this.policies = policies;
66 @GetMapping("/services")
67 @ApiOperation(value = "Returns service information")
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) {
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));
82 String res = gson.toJson(servicesStatus);
83 return new ResponseEntity<String>(res, HttpStatus.OK);
86 private ServiceStatus toServiceStatus(Service s) {
87 return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds());
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) {
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);
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 = "serviceName", required = true) String serviceName) {
109 Service service = removeService(serviceName);
110 // Remove the policies from the repo and let the consistency monitoring
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);
119 @ApiOperation(value = "Keep the poilicies alive for a service")
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 = "serviceName", required = true) String serviceName) {
127 services.getService(serviceName).ping();
128 return new ResponseEntity<String>("OK", HttpStatus.OK);
129 } catch (Exception e) {
130 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NOT_FOUND);
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());
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);
151 private Service toService(ServiceRegistrationInfo s) {
152 return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);