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.IOException;
24 import java.util.Optional;
25 import org.openecomp.mso.client.dmaap.DmaapConsumer;
26 import org.openecomp.mso.client.exceptions.SDNOException;
27 import org.openecomp.mso.jsonpath.JsonPathUtil;
29 public class SDNOHealthCheckDmaapConsumer extends DmaapConsumer {
31 private final String uuid;
32 private boolean continuePolling = true;
33 private final static String healthDiagnosticPath = "body.output.*";
35 public SDNOHealthCheckDmaapConsumer() throws IOException {
39 public SDNOHealthCheckDmaapConsumer(String uuid) throws IOException {
45 public String getUserName() {
46 return msoProperties.get("sdno.health-check.dmaap.username");
50 public String getPassword() {
51 return msoProperties.get("sdno.health-check.dmaap.password");
55 public String getTopic() {
56 return msoProperties.get("sdno.health-check.dmaap.subscriber.topic");
60 public Optional<String> getHost() {
61 return Optional.ofNullable(msoProperties.get("sdno.health-check.dmaap.subscriber.host"));
65 public boolean continuePolling() {
66 return continuePolling;
70 public void stopProcessingMessages() {
71 continuePolling = false;
74 public void processMessage(String message) throws Exception {
75 if (isHealthDiagnostic(message, this.getRequestId())) {
76 if (!healthDiagnosticSuccessful(message)) {
77 Optional<String> statusMessage = this.getStatusMessage(message);
78 if (statusMessage.isPresent()) {
79 throw new SDNOException("failed with message " + statusMessage.get());
81 throw new SDNOException("failed with no status message");
84 auditLogger.info("successful health diagnostic found for request: " + this.getRequestId());
85 stopProcessingMessages();
91 public boolean isAccepted(String message) {
92 if (isResultInfo(message)) {
93 Optional<String> code = isAccepted(message, this.getRequestId());
94 if (code.isPresent()) {
95 if ("202".equals(code.get())) {
98 //TODO check other statuses 400 and 500
109 public boolean isFailure(String message) {
110 if (isResultInfo(message)) {
111 Optional<String> code = isFailure(message, this.getRequestId());
112 if (code.isPresent()) {
113 if ("500".equals(code.get())) {
116 //TODO check other statuses 400 and 500
127 public String getRequestId() {
131 protected Optional<String> isAccepted(String json, String uuid) {
132 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='ACCEPTED' && @.request-id=='%s')].code", uuid));
135 protected Optional<String> isFailure(String json, String uuid) {
136 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='FAILURE' && @.request-id=='%s')].code", uuid));
139 protected boolean isResultInfo(String json) {
140 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
143 protected boolean isHealthDiagnostic(String json, String uuid) {
144 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
147 protected boolean healthDiagnosticSuccessful(String json) {
148 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
151 protected Optional<String> getStatusMessage(String json) {
152 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
156 public int getMaximumElapsedTime() {