summaryrefslogtreecommitdiffstats
path: root/services/services-engine/src/main/java/org/onap/policy/apex/service/engine/event/impl/filecarrierplugin/consumer/ApexFileEventConsumer.java
blob: e99663c9ee7ae5f68929528dd28269400cec9540 (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
/*-
 * ============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.service.engine.event.impl.filecarrierplugin.consumer;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
import org.onap.policy.apex.service.engine.event.PeeredReference;
import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Concrete implementation an Apex event consumer that reads events from a file. This consumer also implements
 * ApexEventProducer and therefore can be used as a synchronous consumer.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ApexFileEventConsumer implements ApexEventConsumer, Runnable {
    // Get a reference to the logger
    private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventConsumer.class);

    // Recurring string constants
    private static final String APEX_FILE_CONSUMER_PREAMBLE = "ApexFileConsumer \"";

    // The input stream to read events from
    private InputStream eventInputStream;

    // The text block reader that will read text blocks from the contents of the file
    private TextBlockReader textBlockReader;

    // The event receiver that will receive asynchronous events from this consumer
    private ApexEventReceiver eventReceiver = null;

    // The consumer thread and stopping flag
    private Thread consumerThread;

    // The name for this consumer
    private String consumerName = null;

    // The specific carrier technology parameters for this consumer
    private FileCarrierTechnologyParameters fileCarrierTechnologyParameters;

    // The peer references for this event handler
    private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(
                    EventHandlerPeeredMode.class);

    // Holds the next identifier for event execution.
    private static AtomicLong nextExecutionID = new AtomicLong(0L);

    /**
     * Private utility to get the next candidate value for a Execution ID. This value will always be unique in a single
     * JVM
     *
     * @return the next candidate value for a Execution ID
     */
    private static synchronized long getNextExecutionId() {
        return nextExecutionID.getAndIncrement();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void init(final String name, final EventHandlerParameters consumerParameters,
                    final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
        this.eventReceiver = incomingEventReceiver;
        this.consumerName = name;

        // Get and check the Apex parameters from the parameter service
        if (consumerParameters == null) {
            final String errorMessage = "Consumer parameters for ApexFileConsumer \"" + consumerName + "\" is null";
            LOGGER.warn(errorMessage);
            throw new ApexEventException(errorMessage);
        }

        // Check and get the file Properties
        if (!(consumerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
            final String errorMessage = "specified consumer properties for ApexFileConsumer \"" + consumerName
                            + "\" are not applicable to a File consumer";
            LOGGER.warn(errorMessage);
            throw new ApexEventException(errorMessage);
        }
        fileCarrierTechnologyParameters = (FileCarrierTechnologyParameters) consumerParameters
                        .getCarrierTechnologyParameters();

        // Open the file producing events
        try {
            if (fileCarrierTechnologyParameters.isStandardIo()) {
                eventInputStream = System.in;
            } else {
                eventInputStream = new FileInputStream(fileCarrierTechnologyParameters.getFileName());
            }

            // Get an event composer for our event source
            textBlockReader = new TextBlockReaderFactory().getTaggedReader(eventInputStream,
                            consumerParameters.getEventProtocolParameters());
        } catch (final IOException e) {
            final String errorMessage = APEX_FILE_CONSUMER_PREAMBLE + consumerName
                            + "\" failed to open file for reading: \"" + fileCarrierTechnologyParameters.getFileName()
                            + "\"";
            LOGGER.warn(errorMessage, e);
            throw new ApexEventException(errorMessage, e);
        }

        if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
            ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
        }
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public String getName() {
        return consumerName;
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
        return peerReferenceMap.get(peeredMode);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
        peerReferenceMap.put(peeredMode, peeredReference);
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void start() {
        // Configure and start the event reception thread
        final String threadName = this.getClass().getName() + " : " + consumerName;
        consumerThread = new ApplicationThreadFactory(threadName).newThread(this);
        consumerThread.setDaemon(true);
        consumerThread.start();
    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void run() {
        // Check that we have been initialized in async or sync mode
        if (eventReceiver == null) {
            LOGGER.warn("\"{}\" has not been initilaized for either asynchronous or synchronous event handling",
                            consumerName);
            return;
        }

        // Read the events from the file while there are still events in the file
        try {
            // Read all the text blocks
            TextBlock textBlock;
            do {
                // Read the text block
                textBlock = textBlockReader.readTextBlock();

                // Process the event from the text block if there is one there
                if (textBlock.getText() != null) {
                    eventReceiver.receiveEvent(getNextExecutionId(), null, textBlock.getText());
                }
            }
            while (!textBlock.isEndOfText());
        } catch (final Exception e) {
            LOGGER.warn("\"" + consumerName + "\" failed to read event from file: \""
                            + fileCarrierTechnologyParameters.getFileName() + "\"", e);
        } finally {
            try {
                eventInputStream.close();
            } catch (final IOException e) {
                LOGGER.warn(APEX_FILE_CONSUMER_PREAMBLE + consumerName + "\" failed to close file: \""
                                + fileCarrierTechnologyParameters.getFileName() + "\"", e);
            }
        }

    }

    /**
     * {@inheritDoc}.
     */
    @Override
    public void stop() {
        try {
            eventInputStream.close();
        } catch (final IOException e) {
            LOGGER.warn(APEX_FILE_CONSUMER_PREAMBLE + consumerName + "\" failed to close file for reading: \""
                            + fileCarrierTechnologyParameters.getFileName() + "\"", e);
        }

        if (consumerThread.isAlive() && !consumerThread.isInterrupted()) {
            consumerThread.interrupt();
        }
    }
}