d989ba36175971bf2fc353a8a6e130ce53f3165b
[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.time.Instant;
27 import java.util.Timer;
28 import java.util.TimerTask;
29 import java.util.concurrent.BlockingQueue;
30
31 import org.onap.policy.appclcm.AppcLcmBody;
32 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
33 import org.onap.policy.appclcm.AppcLcmInput;
34 import org.onap.policy.appclcm.AppcLcmOutput;
35 import org.onap.policy.controlloop.util.Serialization;
36
37 /**
38  * Respond to an APPC request with a given delay.
39  */
40 public class AppcResponseCreator {
41     // The request from APPC
42     private final String jsonRequestString;
43
44     // The queue for APPC responses
45     private final BlockingQueue<String> appcResponseQueue;
46
47     // The timer task for response generation
48     private final Timer appcTimer;
49
50     private static final Gson gson = new GsonBuilder()
51                     .registerTypeAdapter(Instant.class, new Serialization.GsonInstantAdapter()).create();
52
53     /**
54      * Respond to the given APPC request after the given amount of milliseconds.
55      *
56      * @param appcResponseQueue the queue into which to put the APPC response
57      * @param jsonRequestString the request JSON string
58      * @param milliSecondsToWait the number of milliseconds to wait
59      */
60     public AppcResponseCreator(BlockingQueue<String> appcResponseQueue, String jsonRequestString,
61                     long milliSecondsToWait) {
62         this.jsonRequestString = jsonRequestString;
63         this.appcResponseQueue = appcResponseQueue;
64
65         appcTimer = new Timer();
66         appcTimer.schedule(new AppcTimerTask(), milliSecondsToWait);
67     }
68
69     private class AppcTimerTask extends TimerTask {
70         /**
71          * {@inheritDoc}.
72          */
73         @Override
74         public void run() {
75
76             AppcLcmDmaapWrapper requestWrapper = null;
77             requestWrapper = gson.fromJson(jsonRequestString, AppcLcmDmaapWrapper.class);
78
79             AppcLcmInput request = requestWrapper.getBody().getInput();
80
81             AppcLcmOutput response = new AppcLcmOutput(request);
82             response.getStatus().setCode(400);
83             response.getStatus().setMessage("Restart Successful");
84
85             AppcLcmDmaapWrapper responseWrapper = new AppcLcmDmaapWrapper();
86             responseWrapper.setBody(new AppcLcmBody());
87             responseWrapper.getBody().setOutput(response);
88
89             responseWrapper.setVersion(requestWrapper.getVersion());
90             responseWrapper.setRpcName(requestWrapper.getRpcName());
91             responseWrapper.setCorrelationId(requestWrapper.getCorrelationId());
92             responseWrapper.setType(requestWrapper.getType());
93
94             appcResponseQueue.add(gson.toJson(responseWrapper));
95         }
96     }
97 }