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