bmpn sonar issue fix
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterCreateMockTransformer.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  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
10  * ================================================================================ 
11  * Licensed under the Apache License, Version 2.0 (the "License"); 
12  * you may not use this file except in compliance with the License. 
13  * You may obtain a copy of the License at 
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0 
16  * 
17  * Unless required by applicable law or agreed to in writing, software 
18  * distributed under the License is distributed on an "AS IS" BASIS, 
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
20  * See the License for the specific language governing permissions and 
21  * limitations under the License. 
22  * ============LICENSE_END========================================================= 
23  */ 
24
25 package org.openecomp.mso.bpmn.mock;
26
27 import javax.xml.ws.Endpoint;
28
29 import org.jboss.resteasy.client.ClientRequest;
30 import org.jboss.resteasy.client.ClientResponse;
31 import org.openecomp.mso.logger.MsoLogger;
32
33 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
34 import com.github.tomakehurst.wiremock.common.FileSource;
35 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
36 import com.github.tomakehurst.wiremock.http.Request;
37 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
38
39 import org.openecomp.mso.logger.MsoLogger;
40 /**
41  * Please describe the VnfAdapterCreateMockTransformer.java class
42  *
43  */
44 public class VnfAdapterCreateMockTransformer extends ResponseTransformer {
45
46         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
47         
48         private String notifyCallbackResponse;
49         private String ackResponse;
50
51         public VnfAdapterCreateMockTransformer() {
52                 notifyCallbackResponse = FileUtil.readResourceFile("__files/vnfAdapterMocks/vnfCreateSimResponse.xml"); // default response
53         }
54
55         @Override
56         public String name() {
57                 return "vnf-adapter-create-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 responseMessageId = "";
69                 String updatedResponse = "";
70
71                 try {
72                         // try supplied response file (if any)
73                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
74                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
75                         notifyCallbackResponse = ackResponse;
76                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
77                     updatedResponse = ackResponse.replace(responseMessageId, messageId);
78                 } catch (Exception ex) {
79                         LOGGER.debug("Exception :",ex);
80                         System.out.println(" ******* Use default response file in '__files/vnfAdapterMocks/vnfCreateSimResponse.xml'");
81                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
82                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
83                 }
84
85                 System.out.println("response (mock) messageId       : " + responseMessageId);
86                 System.out.println("request  (replacement) messageId: " + messageId);
87
88                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
89                 System.out.println("vnf Response (after):" + updatedResponse);
90
91                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
92                 int delay = 300;
93                 if (vnfDelay != null) {
94                         delay = Integer.parseInt(vnfDelay.toString());
95                 }
96
97                 //Kick off callback thread
98                 System.out.println("VnfAdapterCreateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);
99                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
100                 callbackResponseThread.start();
101
102                 return ResponseDefinitionBuilder
103                            .like(responseDefinition).but()
104                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
105                            .build();
106
107         }
108
109         @Override
110         public boolean applyGlobally() {
111             return false;
112         }
113
114         private class CallbackResponseThread extends Thread {
115
116                 private String callbackUrl;
117                 private String payLoad;
118                 private int delay;
119
120                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
121                         this.callbackUrl = callbackUrl;
122                         this.payLoad = payLoad;
123                         this.delay = delay;
124                 }
125
126                 @SuppressWarnings("deprecation")
127                 public void run () {
128                         try {
129                                 //Delay sending callback response
130                                 sleep(delay);
131                         } catch (InterruptedException e1) {
132                                 // TODO Auto-generated catch block
133                                 LOGGER.debug("Exception :",e1);
134                         }
135                         System.out.println("Sending callback response to url: " + callbackUrl);
136                         ClientRequest request = new ClientRequest(callbackUrl);
137                         request.body("text/xml", payLoad);
138                         //System.out.println("payLoad: " + payLoad);
139
140                         try {
141                                 ClientResponse result = request.post();
142                                 System.out.println("Successfully posted callback? Status: " + result.getStatus());
143                                 //System.err.println("Successfully posted callback:" + result.getStatus());
144                         } catch (Exception e) {
145                                 // TODO Auto-generated catch block
146                                 System.out.println("catch error in - request.post() ");
147                                 LOGGER.debug("Exception :",e);
148                         }
149                 }
150
151         }
152
153
154 }