2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.openecomp.mso.bpmn.mock;
21 import org.jboss.resteasy.client.ClientRequest;
22 import org.jboss.resteasy.client.ClientResponse;
24 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
25 import com.github.tomakehurst.wiremock.common.FileSource;
26 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
27 import com.github.tomakehurst.wiremock.http.Request;
28 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
30 import org.openecomp.mso.logger.MsoLogger;
32 * Please describe the VnfAdapterCreateMockTransformer.java class
35 public class VnfAdapterRollbackMockTransformer extends ResponseTransformer {
37 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
39 private String notifyCallbackResponse;
40 private String ackResponse;
41 private String messageId;
43 public VnfAdapterRollbackMockTransformer() {
44 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
47 public VnfAdapterRollbackMockTransformer(String messageId) {
48 this.messageId = messageId;
52 public String name() {
53 return "vnf-adapter-rollback-transformer";
57 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
58 FileSource fileSource) {
60 String requestBody = request.getBodyAsString();
62 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
63 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
64 String responseMessageId = "";
65 String updatedResponse = "";
68 // try supplied response file (if any)
69 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
70 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
71 notifyCallbackResponse = ackResponse;
72 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
73 updatedResponse = ackResponse.replace(responseMessageId, messageId);
74 } catch (Exception ex) {
75 LOGGER.debug("Exception :",ex);
76 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
77 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
78 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
81 System.out.println("response (mock) messageId : " + responseMessageId);
82 System.out.println("request (replacement) messageId: " + messageId);
84 System.out.println("vnf Response (before):" + notifyCallbackResponse);
85 System.out.println("vnf Response (after):" + updatedResponse);
87 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
89 if (vnfDelay != null) {
90 delay = Integer.parseInt(vnfDelay.toString());
93 //Kick off callback thread
94 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
95 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
96 callbackResponseThread.start();
98 return ResponseDefinitionBuilder
99 .like(responseDefinition).but()
100 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
106 public boolean applyGlobally() {
110 private class CallbackResponseThread extends Thread {
112 private String callbackUrl;
113 private String payLoad;
116 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
117 this.callbackUrl = callbackUrl;
118 this.payLoad = payLoad;
124 //Delay sending callback response
126 } catch (InterruptedException e1) {
127 LOGGER.debug("Exception :",e1);
129 System.out.println("Sending callback response to url: " + callbackUrl);
130 ClientRequest request = new ClientRequest(callbackUrl);
131 request.body("text/xml", payLoad);
132 //System.err.println(payLoad);
134 ClientResponse result = request.post();
135 System.out.println("Successfully posted callback? Status: " + result.getStatus());
136 //System.err.println("Successfully posted callback:" + result.getStatus());
137 } catch (Exception e) {
138 System.out.println("catch error in - request.post() ");
139 LOGGER.debug("Exception :",e);