b0dac33724f5ef183dbcf57c4d0c41e5818b5950
[policy/apex-pdp.git] / services / services-engine / src / main / java / org / onap / policy / apex / service / engine / event / impl / filecarrierplugin / producer / ApexFileEventProducer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.service.engine.event.impl.filecarrierplugin.producer;
23
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.PrintStream;
27 import java.util.Properties;
28
29 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
30 import org.onap.policy.apex.service.engine.event.ApexEventException;
31 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
32 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
33 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Concrete implementation of an Apex event producer that sends events to a file.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class ApexFileEventProducer extends ApexPluginsEventProducer {
44     // Get a reference to the logger
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
46
47     // The output stream to write events to
48     private PrintStream eventOutputStream;
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public void init(final String producerName, final EventHandlerParameters producerParameters)
55             throws ApexEventException {
56         this.name = producerName;
57
58         // Get and check the Apex parameters from the parameter service
59         if (producerParameters == null) {
60             final String errorMessage = "Producer parameters for ApexFileProducer \"" + producerName + "\" is null";
61             LOGGER.warn(errorMessage);
62             throw new ApexEventException(errorMessage);
63         }
64
65         // Check and get the file Properties
66         if (!(producerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
67             final String errorMessage = "specified producer properties for ApexFileProducer \"" + producerName
68                     + "\" are not applicable to a FILE producer";
69             LOGGER.warn(errorMessage);
70             throw new ApexEventException(errorMessage);
71         }
72         final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
73                 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
74
75         // Now we create a writer for events
76         try {
77             if (fileCarrierTechnologyParameters.isStandardError()) {
78                 eventOutputStream = System.err;
79             } else if (fileCarrierTechnologyParameters.isStandardIo()) {
80                 eventOutputStream = System.out;
81             } else {
82                 eventOutputStream =
83                         new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
84             }
85         } catch (final IOException e) {
86             final String errorMessage = "ApexFileProducer \"" + producerName + "\" failed to open file for writing: \""
87                     + fileCarrierTechnologyParameters.getFileName() + "\"";
88             throw new ApexEventException(errorMessage, e);
89         }
90
91         if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
92             ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
93         }
94     }
95
96     /**
97      * {@inheritDoc}.
98      */
99     @Override
100     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
101             final Object event) {
102         super.sendEvent(executionId, executionProperties, eventName, event);
103
104         // Cast the event to a string, if our conversion is correctly configured, this cast should
105         // always work
106         String stringEvent = null;
107         try {
108             stringEvent = (String) event;
109         } catch (final Exception e) {
110             final String errorMessage = "error in ApexFileProducer \"" + name + "\" while transferring event \"" + event
111                     + "\" to the output stream";
112             throw new ApexEventRuntimeException(errorMessage, e);
113         }
114
115         eventOutputStream.println(stringEvent);
116         eventOutputStream.flush();
117     }
118
119     /**
120      * {@inheritDoc}.
121      */
122     @Override
123     public void stop() {
124         eventOutputStream.close();
125     }
126 }