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===================================
20 package org.oransc.policyagent.controllers;
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
25 import java.time.Duration;
26 import java.util.Collection;
27 import java.util.Vector;
29 import org.oransc.policyagent.configuration.ApplicationConfig;
30 import org.oransc.policyagent.exceptions.ServiceException;
31 import org.oransc.policyagent.repository.Service;
32 import org.oransc.policyagent.repository.Services;
33 import org.springframework.beans.factory.annotation.Autowired;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.ResponseEntity;
36 import org.springframework.web.bind.annotation.GetMapping;
37 import org.springframework.web.bind.annotation.PutMapping;
38 import org.springframework.web.bind.annotation.RequestBody;
39 import org.springframework.web.bind.annotation.RequestParam;
40 import org.springframework.web.bind.annotation.RestController;
43 public class ServiceController {
45 private final ApplicationConfig appConfig;
46 private final Services services;
47 private static Gson gson = new GsonBuilder() //
52 ServiceController(ApplicationConfig config, Services services) {
53 this.appConfig = config;
54 this.services = services;
57 @GetMapping("/service")
58 public ResponseEntity<String> getService( //
59 @RequestParam(name = "name", required = true) String name) {
61 Service s = services.getService(name);
62 String res = gson.toJson(toServiceStatus(s));
63 return new ResponseEntity<String>(res, HttpStatus.OK);
65 } catch (ServiceException e) {
66 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
70 private ServiceStatus toServiceStatus(Service s) {
71 return ImmutableServiceStatus.builder() //
73 .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
74 .timeSincePing(s.timeSinceLastPing().toSeconds()) //
78 @PutMapping("/service")
79 public ResponseEntity<String> putService( //
80 @RequestBody String jsonBody) {
82 ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class);
83 this.services.put(toService(s));
84 return new ResponseEntity<String>("OK", HttpStatus.OK);
85 } catch (Exception e) {
86 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
90 private Service toService(ServiceRegistrationInfo s) {
91 return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
94 @GetMapping("/services")
95 public ResponseEntity<?> getServices() {
96 Collection<Service> allServices = this.services.getAll();
97 Collection<ServiceStatus> result = new Vector<>(allServices.size());
98 for (Service s : allServices) {
99 result.add(toServiceStatus(s));
101 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
104 @PutMapping("/service/ping")
105 public ResponseEntity<String> ping( //
106 @RequestBody String name) {
108 Service s = services.getService(name);
110 return new ResponseEntity<String>("OK", HttpStatus.OK);
111 } catch (ServiceException e) {
112 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);