2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.mock;
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.UriBuilder;
26 import org.onap.so.client.HttpClient;
27 import org.onap.so.logger.MsoLogger;
28 import org.onap.so.utils.TargetEntity;
30 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
31 import com.github.tomakehurst.wiremock.common.FileSource;
32 import com.github.tomakehurst.wiremock.extension.Parameters;
33 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
38 * Please describe the VnfAdapterCreateMockTransformer.java class
41 public class VnfAdapterCreateMockTransformer extends ResponseDefinitionTransformer {
43 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterCreateMockTransformer.class);
45 private String notifyCallbackResponse;
46 private String ackResponse;
48 public VnfAdapterCreateMockTransformer() {
49 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
53 public String getName() {
54 return "vnf-adapter-create-transformer";
58 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
59 FileSource fileSource, Parameters parameters) {
61 String requestBody = request.getBodyAsString();
63 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
64 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
65 String responseMessageId = "";
66 String updatedResponse = "";
69 // try supplied response file (if any)
70 System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
71 ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
72 notifyCallbackResponse = ackResponse;
73 responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
74 updatedResponse = ackResponse.replace(responseMessageId, messageId);
75 } catch (Exception ex) {
76 LOGGER.debug("Exception :",ex);
77 System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfCreateSimResponse.xml'");
78 responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
79 updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
82 System.out.println("response (mock) messageId : " + responseMessageId);
83 System.out.println("request (replacement) messageId: " + messageId);
85 System.out.println("vnf Response (before):" + notifyCallbackResponse);
86 System.out.println("vnf Response (after):" + updatedResponse);
88 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
90 if (vnfDelay != null) {
91 delay = Integer.parseInt(vnfDelay.toString());
94 //Kick off callback thread
95 System.out.println("VnfAdapterCreateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
96 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
97 callbackResponseThread.start();
99 return ResponseDefinitionBuilder
100 .like(responseDefinition).but()
101 .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
107 public boolean applyGlobally() {
111 private class CallbackResponseThread extends Thread {
113 private String callbackUrl;
114 private String payLoad;
117 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
118 this.callbackUrl = callbackUrl;
119 this.payLoad = payLoad;
123 @SuppressWarnings("deprecation")
126 //Delay sending callback response
128 } catch (InterruptedException e1) {
129 // TODO Auto-generated catch block
130 LOGGER.debug("Exception :",e1);
132 LOGGER.debug("Sending callback response to url: " + callbackUrl);
134 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
135 Response response = client.post(payLoad);
136 LOGGER.debug("Successfully posted callback? Status: " + response.getStatus());
137 //System.err.println("Successfully posted callback:" + result.getStatus());
138 } catch (Exception e) {
139 // TODO Auto-generated catch block
140 LOGGER.debug("catch error in - request.post() ");
141 LOGGER.debug("Exception :",e);