bb85ad147c38ca7879906ac1b0da9b11cbac08ec
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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.onap.ccsdk.oran.a1policymanagementservice.controllers.v1;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import io.swagger.annotations.Api;
27 import io.swagger.annotations.ApiOperation;
28 import io.swagger.annotations.ApiParam;
29 import io.swagger.annotations.ApiResponse;
30 import io.swagger.annotations.ApiResponses;
31
32 import java.net.MalformedURLException;
33 import java.net.URL;
34 import java.time.Duration;
35 import java.util.ArrayList;
36 import java.util.Collection;
37
38 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.VoidResponse;
39 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
40 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
41 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
42 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
43 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.web.bind.annotation.DeleteMapping;
48 import org.springframework.web.bind.annotation.GetMapping;
49 import org.springframework.web.bind.annotation.PutMapping;
50 import org.springframework.web.bind.annotation.RequestBody;
51 import org.springframework.web.bind.annotation.RequestParam;
52 import org.springframework.web.bind.annotation.RestController;
53
54 @RestController
55 @Api(tags = Consts.V1_API_NAME)
56 public class ServiceController {
57
58     private final Services services;
59     private final Policies policies;
60
61     private static Gson gson = new GsonBuilder() //
62         .create(); //
63
64     @Autowired
65     ServiceController(Services services, Policies policies) {
66         this.services = services;
67         this.policies = policies;
68     }
69
70     @GetMapping("/services")
71     @ApiOperation(value = "Returns service information")
72     @ApiResponses(
73         value = { //
74             @ApiResponse(code = 200, message = "OK", response = ServiceStatus.class, responseContainer = "List"), //
75             @ApiResponse(code = 404, message = "Service is not found", response = String.class)})
76     public ResponseEntity<String> getServices(//
77         @ApiParam(name = "name", required = false, value = "The name of the service") //
78         @RequestParam(name = "name", required = false) String name) {
79         if (name != null && this.services.get(name) == null) {
80             return new ResponseEntity<>("Service not found", HttpStatus.NOT_FOUND);
81         }
82
83         Collection<ServiceStatus> servicesStatus = new ArrayList<>();
84         for (Service s : this.services.getAll()) {
85             if (name == null || name.equals(s.getName())) {
86                 servicesStatus.add(toServiceStatus(s));
87             }
88         }
89
90         String res = gson.toJson(servicesStatus);
91         return new ResponseEntity<>(res, HttpStatus.OK);
92     }
93
94     private ServiceStatus toServiceStatus(Service s) {
95         return new ServiceStatus(s.getName(), s.getKeepAliveInterval().toSeconds(), s.timeSinceLastPing().toSeconds(),
96             s.getCallbackUrl());
97     }
98
99     private void validateRegistrationInfo(ServiceRegistrationInfo registrationInfo)
100         throws ServiceException, MalformedURLException {
101         if (registrationInfo.serviceName.isEmpty()) {
102             throw new ServiceException("Missing mandatory parameter 'serviceName'");
103         }
104         if (registrationInfo.keepAliveIntervalSeconds < 0) {
105             throw new ServiceException("Keepalive interval shoul be greater or equal to 0");
106         }
107         if (!registrationInfo.callbackUrl.isEmpty()) {
108             new URL(registrationInfo.callbackUrl);
109         }
110     }
111
112     @ApiOperation(value = "Register a service")
113     @ApiResponses(
114         value = { //
115             @ApiResponse(code = 200, message = "Service updated", response = String.class),
116             @ApiResponse(code = 201, message = "Service created", response = String.class), //
117             @ApiResponse(code = 400, message = "The ServiceRegistrationInfo is not accepted", response = String.class)})
118     @PutMapping("/service")
119     public ResponseEntity<String> putService(//
120         @RequestBody ServiceRegistrationInfo registrationInfo) {
121         try {
122             validateRegistrationInfo(registrationInfo);
123             final boolean isCreate = this.services.get(registrationInfo.serviceName) == null;
124             this.services.put(toService(registrationInfo));
125             return new ResponseEntity<>("OK", isCreate ? HttpStatus.CREATED : HttpStatus.OK);
126         } catch (Exception e) {
127             return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
128         }
129     }
130
131     @ApiOperation(value = "Delete a service")
132     @ApiResponses(
133         value = { //
134             @ApiResponse(code = 204, message = "Service deleted"),
135             @ApiResponse(code = 204, message = "Not used", response = VoidResponse.class),
136             @ApiResponse(code = 404, message = "Service not found", response = String.class)})
137     @DeleteMapping("/services")
138     public ResponseEntity<String> deleteService(//
139         @ApiParam(name = "name", required = true, value = "The name of the service") //
140         @RequestParam(name = "name", required = true) String serviceName) {
141         try {
142             Service service = removeService(serviceName);
143             // Remove the policies from the repo and let the consistency monitoring
144             // do the rest.
145             removePolicies(service);
146             return new ResponseEntity<>("OK", HttpStatus.NO_CONTENT);
147         } catch (Exception e) {
148             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
149         }
150     }
151
152     @ApiOperation(value = "Heartbeat from a serice")
153     @ApiResponses(
154         value = { //
155             @ApiResponse(code = 200, message = "Service supervision timer refreshed, OK"),
156             @ApiResponse(code = 404, message = "The service is not found, needs re-registration")})
157     @PutMapping("/services/keepalive")
158     public ResponseEntity<String> keepAliveService(//
159         @ApiParam(name = "name", required = true, value = "The name of the service") //
160         @RequestParam(name = "name", required = true) String serviceName) {
161         try {
162             services.getService(serviceName).keepAlive();
163             return new ResponseEntity<>("OK", HttpStatus.OK);
164         } catch (ServiceException e) {
165             return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
166         }
167     }
168
169     private Service removeService(String name) throws ServiceException {
170         Service service = this.services.getService(name); // Just to verify that it exists
171         this.services.remove(service.getName());
172         return service;
173     }
174
175     private void removePolicies(Service service) {
176         Collection<Policy> policyList = this.policies.getForService(service.getName());
177         for (Policy policy : policyList) {
178             this.policies.remove(policy);
179         }
180     }
181
182     private Service toService(ServiceRegistrationInfo s) {
183         return new Service(s.serviceName, Duration.ofSeconds(s.keepAliveIntervalSeconds), s.callbackUrl);
184     }
185
186 }