f23d882b53d8045a4f1575c27003728a36daa91d
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / 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.openecomp.mso.client.sdno.dmaap;
22
23 import java.io.FileNotFoundException;
24 import java.io.IOException;
25 import java.util.Optional;
26
27 import org.openecomp.mso.client.dmaap.DmaapConsumer;
28 import org.openecomp.mso.client.exceptions.SDNOException;
29 import org.openecomp.mso.jsonpath.JsonPathUtil;
30
31 public class SDNOHealthCheckDmaapConsumer extends DmaapConsumer {
32
33         private final String uuid;
34         private boolean continuePolling = true;
35         private final static String healthDiagnosticPath = "body.output.*";
36
37         public SDNOHealthCheckDmaapConsumer() throws FileNotFoundException, IOException {
38                 this("none");
39         }
40         
41         public SDNOHealthCheckDmaapConsumer(String uuid) throws FileNotFoundException, IOException {
42                 super();
43                 this.uuid = uuid;
44         }
45         
46         @Override
47         public String getUserName() {
48                 return msoProperties.get("sdno.health-check.dmaap.username");
49         }
50
51         @Override
52         public String getPassword() {
53                 return msoProperties.get("sdno.health-check.dmaap.password");
54         }
55
56         @Override
57         public String getTopic() {
58                 return msoProperties.get("sdno.health-check.dmaap.subscriber.topic");
59         }
60
61         @Override
62         public boolean continuePolling() {
63                 return continuePolling;
64         }
65         
66         @Override
67         public void stopProcessingMessages() {
68                 continuePolling = false;
69         }
70         @Override
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());
77                                 } else {
78                                         throw new SDNOException("failed with no status message");
79                                 }
80                         } else {
81                                 auditLogger.info("successful health diagnostic found for request: " + this.getRequestId());
82                                 stopProcessingMessages();
83                         }
84                 }
85         }
86         
87         @Override
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())) {
93                                         return true;
94                                 } else {
95                                         //TODO check other statuses 400 and 500
96                                 }
97                         } else {
98                                 //TODO throw error
99                         }
100                 }
101                 
102                 return false;
103         }
104         
105         @Override
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())) {
111                                         return true;
112                                 } else {
113                                         //TODO check other statuses 400 and 500
114                                 }
115                         } else {
116                                 //TODO throw error
117                         }
118                 }
119                 
120                 return false;
121         }
122         
123         @Override
124         public String getRequestId() {
125                 return uuid;
126         }
127         
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));
130         }
131         
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));
134         }
135         
136         protected boolean isResultInfo(String json) {
137                 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
138         }
139         
140         protected boolean isHealthDiagnostic(String json, String uuid) {
141                 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
142         }
143         
144         protected boolean healthDiagnosticSuccessful(String json) {
145                 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
146         }
147         
148         protected Optional<String> getStatusMessage(String json) {
149                 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
150         }
151         
152         @Override
153         public int getMaximumElapsedTime() {
154                 return 300000;
155         }
156 }