AT&T 1712 and 1802 release code
[so.git] / common / 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 Optional<String> getHost() {
63                 return Optional.ofNullable(msoProperties.get("sdno.health-check.dmaap.subscriber.host"));
64         }
65
66         @Override
67         public boolean continuePolling() {
68                 return continuePolling;
69         }
70         
71         @Override
72         public void stopProcessingMessages() {
73                 continuePolling = false;
74         }
75         @Override
76         public void processMessage(String message) throws Exception {
77                 if (isHealthDiagnostic(message, this.getRequestId())) {
78                         if (!healthDiagnosticSuccessful(message)) {
79                                 Optional<String> statusMessage = this.getStatusMessage(message);
80                                 if (statusMessage.isPresent()) {
81                                         throw new SDNOException("failed with message " + statusMessage.get());
82                                 } else {
83                                         throw new SDNOException("failed with no status message");
84                                 }
85                         } else {
86                                 auditLogger.info("successful health diagnostic found for request: " + this.getRequestId());
87                                 stopProcessingMessages();
88                         }
89                 }
90         }
91         
92         @Override
93         public boolean isAccepted(String message) {
94                 if (isResultInfo(message)) {
95                         Optional<String> code = isAccepted(message, this.getRequestId());
96                         if (code.isPresent()) {
97                                 if ("202".equals(code.get())) {
98                                         return true;
99                                 } else {
100                                         //TODO check other statuses 400 and 500
101                                 }
102                         } else {
103                                 //TODO throw error
104                         }
105                 }
106                 
107                 return false;
108         }
109         
110         @Override
111         public boolean isFailure(String message) {
112                 if (isResultInfo(message)) {
113                         Optional<String> code = isFailure(message, this.getRequestId());
114                         if (code.isPresent()) {
115                                 if ("500".equals(code.get())) {
116                                         return true;
117                                 } else {
118                                         //TODO check other statuses 400 and 500
119                                 }
120                         } else {
121                                 //TODO throw error
122                         }
123                 }
124                 
125                 return false;
126         }
127         
128         @Override
129         public String getRequestId() {
130                 return uuid;
131         }
132         
133         protected Optional<String> isAccepted(String json, String uuid) {
134                 return JsonPathUtil.getInstance().locateResult(json, 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, String.format("$.result-info[?(@.status=='FAILURE' && @.request-id=='%s')].code", uuid));
139         }
140         
141         protected boolean isResultInfo(String json) {
142                 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
143         }
144         
145         protected boolean isHealthDiagnostic(String json, String uuid) {
146                 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
147         }
148         
149         protected boolean healthDiagnosticSuccessful(String json) {
150                 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
151         }
152         
153         protected Optional<String> getStatusMessage(String json) {
154                 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".error-message");
155         }
156         
157         @Override
158         public int getMaximumElapsedTime() {
159                 return 300000;
160         }
161 }