7235d2cbe9cb545b7c8a5cadbf4c95e1a4da6fd6
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / status / StatusResource.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.apis.status;
17
18 import org.onap.nbi.apis.status.model.ApplicationStatus;
19 import org.onap.nbi.apis.status.model.StatusType;
20 import org.onap.nbi.commons.JsonRepresentation;
21 import org.onap.nbi.commons.ResourceManagement;
22 import org.springframework.beans.factory.annotation.Autowired;
23 import org.springframework.http.HttpStatus;
24 import org.springframework.http.MediaType;
25 import org.springframework.http.ResponseEntity;
26 import org.springframework.web.bind.annotation.GetMapping;
27 import org.springframework.web.bind.annotation.RequestMapping;
28 import org.springframework.web.bind.annotation.RestController;
29 import javax.servlet.http.HttpServletRequest;
30
31
32 @RestController
33 @RequestMapping("/status")
34 public class StatusResource extends ResourceManagement<ApplicationStatus> {
35
36     @Autowired
37     private StatusService statusService;
38
39     private JsonRepresentation fullRepresentation = new JsonRepresentation().add("name").add("status").add("version")
40             .add("components.name").add("components.status");
41
42     @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
43     public ResponseEntity<Object> status(HttpServletRequest request) {
44
45         ResponseEntity<Object> responseEntity = null;
46
47         final String[] splitPath = request.getRequestURI().split("/");
48
49         final String applicationName = splitPath[1];
50         final String applicationVersion = splitPath[3];
51
52         final ApplicationStatus applicationStatus = this.statusService.get(applicationName, applicationVersion);
53
54         final boolean isServiceFullyFunctional =
55                 StatusType.OK.equals(applicationStatus.getStatus()) ? applicationStatus.getComponents().stream()
56                         .allMatch(componentStatus -> StatusType.OK.equals(componentStatus.getStatus())) : false;
57
58         // filter object
59         Object response = this.getEntity(applicationStatus, fullRepresentation);
60
61         if (isServiceFullyFunctional) {
62             responseEntity = ResponseEntity.ok(response);
63         } else {
64             responseEntity = ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
65         }
66
67         return responseEntity;
68     }
69
70 }