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