bmpn sonar issue fix
[so.git] / bpmn / MSOMockServer / src / main / java / org / openecomp / mso / bpmn / mock / VnfAdapterUpdateMockTransformer.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 org.jboss.resteasy.client.ClientRequest;
28 import org.jboss.resteasy.client.ClientResponse;
29 import org.openecomp.mso.logger.MsoLogger;
30
31 import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
32 import com.github.tomakehurst.wiremock.common.FileSource;
33 import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
34 import com.github.tomakehurst.wiremock.http.Request;
35 import com.github.tomakehurst.wiremock.http.ResponseDefinition;
36
37 import org.openecomp.mso.logger.MsoLogger;
38 /**
39  * Please describe the VnfAdapterUpdateMockTransformer.java class
40  *
41  */
42 public class VnfAdapterUpdateMockTransformer extends ResponseTransformer {
43
44         private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
45         
46         private String notifyCallbackResponse;
47         private String requestId;
48         private String ackResponse;
49
50         public VnfAdapterUpdateMockTransformer() {
51                 notifyCallbackResponse = FileUtil.readResourceFile("vnfAdapter/vnfUpdateSimResponse.xml");
52         }
53
54         public VnfAdapterUpdateMockTransformer(String requestId) {
55                 this.requestId = requestId;
56         }
57
58
59         public String name() {
60                 return "vnf-adapter-update-transformer";
61         }
62
63         @Override
64         public ResponseDefinition transform(Request request, ResponseDefinition responseDefinition,
65                         FileSource fileSource) {
66
67                 String requestBody = request.getBodyAsString();
68
69                 String notficationUrl = requestBody.substring(requestBody.indexOf("<notificationUrl>")+17, requestBody.indexOf("</notificationUrl>"));
70                 String messageId = requestBody.substring(requestBody.indexOf("<messageId>")+11, requestBody.indexOf("</messageId>"));
71                 String responseMessageId = "";
72                 String updatedResponse = "";
73                 
74                 try {
75                         // try supplied response file (if any)
76                         System.out.println(" Supplied fileName: " + responseDefinition.getBodyFileName());
77                     ackResponse = FileUtil.readResourceFile("__files/" + responseDefinition.getBodyFileName());
78                         notifyCallbackResponse = ackResponse;
79                         responseMessageId = ackResponse.substring(ackResponse.indexOf("<messageId>")+11, ackResponse.indexOf("</messageId>"));
80                     updatedResponse = ackResponse.replace(responseMessageId, messageId); 
81                 } catch (Exception ex) {
82                         LOGGER.debug("Exception :",ex);
83                         System.out.println(" ******* Use default response file in 'vnfAdapter/vnfUpdateSimResponse.xml'");
84                     responseMessageId = notifyCallbackResponse.substring(notifyCallbackResponse.indexOf("<messageId>")+11, notifyCallbackResponse.indexOf("</messageId>"));
85                         updatedResponse = notifyCallbackResponse.replace(responseMessageId, messageId);
86                 }
87                 
88                 System.out.println("response (mock) messageId       : " + responseMessageId);           
89                 System.out.println("request  (replacement) messageId: " + messageId);
90                 
91                 System.out.println("vnf Response (before):" + notifyCallbackResponse);
92                 System.out.println("vnf Response (after):" + updatedResponse);
93                 
94                 Object vnfDelay = MockResource.getMockProperties().get("vnf_delay");
95                 int delay = 300;
96                 if (vnfDelay != null) {
97                         delay = Integer.parseInt(vnfDelay.toString());
98                 }
99
100                 //Kick off callback thread
101                 System.out.println("VnfAdapterUpdateMockTransformer notficationUrl: " + notficationUrl + ":delay: " + delay);           
102                 CallbackResponseThread callbackResponseThread = new CallbackResponseThread(notficationUrl,updatedResponse, delay);
103                 callbackResponseThread.start();
104
105                 return ResponseDefinitionBuilder
106                            .like(responseDefinition).but()
107                            .withStatus(200).withBody(updatedResponse).withHeader("Content-Type", "text/xml")
108                            .build();
109
110         }
111
112         @Override
113         public boolean applyGlobally() {
114             return false;
115         }
116
117         private class CallbackResponseThread extends Thread {
118
119                 private String callbackUrl;
120                 private String payLoad;
121                 private int delay;
122
123                 public CallbackResponseThread(String callbackUrl, String payLoad, int delay) {
124                         this.callbackUrl = callbackUrl;
125                         this.payLoad = payLoad;
126                         this.delay = delay;
127                 }
128
129                 public void run () {
130                         try {
131                                 //Delay sending callback response
132                                 sleep(delay);
133                         } catch (InterruptedException e1) {
134                                 LOGGER.debug("Exception :", e1);
135                         }
136                         System.out.println("Sending callback response to url: " + callbackUrl);                 
137                         ClientRequest request = new ClientRequest(callbackUrl);
138                         request.body("text/xml", payLoad);
139                         //System.err.println(payLoad);
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                                 System.out.println("catch error in - request.post() ");
146                                 LOGGER.debug("Exception :",e);
147                         }
148                 }
149
150         }
151 }
152