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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.service.engine.event.impl.eventrequestor;
24 import java.util.EnumMap;
26 import java.util.Properties;
28 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
31 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
32 import org.onap.policy.apex.service.engine.event.PeeredReference;
33 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Concrete implementation of an Apex event producer that sends one or more events to its peered event requestor
43 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class EventRequestorProducer implements ApexEventProducer {
47 private static final Logger LOGGER = LoggerFactory.getLogger(EventRequestorProducer.class);
49 // The name for this producer
50 private String name = null;
52 // The peer references for this event handler
53 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
54 new EnumMap<>(EventHandlerPeeredMode.class);
56 // The number of events sent
57 private int eventsSent = 0;
63 public void init(final String producerName, final EventHandlerParameters producerParameters)
64 throws ApexEventException {
65 this.name = producerName;
67 // Check and get the producer Properties
68 if (!(producerParameters
69 .getCarrierTechnologyParameters() instanceof EventRequestorCarrierTechnologyParameters)) {
70 final String errorMessage =
71 "specified consumer properties are not applicable to event requestor producer (" + this.name + ")";
72 LOGGER.warn(errorMessage);
73 throw new ApexEventException(errorMessage);
76 // Check if we are in peered mode
77 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
78 final String errorMessage = "Event Requestor producer (" + this.name
79 + ") must run in peered requestor mode with a Event Requestor consumer";
80 LOGGER.warn(errorMessage);
81 throw new ApexEventException(errorMessage);
89 public String getName() {
94 * Get the number of events sent to date.
96 * @return the number of events received
98 public int getEventsSent() {
106 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
107 return peerReferenceMap.get(peeredMode);
114 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
115 peerReferenceMap.put(peeredMode, peeredReference);
122 public void sendEvent(final long executionId, final Properties executorProperties, final String eventName,
123 final Object eventObject) {
124 // Check if this is a synchronized event, if so we have received a reply
125 final SynchronousEventCache synchronousEventCache =
126 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
127 if (synchronousEventCache != null) {
128 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
131 // Find the peered consumer for this producer
132 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
133 if (peeredRequestorReference != null) {
134 // Find the event Response Consumer that will handle this request
135 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
136 if (!(consumer instanceof EventRequestorConsumer)) {
137 final String errorMessage = "send of event to event consumer \""
138 + peeredRequestorReference.getPeeredConsumer() + "\" failed,"
139 + " event response consumer is not an instance of EventRequestorConsumer\n" + eventObject;
140 LOGGER.warn(errorMessage);
141 throw new ApexEventRuntimeException(errorMessage);
144 // Use the consumer to handle this event
145 final EventRequestorConsumer eventRequstConsumer = (EventRequestorConsumer) consumer;
146 eventRequstConsumer.processEvent(eventObject);
150 // No peered consumer defined
151 final String errorMessage = "send of event failed, event response consumer is not defined\n" + eventObject;
152 LOGGER.warn(errorMessage);
153 throw new ApexEventRuntimeException(errorMessage);
162 // For event requestor, all the implementation is in the consumer