bmpn sonar issue fix
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterQueryMockTransformer.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
4  * ================================================================================ 
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================ 
8  * Licensed under the Apache License, Version 2.0 (the "License"); 
9  * you may not use this file except in compliance with the License. 
10  * You may obtain a copy of the License at 
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0 
13  * 
14  * Unless required by applicable law or agreed to in writing, software 
15  * distributed under the License is distributed on an "AS IS" BASIS, 
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
17  * See the License for the specific language governing permissions and 
18  * limitations under the License. 
19  * ============LICENSE_END========================================================= 
20  */ 
21
22 package org.openecomp.mso.bpmn.mock;
23
24
25 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
26
27 import org.jboss.resteasy.client.ClientRequest;
28 import org.jboss.resteasy.client.ClientResponse;
29 import org.openecomp.mso.logger.MsoLogger;
30
31 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
32 import com.github.tomakehurst.wiremock.common.FileSource;
33 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
36
37 import org.openecomp.mso.logger.MsoLogger;
38 /**
39  * Please describe the VnfAdapterQueryMockTransformer.java class
40  *
41  */
42
43
44 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
45         
46         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
47         
48         private String notifyCallbackResponse;
49         private String ackResponse;
50         private String messageId;
51
52         public VnfAdapterQueryMockTransformer() {
53                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
54         }
55         
56         public VnfAdapterQueryMockTransformer(String messageId) {
57                 this.messageId = messageId;
58         }
59
60         @Override
61         public String name() {
62                 return "vnf-adapter-query-transformer";
63         }
64         
65         @Override
66         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
67                         FileSource fileSource) {
68
69                 String requestBody = request.getBodyAsString();
70
71                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
72                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
73         //      String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
74         
75                 String responseMessageId = "";
76                 String updatedResponse = "";
77                 
78         //      if (ackResponse == null) {
79                         //System.err.println("file:" + responseDefinition.getBodyFileName());
80                 //      ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
81                 //}
82
83                 
84                 try {
85                         // try supplied response file (if any)
86                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
87                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
88                         notifyCallbackResponse = ackResponse;
89                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
90                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
91                 } catch (Exception ex) {
92                         LOGGER.debug("Exception :",ex);
93                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
94                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
95                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
96                 }
97                 
98                 System.out.println("response (mock) messageId       : " + responseMessageId);           
99                 System.out.println("request  (replacement) messageId: " + messageId);
100                 
101                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
102                 System.out.println("vnf Response (after):" + updatedResponse);
103                 
104                 
105                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
106                 int delay = 300;
107                 if (vnfDelay != null) {
108                         delay = Integer.parseInt(vnfDelay.toString());
109                 }
110
111                 //Kick off callback thread
112                 
113                 //System.out.println("notficationUrl" + notficationUrl);
114                 //System.out.println("updatedResponse" + updatedResponse);
115                 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
116                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
117                 System.out.println("Inside Callback" );
118                 callbackResponseThread.start();
119
120                                 return ResponseDefinitionBuilder
121                              .like(responseDefinition).but()
122                              .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
123                              .build();
124         }
125
126         @Override
127         public boolean applyGlobally() {
128             return false;
129         }
130
131         private class CallbackResponseThread extends Thread {
132
133                 private String callbackUrl;
134                 private String payLoad;
135                 private int delay;
136
137                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
138                         this.callbackUrl = callbackUrl;
139                         this.payLoad = payLoad;
140                         this.delay = delay;
141                 }
142
143                 public void run () {
144                         try {
145                                 //Delay sending callback response
146                                 sleep(delay);
147                         } catch (InterruptedException e1) {
148                                 LOGGER.debug("Exception :",e1);
149                         }
150                         ClientRequest request = new ClientRequest(callbackUrl);
151                         request.body("text/xml", payLoad);
152                         //System.err.println(payLoad);
153                         try {
154                                 ClientResponse result = request.post();
155                                 //System.err.println("Successfully posted callback:" + result.getStatus());
156                         } catch (Exception e) {
157                                 LOGGER.debug("Exception :",e);
158                         }
159                 }
160
161         }
162
163
164 }