Containerization feature of SO
[so.git] / bpmn / MSOCommonBPMN / src / test / java / org / onap / so / bpmn / mock / VnfAdapterCreateMockTransformer.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.Response;
24 import javax.ws.rs.core.UriBuilder;
25
26 import org.onap.so.client.HttpClient;
27 import org.onap.so.logger.MsoLogger;
28 import org.onap.so.utils.TargetEntity;
29
30 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
31 import com.github.tomakehurst.wiremock.common.FileSource;
32 import com.github.tomakehurst.wiremock.extension.Parameters;
33 import com.github.tomakehurst.wiremock.extension.ResponseDefinitionTransformer;
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 ResponseDefinitionTransformer {
42
43         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, VnfAdapterCreateMockTransformer.class);
44         
45         private String notifyCallbackResponse;
46         private String ackResponse;
47
48         public VnfAdapterCreateMockTransformer() {
49                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
50         }
51
52         @Override
53         public String getName() {
54                 return "vnf-adapter-create-transformer";
55         }
56
57         @Override
58         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
59                         FileSource fileSource, Parameters parameters) {
60
61                 String requestBody = request.getBodyAsString();
62
63                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
64                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
65                 String responseMessageId = "";
66                 String updatedResponse = "";
67
68                 try {
69                         // try supplied response file (if any)
70                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
71                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
72                         notifyCallbackResponse = ackResponse;
73                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
74                     updatedResponse = ackResponse.replace(responseMessageId, messageId);
75                 } catch (Exception ex) {
76                         LOGGER.debug("Exception :",ex);
77                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfCreateSimResponse.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("VnfAdapterCreateMockTransformer 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                 @SuppressWarnings("deprecation")
124                 public void run () {
125                         try {
126                                 //Delay sending callback response
127                                 sleep(delay);
128                         } catch (InterruptedException e1) {
129                                 // TODO Auto-generated catch block
130                                 LOGGER.debug("Exception :",e1);
131                         }
132                         LOGGER.debug("Sending callback response to url: " + callbackUrl);
133                         try {
134                                 HttpClient client = new HttpClient(UriBuilder.fromUri(callbackUrl).build().toURL(), "text/xml", TargetEntity.VNF_ADAPTER);
135                                 Response response = client.post(payLoad);
136                                 LOGGER.debug("Successfully posted callback? Status: " + response.getStatus());
137                                 //System.err.println("Successfully posted callback:" + result.getStatus());
138                         } catch (Exception e) {
139                                 // TODO Auto-generated catch block
140                             LOGGER.debug("catch error in - request.post() ");
141                                 LOGGER.debug("Exception :",e);
142                         }
143                 }
144
145         }
146
147
148 }