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.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestParam;
47 import org.springframework.web.bind.annotation.RestController;
50 public class ServiceController {
52 private final Services services;
53 private final Policies policies;
55 private static Gson gson = new GsonBuilder() //
60 ServiceController(Services services, Policies policies) {
61 this.services = services;
62 this.policies = policies;
65 @GetMapping("/services")
66 @ApiOperation(value = "Returns service information", response = ServiceStatus.class)
67 @ApiResponses(value = {@ApiResponse(code = 200, message = "OK")})
68 public ResponseEntity<String> getServices( //
69 @RequestParam(name = "name", required = false) String name) {
71 Collection<ServiceStatus> servicesStatus = new Vector<>();
72 synchronized (this.services) {
73 for (Service s : this.services.getAll()) {
74 if (name == null || name.equals(s.name())) {
75 servicesStatus.add(toServiceStatus(s));
80 String res = gson.toJson(servicesStatus);
81 return new ResponseEntity<String>(res, HttpStatus.OK);
84 private ServiceStatus toServiceStatus(Service s) {
85 return ImmutableServiceStatus.builder() //
87 .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
88 .timeSincePing(s.timeSinceLastPing().toSeconds()) //
92 @PutMapping("/service")
93 public ResponseEntity<String> putService( //
94 @RequestBody String jsonBody) {
96 ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class);
97 this.services.put(toService(s));
98 return new ResponseEntity<String>("OK", HttpStatus.OK);
99 } catch (Exception e) {
100 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
104 @DeleteMapping("/services")
105 public ResponseEntity<String> deleteService( //
106 @RequestParam(name = "name", required = true) String name) {
108 Service service = removeService(name);
109 // Remove the policies from the repo and let the consistency monitoring
111 removePolicies(service);
112 return new ResponseEntity<String>("OK", HttpStatus.NO_CONTENT);
113 } catch (Exception e) {
114 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
118 private Service removeService(String name) throws ServiceException {
119 synchronized (this.services) {
120 Service service = this.services.getService(name);
121 this.services.remove(service.name());
126 private void removePolicies(Service service) {
127 synchronized (this.policies) {
128 Vector<Policy> policyList = new Vector<>(this.policies.getForService(service.name()));
129 for (Policy policy : policyList) {
130 this.policies.remove(policy);
135 private Service toService(ServiceRegistrationInfo s) {
136 return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()), s.callbackUrl());