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;
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.ApexEventProducer;
32 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
33 import org.onap.policy.apex.service.engine.event.PeeredReference;
34 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
35 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.FileCarrierTechnologyParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Concrete implementation of an Apex event producer that sends events to a file.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class ApexFileEventProducer implements ApexEventProducer {
47 // Get a reference to the logger
48 private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
50 // The name for this producer
51 private String producerName = null;
53 // The output stream to write events to
54 private PrintStream eventOutputStream;
56 // The peer references for this event handler
57 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
58 new EnumMap<>(EventHandlerPeeredMode.class);
63 * @see org.onap.policy.apex.apps.uservice.producer.ApexEventProducer#init()
66 public void init(final String name, final EventHandlerParameters producerParameters) throws ApexEventException {
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);
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);
83 final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
84 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
86 // Now we create a writer for events
88 if (fileCarrierTechnologyParameters.isStandardError()) {
89 eventOutputStream = System.err;
90 } else if (fileCarrierTechnologyParameters.isStandardIo()) {
91 eventOutputStream = System.out;
94 new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
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);
103 if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
104 ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
111 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getName()
114 public String getName() {
121 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getPeeredReference(org.onap.
122 * policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode)
125 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
126 return peerReferenceMap.get(peeredMode);
132 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#setPeeredReference(org.onap.
133 * policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode,
134 * org.onap.policy.apex.service.engine.event.PeeredReference)
137 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
138 peerReferenceMap.put(peeredMode, peeredReference);
144 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#sendEvent(long,
145 * java.lang.String, java.lang.Object)
148 public void sendEvent(final long executionId, final String eventName, final Object event) {
149 // Check if this is a synchronized event, if so we have received a reply
150 final SynchronousEventCache synchronousEventCache =
151 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
152 if (synchronousEventCache != null) {
153 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
156 // Cast the event to a string, if our conversion is correctly configured, this cast should
158 String stringEvent = null;
160 stringEvent = (String) event;
161 } catch (final Exception e) {
162 final String errorMessage = "error in ApexFileProducer \"" + producerName + "\" while transferring event \""
163 + event + "\" to the output stream";
164 LOGGER.debug(errorMessage, e);
165 throw new ApexEventRuntimeException(errorMessage, e);
168 eventOutputStream.println(stringEvent);
169 eventOutputStream.flush();
175 * @see org.onap.policy.apex.apps.uservice.producer.ApexEventProducer#stop()
179 eventOutputStream.close();