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.openecomp.mso.bpmn.mock;
23 import org.jboss.resteasy.client.ClientRequest;
24 import org.jboss.resteasy.client.ClientResponse;
26 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
27 import com.github.tomakehurst.wiremock.common.FileSource;
28 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
29 import com.github.tomakehurst.wiremock.http.Request;
30 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
32 public class SDNCAdapterNetworkTopologyMockTransformer extends ResponseTransformer {
34 private String callbackResponse;
35 private String requestId;
37 public SDNCAdapterNetworkTopologyMockTransformer() {
38 callbackResponse = ""; // FileUtil.readResourceFile("__files/sdncDeleteNetworkTopologySimResponse.xml");
41 public SDNCAdapterNetworkTopologyMockTransformer(String requestId) {
42 this.requestId = requestId;
45 public String name() {
46 return "network-topology-operation-transformer";
50 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource fileSource) {
51 String requestBody = request.getBodyAsString();
53 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
54 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
55 System.out.println("request callbackUrl : " + callbackUrl);
56 System.out.println("request requestId : " + requestId);
58 System.out.println("file path/name : " + responseDefinition.getBodyFileName());
59 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
60 // extract Response responseRequestId
61 String responseRequestId = callbackResponse.substring(callbackResponse.indexOf("<RequestId>")+11, callbackResponse.indexOf("</RequestId>"));
62 System.out.println("response requestId: " + responseRequestId);
63 System.out.println("callbackResponse (before): " + callbackResponse);
64 callbackResponse = callbackResponse.replace(responseRequestId, requestId);
65 if (this.requestId != null) {
66 callbackResponse = callbackResponse.replace(this.requestId, requestId);
68 callbackResponse = callbackResponse.replace(responseRequestId, requestId);
70 System.out.println("callbackResponse (after):" + callbackResponse);
72 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
74 if (sdncDelay != null) {
75 delay = Integer.parseInt(sdncDelay.toString());
78 //Kick off callback thread
79 System.out.println("(NetworkTopologyMockTransformer) callback Url:" + callbackUrl + ":delay:" + delay);
80 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
81 calbackResponseThread.start();
83 //return 200 OK with body
84 return ResponseDefinitionBuilder
85 .like(responseDefinition).but()
86 .withStatus(200).withBody(callbackResponse).withHeader("Content-Type", "text/xml")
91 public boolean applyGlobally() {
95 private class CallbackResponseThread extends Thread {
97 private String callbackUrl;
98 private String payLoad;
101 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
102 this.callbackUrl = callbackUrl;
103 this.payLoad = payLoad;
109 //Delay sending callback response
111 } catch (InterruptedException e1) {
112 // TODO Auto-generated catch block
113 e1.printStackTrace();
115 System.out.println("Sending callback response to url: " + callbackUrl);
116 ClientRequest request = new ClientRequest(callbackUrl);
117 request.body("text/xml", payLoad);
118 //System.err.println(payLoad);
120 ClientResponse result = request.post();
121 System.out.println("Successfully posted callback? Status: " + result.getStatus());
122 } catch (Exception e) {
123 // TODO Auto-generated catch block
124 System.out.println("catch error in - request.post() ");