5209f6453655969bde486d314b6fcda72f62d90e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.EnumMap;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
32 import org.onap.policy.apex.service.engine.event.ApexEventException;
33 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
34 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
35 import org.onap.policy.apex.service.engine.event.PeeredReference;
36 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
37 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
39 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * Concrete implementation of an Apex event producer that sends events to a file.
45  *
46  * @author Liam Fallon (liam.fallon@ericsson.com)
47  */
48 public class ApexFileEventProducer implements ApexEventProducer {
49     // Get a reference to the logger
50     private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
51
52     // The name for this producer
53     private String producerName = null;
54
55     // The output stream to write events to
56     private PrintStream eventOutputStream;
57
58     // The peer references for this event handler
59     private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
60             new EnumMap<>(EventHandlerPeeredMode.class);
61
62     /**
63      * {@inheritDoc}.
64      */
65     @Override
66     public void init(final String name, final EventHandlerParameters producerParameters) throws ApexEventException {
67         producerName = name;
68
69         // Get and check the Apex parameters from the parameter service
70         if (producerParameters == null) {
71             final String errorMessage = "Producer parameters for ApexFileProducer \"" + producerName + "\" is null";
72             LOGGER.warn(errorMessage);
73             throw new ApexEventException(errorMessage);
74         }
75
76         // Check and get the file Properties
77         if (!(producerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
78             final String errorMessage = "specified producer properties for ApexFileProducer \"" + producerName
79                     + "\" are not applicable to a FILE producer";
80             LOGGER.warn(errorMessage);
81             throw new ApexEventException(errorMessage);
82         }
83         final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
84                 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
85
86         // Now we create a writer for events
87         try {
88             if (fileCarrierTechnologyParameters.isStandardError()) {
89                 eventOutputStream = System.err;
90             } else if (fileCarrierTechnologyParameters.isStandardIo()) {
91                 eventOutputStream = System.out;
92             } else {
93                 eventOutputStream =
94                         new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
95             }
96         } catch (final IOException e) {
97             final String errorMessage = "ApexFileProducer \"" + producerName + "\" failed to open file for writing: \""
98                     + fileCarrierTechnologyParameters.getFileName() + "\"";
99             LOGGER.warn(errorMessage, e);
100             throw new ApexEventException(errorMessage, e);
101         }
102
103         if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
104             ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
105         }
106     }
107
108     /**
109      * {@inheritDoc}.
110      */
111     @Override
112     public String getName() {
113         return producerName;
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
120     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
121         return peerReferenceMap.get(peeredMode);
122     }
123
124     /**
125      * {@inheritDoc}.
126      */
127     @Override
128     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
129         peerReferenceMap.put(peeredMode, peeredReference);
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
137             final Object event) {
138         // Check if this is a synchronized event, if so we have received a reply
139         final SynchronousEventCache synchronousEventCache =
140                 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
141         if (synchronousEventCache != null) {
142             synchronousEventCache.removeCachedEventToApexIfExists(executionId);
143         }
144
145         // Cast the event to a string, if our conversion is correctly configured, this cast should
146         // always work
147         String stringEvent = null;
148         try {
149             stringEvent = (String) event;
150         } catch (final Exception e) {
151             final String errorMessage = "error in ApexFileProducer \"" + producerName + "\" while transferring event \""
152                     + event + "\" to the output stream";
153             LOGGER.debug(errorMessage, e);
154             throw new ApexEventRuntimeException(errorMessage, e);
155         }
156
157         eventOutputStream.println(stringEvent);
158         eventOutputStream.flush();
159     }
160
161     /**
162      * {@inheritDoc}.
163      */
164     @Override
165     public void stop() {
166         eventOutputStream.close();
167     }
168 }