db8413c6fb5afcacf68ba15d33774b29aa4d96af
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.producer;
22
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.util.EnumMap;
27 import java.util.Map;
28 import java.util.Properties;
29
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;
41
42 /**
43  * Concrete implementation of an Apex event producer that sends events to a file.
44  *
45  * @author Liam Fallon (liam.fallon@ericsson.com)
46  */
47 public class ApexFileEventProducer implements ApexEventProducer {
48     // Get a reference to the logger
49     private static final Logger LOGGER = LoggerFactory.getLogger(ApexFileEventProducer.class);
50
51     // The name for this producer
52     private String producerName = null;
53
54     // The output stream to write events to
55     private PrintStream eventOutputStream;
56
57     // The peer references for this event handler
58     private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
59             new EnumMap<>(EventHandlerPeeredMode.class);
60
61     /**
62      * {@inheritDoc}
63      */
64     @Override
65     public void init(final String name, final EventHandlerParameters producerParameters) throws ApexEventException {
66         producerName = name;
67
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);
73         }
74
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);
81         }
82         final FileCarrierTechnologyParameters fileCarrierTechnologyParameters =
83                 (FileCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
84
85         // Now we create a writer for events
86         try {
87             if (fileCarrierTechnologyParameters.isStandardError()) {
88                 eventOutputStream = System.err;
89             } else if (fileCarrierTechnologyParameters.isStandardIo()) {
90                 eventOutputStream = System.out;
91             } else {
92                 eventOutputStream =
93                         new PrintStream(new FileOutputStream(fileCarrierTechnologyParameters.getFileName()), true);
94             }
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);
100         }
101
102         if (fileCarrierTechnologyParameters.getStartDelay() > 0) {
103             ThreadUtilities.sleep(fileCarrierTechnologyParameters.getStartDelay());
104         }
105     }
106
107     /**
108      * {@inheritDoc}
109      */
110     @Override
111     public String getName() {
112         return producerName;
113     }
114
115     /**
116      * {@inheritDoc}
117      */
118     @Override
119     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
120         return peerReferenceMap.get(peeredMode);
121     }
122
123     /**
124      * {@inheritDoc}
125      */
126     @Override
127     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
128         peerReferenceMap.put(peeredMode, peeredReference);
129     }
130
131     /**
132      * {@inheritDoc}
133      */
134     @Override
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);
142         }
143
144         // Cast the event to a string, if our conversion is correctly configured, this cast should
145         // always work
146         String stringEvent = null;
147         try {
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);
154         }
155
156         eventOutputStream.println(stringEvent);
157         eventOutputStream.flush();
158     }
159
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public void stop() {
165         eventOutputStream.close();
166     }
167 }