Added queryparam 'ric' for get_types REST API
[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 java.time.Duration;
24 import java.util.Collection;
25 import java.util.Vector;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29
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 Services services;
46     private static Gson gson = new GsonBuilder() //
47         .serializeNulls() //
48         .create(); //
49
50     @Autowired
51     ServiceController(Services services) {
52         this.services = services;
53     }
54
55     @GetMapping("/service")
56     public ResponseEntity<String> getService( //
57         @RequestParam(name = "name", required = true) String name) {
58         try {
59             Service s = services.getService(name);
60             String res = gson.toJson(toServiceStatus(s));
61             return new ResponseEntity<String>(res, HttpStatus.OK);
62
63         } catch (ServiceException e) {
64             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
65         }
66     }
67
68     private ServiceStatus toServiceStatus(Service s) {
69         return ImmutableServiceStatus.builder() //
70             .name(s.getName()) //
71             .keepAliveInterval(s.getKeepAliveInterval().toSeconds()) //
72             .timeSincePing(s.timeSinceLastPing().toSeconds()) //
73             .build();
74     }
75
76     @PutMapping("/service")
77     public ResponseEntity<String> putService( //
78         @RequestBody String jsonBody) {
79         try {
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);
85         }
86     }
87
88     private Service toService(ServiceRegistrationInfo s) {
89         return new Service(s.name(), Duration.ofSeconds(s.keepAliveInterval()));
90     }
91
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));
98         }
99         return new ResponseEntity<>(gson.toJson(result), HttpStatus.OK);
100     }
101
102     @PutMapping("/service/ping")
103     public ResponseEntity<String> ping( //
104         @RequestBody String name) {
105         try {
106             Service s = services.getService(name);
107             s.ping();
108             return new ResponseEntity<String>("OK", HttpStatus.OK);
109         } catch (ServiceException e) {
110             return new ResponseEntity<String>(e.getMessage(), HttpStatus.NO_CONTENT);
111         }
112     }
113
114 }