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