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;
33 import org.onap.policy.apex.core.protocols.Message;
34 import org.onap.policy.apex.core.protocols.engdep.messages.EngineServiceInfoResponse;
35 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineInfo;
36 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineServiceInfo;
37 import org.onap.policy.apex.core.protocols.engdep.messages.GetEngineStatus;
38 import org.onap.policy.apex.core.protocols.engdep.messages.Response;
39 import org.onap.policy.apex.core.protocols.engdep.messages.StartEngine;
40 import org.onap.policy.apex.core.protocols.engdep.messages.StartPeriodicEvents;
41 import org.onap.policy.apex.core.protocols.engdep.messages.StopEngine;
42 import org.onap.policy.apex.core.protocols.engdep.messages.StopPeriodicEvents;
43 import org.onap.policy.apex.core.protocols.engdep.messages.UpdateModel;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.common.utils.resources.TextFileUtils;
48 * Dummy deployment client.
50 public class DummyDeploymentClient extends DeploymentClient implements Runnable {
51 private static final AxArtifactKey MODEL_KEY = new AxArtifactKey("Model", "0.0.1");
52 private static final AxArtifactKey ENGINE_KEY = new AxArtifactKey("Engine", "0.0.1");
53 private static final AxArtifactKey ENGINE_SERVICE_KEY = new AxArtifactKey("EngineService", "0.0.1");
55 private Thread thisThread;
57 private final BlockingQueue<Message> receiveQueue = new LinkedBlockingQueue<>();
59 private boolean started = false;
61 private boolean initSuccessful = false;
62 private boolean deployModelSuccessful = false;
63 private boolean startEngineSuccessful = false;
64 private boolean stopEngineSuccessful = false;
65 private boolean startPeriodicSuccessful = false;
66 private boolean stopPeriodicSuccessful = false;
67 private boolean statusSuccessful = false;
68 private boolean infoSuccessful = false;
70 public DummyDeploymentClient(String host, int port) {
79 // Set up the thread name
80 thisThread = Thread.currentThread();
81 thisThread.setName(DeploymentClient.class.getName() + "-" + getHost() + ":" + getPort());
85 // Loop forever, sending messages as they appear on the queue
86 await().atLeast(50, TimeUnit.MILLISECONDS).until(() -> !(started && !thisThread.isInterrupted()));
87 // Thread has been interrupted
93 * Send an EngDep message to the Apex server.
95 * @param message the message to send to the Apex server
98 public void sendMessage(final Message message) {
99 if (message instanceof GetEngineServiceInfo) {
100 handleEngineServiceInfo(message);
101 } else if (message instanceof UpdateModel) {
102 deployModelSuccessful = handleAndReturnMessage(message, deployModelSuccessful);
103 } else if (message instanceof StartEngine) {
104 startEngineSuccessful = handleAndReturnMessage(message, startEngineSuccessful);
105 } else if (message instanceof StopEngine) {
106 stopEngineSuccessful = handleAndReturnMessage(message, stopEngineSuccessful);
107 } else if (message instanceof StartPeriodicEvents) {
108 startPeriodicSuccessful = handleAndReturnMessage(message, startPeriodicSuccessful);
109 } else if (message instanceof StopPeriodicEvents) {
110 stopPeriodicSuccessful = handleAndReturnMessage(message, stopPeriodicSuccessful);
111 } else if (message instanceof GetEngineStatus) {
112 statusSuccessful = handleAndReturnEngineStatus(message, statusSuccessful);
113 } else if (message instanceof GetEngineInfo) {
114 infoSuccessful = handleAndReturnMessage(message, infoSuccessful);
119 * Handle the EngineServiceInfo message.
121 * @param message the EngineServiceInfo message
123 private void handleEngineServiceInfo(final Message message) {
124 EngineServiceInfoResponse infoResponse = new EngineServiceInfoResponse(ENGINE_KEY, initSuccessful, message);
125 infoResponse.setApexModelKey(MODEL_KEY);
127 List<AxArtifactKey> engineKeyList = new ArrayList<>();
128 engineKeyList.add(ENGINE_KEY);
129 infoResponse.setEngineKeyArray(engineKeyList);
131 infoResponse.setEngineServiceKey(ENGINE_SERVICE_KEY);
133 receiveQueue.add(infoResponse);
135 initSuccessful = !initSuccessful;
139 * Handle and return the response to the engine status message.
141 * @param message the incoming status message
142 * @param successFlag true if the result should be successful
143 * @return engine status success or not
145 private boolean handleAndReturnEngineStatus(Message message, boolean successFlag) {
146 if ("DoNotRespond".equals(message.getTarget().getName())) {
150 if ("ReturnBadMessage".equals(message.getTarget().getName())) {
151 receiveQueue.add(message);
155 if ("ReturnBadResponse".equals(message.getTarget().getName())) {
156 Response badResponse = new Response(ENGINE_KEY, successFlag, new StartEngine(message.getTarget()));
157 receiveQueue.add(badResponse);
161 Response response = new Response(ENGINE_KEY, successFlag, message);
165 response.setMessageData(TextFileUtils.getTextFileAsString("src/test/resources/models/SmallModel.json"));
166 } catch (IOException e) {
170 response.setMessageData("Operation failed");
173 receiveQueue.add(response);
178 * Handle and return a message.
180 * @param message the message
182 private boolean handleAndReturnMessage(final Message message, final boolean successFlag) {
183 Response response = new Response(ENGINE_KEY, successFlag, message);
186 response.setMessageData("Operation was successful");
188 response.setMessageData("Operation failed");
191 receiveQueue.add(response);
196 * Stop the deployment client.
199 public void stopClient() {
200 if (thisThread != null) {
201 thisThread.interrupt();
207 * Checks if the client thread is started.
209 * @return true, if the client thread is started
212 public boolean isStarted() {
217 * Allows users of this class to get a reference to the receive queue to receove messages.
219 * @return the receive queue
222 public BlockingQueue<Message> getReceiveQueue() {