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.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
35 import org.oransc.policyagent.exceptions.ServiceException;
36 import org.oransc.policyagent.repository.Policies;
37 import org.oransc.policyagent.repository.Policy;
38 import org.oransc.policyagent.repository.Service;
39 import org.oransc.policyagent.repository.Services;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PostMapping;
46 import org.springframework.web.bind.annotation.PutMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestParam;
49 import org.springframework.web.bind.annotation.RestController;
52 public class ServiceController {
54 private final Services services;
55 private final Policies policies;
57 private static Gson gson = new GsonBuilder() //
62 ServiceController(Services services, Policies policies) {
63 this.services = services;
64 this.policies = policies;
67 @GetMapping("/services")
68 @ApiOperation(value = "Returns service information")
70 value = {@ApiResponse(code = 200, message = "OK", response = ServiceStatus.class, responseContainer = "List")})
71 public ResponseEntity<String> getServices(//
72 @RequestParam(name = "name", required = false) String name) {
74 Collection<ServiceStatus> servicesStatus = new ArrayList<>();
75 synchronized (this.services) {
76 for (Service s : this.services.getAll()) {
77 if (name == null || name.equals(s.getName())) {
78 servicesStatus.add(toServiceStatus(s));
83 String res = gson.toJson(servicesStatus);
84 return new ResponseEntity<>(res, HttpStatus.OK);
87 private ServiceStatus toServiceStatus(Service s) {
88 return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds());
91 @ApiOperation(value = "Register a service")
92 @ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
93 @PutMapping("/service")
94 public ResponseEntity<String> putService(//
95 @RequestBody ServiceRegistrationInfo registrationInfo) {
97 this.services.put(toService(registrationInfo));
98 return new ResponseEntity<>("OK", HttpStatus.OK);
99 } catch (Exception e) {
100 return new ResponseEntity<>(e.getMessage(), HttpStatus.NO_CONTENT);
104 @ApiOperation(value = "Delete a service")
105 @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
106 @DeleteMapping("/services")
107 public ResponseEntity<String> deleteService(//
108 @RequestParam(name = "name", required = true) String serviceName) {
110 Service service = removeService(serviceName);
111 // Remove the policies from the repo and let the consistency monitoring
113 removePolicies(service);
114 return new ResponseEntity<>("OK", HttpStatus.NO_CONTENT);
115 } catch (Exception e) {
116 return new ResponseEntity<>(e.getMessage(), HttpStatus.NO_CONTENT);
120 @ApiOperation(value = "Keep the poilicies alive for a service")
122 value = {@ApiResponse(code = 200, message = "Policies timeout supervision refreshed"),
123 @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
124 @PostMapping("/services/keepalive")
125 public ResponseEntity<String> keepAliveService(//
126 @RequestParam(name = "name", required = true) String serviceName) {
128 services.getService(serviceName).ping();
129 return new ResponseEntity<>("OK", HttpStatus.OK);
130 } catch (Exception e) {
131 return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
135 private Service removeService(String name) throws ServiceException {
136 synchronized (this.services) {
137 Service service = this.services.getService(name);
138 this.services.remove(service.getName());
143 private void removePolicies(Service service) {
144 synchronized (this.policies) {
145 List<Policy> policyList = new ArrayList<>(this.policies.getForService(service.getName()));
146 for (Policy policy : policyList) {
147 this.policies.remove(policy);
152 private Service toService(ServiceRegistrationInfo s) {
153 return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);