[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterCreateMockTransformer.java
1 /*
2  * © 2014 AT&T Intellectual Property. All rights reserved. Used under license from AT&T Intellectual Property.
3  */
4 /*- 
5  * ============LICENSE_START======================================================= 
6  * OPENECOMP - MSO 
7  * ================================================================================ 
8  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. 
9  * ================================================================================ 
10  * Licensed under the Apache License, Version 2.0 (the "License"); 
11  * you may not use this file except in compliance with the License. 
12  * You may obtain a copy of the License at 
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0 
15  * 
16  * Unless required by applicable law or agreed to in writing, software 
17  * distributed under the License is distributed on an "AS IS" BASIS, 
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
19  * See the License for the specific language governing permissions and 
20  * limitations under the License. 
21  * ============LICENSE_END========================================================= 
22  */ 
23
24 package org.openecomp.mso.bpmn.mock;
25
26 import javax.xml.ws.Endpoint;
27
28 import org.jboss.resteasy.client.ClientRequest;
29 import org.jboss.resteasy.client.ClientResponse;
30
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;
36
37 /**
38  * Please describe the VnfAdapterCreateMockTransformer.java class
39  *
40  */
41 public class VnfAdapterCreateMockTransformer extends ResponseTransformer {
42
43         private String notifyCallbackResponse;
44         private String ackResponse;
45
46         public VnfAdapterCreateMockTransformer() {
47                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
48         }
49
50         @Override
51         public String name() {
52                 return "vnf-adapter-create-transformer";
53         }
54
55         @Override
56         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
57                         FileSource fileSource) {
58
59                 String requestBody = request.getBodyAsString();
60
61                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
62                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
63                 String responseMessageId = "";
64                 String updatedResponse = "";
65
66                 try {
67                         // try supplied response file (if any)
68                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
69                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
70                         notifyCallbackResponse = ackResponse;
71                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
72                     updatedResponse = ackResponse.replace(responseMessageId, messageId);
73                 } catch (Exception ex) {
74                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfCreateSimResponse.xml'");
75                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
76                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
77                 }
78
79                 System.out.println("response (mock) messageId       : " + responseMessageId);
80                 System.out.println("request  (replacement) messageId: " + messageId);
81
82                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
83                 System.out.println("vnf Response (after):" + updatedResponse);
84
85                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
86                 int delay = 300;
87                 if (vnfDelay != null) {
88                         delay = Integer.parseInt(vnfDelay.toString());
89                 }
90
91                 //Kick off callback thread
92                 System.out.println("VnfAdapterCreateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
93                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
94                 callbackResponseThread.start();
95
96                 return ResponseDefinitionBuilder
97                            .like(responseDefinition).but()
98                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
99                            .build();
100
101         }
102
103         @Override
104         public boolean applyGlobally() {
105             return false;
106         }
107
108         private class CallbackResponseThread extends Thread {
109
110                 private String callbackUrl;
111                 private String payLoad;
112                 private int delay;
113
114                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
115                         this.callbackUrl = callbackUrl;
116                         this.payLoad = payLoad;
117                         this.delay = delay;
118                 }
119
120                 @SuppressWarnings("deprecation")
121                 public void run () {
122                         try {
123                                 //Delay sending callback response
124                                 sleep(delay);
125                         } catch (InterruptedException e1) {
126                                 // TODO Auto-generated catch block
127                                 e1.printStackTrace();
128                         }
129                         System.out.println("Sending callback response to url: " + callbackUrl);
130                         ClientRequest request = new ClientRequest(callbackUrl);
131                         request.body("text/xml", payLoad);
132                         //System.out.println("payLoad: " + payLoad);
133
134                         try {
135                                 ClientResponse result = request.post();
136                                 System.out.println("Successfully posted callback? Status: " + result.getStatus());
137                                 //System.err.println("Successfully posted callback:" + result.getStatus());
138                         } catch (Exception e) {
139                                 // TODO Auto-generated catch block
140                                 System.out.println("catch error in - request.post() ");
141                                 e.printStackTrace();
142                         }
143                 }
144
145         }
146
147
148 }