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