Correcting varriable name
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / sdno / SDNOValidatorImpl.java
1 package org.openecomp.mso.client.sdno;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.util.Optional;
6
7 import org.openecomp.mso.client.dmaap.Consumer;
8 import org.openecomp.mso.client.dmaap.DmaapConsumer;
9 import org.openecomp.mso.client.dmaap.DmaapPublisher;
10 import org.openecomp.mso.client.exceptions.SDNOException;
11 import org.openecomp.mso.jsonpath.JsonPathUtil;
12
13 public class SDNOValidatorImpl implements SDNOValidator, Consumer {
14
15         private final static String aafUserName = "something";
16         private final static String clientName = "MSO";
17         private final static String healthDiagnosticPath = "body.output.response-healthdiagnostic";
18         private final static String producerFilePath = "";
19         private String uuid;
20         private boolean continuePolling = true;
21         @Override
22         public void healthDiagnostic(String vnfName, String uuid) {
23                 //Query A&AI data
24                 // setup SDNO Entity
25                 //Call SDNO for Health Diagnostic
26                 //create producer file for MRClient https://wiki.web.att.com/display/MessageRouter/DMaaP_MR_JavaReferenceClient
27                 //  final MRBatchingPublisher pub = MRClientFactory.createBatchingPublisher(producerFilePath);
28                 //      pub.send("Mypartitionkey",JSON.toString(object));
29                 //create consumer file for MRClient https://wiki.web.att.com/display/MessageRouter/DMaaP_MR_JavaReferenceClient
30                 //check for error in subscription feed filter via jsonpath 
31                 //block and continue to poll waiting for response
32         }
33
34         protected SDNO buildRequestDiagnostic(String vnfName, String uuid, String oamIp) {
35                 
36                 Input input = new Input();
37                 SDNO parentRequest = new SDNO();
38                 Body body = new Body();
39                 parentRequest.setBody(body);
40                 parentRequest.setNodeType("vPE");
41                 parentRequest.setOperation("health-diagnostic");
42                 
43                 body.setInput(input);
44                 
45                 RequestHealthDiagnostic request = new RequestHealthDiagnostic();
46                 request.setRequestClientName(clientName);
47                 request.setRequestNodeName(vnfName);
48                 request.setRequestNodeIp(oamIp); //generic-vnf oam ip
49                 request.setRequestUserId(aafUserName); //mech id?
50                 request.setRequestId(uuid); //something to identify this request by for polling
51                 
52                 input.setRequestHealthDiagnostic(request);
53                 
54                 return parentRequest;
55         }
56         protected void submitRequest(String json) throws FileNotFoundException, IOException, InterruptedException {
57                 DmaapPublisher publisher = new DmaapPublisher(this.producerFilePath);
58                 publisher.send(json);
59         }
60         protected boolean pollForResponse(DmaapConsumer consumer, String uuid) throws Exception {
61                 this.uuid = uuid;
62                 return consumer.consume(this);
63         }
64         
65         @Override
66         public boolean continuePolling() {
67                 return continuePolling;
68         }
69         
70         @Override
71         public void stopProcessingMessages() {
72                 continuePolling = false;
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(statusMessage.get());
81                                 } else {
82                                         throw new SDNOException();
83                                 }
84                         } else {
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 String getRequestId() {
110                 return uuid;
111         }
112         
113         protected Optional<String> isAccepted(String json, String uuid) {
114                 return JsonPathUtil.getInstance().locateResult(json, String.format("$.result-info[?(@.status=='ACCEPTED' && @.request-id=='%s')].code", uuid));
115         }
116         
117         protected boolean isResultInfo(String json) {
118                 return JsonPathUtil.getInstance().pathExists(json, "$[?(@.result-info)]");
119         }
120         
121         protected boolean isHealthDiagnostic(String json, String uuid) {
122                 return JsonPathUtil.getInstance().pathExists(json, String.format("$[?(@.result-info.request-id=='%s')].%s", uuid, healthDiagnosticPath));
123         }
124         
125         protected boolean healthDiagnosticSuccessful(String json) {
126                 return JsonPathUtil.getInstance().pathExists(json, "$." + healthDiagnosticPath + "[?(@.response-status=='Success')]");
127         }
128         
129         protected Optional<String> getStatusMessage(String json) {
130                 return JsonPathUtil.getInstance().locateResult(json, "$." + healthDiagnosticPath + ".response-details-json");
131         }
132         
133 }