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 java.time.Duration;
27 import java.util.Collection;
28 import java.util.Vector;
30 import org.oransc.policyagent.configuration.ApplicationConfig;
31 import org.oransc.policyagent.exceptions.ServiceException;
32 import org.oransc.policyagent.repository.Service;
33 import org.oransc.policyagent.repository.Services;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.http.HttpStatus;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.GetMapping;
38 import org.springframework.web.bind.annotation.PutMapping;
39 import org.springframework.web.bind.annotation.RequestBody;
40 import org.springframework.web.bind.annotation.RequestParam;
41 import org.springframework.web.bind.annotation.RestController;
44 public class ServiceController {
46 private final ApplicationConfig appConfig;
47 private final Services services;
48 private static Gson gson = new GsonBuilder() //
53 ServiceController(ApplicationConfig config, Services services) {
54 this.appConfig = config;
55 this.services = services;
58 @GetMapping("/service")
59 public ResponseEntity<String> getService( //
60 @RequestParam(name = "name", required = true) String name) {
62 Service s = services.getService(name);
63 String res = gson.toJson(toServiceStatus(s));
64 return new ResponseEntity<String>(res, HttpStatus.OK);
66 } catch (ServiceException e) {
67 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
71 private ServiceStatus toServiceStatus(Service s) {
72 return ImmutableServiceStatus.builder() //
74 .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
75 .timeSincePing(s.timeSinceLastPing().toSeconds()) //
79 @PutMapping("/service")
80 public ResponseEntity<String> putService( //
81 @RequestBody String jsonBody) {
83 ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class);
84 this.services.put(toService(s));
85 return new ResponseEntity<String>("OK", HttpStatus.OK);
86 } catch (Exception e) {
87 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
91 private Service toService(ServiceRegistrationInfo s) {
92 return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
95 @GetMapping("/services")
96 public ResponseEntity<?> getServices() {
97 Collection<Service> allServices = this.services.getAll();
98 Collection<ServiceStatus> result = new Vector<>(allServices.size());
99 for (Service s : allServices) {
100 result.add(toServiceStatus(s));
102 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
105 @PutMapping("/service/ping")
106 public ResponseEntity<String> ping( //
107 @RequestBody String name) {
109 Service s = services.getService(name);
111 return new ResponseEntity<String>("OK", HttpStatus.OK);
112 } catch (ServiceException e) {
113 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);