Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / sdno / dmaap / SDNOHealthCheckDmaapConsumer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.sdno.dmaap;
22
23 import java.io.IOException;
24 import java.util.Optional;
25 import org.onap.so.client.dmaap.DmaapConsumer;
26 import org.onap.so.client.exceptions.SDNOException;
27 import org.onap.so.jsonpath.JsonPathUtil;
28
29 public class SDNOHealthCheckDmaapConsumer extends DmaapConsumer {
30
31     private final String uuid;
32     private boolean continuePolling = true;
33     private final static String healthDiagnosticPath = "body.output.*";
34
35     public SDNOHealthCheckDmaapConsumer() throws IOException {
36         this("none");
37     }
38
39     public SDNOHealthCheckDmaapConsumer(String uuid) throws IOException {
40         super();
41         this.uuid = uuid;
42     }
43
44     @Override
45     public String getAuth() {
46         return msoProperties.get("sdno.health-check.dmaap.auth");
47     }
48
49     @Override
50     public String getKey() {
51         return msoProperties.get("mso.msoKey");
52     }
53
54     @Override
55     public String getTopic() {
56         return msoProperties.get("sdno.health-check.dmaap.subscriber.topic");
57     }
58
59     @Override
60     public Optional<String> getHost() {
61         return Optional.ofNullable(msoProperties.get("sdno.health-check.dmaap.subscriber.host"));
62     }
63
64     @Override
65     public boolean continuePolling() {
66         return continuePolling;
67     }
68
69     @Override
70     public void stopProcessingMessages() {
71         continuePolling = false;
72     }
73
74     @Override
75     public void processMessage(String message) throws Exception {
76         if (isHealthDiagnostic(message, this.getRequestId())) {
77             if (!healthDiagnosticSuccessful(message)) {
78                 Optional<String> statusMessage = this.getStatusMessage(message);
79                 if (statusMessage.isPresent()) {
80                     throw new SDNOException("failed with message " + statusMessage.get());
81                 } else {
82                     throw new SDNOException("failed with no status message");
83                 }
84             } else {
85                 logger.info("successful health diagnostic found for request: {}", getRequestId());
86                 stopProcessingMessages();
87             }
88         }
89     }
90
91     @Override
92     public boolean isAccepted(String message) {
93         if (isResultInfo(message)) {
94             Optional<String> code = isAccepted(message, this.getRequestId());
95             if (code.isPresent()) {
96                 if ("202".equals(code.get())) {
97                     return true;
98                 } else {
99                     // TODO check other statuses 400 and 500
100                 }
101             } else {
102                 // TODO throw error
103             }
104         }
105
106         return false;
107     }
108
109     @Override
110     public boolean isFailure(String message) {
111         if (isResultInfo(message)) {
112             Optional<String> code = isFailure(message, this.getRequestId());
113             if (code.isPresent()) {
114                 if ("500".equals(code.get())) {
115                     return true;
116                 } else {
117                     // TODO check other statuses 400 and 500
118                 }
119             } else {
120                 // TODO throw error
121             }
122         }
123
124         return false;
125     }
126
127     @Override
128     public String getRequestId() {
129         return uuid;
130     }
131
132     protected Optional<String> isAccepted(String json, String uuid) {
133         return JsonPathUtil.getInstance().locateResult(json,
134                 String.format("$.result-info[?(@.status=='ACCEPTED' && @.request-id=='%s')].code", uuid));
135     }
136
137     protected Optional<String> isFailure(String json, String uuid) {
138         return JsonPathUtil.getInstance().locateResult(json,
139                 String.format("$.result-info[?(@.status=='FAILURE' && @.request-id=='%s')].code", uuid));
140     }
141
142     protected boolean isResultInfo(String json) {
143         return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
144     }
145
146     protected boolean isHealthDiagnostic(String json, String uuid) {
147         return JsonPathUtil.getInstance().pathExists(json,
148                 String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
149     }
150
151     protected boolean healthDiagnosticSuccessful(String json) {
152         return JsonPathUtil.getInstance().pathExists(json,
153                 "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
154     }
155
156     protected Optional<String> getStatusMessage(String json) {
157         return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
158     }
159
160     @Override
161     public int getMaximumElapsedTime() {
162         return 600000;
163     }
164 }