Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / mock / VnfAdapterRollbackMockTransformer.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.onap.so.bpmn.mock;
22
23 import javax.ws.rs.core.UriBuilder;
24
25 import org.onap.so.client.HttpClient;
26 import org.onap.so.logger.MsoLogger;
27 import org.onap.so.utils.TargetEntity;
28
29 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30 import com.github.tomakehurst.wiremock.common.FileSource;
31 import com.github.tomakehurst.wiremock.extension.Parameters;
32 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
33 import com.github.tomakehurst.wiremock.http.Request;
34 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
35
36 /**
37  * Please describe the VnfAdapterCreateMockTransformer.java class
38  *
39  */
40 public class VnfAdapterRollbackMockTransformer extends ResponseDefinitionTransformer {
41
42         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterRollbackMockTransformer.class);
43
44         private String notifyCallbackResponse;
45         private String ackResponse;
46         private String messageId;
47
48         public VnfAdapterRollbackMockTransformer() {
49                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfRollbackSimResponse.xml");
50         }
51         
52         public VnfAdapterRollbackMockTransformer(String messageId) {
53                 this.messageId = messageId;
54         }
55
56         @Override
57         public String getName() {
58                 return "vnf-adapter-rollback-transformer";
59         }
60
61         @Override
62         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
63                         FileSource fileSource, Parameters parameters) {
64
65                 String requestBody = request.getBodyAsString();
66
67                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
68                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
69                 String responseMessageId = "";
70                 String updatedResponse = "";
71                 
72                 try {
73                         // try supplied response file (if any)
74                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
75                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
76                         notifyCallbackResponse = ackResponse;
77                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
78                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
79                 } catch (Exception ex) {
80                         LOGGER.debug("Exception :",ex);
81                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfRollbackSimResponse.xml'");
82                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
83                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
84                 }
85                 
86                 System.out.println("response (mock) messageId       : " + responseMessageId);           
87                 System.out.println("request  (replacement) messageId: " + messageId);
88                 
89                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
90                 System.out.println("vnf Response (after):" + updatedResponse);
91
92                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
93                 int delay = 300;
94                 if (vnfDelay != null) {
95                         delay = Integer.parseInt(vnfDelay.toString());
96                 }
97
98                 //Kick off callback thread
99                 System.out.println("VnfAdapterRollbackMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);         
100                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
101                 callbackResponseThread.start();
102
103                 return ResponseDefinitionBuilder
104                            .like(responseDefinition).but()
105                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
106                            .build();
107                 
108         }
109
110         @Override
111         public boolean applyGlobally() {
112             return false;
113         }
114
115         private class CallbackResponseThread extends Thread {
116
117                 private String callbackUrl;
118                 private String payLoad;
119                 private int delay;
120
121                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
122                         this.callbackUrl = callbackUrl;
123                         this.payLoad = payLoad;
124                         this.delay = delay;
125                 }
126
127                 public void run () {
128                         try {
129                                 //Delay sending callback response
130                                 sleep(delay);
131                         } catch (InterruptedException e1) {
132                                 LOGGER.debug("Exception :",e1);
133                         }
134
135                         try {
136                                 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
137                                 client.post(payLoad);
138                         } catch (Exception e) {
139                                 System.out.println("catch error in - request.post() ");                         
140                                 LOGGER.debug("Exception :",e);
141                         }
142                 }
143
144         }
145 }