Added ServiceController and Service supervision
[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 package org.oransc.policyagent.controllers;
21
22 import com.google.gson.Gson;
23 import com.google.gson.GsonBuilder;
24
25 import java.time.Duration;
26 import java.util.Collection;
27 import java.util.Vector;
28
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;
41
42 @RestController
43 public class ServiceController {
44
45     private final ApplicationConfig appConfig;
46     private final Services services;
47     private static Gson gson = new GsonBuilder() //
48         .serializeNulls() //
49         .create(); //
50
51     @Autowired
52     ServiceController(ApplicationConfig config, Services services) {
53         this.appConfig = config;
54         this.services = services;
55     }
56
57     @GetMapping("/service")
58     public ResponseEntity<String> getService( //
59         @RequestParam(name = "name", required = true) String name) {
60         try {
61             Service s = services.getService(name);
62             String res = gson.toJson(toServiceStatus(s));
63             return new ResponseEntity<String>(res, HttpStatus.OK);
64
65         } catch (ServiceException e) {
66             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
67         }
68     }
69
70     private ServiceStatus toServiceStatus(Service s) {
71         return ImmutableServiceStatus.builder() //
72             .name(s.getName()) //
73             .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
74             .timeSincePing(s.timeSinceLastPing().toSeconds()) //
75             .build();
76     }
77
78     @PutMapping("/service")
79     public ResponseEntity<String> putService( //
80         @RequestBody String jsonBody) {
81         try {
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);
87         }
88     }
89
90     private Service toService(ServiceRegistrationInfo s) {
91         return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
92     }
93
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));
100         }
101         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
102     }
103
104     @PutMapping("/service/ping")
105     public ResponseEntity<String> ping( //
106         @RequestBody String name) {
107         try {
108             Service s = services.getService(name);
109             s.ping();
110             return new ResponseEntity<String>("OK", HttpStatus.OK);
111         } catch (ServiceException e) {
112             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
113         }
114     }
115
116 }