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;
29 import org.openecomp.mso.logger.MsoLogger;
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;
37 * Please describe the VnfAdapterCreateMockTransformer.java class
40 public class VnfAdapterDeleteMockTransformer extends ResponseTransformer {
42 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
44 private String notifyCallbackResponse;
45 private String ackResponse;
47 public VnfAdapterDeleteMockTransformer() {
48 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfDeleteSimResponse.xml");
52 public String name() {
53 return "vnf-adapter-delete-transformer";
57 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
58 FileSource fileSource) {
60 // System.err.println("notifyCallbackResponse:" + notifyCallbackResponse);
62 String requestBody = request.getBodyAsString();
64 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
65 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
66 String responseMessageId = "";
67 String updatedResponse = "";
70 // try supplied response file (if any)
71 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
72 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
73 notifyCallbackResponse = ackResponse;
74 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
75 updatedResponse = ackResponse.replace(responseMessageId, messageId);
76 } catch (Exception ex) {
77 LOGGER.debug("Exception :",ex);
78 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfDeleteSimResponse.xml'");
79 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
80 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
83 System.out.println("response (mock) messageId : " + responseMessageId);
84 System.out.println("request (replacement) messageId: " + messageId);
86 System.out.println("vnf Response (before):" + notifyCallbackResponse);
87 System.out.println("vnf Response (after):" + updatedResponse);
89 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
91 if (vnfDelay != null) {
92 delay = Integer.parseInt(vnfDelay.toString());
95 //Kick off callback thread
96 System.out.println("VnfAdapterDeleteMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
97 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
98 callbackResponseThread.start();
100 return ResponseDefinitionBuilder
101 .like(responseDefinition).but()
102 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
108 public boolean applyGlobally() {
112 private class CallbackResponseThread extends Thread {
114 private String callbackUrl;
115 private String payLoad;
118 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
119 this.callbackUrl = callbackUrl;
120 this.payLoad = payLoad;
124 @SuppressWarnings("deprecation")
127 //Delay sending callback response
129 } catch (InterruptedException e1) {
130 // TODO Auto-generated catch block
131 LOGGER.debug("Exception :",e1);
133 System.out.println("Sending callback response to url: " + callbackUrl);
134 ClientRequest request = new ClientRequest(callbackUrl);
135 request.body("text/xml", payLoad);
136 //System.err.println(payLoad);
138 ClientResponse result = request.post();
139 System.out.println("Successfully posted callback? Status: " + result.getStatus());
140 //System.err.println("Successfully posted callback:" + result.getStatus());
141 } catch (Exception e) {
142 // TODO Auto-generated catch block
143 System.out.println("catch error in - request.post() ");
144 LOGGER.debug("Exception :",e);