2 * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.
5 * ============LICENSE_START=======================================================
7 * ================================================================================
8 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
9 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
10 * ================================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.mso.bpmn.mock;
27 import org.jboss.resteasy.client.ClientRequest;
28 import org.jboss.resteasy.client.ClientResponse;
30 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
31 import com.github.tomakehurst.wiremock.common.FileSource;
32 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
33 import com.github.tomakehurst.wiremock.http.Request;
34 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
36 import org.openecomp.mso.logger.MsoLogger;
38 * Please describe the VnfAdapterCreateMockTransformer.java class
41 public class VnfAdapterRollbackMockTransformer extends ResponseTransformer {
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
45 private String notifyCallbackResponse;
46 private String ackResponse;
47 private String messageId;
49 public VnfAdapterRollbackMockTransformer() {
50 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
53 public VnfAdapterRollbackMockTransformer(String messageId) {
54 this.messageId = messageId;
58 public String name() {
59 return "vnf-adapter-rollback-transformer";
63 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
64 FileSource fileSource) {
66 String requestBody = request.getBodyAsString();
68 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
69 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
70 String responseMessageId = "";
71 String updatedResponse = "";
74 // try supplied response file (if any)
75 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
76 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
77 notifyCallbackResponse = ackResponse;
78 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
79 updatedResponse = ackResponse.replace(responseMessageId, messageId);
80 } catch (Exception ex) {
81 LOGGER.debug("Exception :",ex);
82 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
83 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
84 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
87 System.out.println("response (mock) messageId : " + responseMessageId);
88 System.out.println("request (replacement) messageId: " + messageId);
90 System.out.println("vnf Response (before):" + notifyCallbackResponse);
91 System.out.println("vnf Response (after):" + updatedResponse);
93 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
95 if (vnfDelay != null) {
96 delay = Integer.parseInt(vnfDelay.toString());
99 //Kick off callback thread
100 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
101 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
102 callbackResponseThread.start();
104 return ResponseDefinitionBuilder
105 .like(responseDefinition).but()
106 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
112 public boolean applyGlobally() {
116 private class CallbackResponseThread extends Thread {
118 private String callbackUrl;
119 private String payLoad;
122 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
123 this.callbackUrl = callbackUrl;
124 this.payLoad = payLoad;
130 //Delay sending callback response
132 } catch (InterruptedException e1) {
133 LOGGER.debug("Exception :",e1);
135 System.out.println("Sending callback response to url: " + callbackUrl);
136 ClientRequest request = new ClientRequest(callbackUrl);
137 request.body("text/xml", payLoad);
138 //System.err.println(payLoad);
140 ClientResponse result = request.post();
141 System.out.println("Successfully posted callback? Status: " + result.getStatus());
142 //System.err.println("Successfully posted callback:" + result.getStatus());
143 } catch (Exception e) {
144 System.out.println("catch error in - request.post() ");
145 LOGGER.debug("Exception :",e);