Change to JUnit5 and clean up pom
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / controllers / ServiceController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.controllers;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.time.Duration;
27 import java.util.Collection;
28 import java.util.Vector;
29
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;
42
43 @RestController
44 public class ServiceController {
45
46     private final ApplicationConfig appConfig;
47     private final Services services;
48     private static Gson gson = new GsonBuilder() //
49         .serializeNulls() //
50         .create(); //
51
52     @Autowired
53     ServiceController(ApplicationConfig config, Services services) {
54         this.appConfig = config;
55         this.services = services;
56     }
57
58     @GetMapping("/service")
59     public ResponseEntity<String> getService( //
60         @RequestParam(name = "name", required = true) String name) {
61         try {
62             Service s = services.getService(name);
63             String res = gson.toJson(toServiceStatus(s));
64             return new ResponseEntity<String>(res, HttpStatus.OK);
65
66         } catch (ServiceException e) {
67             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
68         }
69     }
70
71     private ServiceStatus toServiceStatus(Service s) {
72         return ImmutableServiceStatus.builder() //
73             .name(s.getName()) //
74             .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
75             .timeSincePing(s.timeSinceLastPing().toSeconds()) //
76             .build();
77     }
78
79     @PutMapping("/service")
80     public ResponseEntity<String> putService( //
81         @RequestBody String jsonBody) {
82         try {
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);
88         }
89     }
90
91     private Service toService(ServiceRegistrationInfo s) {
92         return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
93     }
94
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));
101         }
102         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
103     }
104
105     @PutMapping("/service/ping")
106     public ResponseEntity<String> ping( //
107         @RequestBody String name) {
108         try {
109             Service s = services.getService(name);
110             s.ping();
111             return new ResponseEntity<String>("OK", HttpStatus.OK);
112         } catch (ServiceException e) {
113             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
114         }
115     }
116
117 }