4eda2eeba9e59c115eaf9f1879392ec2ddbe71dd
[policy/apex-pdp.git] / core / core-deployment / src / test / java / org / onap / policy / apex / core / deployment / DummyDeploymentClient.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.core.deployment;
23
24 import static org.awaitility.Awaitility.await;
25
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
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;
46
47 /**
48  * Dummy deployment client.
49  */
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");
54
55     private Thread thisThread;
56
57     private final BlockingQueue<Message> receiveQueue = new LinkedBlockingQueue<>();
58
59     private boolean started = false;
60
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;
69
70     public DummyDeploymentClient(String host, int port) {
71         super(host, port);
72     }
73
74     /**
75      * {@inheritDoc}.
76      */
77     @Override
78     public void run() {
79         // Set up the thread name
80         thisThread = Thread.currentThread();
81         thisThread.setName(DeploymentClient.class.getName() + "-" + getHost() + ":" + getPort());
82
83         started = true;
84
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
88         thisThread = null;
89         started = false;
90     }
91
92     /**
93      * Send an EngDep message to the Apex server.
94      *
95      * @param message the message to send to the Apex server
96      */
97     @Override
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);
115         }
116     }
117
118     /**
119      * Handle the EngineServiceInfo message.
120      *
121      * @param message the EngineServiceInfo message
122      */
123     private void handleEngineServiceInfo(final Message message) {
124         EngineServiceInfoResponse infoResponse = new EngineServiceInfoResponse(ENGINE_KEY, initSuccessful, message);
125         infoResponse.setApexModelKey(MODEL_KEY);
126
127         List<AxArtifactKey> engineKeyList = new ArrayList<>();
128         engineKeyList.add(ENGINE_KEY);
129         infoResponse.setEngineKeyArray(engineKeyList);
130
131         infoResponse.setEngineServiceKey(ENGINE_SERVICE_KEY);
132
133         receiveQueue.add(infoResponse);
134
135         initSuccessful = !initSuccessful;
136     }
137
138     /**
139      * Handle and return the response to the engine status message.
140      *
141      * @param message the incoming status message
142      * @param successFlag true if the result should be successful
143      * @return engine status success or not
144      */
145     private boolean handleAndReturnEngineStatus(Message message, boolean successFlag) {
146         if ("DoNotRespond".equals(message.getTarget().getName())) {
147             return !successFlag;
148         }
149
150         if ("ReturnBadMessage".equals(message.getTarget().getName())) {
151             receiveQueue.add(message);
152             return !successFlag;
153         }
154
155         if ("ReturnBadResponse".equals(message.getTarget().getName())) {
156             Response badResponse = new Response(ENGINE_KEY, successFlag, new StartEngine(message.getTarget()));
157             receiveQueue.add(badResponse);
158             return !successFlag;
159         }
160
161         Response response = new Response(ENGINE_KEY, successFlag, message);
162
163         if (successFlag) {
164             try {
165                 response.setMessageData(TextFileUtils.getTextFileAsString("src/test/resources/models/SmallModel.json"));
166             } catch (IOException e) {
167                 e.printStackTrace();
168             }
169         } else {
170             response.setMessageData("Operation failed");
171         }
172
173         receiveQueue.add(response);
174         return !successFlag;
175     }
176
177     /**
178      * Handle and return a message.
179      *
180      * @param message the message
181      */
182     private boolean handleAndReturnMessage(final Message message, final boolean successFlag) {
183         Response response = new Response(ENGINE_KEY, successFlag, message);
184
185         if (successFlag) {
186             response.setMessageData("Operation was successful");
187         } else {
188             response.setMessageData("Operation failed");
189         }
190
191         receiveQueue.add(response);
192         return !successFlag;
193     }
194
195     /**
196      * Stop the deployment client.
197      */
198     @Override
199     public void stopClient() {
200         if (thisThread != null) {
201             thisThread.interrupt();
202         }
203         started = false;
204     }
205
206     /**
207      * Checks if the client thread is started.
208      *
209      * @return true, if the client thread is started
210      */
211     @Override
212     public boolean isStarted() {
213         return started;
214     }
215
216     /**
217      * Allows users of this class to get a reference to the receive queue to receove messages.
218      *
219      * @return the receive queue
220      */
221     @Override
222     public BlockingQueue<Message> getReceiveQueue() {
223         return receiveQueue;
224     }
225 }