f5f2b2792d0456b18c5336f3b6966c5e3eadb885
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.domains.onap.vcpe;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25
26 import java.util.Timer;
27 import java.util.TimerTask;
28 import java.util.concurrent.BlockingQueue;
29
30 import org.onap.policy.appclcm.LcmRequest;
31 import org.onap.policy.appclcm.LcmRequestWrapper;
32 import org.onap.policy.appclcm.LcmResponse;
33 import org.onap.policy.appclcm.LcmResponseWrapper;
34 import org.onap.policy.appclcm.util.Serialization;
35
36 /**
37  * Respond to an APPC request with a given delay.
38  */
39 public class AppcResponseCreator {
40     // The request from APPC
41     private final String jsonRequestString;
42
43     // The queue for APPC responses
44     private final BlockingQueue<String> appcResponseQueue;
45
46     // The timer task for response generation
47     private final Timer appcTimer;
48
49     /**
50      * Respond to the given APPC request after the given amount of milliseconds.
51      * 
52      * @param appcResponseQueue the queue into which to put the APPC response
53      * @param jsonRequestString the request JSON string
54      * @param milliSecondsToWait the number of milliseconds to wait
55      */
56     public AppcResponseCreator(BlockingQueue<String> appcResponseQueue, String jsonRequestString,
57                     long milliSecondsToWait) {
58         this.jsonRequestString = jsonRequestString;
59         this.appcResponseQueue = appcResponseQueue;
60
61         appcTimer = new Timer();
62         appcTimer.schedule(new AppcTimerTask(), milliSecondsToWait);
63     }
64
65     private class AppcTimerTask extends TimerTask {
66         /*
67          * (non-Javadoc)
68          * 
69          * @see java.util.TimerTask#run()
70          */
71         @Override
72         public void run() {
73             Gson gson = new GsonBuilder().registerTypeAdapter(LcmRequest.class, new Serialization.RequestAdapter())
74                             .registerTypeAdapter(LcmResponse.class, new Serialization.ResponseAdapter())
75                             .setPrettyPrinting().create();
76
77             LcmRequestWrapper requestWrapper = gson.fromJson(jsonRequestString, LcmRequestWrapper.class);
78
79             LcmResponse response = new LcmResponse(requestWrapper.getBody());
80             response.getStatus().setCode(400);
81             response.getStatus().setMessage("Restart Successful");
82
83             LcmResponseWrapper responseWrapper = new LcmResponseWrapper();
84             responseWrapper.setBody(response);
85
86             responseWrapper.setVersion(requestWrapper.getVersion());
87             responseWrapper.setRpcName(requestWrapper.getRpcName());
88             responseWrapper.setCorrelationId(requestWrapper.getCorrelationId());
89             responseWrapper.setType(requestWrapper.getType());
90
91             appcResponseQueue.add(gson.toJson(responseWrapper, LcmResponseWrapper.class));
92         }
93     }
94 }