diff options
author | Steve Smokowski <ss835w@att.com> | 2019-08-12 13:49:13 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2019-08-12 13:49:13 +0000 |
commit | 7ff9326295fe389e5cfeb332291ec7e46da2f20e (patch) | |
tree | fa5a90b6eebedd75c6f4358ea150ca929ef824bc /mso-api-handlers/mso-api-handler-infra | |
parent | 6387c2174b5fe53dda56ebaf7b51b4ad6e8239a4 (diff) | |
parent | 2a6e3dca63c0cb67db3b65b0e5bf6085b5458e9b (diff) |
Merge "health check now entirely config based"
Diffstat (limited to 'mso-api-handlers/mso-api-handler-infra')
13 files changed, 459 insertions, 338 deletions
diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GenericStringConverter.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GenericStringConverter.java new file mode 100644 index 0000000000..80144d8ca1 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GenericStringConverter.java @@ -0,0 +1,33 @@ +package org.onap.so.apihandlerinfra; + +import java.net.URI; +import java.util.Set; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.ConfigurationPropertiesBinding; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.core.convert.converter.GenericConverter; +import org.springframework.stereotype.Component; +import com.google.common.collect.ImmutableSet; + +@Component +@ConfigurationPropertiesBinding +public class GenericStringConverter implements GenericConverter { + + @Autowired + private HealthCheckConverter converter; + + @Override + public Set<ConvertiblePair> getConvertibleTypes() { + + ConvertiblePair[] pairs = new ConvertiblePair[] {new ConvertiblePair(String.class, Subsystem.class), + new ConvertiblePair(String.class, URI.class)}; + return ImmutableSet.copyOf(pairs); + } + + @Override + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + + return converter.convert(source, sourceType, targetType); + + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandler.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandler.java index 3d4b2c76fb..0379ae3578 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandler.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandler.java @@ -25,11 +25,8 @@ package org.onap.so.apihandlerinfra; import java.net.URI; import java.util.Collections; -import org.onap.so.logger.LoggingAnchor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; +import java.util.List; +import java.util.stream.Collectors; import javax.annotation.PostConstruct; import javax.transaction.Transactional; import javax.ws.rs.DefaultValue; @@ -42,14 +39,20 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import org.apache.http.HttpStatus; +import org.onap.so.apihandlerinfra.HealthCheckConfig.Endpoint; +import org.onap.so.logger.LoggingAnchor; import org.onap.so.logger.MessageEnum; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import org.springframework.http.HttpMethod; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -59,45 +62,38 @@ import io.swagger.annotations.ApiOperation; @Api(value = "/globalhealthcheck", description = "APIH Infra Global Health Check") public class GlobalHealthcheckHandler { private static Logger logger = LoggerFactory.getLogger(GlobalHealthcheckHandler.class); - private static final String CONTEXTPATH_PROPERTY = "management.context-path"; - private static final String PROPERTY_DOMAIN = "mso.health.endpoints"; - private static final String CATALOGDB_PROPERTY = PROPERTY_DOMAIN + ".catalogdb"; - private static final String REQUESTDB_PROPERTY = PROPERTY_DOMAIN + ".requestdb"; - private static final String SDNC_PROPERTY = PROPERTY_DOMAIN + ".sdnc"; - private static final String OPENSTACK_PROPERTY = PROPERTY_DOMAIN + ".openstack"; - private static final String BPMN_PROPERTY = PROPERTY_DOMAIN + ".bpmn"; - private static final String ASDC_PROPERTY = PROPERTY_DOMAIN + ".asdc"; - private static final String REQUESTDBATTSVC_PROPERTY = PROPERTY_DOMAIN + ".requestdbattsvc"; - private static final String DEFAULT_PROPERTY_VALUE = ""; + protected static final String CONTEXTPATH_PROPERTY = "management.endpoints.web.base-path"; + protected static final String PROPERTY_DOMAIN = "mso.health"; + protected static final String CATALOGDB_PROPERTY = PROPERTY_DOMAIN + ".endpoints.catalogdb"; + protected static final String REQUESTDB_PROPERTY = PROPERTY_DOMAIN + ".endpoints.requestdb"; + protected static final String SDNC_PROPERTY = PROPERTY_DOMAIN + ".endpoints.sdnc"; + protected static final String OPENSTACK_PROPERTY = PROPERTY_DOMAIN + ".endpoints.openstack"; + protected static final String BPMN_PROPERTY = PROPERTY_DOMAIN + ".endpoints.bpmn"; + protected static final String ASDC_PROPERTY = PROPERTY_DOMAIN + ".endpoints.asdc"; + protected static final String REQUESTDBATTSVC_PROPERTY = PROPERTY_DOMAIN + ".endpoints.requestdbattsvc"; + protected static final String MSO_AUTH_PROPERTY = PROPERTY_DOMAIN + ".auth"; + protected static final String DEFAULT_PROPERTY_VALUE = ""; // e.g. /manage private String actuatorContextPath; - private String endpointCatalogdb; - private String endpointRequestdb; - private String endpointSdnc; - private String endpointOpenstack; - private String endpointBpmn; - private String endpointAsdc; - private String endpointRequestdbAttsvc; @Autowired private Environment env; @Autowired private RestTemplate restTemplate; - private final String health = "/health"; + @Autowired + private HealthCheckConfig config; + + private static final String HEALTH = "/health"; + + private String msoAuth; @PostConstruct protected void init() { actuatorContextPath = env.getProperty(CONTEXTPATH_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointCatalogdb = env.getProperty(CATALOGDB_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointRequestdb = env.getProperty(REQUESTDB_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointSdnc = env.getProperty(SDNC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointOpenstack = env.getProperty(OPENSTACK_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointBpmn = env.getProperty(BPMN_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointAsdc = env.getProperty(ASDC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); - endpointRequestdbAttsvc = env.getProperty(REQUESTDBATTSVC_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); + msoAuth = env.getProperty(MSO_AUTH_PROPERTY, String.class, DEFAULT_PROPERTY_VALUE); } @GET @@ -108,29 +104,25 @@ public class GlobalHealthcheckHandler { @Context ContainerRequestContext requestContext) { Response HEALTH_CHECK_RESPONSE = null; // Build internal response object - HealthcheckResponse rsp = new HealthcheckResponse(); + HealthCheckResponse rsp = new HealthCheckResponse(); try { // Generated RequestId String requestId = requestContext.getProperty("requestId").toString(); logger.info(LoggingAnchor.TWO, MessageEnum.APIH_GENERATED_REQUEST_ID.toString(), requestId); - // set APIH status, this is the main entry point - rsp.setApih(HealthcheckStatus.UP.toString()); - // set BPMN - rsp.setBpmn(querySubsystemHealth(MsoSubsystems.BPMN)); - // set SDNCAdapter - rsp.setSdncAdapter(querySubsystemHealth(MsoSubsystems.SDNC)); - // set ASDCController - rsp.setAsdcController(querySubsystemHealth(MsoSubsystems.ASDC)); - // set CatalogDbAdapter - rsp.setCatalogdbAdapter(querySubsystemHealth(MsoSubsystems.CATALOGDB)); - // set RequestDbAdapter - rsp.setRequestdbAdapter(querySubsystemHealth(MsoSubsystems.REQUESTDB)); - // set OpenStackAdapter - rsp.setOpenstackAdapter(querySubsystemHealth(MsoSubsystems.OPENSTACK)); - // set RequestDbAdapterAttSvc - rsp.setRequestdbAdapterAttsvc(querySubsystemHealth(MsoSubsystems.REQUESTDBATT)); + List<Endpoint> endpoints = config.getEndpoints().stream().filter(item -> { + if (!enableBpmn && SoSubsystems.BPMN.equals(item.getSubsystem())) { + return false; + } else { + return true; + } + }).collect(Collectors.toList()); + + for (Endpoint endpoint : endpoints) { + rsp.getSubsystems().add(querySubsystemHealth(endpoint)); + } + // set Message rsp.setMessage(String.format("HttpStatus: %s", HttpStatus.SC_OK)); logger.info(rsp.toString()); @@ -149,70 +141,51 @@ public class GlobalHealthcheckHandler { protected HttpEntity<String> buildHttpEntityForRequest() { HttpHeaders headers = new HttpHeaders(); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); - headers.set("Content-Type", "application/json"); + headers.set(HttpHeaders.CONTENT_TYPE, "application/json"); + headers.set(HttpHeaders.AUTHORIZATION, msoAuth); HttpEntity<String> entity = new HttpEntity<>("parameters", headers); return entity; } - protected String querySubsystemHealth(MsoSubsystems subsystem) { + protected HealthCheckSubsystem querySubsystemHealth(Endpoint subsystem) { + HealthCheckStatus status = HealthCheckStatus.DOWN; + URI uri = subsystem.getUri(); try { // get port number for the subsystem - String ept = getEndpointUrlForSubsystemEnum(subsystem); - // build final endpoint url - UriBuilder builder = UriBuilder.fromPath(ept).path(actuatorContextPath).path(health); - URI uri = builder.build(); - logger.info("Calculated URL: {}", uri.toString()); + uri = UriBuilder.fromUri(subsystem.getUri()).path(actuatorContextPath).path(HEALTH).build(); + logger.info("Calculated URL: {}", uri); ResponseEntity<SubsystemHealthcheckResponse> result = restTemplate.exchange(uri, HttpMethod.GET, buildHttpEntityForRequest(), SubsystemHealthcheckResponse.class); - return processResponseFromSubsystem(result, subsystem); + status = processResponseFromSubsystem(result, subsystem); + } catch (Exception ex) { logger.error("Exception occured in GlobalHealthcheckHandler.querySubsystemHealth() ", ex); - return HealthcheckStatus.DOWN.toString(); } + + return new HealthCheckSubsystem(subsystem.getSubsystem(), uri, status); } - protected String processResponseFromSubsystem(ResponseEntity<SubsystemHealthcheckResponse> result, - MsoSubsystems subsystem) { + protected HealthCheckStatus processResponseFromSubsystem(ResponseEntity<SubsystemHealthcheckResponse> result, + Endpoint endpoint) { if (result == null || result.getStatusCodeValue() != HttpStatus.SC_OK) { logger.error(String.format("Globalhealthcheck: checking subsystem: %s failed ! result object is: %s", - subsystem, result == null ? "NULL" : result)); - return HealthcheckStatus.DOWN.toString(); + endpoint.getSubsystem(), result == null ? "NULL" : result)); + return HealthCheckStatus.DOWN; } SubsystemHealthcheckResponse body = result.getBody(); String status = body.getStatus(); if ("UP".equalsIgnoreCase(status)) { - return HealthcheckStatus.UP.toString(); + return HealthCheckStatus.UP; } else { - logger.error("{}, query health endpoint did not return UP status!", subsystem); - return HealthcheckStatus.DOWN.toString(); + logger.error("{}, query health endpoint did not return UP status!", endpoint.getSubsystem()); + return HealthCheckStatus.DOWN; } } - - protected String getEndpointUrlForSubsystemEnum(MsoSubsystems subsystem) { - switch (subsystem) { - case SDNC: - return this.endpointSdnc; - case ASDC: - return this.endpointAsdc; - case BPMN: - return this.endpointBpmn; - case CATALOGDB: - return this.endpointCatalogdb; - case OPENSTACK: - return this.endpointOpenstack; - case REQUESTDB: - return this.endpointRequestdb; - case REQUESTDBATT: - return this.endpointRequestdbAttsvc; - default: - return ""; - } - } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheck.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheck.java new file mode 100644 index 0000000000..1899cdd765 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheck.java @@ -0,0 +1,84 @@ +package org.onap.so.apihandlerinfra; + +import java.net.URI; +import javax.ws.rs.core.UriBuilder; +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "mso.health.enpoints") +public class HealthCheck { + + private Subsystem subsystem; + private URI uri; + private HealthCheckStatus status = HealthCheckStatus.DOWN; + + public HealthCheck() { + + } + + public HealthCheck(String subsystem, String uri) { + this.subsystem = SoSubsystems.valueOf(subsystem.toUpperCase()); + this.uri = UriBuilder.fromUri(uri).build(); + } + + public HealthCheck(Subsystem subsystem, URI uri) { + this.subsystem = subsystem; + this.uri = uri; + } + + public HealthCheck(Subsystem subsystem, URI uri, HealthCheckStatus status) { + this.subsystem = subsystem; + this.uri = uri; + this.status = status; + } + + public Subsystem getSubsystem() { + return subsystem; + } + + public void setSubsystem(Subsystem component) { + this.subsystem = component; + } + + public URI getUri() { + return uri; + } + + public void setUri(URI uri) { + this.uri = uri; + } + + public HealthCheckStatus getStatus() { + return status; + } + + public void setStatus(HealthCheckStatus status) { + this.status = status; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("subsystem", subsystem).append("uri", uri).append("status", status) + .toString(); + } + + @Override + public boolean equals(final Object other) { + if (!(other instanceof HealthCheck)) { + return false; + } + HealthCheck castOther = (HealthCheck) other; + return new EqualsBuilder().append(subsystem, castOther.subsystem).append(uri, castOther.uri) + .append(status, castOther.status).isEquals(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(subsystem).append(uri).append(status).toHashCode(); + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConfig.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConfig.java new file mode 100644 index 0000000000..11fd94bc91 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConfig.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.apihandlerinfra; + +import java.net.URI; +import java.util.List; +import javax.validation.constraints.NotNull; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.validation.annotation.Validated; + +@Configuration +@ConfigurationProperties(prefix = "mso.health") +@Validated +public class HealthCheckConfig { + + @NotNull + private List<Endpoint> endpoints; + + public List<Endpoint> getEndpoints() { + return endpoints; + } + + public void setEndpoints(List<Endpoint> endpoints) { + this.endpoints = endpoints; + } + + @Override + public String toString() { + return new ToStringBuilder(this).append("endpoints", this.endpoints).toString(); + } + + @Validated + public static class Endpoint { + @NotNull + private Subsystem subsystem; + @NotNull + private URI uri; + + public Endpoint() { + + } + + public Endpoint(Subsystem subsystem, URI uri) { + this.subsystem = subsystem; + this.uri = uri; + } + + public Subsystem getSubsystem() { + return subsystem; + } + + public void setSubsystem(Subsystem subsystem) { + this.subsystem = subsystem; + } + + public URI getUri() { + return uri; + } + + public void setUri(URI uri) { + this.uri = uri; + } + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConverter.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConverter.java new file mode 100644 index 0000000000..ed06018e7b --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckConverter.java @@ -0,0 +1,22 @@ +package org.onap.so.apihandlerinfra; + +import java.net.URI; +import javax.ws.rs.core.UriBuilder; +import org.springframework.core.convert.TypeDescriptor; +import org.springframework.stereotype.Component; + +@Component +public class HealthCheckConverter { + + + public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { + if (sourceType.getType() == String.class && targetType.getType() == Subsystem.class) { + return SoSubsystems.valueOf(((String) source).toUpperCase()); + } else if (sourceType.getType() == String.class && targetType.getType() == URI.class) { + return UriBuilder.fromUri((String) source).build(); + } else { + return source; + } + } + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckResponse.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckResponse.java new file mode 100644 index 0000000000..5400249c65 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckResponse.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.apihandlerinfra; + +import java.util.ArrayList; +import java.util.List; + +public class HealthCheckResponse { + + + private List<HealthCheckSubsystem> subsystems = new ArrayList<>(); + private String message; + + + public List<HealthCheckSubsystem> getSubsystems() { + return subsystems; + } + + public void setSubsystems(List<HealthCheckSubsystem> subsystems) { + this.subsystems = subsystems; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthcheckStatus.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckStatus.java index 077a3c2d60..6b31c1f1ed 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthcheckStatus.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckStatus.java @@ -19,12 +19,12 @@ */ package org.onap.so.apihandlerinfra; -public enum HealthcheckStatus { +public enum HealthCheckStatus { UP("UP"), DOWN("DOWN"); private String status; - private HealthcheckStatus(String status) { + private HealthCheckStatus(String status) { this.status = status; } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckSubsystem.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckSubsystem.java new file mode 100644 index 0000000000..e1335b952c --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthCheckSubsystem.java @@ -0,0 +1,40 @@ +package org.onap.so.apihandlerinfra; + +import java.net.URI; + +public class HealthCheckSubsystem { + + private Subsystem subsystem; + private URI uri; + private HealthCheckStatus status; + + public HealthCheckSubsystem(Subsystem subsystem, URI uri, HealthCheckStatus status) { + this.subsystem = subsystem; + this.uri = uri; + this.status = status; + } + + public Subsystem getSubsystem() { + return subsystem; + } + + public void setSubsystem(Subsystem subsystem) { + this.subsystem = subsystem; + } + + public URI getUri() { + return uri; + } + + public void setUri(URI uri) { + this.uri = uri; + } + + public HealthCheckStatus getStatus() { + return status; + } + + public void setStatus(HealthCheckStatus status) { + this.status = status; + } +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthcheckResponse.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthcheckResponse.java deleted file mode 100644 index fad3dd4055..0000000000 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/HealthcheckResponse.java +++ /dev/null @@ -1,116 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ -package org.onap.so.apihandlerinfra; - -import org.apache.commons.lang3.builder.ToStringBuilder; - -public class HealthcheckResponse { - private String apih; - private String bpmn; - private String sdncAdapter; - private String asdcController; - private String catalogdbAdapter; - private String requestdbAdapter; - private String openstackAdapter; - private String requestdbAdapterAttsvc; - private String message = ""; - - public String getApih() { - return apih; - } - - public void setApih(String apih) { - this.apih = apih; - } - - public String getBpmn() { - return bpmn; - } - - public void setBpmn(String bpmn) { - this.bpmn = bpmn; - } - - public String getSdncAdapter() { - return sdncAdapter; - } - - public void setSdncAdapter(String sdncAdapter) { - this.sdncAdapter = sdncAdapter; - } - - public String getAsdcController() { - return asdcController; - } - - public void setAsdcController(String asdcController) { - this.asdcController = asdcController; - } - - public String getCatalogdbAdapter() { - return catalogdbAdapter; - } - - public void setCatalogdbAdapter(String catalogdbAdapter) { - this.catalogdbAdapter = catalogdbAdapter; - } - - public String getRequestdbAdapter() { - return requestdbAdapter; - } - - public void setRequestdbAdapter(String requestdbAdapter) { - this.requestdbAdapter = requestdbAdapter; - } - - public String getOpenstackAdapter() { - return openstackAdapter; - } - - public void setOpenstackAdapter(String openstackAdapter) { - this.openstackAdapter = openstackAdapter; - } - - public String getRequestdbAdapterAttsvc() { - return requestdbAdapterAttsvc; - } - - public void setRequestdbAdapterAttsvc(String requestdbAdapterAttsvc) { - this.requestdbAdapterAttsvc = requestdbAdapterAttsvc; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - @Override - public String toString() { - return new ToStringBuilder(this).append("apih", this.apih).append("pbmn", this.bpmn) - .append("sdncAdapter", this.sdncAdapter).append("asdcController", this.asdcController) - .append("catalogdbAdapter", this.catalogdbAdapter).append("requestdbAdapter", this.requestdbAdapter) - .append("openstackAdapter", this.openstackAdapter) - .append("requestdbAdapterAttsvc", this.requestdbAdapterAttsvc).append("message", this.message) - .toString(); - } -} diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/MsoSubsystems.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/SoSubsystems.java index 13f1e52068..5842531dc3 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/MsoSubsystems.java +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/SoSubsystems.java @@ -19,18 +19,18 @@ */ package org.onap.so.apihandlerinfra; -public enum MsoSubsystems { +public enum SoSubsystems implements Subsystem { APIH("API Handler"), ASDC("ASDC Controller"), BPMN("BPMN Infra"), CATALOGDB("CatalogDb Adapter"), OPENSTACK("Openstack Adapter"), REQUESTDB("RequestDB Adapter"), - REQUESTDBATT("RequestDB Adapter ATT Svc"), SDNC("SDNC Adapter"); + private String subsystem; - private MsoSubsystems(String subsystem) { + private SoSubsystems(String subsystem) { this.subsystem = subsystem; } diff --git a/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/Subsystem.java b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/Subsystem.java new file mode 100644 index 0000000000..88626f3168 --- /dev/null +++ b/mso-api-handlers/mso-api-handler-infra/src/main/java/org/onap/so/apihandlerinfra/Subsystem.java @@ -0,0 +1,5 @@ +package org.onap.so.apihandlerinfra; + +public interface Subsystem { + +} diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandlerTest.java b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandlerTest.java index 928b488f6a..0291cfd2ea 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandlerTest.java +++ b/mso-api-handlers/mso-api-handler-infra/src/test/java/org/onap/so/apihandlerinfra/GlobalHealthcheckHandlerTest.java @@ -20,70 +20,67 @@ package org.onap.so.apihandlerinfra; -import static org.junit.Assert.assertArrayEquals; +import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyObject; +import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.anyString; import java.net.URI; -import java.util.Collections; -import java.util.List; -import org.springframework.test.util.ReflectionTestUtils; import javax.ws.rs.container.ContainerRequestContext; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; -import org.springframework.core.ParameterizedTypeReference; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; import org.json.JSONException; -import org.junit.Rule; import org.junit.Test; -import org.mockito.InjectMocks; +import org.junit.runner.RunWith; import org.mockito.ArgumentMatchers; -import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.Spy; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; +import org.onap.so.apihandlerinfra.HealthCheckConfig.Endpoint; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.RestTemplate; +import com.fasterxml.jackson.core.JsonProcessingException; +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = {GenericStringConverter.class, HealthCheckConverter.class}, + initializers = {ConfigFileApplicationContextInitializer.class}) +@ActiveProfiles("test") +@EnableConfigurationProperties({HealthCheckConfig.class}) public class GlobalHealthcheckHandlerTest { - @Mock - RestTemplate restTemplate; + @MockBean + private RestTemplate restTemplate; - @Mock - ContainerRequestContext requestContext; + @MockBean + private ContainerRequestContext requestContext; - @InjectMocks - @Spy - GlobalHealthcheckHandler globalhealth; - - @Rule - public MockitoRule mockitoRule = MockitoJUnit.rule(); + @SpyBean + private GlobalHealthcheckHandler globalhealth; @Test public void testQuerySubsystemHealthNullResult() { ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage"); - ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8080"); Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(null); - String result = globalhealth.querySubsystemHealth(MsoSubsystems.BPMN); - System.out.println(result); - assertEquals(HealthcheckStatus.DOWN.toString(), result); + HealthCheckSubsystem result = globalhealth + .querySubsystemHealth(new Endpoint(SoSubsystems.BPMN, UriBuilder.fromPath("http://localhost").build())); + assertEquals(HealthCheckStatus.DOWN, result.getStatus()); } @Test public void testQuerySubsystemHealthNotNullResult() { ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage"); - ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080"); SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse(); subSystemResponse.setStatus("UP"); @@ -92,20 +89,13 @@ public class GlobalHealthcheckHandlerTest { Mockito.when(restTemplate.exchange(ArgumentMatchers.any(URI.class), ArgumentMatchers.any(HttpMethod.class), ArgumentMatchers.<HttpEntity<?>>any(), ArgumentMatchers.<Class<Object>>any())).thenReturn(r); - String result = globalhealth.querySubsystemHealth(MsoSubsystems.ASDC); - System.out.println(result); - assertEquals(HealthcheckStatus.UP.toString(), result); + HealthCheckSubsystem result = globalhealth + .querySubsystemHealth(new Endpoint(SoSubsystems.ASDC, UriBuilder.fromPath("http://localhost").build())); + assertEquals(HealthCheckStatus.UP, result.getStatus()); } private Response globalHealthcheck(String status) { ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage"); - ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080"); - ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081"); - ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082"); - ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083"); - ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084"); - ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085"); - ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086"); SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse(); @@ -116,70 +106,41 @@ public class GlobalHealthcheckHandlerTest { Mockito.when(requestContext.getProperty(anyString())).thenReturn("1234567890"); Response response = globalhealth.globalHealthcheck(true, requestContext); - return response; } @Test - public void globalHealthcheckAllUPTest() throws JSONException { - Response response = globalHealthcheck("UP"); - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - HealthcheckResponse root; - root = (HealthcheckResponse) response.getEntity(); - String apistatus = root.getApih(); - assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); - - String bpmnstatus = root.getBpmn(); - assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); - - String sdncstatus = root.getSdncAdapter(); - assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); + public void globalHealthcheckAllUPTest() throws JSONException, JsonProcessingException { - String asdcstatus = root.getAsdcController(); - assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); + HealthCheckResponse expected = new HealthCheckResponse(); - String catastatus = root.getCatalogdbAdapter(); - assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); - - String reqdbstatus = root.getRequestdbAdapter(); - assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); - - String openstatus = root.getOpenstackAdapter(); - assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); + for (Subsystem system : SoSubsystems.values()) { + expected.getSubsystems().add(new HealthCheckSubsystem(system, + UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.UP)); + } + expected.setMessage("HttpStatus: 200"); + Response response = globalHealthcheck("UP"); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + HealthCheckResponse root; + root = (HealthCheckResponse) response.getEntity(); + assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem")); - String reqdbattstatus = root.getRequestdbAdapterAttsvc(); - assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); } @Test public void globalHealthcheckAllDOWNTest() throws JSONException { + HealthCheckResponse expected = new HealthCheckResponse(); + + for (Subsystem system : SoSubsystems.values()) { + expected.getSubsystems().add(new HealthCheckSubsystem(system, + UriBuilder.fromUri("http://localhost").build(), HealthCheckStatus.DOWN)); + } + expected.setMessage("HttpStatus: 200"); Response response = globalHealthcheck("DOWN"); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - HealthcheckResponse root; - root = (HealthcheckResponse) response.getEntity(); - String apistatus = root.getApih(); - assertTrue(apistatus.equalsIgnoreCase(HealthcheckStatus.UP.toString())); - - String bpmnstatus = root.getBpmn(); - assertTrue(bpmnstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String sdncstatus = root.getSdncAdapter(); - assertTrue(sdncstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String asdcstatus = root.getAsdcController(); - assertTrue(asdcstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String catastatus = root.getCatalogdbAdapter(); - assertTrue(catastatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String reqdbstatus = root.getRequestdbAdapter(); - assertTrue(reqdbstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String openstatus = root.getOpenstackAdapter(); - assertTrue(openstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); - - String reqdbattstatus = root.getRequestdbAdapterAttsvc(); - assertTrue(reqdbattstatus.equalsIgnoreCase(HealthcheckStatus.DOWN.toString())); + HealthCheckResponse root; + root = (HealthCheckResponse) response.getEntity(); + assertThat(root, sameBeanAs(expected).ignoring("subsystems.uri").ignoring("subsystems.subsystem")); } @Test @@ -189,40 +150,15 @@ public class GlobalHealthcheckHandlerTest { assertEquals(MediaType.APPLICATION_JSON, he.getHeaders().getContentType()); } - @Test - public void getEndpointUrlForSubsystemEnumTest() { - ReflectionTestUtils.setField(globalhealth, "actuatorContextPath", "/manage"); - ReflectionTestUtils.setField(globalhealth, "endpointAsdc", "http://localhost:8080"); - ReflectionTestUtils.setField(globalhealth, "endpointSdnc", "http://localhost:8081"); - ReflectionTestUtils.setField(globalhealth, "endpointBpmn", "http://localhost:8082"); - ReflectionTestUtils.setField(globalhealth, "endpointCatalogdb", "http://localhost:8083"); - ReflectionTestUtils.setField(globalhealth, "endpointOpenstack", "http://localhost:8084"); - ReflectionTestUtils.setField(globalhealth, "endpointRequestdb", "http://localhost:8085"); - ReflectionTestUtils.setField(globalhealth, "endpointRequestdbAttsvc", "http://localhost:8086"); - - String result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.ASDC); - assertEquals("http://localhost:8080", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.SDNC); - assertEquals("http://localhost:8081", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.BPMN); - assertEquals("http://localhost:8082", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.CATALOGDB); - assertEquals("http://localhost:8083", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.OPENSTACK); - assertEquals("http://localhost:8084", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDB); - assertEquals("http://localhost:8085", result); - result = globalhealth.getEndpointUrlForSubsystemEnum(MsoSubsystems.REQUESTDBATT); - assertEquals("http://localhost:8086", result); - } @Test public void processResponseFromSubsystemTest() { SubsystemHealthcheckResponse subSystemResponse = new SubsystemHealthcheckResponse(); subSystemResponse.setStatus("UP"); ResponseEntity<SubsystemHealthcheckResponse> r = new ResponseEntity<>(subSystemResponse, HttpStatus.OK); - String result = globalhealth.processResponseFromSubsystem(r, MsoSubsystems.BPMN); - assertEquals("UP", result); + Endpoint endpoint = new Endpoint(SoSubsystems.BPMN, UriBuilder.fromUri("http://localhost").build()); + HealthCheckStatus result = globalhealth.processResponseFromSubsystem(r, endpoint); + assertEquals(HealthCheckStatus.UP, result); } } diff --git a/mso-api-handlers/mso-api-handler-infra/src/test/resources/application-test.yaml b/mso-api-handlers/mso-api-handler-infra/src/test/resources/application-test.yaml index 2e1c6a9bdc..27e1ae90d2 100644 --- a/mso-api-handlers/mso-api-handler-infra/src/test/resources/application-test.yaml +++ b/mso-api-handlers/mso-api-handler-infra/src/test/resources/application-test.yaml @@ -9,14 +9,20 @@ server: mso: health: endpoints: - catalogdb: http://localhost:${wiremock.server.port} - requestdb: http://localhost:${wiremock.server.port} - sdnc: http://localhost:${wiremock.server.port} - openstack: http://localhost:${wiremock.server.port} - bpmn: http://localhost:${wiremock.server.port} - asdc: http://localhost:${wiremock.server.port} - requestdbattsvc: http://localhost:${wiremock.server.port} - + - subsystem: apih + uri: http://localhost:${wiremock.server.port} + - subsystem: asdc + uri: http://localhost:${wiremock.server.port} + - subsystem: bpmn + uri: http://localhost:${wiremock.server.port} + - subsystem: catalogdb + uri: http://localhost:${wiremock.server.port} + - subsystem: openstack + uri: http://localhost:${wiremock.server.port} + - subsystem: requestdb + uri: http://localhost:${wiremock.server.port} + - subsystem: sdnc + uri: http://localhost:${wiremock.server.port} infra-requests: archived: period: 180 @@ -118,7 +124,13 @@ mariaDB4j: port: 3307 databaseName: catalogdb databaseName2: requestdb - +#Actuator +management: + endpoints: + web: + base-path: /manage + exposure: + include: "*" org: onap: |