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