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.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()), s.callbackUrl());
92 @GetMapping("/services")
93 public ResponseEntity<?> getServices() {
94 Collection<ServiceStatus> result = new Vector<>();
95 synchronized (this.services) {
96 for (Service s : this.services.getAll()) {
97 result.add(toServiceStatus(s));
100 return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
103 @PutMapping("/service/ping")
104 public ResponseEntity<String> ping( //
105 @RequestBody String name) {
107 Service s = services.getService(name);
109 return new ResponseEntity<String>("OK", HttpStatus.OK);
110 } catch (ServiceException e) {
111 return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);