2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.domains.onap.vcpe;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import java.time.Instant;
28 import java.util.Timer;
29 import java.util.TimerTask;
30 import java.util.concurrent.BlockingQueue;
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.common.gson.InstantAsMillisTypeAdapter;
38 * Respond to an APPC request with a given delay.
40 public class AppcResponseCreator {
41 // The request from APPC
42 private final String jsonRequestString;
44 // The queue for APPC responses
45 private final BlockingQueue<String> appcResponseQueue;
47 // The timer task for response generation
48 private final Timer appcTimer;
50 private static final Gson gson = new GsonBuilder()
51 .registerTypeAdapter(Instant.class, new InstantAsMillisTypeAdapter()).setPrettyPrinting().create();
53 private static Integer nextResponseCode = 400;
56 * Respond to the given APPC request after the given amount of milliseconds.
58 * @param appcResponseQueue the queue into which to put the APPC response
59 * @param jsonRequestString the request JSON string
60 * @param milliSecondsToWait the number of milliseconds to wait
62 public AppcResponseCreator(BlockingQueue<String> appcResponseQueue, String jsonRequestString,
63 long milliSecondsToWait) {
64 this.jsonRequestString = jsonRequestString;
65 this.appcResponseQueue = appcResponseQueue;
67 appcTimer = new Timer();
68 appcTimer.schedule(new AppcTimerTask(), milliSecondsToWait);
71 private class AppcTimerTask extends TimerTask {
78 AppcLcmDmaapWrapper requestWrapper = null;
79 requestWrapper = gson.fromJson(jsonRequestString, AppcLcmDmaapWrapper.class);
81 AppcLcmInput request = requestWrapper.getBody().getInput();
83 AppcLcmOutput response = new AppcLcmOutput(request);
85 response.getStatus().setCode(nextResponseCode);
86 if (nextResponseCode == 400) {
87 response.getStatus().setMessage("Restart Successful");
88 nextResponseCode = 100;
89 } else if (nextResponseCode == 100) {
90 response.getStatus().setMessage("Restart Request Accepted");
91 nextResponseCode = 200;
93 response.getStatus().setMessage("Error in Restart Operation");
94 nextResponseCode = 400;
97 response.setPayload("");
99 AppcLcmDmaapWrapper responseWrapper = new AppcLcmDmaapWrapper();
100 responseWrapper.setBody(new AppcLcmBody());
101 responseWrapper.getBody().setOutput(response);
103 responseWrapper.setVersion(requestWrapper.getVersion());
104 responseWrapper.setRpcName(requestWrapper.getRpcName());
105 responseWrapper.setCorrelationId(requestWrapper.getCorrelationId());
106 responseWrapper.setType(requestWrapper.getType());
108 appcResponseQueue.add(gson.toJson(responseWrapper));