Change the header to SO
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterRollbackMockTransformer.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  * ONAP - SO 
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 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 VnfAdapterCreateMockTransformer.java class
37  *
38  */
39 public class VnfAdapterRollbackMockTransformer extends ResponseTransformer {
40
41         private String notifyCallbackResponse;
42         private String ackResponse;
43         private String messageId;
44
45         public VnfAdapterRollbackMockTransformer() {
46                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
47         }
48         
49         public VnfAdapterRollbackMockTransformer(String messageId) {
50                 this.messageId = messageId;
51         }
52
53         @Override
54         public String name() {
55                 return "vnf-adapter-rollback-transformer";
56         }
57
58         @Override
59         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
60                         FileSource fileSource) {
61
62                 String requestBody = request.getBodyAsString();
63
64                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
65                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
66                 String responseMessageId = "";
67                 String updatedResponse = "";
68                 
69                 try {
70                         // try supplied response file (if any)
71                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
72                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
73                         notifyCallbackResponse = ackResponse;
74                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
75                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
76                 } catch (Exception ex) {
77                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
78                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
79                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
80                 }
81                 
82                 System.out.println("response (mock) messageId       : " + responseMessageId);           
83                 System.out.println("request  (replacement) messageId: " + messageId);
84                 
85                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
86                 System.out.println("vnf Response (after):" + updatedResponse);
87
88                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
89                 int delay = 300;
90                 if (vnfDelay != null) {
91                         delay = Integer.parseInt(vnfDelay.toString());
92                 }
93
94                 //Kick off callback thread
95                 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);         
96                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
97                 callbackResponseThread.start();
98
99                 return ResponseDefinitionBuilder
100                            .like(responseDefinition).but()
101                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
102                            .build();
103                 
104         }
105
106         @Override
107         public boolean applyGlobally() {
108             return false;
109         }
110
111         private class CallbackResponseThread extends Thread {
112
113                 private String callbackUrl;
114                 private String payLoad;
115                 private int delay;
116
117                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
118                         this.callbackUrl = callbackUrl;
119                         this.payLoad = payLoad;
120                         this.delay = delay;
121                 }
122
123                 public void run () {
124                         try {
125                                 //Delay sending callback response
126                                 sleep(delay);
127                         } catch (InterruptedException e1) {
128                                 // TODO Auto-generated catch block
129                                 e1.printStackTrace();
130                         }
131                         System.out.println("Sending callback response to url: " + callbackUrl);
132                         ClientRequest request = new ClientRequest(callbackUrl);
133                         request.body("text/xml", payLoad);
134                         //System.err.println(payLoad);
135                         try {
136                                 ClientResponse result = request.post();
137                                 System.out.println("Successfully posted callback? Status: " + result.getStatus());
138                                 //System.err.println("Successfully posted callback:" + result.getStatus());
139                         } catch (Exception e) {
140                                 // TODO Auto-generated catch block
141                                 System.out.println("catch error in - request.post() ");                         
142                                 e.printStackTrace();
143                         }
144                 }
145
146         }
147 }