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 boolean continuePolling() {
63 return continuePolling;
67 public void stopProcessingMessages() {
68 continuePolling = false;
71 public void processMessage(String message) throws Exception {
72 if (isHealthDiagnostic(message, this.getRequestId())) {
73 if (!healthDiagnosticSuccessful(message)) {
74 Optional<String> statusMessage = this.getStatusMessage(message);
75 if (statusMessage.isPresent()) {
76 throw new SDNOException("failed with message " + statusMessage.get());
78 throw new SDNOException("failed with no status message");
81 auditLogger.info("successful health diagnostic found for request: " + this.getRequestId());
82 stopProcessingMessages();
88 public boolean isAccepted(String message) {
89 if (isResultInfo(message)) {
90 Optional<String> code = isAccepted(message, this.getRequestId());
91 if (code.isPresent()) {
92 if ("202".equals(code.get())) {
95 //TODO check other statuses 400 and 500
106 public boolean isFailure(String message) {
107 if (isResultInfo(message)) {
108 Optional<String> code = isFailure(message, this.getRequestId());
109 if (code.isPresent()) {
110 if ("500".equals(code.get())) {
113 //TODO check other statuses 400 and 500
124 public String getRequestId() {
128 protected Optional<String> isAccepted(String json, String uuid) {
129 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='ACCEPTED' && @.request-id=='%s')].code", uuid));
132 protected Optional<String> isFailure(String json, String uuid) {
133 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='FAILURE' && @.request-id=='%s')].code", uuid));
136 protected boolean isResultInfo(String json) {
137 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
140 protected boolean isHealthDiagnostic(String json, String uuid) {
141 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
144 protected boolean healthDiagnosticSuccessful(String json) {
145 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
148 protected Optional<String> getStatusMessage(String json) {
149 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
153 public int getMaximumElapsedTime() {