2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.service.engine.event.impl.eventrequestor;
25 import java.util.EnumMap;
27 import java.util.Properties;
29 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
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.parameters.eventhandler.EventHandlerParameters;
36 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Concrete implementation of an Apex event producer that sends one or more events to its peered event requestor
44 * @author Liam Fallon (liam.fallon@ericsson.com)
47 public class EventRequestorProducer implements ApexEventProducer {
48 private static final Logger LOGGER = LoggerFactory.getLogger(EventRequestorProducer.class);
50 // The name for this producer
52 private String name = null;
54 // The peer references for this event handler
55 private final Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap =
56 new EnumMap<>(EventHandlerPeeredMode.class);
58 // The number of events sent
60 private int eventsSent = 0;
66 public void init(final String producerName, final EventHandlerParameters producerParameters)
67 throws ApexEventException {
68 this.name = producerName;
70 // Check and get the producer Properties
71 if (!(producerParameters
72 .getCarrierTechnologyParameters() instanceof EventRequestorCarrierTechnologyParameters)) {
73 final String errorMessage =
74 "specified consumer properties are not applicable to event requestor producer (" + this.name + ")";
75 LOGGER.warn(errorMessage);
76 throw new ApexEventException(errorMessage);
79 // Check if we are in peered mode
80 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
81 final String errorMessage = "Event Requestor producer (" + this.name
82 + ") must run in peered requestor mode with a Event Requestor consumer";
83 LOGGER.warn(errorMessage);
84 throw new ApexEventException(errorMessage);
92 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
93 return peerReferenceMap.get(peeredMode);
100 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
101 peerReferenceMap.put(peeredMode, peeredReference);
108 public void sendEvent(final long executionId, final Properties executorProperties, final String eventName,
109 final Object eventObject) {
110 // Check if this is a synchronized event, if so we have received a reply
111 final var synchronousEventCache =
112 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
113 if (synchronousEventCache != null) {
114 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
117 // Find the peered consumer for this producer
118 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
119 if (peeredRequestorReference != null) {
120 // Find the event Response Consumer that will handle this request
121 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
122 if (!(consumer instanceof EventRequestorConsumer)) {
123 final String errorMessage = "send of event to event consumer \""
124 + peeredRequestorReference.getPeeredConsumer() + "\" failed,"
125 + " event response consumer is not an instance of EventRequestorConsumer\n" + eventObject;
126 LOGGER.warn(errorMessage);
127 throw new ApexEventRuntimeException(errorMessage);
130 // Use the consumer to handle this event
131 final EventRequestorConsumer eventRequstConsumer = (EventRequestorConsumer) consumer;
132 eventRequstConsumer.processEvent(eventObject);
136 // No peered consumer defined
137 final String errorMessage = "send of event failed, event response consumer is not defined\n" + eventObject;
138 LOGGER.warn(errorMessage);
139 throw new ApexEventRuntimeException(errorMessage);
148 // For event requestor, all the implementation is in the consumer