aboutsummaryrefslogtreecommitdiffstats
path: root/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/apps/uservice/test/engdep/EngineTestServer.java
blob: 1a8b1d364d35b93043d35e36ccaa67d80746ff14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.apps.uservice.test.engdep;

import java.util.Date;

import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.apex.service.engine.engdep.EngDepMessagingService;
import org.onap.policy.apex.service.engine.event.ApexEvent;
import org.onap.policy.apex.service.engine.runtime.ApexEventListener;
import org.onap.policy.apex.service.engine.runtime.EngineService;
import org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface;
import org.onap.policy.apex.service.engine.runtime.impl.EngineServiceImpl;
import org.onap.policy.apex.service.parameters.engineservice.EngineServiceParameters;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class EngineTestServer is a test Apex service used to test the performance of Apex engines.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class EngineTestServer implements Runnable, EngineServiceEventInterface {
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(EngineTestServer.class);

    private static final int TEST_SERVER_WAIT_TIME = 200;

    // The engine service for sending events to the Apex engines and the EngDEp service for engine
    // administration
    private EngineService engineService = null;
    private EngDepMessagingService messageService = null;

    // The inner class used to receive and process events
    private TestApexListener testApexListener = null;

    // Status flags
    private boolean starting = true;
    private boolean interrupted = false;

    // Parameters for the test
    private final EngineServiceParameters parameters;

    // Apex performance statistics
    private Date statsStartDate = null;
    private long actionEventsReceivedCount = 0;
    private long accumulatedExecutionTime = 0;
    private long totalActionEventsReceivedCount = 0;

    private ApexEvent lastEventReceived = null;

    /**
     * Instantiates a new engine test server to test Apex performance.
     *
     * @param parameters the parameters
     */
    public EngineTestServer(final EngineServiceParameters parameters) {
        this.parameters = parameters;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        LOGGER.debug("engine<-->deployment  test server starting . . .");

        // Set the name of the test server thread
        Thread.currentThread().setName(EngineTestServer.class.getName());

        try {
            // Create the engine service and set the listener for events emitted by the Apex service
            engineService = EngineServiceImpl.create(parameters);
            testApexListener = new TestApexListener();
            engineService.registerActionListener("testApexListener", testApexListener);

            // Create the EngDep messaging service and start it
            messageService = new EngDepMessagingService(engineService, parameters.getDeploymentPort());
            messageService.start();

            // Record the start date for statistics
            statsStartDate = new Date();
        } catch (final Exception e) {
            LOGGER.error("engine<-->deployment test server exception", e);
            e.printStackTrace();
            return;
        }
        LOGGER.debug("engine<-->deployment test server started");

        starting = false;

        while (!interrupted) {
            if (!ThreadUtilities.sleep(TEST_SERVER_WAIT_TIME)) {
                interrupted = true;
            }
        }
    }

    /**
     * Stop the test server.
     */
    public void stopServer() {
        LOGGER.debug("engine<-->deployment test server stopping . . .");

        interrupted = true;
        messageService.stop();

        LOGGER.debug("engine<-->deployment test server stopped");
    }

    /**
     * Checks if the test server is interrupted.
     *
     * @return true, if is interrupted
     */
    public boolean isInterrupted() {
        return interrupted;
    }

    /**
     * Gets the total action events received.
     *
     * @return the total action events received
     */
    public long getTotalActionEventsReceived() {
        return totalActionEventsReceivedCount;
    }

    /**
     * Gets the last action events received.
     *
     * @return the last action event received
     */
    public ApexEvent getLastActionEvent() {
        return lastEventReceived;
    }

    /**
     * Gets the Apex statistics and resets them.
     *
     * @return the statistics
     */
    public long[] getAndResetStats() {
        // Check if we have statistics
        if (statsStartDate == null || actionEventsReceivedCount == 0) {
            return null;
        }

        // Calculate, save, and reset the statistics
        final long[] stats = new long[2];
        synchronized (statsStartDate) {
            final long averageExecutionTime = accumulatedExecutionTime / actionEventsReceivedCount;
            final long measuringTime = new Date().getTime() - statsStartDate.getTime();
            final long transactionsPerMillisecond = actionEventsReceivedCount / measuringTime;
            stats[0] = averageExecutionTime;
            stats[1] = transactionsPerMillisecond;
            statsStartDate = new Date();

            actionEventsReceivedCount = 0;
            accumulatedExecutionTime = 0;
        }

        // Return the statistics
        return stats;
    }

    /**
     * Checks if the test server is starting.
     *
     * @return true, if the server is starting
     */
    public boolean isStarting() {
        return starting;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.onap.policy.apex.service.engine.runtime.EngineServiceEventInterface#sendEvent(org.onap.
     * policy.apex.service.engine.event.ApexEvent)
     */
    @Override
    public void sendEvent(final ApexEvent event) {
        // Send the event onto the service being tested
        engineService.getEngineServiceEventInterface().sendEvent(event);
    }

    /**
     * The listener interface for receiving testApex events. The class that is interested in
     * processing a testApex event implements this interface, and the object created with that class
     * is registered with a component using the component's {@code addTestApexListener} method. When
     * the testApex event occurs, that object's appropriate method is invoked.
     *
     * This class listens for events from the Apex engine
     *
     * @see TestApexEvent
     */
    private final class TestApexListener implements ApexEventListener {

        /*
         * (non-Javadoc)
         *
         * @see
         * org.onap.policy.apex.service.engine.runtime.ApexEventListener#onApexEvent(org.onap.policy
         * .apex.service.engine.event.ApexEvent)
         */
        @Override
        public synchronized void onApexEvent(final ApexEvent apexEvent) {
            LOGGER.debug("result is:" + apexEvent);

            // Check the result event is correct
            checkResult(apexEvent);

            // Calculate the performance of the Apex engine service on this policy execution run and
            // accumulate the total statistics
            final Date testStartTime = new Date((Long) apexEvent.get("TestTimestamp"));
            final Date testEndTime = new Date();
            final long testTime = testEndTime.getTime() - testStartTime.getTime();
            LOGGER.debug("policy execution time: " + testTime + "ms");
            synchronized (statsStartDate) {
                actionEventsReceivedCount++;
                totalActionEventsReceivedCount++;
                accumulatedExecutionTime += testTime;
            }
            lastEventReceived = apexEvent;
        }

        /**
         * Check that a reply event from the Apex engine is valid.
         *
         * @param result the result event from the Apex engine
         */
        private void checkResult(final ApexEvent result) {
            assert result.getName().startsWith("Event0004") || result.getName().startsWith("Event0104");

            // CHECKSTYLE:OFF: checkstyle:magicNumber
            assert result.get("TestSlogan").equals("This is a test slogan");
            assert result.get("TestMatchCase").equals(new Byte((byte) 123));
            assert result.get("TestTemperature").equals(34.5445667);
            assert ((byte) result.get("TestMatchCaseSelected") >= 0 && (byte) result.get("TestMatchCaseSelected") <= 3);
            assert ((byte) result.get("TestEstablishCaseSelected") >= 0
                    && (byte) result.get("TestEstablishCaseSelected") <= 3);
            assert ((byte) result.get("TestDecideCaseSelected") >= 0
                    && (byte) result.get("TestDecideCaseSelected") <= 3);
            assert ((byte) result.get("TestActCaseSelected") >= 0 && (byte) result.get("TestActCaseSelected") <= 3);
            // CHECKSTYLE:ON: checkstyle:magicNumber
        }
    }
}