2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.EnumMap;
28 import java.util.Properties;
30 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
31 import org.onap.policy.apex.service.engine.event.ApexEventException;
32 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
33 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
34 import org.onap.policy.apex.service.engine.event.PeeredReference;
35 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
36 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Concrete implementation of an Apex event producer that sends events to a file.
45 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class ApexFileEventProducer implements ApexEventProducer {
48 // Get a reference to the logger
49 private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
51 // The name for this producer
52 private String producerName = null;
54 // The output stream to write events to
55 private PrintStream eventOutputStream;
57 // The peer references for this event handler
58 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
59 new EnumMap<>(EventHandlerPeeredMode.class);
65 public void init(final String name, final EventHandlerParameters producerParameters) throws ApexEventException {
68 // Get and check the Apex parameters from the parameter service
69 if (producerParameters == null) {
70 final String errorMessage = "Producer parameters for ApexFileProducer \"" + producerName + "\" is null";
71 LOGGER.warn(errorMessage);
72 throw new ApexEventException(errorMessage);
75 // Check and get the file Properties
76 if (!(producerParameters.getCarrierTechnologyParameters() instanceof FileCarrierTechnologyParameters)) {
77 final String errorMessage = "specified producer properties for ApexFileProducer \"" + producerName
78 + "\" are not applicable to a FILE producer";
79 LOGGER.warn(errorMessage);
80 throw new ApexEventException(errorMessage);
82 final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
83 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
85 // Now we create a writer for events
87 if (fileCarrierTechnologyParameters.isStandardError()) {
88 eventOutputStream = System.err;
89 } else if (fileCarrierTechnologyParameters.isStandardIo()) {
90 eventOutputStream = System.out;
93 new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
95 } catch (final IOException e) {
96 final String errorMessage = "ApexFileProducer \"" + producerName + "\" failed to open file for writing: \""
97 + fileCarrierTechnologyParameters.getFileName() + "\"";
98 LOGGER.warn(errorMessage, e);
99 throw new ApexEventException(errorMessage, e);
102 if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
103 ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
111 public String getName() {
119 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
120 return peerReferenceMap.get(peeredMode);
127 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
128 peerReferenceMap.put(peeredMode, peeredReference);
135 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
136 final Object event) {
137 // Check if this is a synchronized event, if so we have received a reply
138 final SynchronousEventCache synchronousEventCache =
139 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
140 if (synchronousEventCache != null) {
141 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
144 // Cast the event to a string, if our conversion is correctly configured, this cast should
146 String stringEvent = null;
148 stringEvent = (String) event;
149 } catch (final Exception e) {
150 final String errorMessage = "error in ApexFileProducer \"" + producerName + "\" while transferring event \""
151 + event + "\" to the output stream";
152 LOGGER.debug(errorMessage, e);
153 throw new ApexEventRuntimeException(errorMessage, e);
156 eventOutputStream.println(stringEvent);
157 eventOutputStream.flush();
165 eventOutputStream.close();