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 java.time.Duration;
24 import java.util.Collection;
25 import java.util.Vector;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
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 Services services;
46 private static Gson gson = new GsonBuilder() //
51 ServiceController(Services services) {
52 this.services = services;
55 @GetMapping("/service")
56 public ResponseEntity<String> getService( //
57 @RequestParam(name = "name", required = true) String name) {
59 Service s = services.getService(name);
60 String res = gson.toJson(toServiceStatus(s));
61 return new ResponseEntity<String>(res, HttpStatus.OK);
63 } catch (ServiceException e) {
64 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
68 private ServiceStatus toServiceStatus(Service s) {
69 return ImmutableServiceStatus.builder() //
71 .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
72 .timeSincePing(s.timeSinceLastPing().toSeconds()) //
76 @PutMapping("/service")
77 public ResponseEntity<String> putService( //
78 @RequestBody String jsonBody) {
80 ServiceRegistrationInfo s = gson.fromJson(jsonBody, ImmutableServiceRegistrationInfo.class);
81 this.services.put(toService(s));
82 return new ResponseEntity<String>("OK", HttpStatus.OK);
83 } catch (Exception e) {
84 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
88 private Service toService(ServiceRegistrationInfo s) {
89 return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
92 @GetMapping("/services")
93 public ResponseEntity<?> getServices() {
94 Collection<Service> allServices = this.services.getAll();
95 Collection<ServiceStatus> result = new Vector<>(allServices.size());
96 for (Service s : allServices) {
97 result.add(toServiceStatus(s));
99 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
102 @PutMapping("/service/ping")
103 public ResponseEntity<String> ping( //
104 @RequestBody String name) {
106 Service s = services.getService(name);
108 return new ResponseEntity<String>("OK", HttpStatus.OK);
109 } catch (ServiceException e) {
110 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);