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