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;
27 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
28 import org.onap.policy.apex.service.engine.event.ApexEventException;
29 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
30 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
31 import org.onap.policy.apex.service.engine.event.PeeredReference;
32 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
33 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Concrete implementation of an Apex event producer that sends one or more events to its peered event requestor
42 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class EventRequestorProducer implements ApexEventProducer {
46 private static final Logger LOGGER = LoggerFactory.getLogger(EventRequestorProducer.class);
48 // The name for this producer
49 private String name = null;
51 // The peer references for this event handler
52 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
53 new EnumMap<>(EventHandlerPeeredMode.class);
55 // The number of events sent
56 private int eventsSent = 0;
62 public void init(final String producerName, final EventHandlerParameters producerParameters)
63 throws ApexEventException {
64 this.name = producerName;
66 // Check and get the producer Properties
67 if (!(producerParameters
68 .getCarrierTechnologyParameters() instanceof EventRequestorCarrierTechnologyParameters)) {
69 final String errorMessage =
70 "specified consumer properties are not applicable to event requestor producer (" + this.name + ")";
71 LOGGER.warn(errorMessage);
72 throw new ApexEventException(errorMessage);
75 // Check if we are in peered mode
76 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
77 final String errorMessage = "Event Requestor producer (" + this.name
78 + ") must run in peered requestor mode with a Event Requestor consumer";
79 LOGGER.warn(errorMessage);
80 throw new ApexEventException(errorMessage);
88 public String getName() {
93 * Get the number of events sent to date.
95 * @return the number of events received
97 public int getEventsSent() {
105 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
106 return peerReferenceMap.get(peeredMode);
113 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
114 peerReferenceMap.put(peeredMode, peeredReference);
121 public void sendEvent(final long executionId, final Properties executorProperties, final String eventName,
122 final Object eventObject) {
123 // Check if this is a synchronized event, if so we have received a reply
124 final SynchronousEventCache synchronousEventCache =
125 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
126 if (synchronousEventCache != null) {
127 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
130 // Find the peered consumer for this producer
131 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
132 if (peeredRequestorReference != null) {
133 // Find the event Response Consumer that will handle this request
134 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
135 if (!(consumer instanceof EventRequestorConsumer)) {
136 final String errorMessage = "send of event to event consumer \""
137 + peeredRequestorReference.getPeeredConsumer() + "\" failed,"
138 + " event response consumer is not an instance of EventRequestorConsumer\n" + eventObject;
139 LOGGER.warn(errorMessage);
140 throw new ApexEventRuntimeException(errorMessage);
143 // Use the consumer to handle this event
144 final EventRequestorConsumer eventRequstConsumer = (EventRequestorConsumer) consumer;
145 eventRequstConsumer.processEvent(eventObject);
149 // No peered consumer defined
150 final String errorMessage = "send of event failed, event response consumer is not defined\n" + eventObject;
151 LOGGER.warn(errorMessage);
152 throw new ApexEventRuntimeException(errorMessage);
161 // For event requestor, all the implementation is in the consumer