bf2c4c21011a5c3367187ea46c50eb45d6fa6e8a
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / consumer / TextBlockReaderFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.service.engine.event.impl.filecarrierplugin.consumer;
23
24 import java.io.InputStream;
25 import org.onap.policy.apex.service.engine.event.ApexEventException;
26 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolParameters;
27 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextCharDelimitedParameters;
28 import org.onap.policy.apex.service.parameters.eventprotocol.EventProtocolTextTokenDelimitedParameters;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 /**
33  * This factory creates text block readers for breaking character streams into blocks of text.
34  *
35  * @author Liam Fallon (liam.fallon@ericsson.com)
36  */
37 public class TextBlockReaderFactory {
38     // The logger for this class
39     private static final XLogger LOGGER = XLoggerFactory.getXLogger(TextBlockReaderFactory.class);
40
41     /**
42      * Get a text block reader for the given event protocol.
43      *
44      * @param inputStream the input stream that will be used for reading
45      * @param eventProtocolParameters the parameters that have been specified for event protocols
46      * @return the tagged reader
47      * @throws ApexEventException On an unsupported event protocol
48      */
49     public TextBlockReader getTaggedReader(final InputStream inputStream,
50             final EventProtocolParameters eventProtocolParameters) throws ApexEventException {
51         // Check the type of event protocol we have
52         if (eventProtocolParameters instanceof EventProtocolTextCharDelimitedParameters) {
53             // We have character delimited textual input
54             final EventProtocolTextCharDelimitedParameters charDelimitedParameters =
55                     (EventProtocolTextCharDelimitedParameters) eventProtocolParameters;
56
57             // Create the text block reader
58             final TextBlockReader characterDelimitedTextBlockReader =
59                     new CharacterDelimitedTextBlockReader(charDelimitedParameters);
60             characterDelimitedTextBlockReader.init(inputStream);
61             return characterDelimitedTextBlockReader;
62         } else if (eventProtocolParameters instanceof EventProtocolTextTokenDelimitedParameters) {
63             // We have token delimited textual input
64             final EventProtocolTextTokenDelimitedParameters tokenDelimitedParameters =
65                     (EventProtocolTextTokenDelimitedParameters) eventProtocolParameters;
66
67             // Create the text block reader
68             final var headerDelimitedTextBlockReader =
69                     new HeaderDelimitedTextBlockReader(tokenDelimitedParameters);
70             headerDelimitedTextBlockReader.init(inputStream);
71             return headerDelimitedTextBlockReader;
72         } else {
73             final String errorMessage =
74                     "could not create text block reader for a textual event protocol, the required type "
75                             + eventProtocolParameters.getLabel() + " is not supported";
76             LOGGER.error(errorMessage);
77             throw new ApexEventException(errorMessage);
78         }
79     }
80 }