Change the header to SO
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterQueryMockTransformer.java
1 /*- 
2  * ============LICENSE_START======================================================= 
3  * ONAP - SO 
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 
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0 
12  * 
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========================================================= 
19  */ 
20
21 package org.openecomp.mso.bpmn.mock;
22
23
24 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
25
26 import org.jboss.resteasy.client.ClientRequest;
27 import org.jboss.resteasy.client.ClientResponse;
28
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
32 import com.github.tomakehurst.wiremock.http.Request;
33 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
34
35 /**
36  * Please describe the VnfAdapterQueryMockTransformer.java class
37  *
38  */
39
40
41 public class VnfAdapterQueryMockTransformer extends ResponseTransformer{
42         
43         private String notifyCallbackResponse;
44         private String ackResponse;
45         private String messageId;
46
47         public VnfAdapterQueryMockTransformer() {
48                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfQuerySimResponse.xml");
49         }
50         
51         public VnfAdapterQueryMockTransformer(String messageId) {
52                 this.messageId = messageId;
53         }
54
55         @Override
56         public String name() {
57                 return "vnf-adapter-query-transformer";
58         }
59         
60         @Override
61         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
62                         FileSource fileSource) {
63
64                 String requestBody = request.getBodyAsString();
65
66                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
67                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
68         //      String updatedResponse = notifyCallbackResponse.replace("b1a82ce6-7f5c-45fd-9273-acaf88fc2137", messageId);
69         
70                 String responseMessageId = "";
71                 String updatedResponse = "";
72                 
73         //      if (ackResponse == null) {
74                         //System.err.println("file:" + responseDefinition.getBodyFileName());
75                 //      ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
76                 //}
77
78                 
79                 try {
80                         // try supplied response file (if any)
81                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
82                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
83                         notifyCallbackResponse = ackResponse;
84                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
85                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
86                 } catch (Exception ex) {
87                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfQuerySimResponse.xml'");
88                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
89                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
90                 }
91                 
92                 System.out.println("response (mock) messageId       : " + responseMessageId);           
93                 System.out.println("request  (replacement) messageId: " + messageId);
94                 
95                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
96                 System.out.println("vnf Response (after):" + updatedResponse);
97                 
98                 
99                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
100                 int delay = 300;
101                 if (vnfDelay != null) {
102                         delay = Integer.parseInt(vnfDelay.toString());
103                 }
104
105                 //Kick off callback thread
106                 
107                 //System.out.println("notficationUrl" + notficationUrl);
108                 //System.out.println("updatedResponse" + updatedResponse);
109                 System.out.println("VnfAdapterQueryMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
110                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
111                 System.out.println("Inside Callback" );
112                 callbackResponseThread.start();
113
114                                 return ResponseDefinitionBuilder
115                              .like(responseDefinition).but()
116                              .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
117                              .build();
118         }
119
120         @Override
121         public boolean applyGlobally() {
122             return false;
123         }
124
125         private class CallbackResponseThread extends Thread {
126
127                 private String callbackUrl;
128                 private String payLoad;
129                 private int delay;
130
131                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
132                         this.callbackUrl = callbackUrl;
133                         this.payLoad = payLoad;
134                         this.delay = delay;
135                 }
136
137                 public void run () {
138                         try {
139                                 //Delay sending callback response
140                                 sleep(delay);
141                         } catch (InterruptedException e1) {
142                                 // TODO Auto-generated catch block
143                                 e1.printStackTrace();
144                         }
145                         ClientRequest request = new ClientRequest(callbackUrl);
146                         request.body("text/xml", payLoad);
147                         //System.err.println(payLoad);
148                         try {
149                                 ClientResponse result = request.post();
150                                 //System.err.println("Successfully posted callback:" + result.getStatus());
151                         } catch (Exception e) {
152                                 // TODO Auto-generated catch block
153                                 e.printStackTrace();
154                         }
155                 }
156
157         }
158
159
160 }