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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.core.deployment;
24 import static org.awaitility.Awaitility.await;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.concurrent.BlockingQueue;
30 import java.util.concurrent.LinkedBlockingQueue;
31 import java.util.concurrent.TimeUnit;
32 import org.onap.policy.apex.core.protocols.Message;
33 import org.onap.policy.apex.core.protocols.engdep.messages.EngineServiceInfoResponse;
34 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
35 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo;
36 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
37 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
38 import org.onap.policy.apex.core.protocols.engdep.messages.StartEngine;
39 import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents;
40 import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine;
41 import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents;
42 import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
47 * Dummy deployment client.
49 public class DummyDeploymentClient extends DeploymentClient implements Runnable {
50 private static final AxArtifactKey MODEL_KEY = new AxArtifactKey("Model", "0.0.1");
51 private static final AxArtifactKey ENGINE_KEY = new AxArtifactKey("Engine", "0.0.1");
52 private static final AxArtifactKey ENGINE_SERVICE_KEY = new AxArtifactKey("EngineService", "0.0.1");
54 private Thread thisThread;
56 private final BlockingQueue<Message> receiveQueue = new LinkedBlockingQueue<>();
58 private boolean started = false;
60 private boolean initSuccessful = false;
61 private boolean deployModelSuccessful = false;
62 private boolean startEngineSuccessful = false;
63 private boolean stopEngineSuccessful = false;
64 private boolean startPeriodicSuccessful = false;
65 private boolean stopPeriodicSuccessful = false;
66 private boolean statusSuccessful = false;
67 private boolean infoSuccessful = false;
69 public DummyDeploymentClient(String host, int port) {
78 // Set up the thread name
79 thisThread = Thread.currentThread();
80 thisThread.setName(DeploymentClient.class.getName() + "-" + getHost() + ":" + getPort());
84 // Loop forever, sending messages as they appear on the queue
85 await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !(started && !thisThread.isInterrupted()));
86 // Thread has been interrupted
92 * Send an EngDep message to the Apex server.
94 * @param message the message to send to the Apex server
97 public void sendMessage(final Message message) {
98 if (message instanceof GetEngineServiceInfo) {
99 handleEngineServiceInfo(message);
100 } else if (message instanceof UpdateModel) {
101 deployModelSuccessful = handleAndReturnMessage(message, deployModelSuccessful);
102 } else if (message instanceof StartEngine) {
103 startEngineSuccessful = handleAndReturnMessage(message, startEngineSuccessful);
104 } else if (message instanceof StopEngine) {
105 stopEngineSuccessful = handleAndReturnMessage(message, stopEngineSuccessful);
106 } else if (message instanceof StartPeriodicEvents) {
107 startPeriodicSuccessful = handleAndReturnMessage(message, startPeriodicSuccessful);
108 } else if (message instanceof StopPeriodicEvents) {
109 stopPeriodicSuccessful = handleAndReturnMessage(message, stopPeriodicSuccessful);
110 } else if (message instanceof GetEngineStatus) {
111 statusSuccessful = handleAndReturnEngineStatus(message, statusSuccessful);
112 } else if (message instanceof GetEngineInfo) {
113 infoSuccessful = handleAndReturnMessage(message, infoSuccessful);
118 * Handle the EngineServiceInfo message.
120 * @param message the EngineServiceInfo message
122 private void handleEngineServiceInfo(final Message message) {
123 EngineServiceInfoResponse infoResponse = new EngineServiceInfoResponse(ENGINE_KEY, initSuccessful, message);
124 infoResponse.setApexModelKey(MODEL_KEY);
126 List<AxArtifactKey> engineKeyList = new ArrayList<>();
127 engineKeyList.add(ENGINE_KEY);
128 infoResponse.setEngineKeyArray(engineKeyList);
130 infoResponse.setEngineServiceKey(ENGINE_SERVICE_KEY);
132 receiveQueue.add(infoResponse);
134 initSuccessful = !initSuccessful;
138 * Handle and return the response to the engine status message.
140 * @param message the incoming status message
141 * @param successFlag true if the result should be successful
142 * @return engine status success or not
144 private boolean handleAndReturnEngineStatus(Message message, boolean successFlag) {
145 if ("DoNotRespond".equals(message.getTarget().getName())) {
149 if ("ReturnBadMessage".equals(message.getTarget().getName())) {
150 receiveQueue.add(message);
154 if ("ReturnBadResponse".equals(message.getTarget().getName())) {
155 Response badResponse = new Response(ENGINE_KEY, successFlag, new StartEngine(message.getTarget()));
156 receiveQueue.add(badResponse);
160 Response response = new Response(ENGINE_KEY, successFlag, message);
164 response.setMessageData(TextFileUtils.getTextFileAsString("src/test/resources/models/SmallModel.json"));
165 } catch (IOException e) {
169 response.setMessageData("Operation failed");
172 receiveQueue.add(response);
177 * Handle and return a message.
179 * @param message the message
181 private boolean handleAndReturnMessage(final Message message, final boolean successFlag) {
182 Response response = new Response(ENGINE_KEY, successFlag, message);
185 response.setMessageData("Operation was successful");
187 response.setMessageData("Operation failed");
190 receiveQueue.add(response);
195 * Stop the deployment client.
198 public void stopClient() {
199 if (thisThread != null) {
200 thisThread.interrupt();
206 * Checks if the client thread is started.
208 * @return true, if the client thread is started
211 public boolean isStarted() {
216 * Allows users of this class to get a reference to the receive queue to receove messages.
218 * @return the receive queue
221 public BlockingQueue<Message> getReceiveQueue() {