2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.mso.bpmn.mock;
24 import org.jboss.resteasy.client.ClientRequest;
25 import org.jboss.resteasy.client.ClientResponse;
27 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
28 import com.github.tomakehurst.wiremock.common.FileSource;
29 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
30 import com.github.tomakehurst.wiremock.http.Request;
31 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
33 import org.openecomp.mso.logger.MsoLogger;
35 public class SDNCAdapterNetworkTopologyMockTransformer extends ResponseTransformer {
37 private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
39 private String callbackResponse;
40 private String requestId;
42 public SDNCAdapterNetworkTopologyMockTransformer() {
43 callbackResponse = ""; // FileUtil.readResourceFile("__files/sdncDeleteNetworkTopologySimResponse.xml");
46 public SDNCAdapterNetworkTopologyMockTransformer(String requestId) {
47 this.requestId = requestId;
50 public String name() {
51 return "network-topology-operation-transformer";
55 public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition, FileSource fileSource) {
56 String requestBody = request.getBodyAsString();
58 String callbackUrl = requestBody.substring(requestBody.indexOf("<sdncadapter:CallbackUrl>")+25, requestBody.indexOf("</sdncadapter:CallbackUrl>"));
59 String requestId = requestBody.substring(requestBody.indexOf("<sdncadapter:RequestId>")+23, requestBody.indexOf("</sdncadapter:RequestId>"));
60 System.out.println("request callbackUrl : " + callbackUrl);
61 System.out.println("request requestId : " + requestId);
63 System.out.println("file path/name : " + responseDefinition.getBodyFileName());
64 callbackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
65 // extract Response responseRequestId
66 String responseRequestId = callbackResponse.substring(callbackResponse.indexOf("<RequestId>")+11, callbackResponse.indexOf("</RequestId>"));
67 System.out.println("response requestId: " + responseRequestId);
68 System.out.println("callbackResponse (before): " + callbackResponse);
69 callbackResponse = callbackResponse.replace(responseRequestId, requestId);
70 if (this.requestId != null) {
71 callbackResponse = callbackResponse.replace(this.requestId, requestId);
73 callbackResponse = callbackResponse.replace(responseRequestId, requestId);
75 System.out.println("callbackResponse (after):" + callbackResponse);
77 Object sdncDelay = MockResource.getMockProperties().get("sdnc_delay");
79 if (sdncDelay != null) {
80 delay = Integer.parseInt(sdncDelay.toString());
83 //Kick off callback thread
84 System.out.println("(NetworkTopologyMockTransformer) callback Url:" + callbackUrl + ":delay:" + delay);
85 CallbackResponseThread calbackResponseThread = new CallbackResponseThread(callbackUrl,callbackResponse, delay);
86 calbackResponseThread.start();
88 //return 200 OK with body
89 return ResponseDefinitionBuilder
90 .like(responseDefinition).but()
91 .withStatus(200).withBody(callbackResponse).withHeader("Content-Type", "text/xml")
96 public boolean applyGlobally() {
100 private class CallbackResponseThread extends Thread {
102 private String callbackUrl;
103 private String payLoad;
106 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
107 this.callbackUrl = callbackUrl;
108 this.payLoad = payLoad;
114 //Delay sending callback response
116 } catch (InterruptedException e1) {
117 // TODO Auto-generated catch block
118 LOGGER.debug("Exception :",e1);
120 LOGGER.debug("Sending callback response to url: " + callbackUrl);
121 ClientRequest request = new ClientRequest(callbackUrl);
122 request.body("text/xml", payLoad);
123 //System.err.println(payLoad);
125 ClientResponse result = request.post();
126 LOGGER.debug("Successfully posted callback? Status: " + result.getStatus());
127 } catch (Exception e) {
128 // TODO Auto-generated catch block
129 LOGGER.debug("catch error in - request.post() ");
130 LOGGER.debug("Exception :",e);