2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.client.sdno.dmaap;
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.Optional;
27 import org.openecomp.mso.client.dmaap.DmaapConsumer;
28 import org.openecomp.mso.client.exceptions.SDNOException;
29 import org.openecomp.mso.jsonpath.JsonPathUtil;
31 public class SDNOHealthCheckDmaapConsumer extends DmaapConsumer {
33 private final String uuid;
34 private boolean continuePolling = true;
35 private final static String healthDiagnosticPath = "body.output.*";
37 public SDNOHealthCheckDmaapConsumer() throws FileNotFoundException, IOException {
41 public SDNOHealthCheckDmaapConsumer(String uuid) throws FileNotFoundException, IOException {
47 public String getUserName() {
48 return msoProperties.get("sdno.health-check.dmaap.username");
52 public String getPassword() {
53 return msoProperties.get("sdno.health-check.dmaap.password");
57 public String getTopic() {
58 return msoProperties.get("sdno.health-check.dmaap.subscriber.topic");
62 public Optional<String> getHost() {
63 return Optional.ofNullable(msoProperties.get("sdno.health-check.dmaap.subscriber.host"));
67 public boolean continuePolling() {
68 return continuePolling;
72 public void stopProcessingMessages() {
73 continuePolling = false;
76 public void processMessage(String message) throws Exception {
77 if (isHealthDiagnostic(message, this.getRequestId())) {
78 if (!healthDiagnosticSuccessful(message)) {
79 Optional<String> statusMessage = this.getStatusMessage(message);
80 if (statusMessage.isPresent()) {
81 throw new SDNOException("failed with message " + statusMessage.get());
83 throw new SDNOException("failed with no status message");
86 auditLogger.info("successful health diagnostic found for request: " + this.getRequestId());
87 stopProcessingMessages();
93 public boolean isAccepted(String message) {
94 if (isResultInfo(message)) {
95 Optional<String> code = isAccepted(message, this.getRequestId());
96 if (code.isPresent()) {
97 if ("202".equals(code.get())) {
100 //TODO check other statuses 400 and 500
111 public boolean isFailure(String message) {
112 if (isResultInfo(message)) {
113 Optional<String> code = isFailure(message, this.getRequestId());
114 if (code.isPresent()) {
115 if ("500".equals(code.get())) {
118 //TODO check other statuses 400 and 500
129 public String getRequestId() {
133 protected Optional<String> isAccepted(String json, String uuid) {
134 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='ACCEPTED' && @.request-id=='%s')].code", uuid));
137 protected Optional<String> isFailure(String json, String uuid) {
138 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='FAILURE' && @.request-id=='%s')].code", uuid));
141 protected boolean isResultInfo(String json) {
142 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
145 protected boolean isHealthDiagnostic(String json, String uuid) {
146 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
149 protected boolean healthDiagnosticSuccessful(String json) {
150 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
153 protected Optional<String> getStatusMessage(String json) {
154 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
158 public int getMaximumElapsedTime() {