10f425302fe8b0e67aa94ce8ad02d15611152a2a
[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 java.util.Timer;
24 import java.util.TimerTask;
25 import java.util.concurrent.BlockingQueue;
26
27 import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
28 import org.onap.policy.appclcm.AppcLcmInput;
29 import org.onap.policy.appclcm.AppcLcmOutput;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * Respond to an APPC request with a given delay.
37  */
38 public class AppcResponseCreator {
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(AppcResponseCreator.class);
40
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     /**
51      * Respond to the given APPC request after the given amount of milliseconds.
52      *
53      * @param appcResponseQueue the queue into which to put the APPC response
54      * @param jsonRequestString the request JSON string
55      * @param milliSecondsToWait the number of milliseconds to wait
56      */
57     public AppcResponseCreator(BlockingQueue<String> appcResponseQueue, String jsonRequestString,
58                     long milliSecondsToWait) {
59         this.jsonRequestString = jsonRequestString;
60         this.appcResponseQueue = appcResponseQueue;
61
62         appcTimer = new Timer();
63         appcTimer.schedule(new AppcTimerTask(), milliSecondsToWait);
64     }
65
66     private class AppcTimerTask extends TimerTask {
67         /**
68          * {@inheritDoc}.
69          */
70         @Override
71         public void run() {
72
73             StandardCoder standardCoder = new StandardCoder();
74
75             AppcLcmDmaapWrapper requestWrapper = null;
76             try {
77                 requestWrapper = standardCoder.decode(jsonRequestString, AppcLcmDmaapWrapper.class);
78             } catch (CoderException e) {
79                 LOGGER.warn("decoding of the APPC request message failed", e);
80                 return;
81             }
82
83             AppcLcmInput request = requestWrapper.getBody().getInput();
84
85             AppcLcmOutput response = new AppcLcmOutput(request);
86             response.getStatus().setCode(400);
87             response.getStatus().setMessage("Restart Successful");
88
89             AppcLcmDmaapWrapper responseWrapper = new AppcLcmDmaapWrapper();
90             responseWrapper.getBody().setOutput(response);
91
92             responseWrapper.setVersion(requestWrapper.getVersion());
93             responseWrapper.setRpcName(requestWrapper.getRpcName());
94             responseWrapper.setCorrelationId(requestWrapper.getCorrelationId());
95             responseWrapper.setType(requestWrapper.getType());
96
97             try {
98                 appcResponseQueue.add(standardCoder.encode(responseWrapper));
99             } catch (CoderException e) {
100                 LOGGER.warn("encoding of the APPC request message failed", e);
101                 return;
102             }
103         }
104     }
105 }