Bugfixes for December 2018
[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 getUserName() {
46                 return msoProperties.get("sdno.health-check.dmaap.username");
47         }
48
49         @Override
50         public String getPassword() {
51                 return msoProperties.get("sdno.health-check.dmaap.password");
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         @Override
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());
80                                 } else {
81                                         throw new SDNOException("failed with no status message");
82                                 }
83                         } else {
84                                 logger.info("successful health diagnostic found for request: {}" ,getRequestId());
85                                 stopProcessingMessages();
86                         }
87                 }
88         }
89         
90         @Override
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())) {
96                                         return true;
97                                 } else {
98                                         //TODO check other statuses 400 and 500
99                                 }
100                         } else {
101                                 //TODO throw error
102                         }
103                 }
104                 
105                 return false;
106         }
107         
108         @Override
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())) {
114                                         return true;
115                                 } else {
116                                         //TODO check other statuses 400 and 500
117                                 }
118                         } else {
119                                 //TODO throw error
120                         }
121                 }
122                 
123                 return false;
124         }
125         
126         @Override
127         public String getRequestId() {
128                 return uuid;
129         }
130         
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));
133         }
134         
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));
137         }
138         
139         protected boolean isResultInfo(String json) {
140                 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
141         }
142         
143         protected boolean isHealthDiagnostic(String json, String uuid) {
144                 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
145         }
146         
147         protected boolean healthDiagnosticSuccessful(String json) {
148                 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
149         }
150         
151         protected Optional<String> getStatusMessage(String json) {
152                 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
153         }
154         
155         @Override
156         public int getMaximumElapsedTime() {
157                 return 600000;
158         }
159 }