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.eventrequestor;
23 import java.util.EnumMap;
25 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;
61 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#init(java.lang.String,
62 * org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters)
65 public void init(final String producerName, final EventHandlerParameters producerParameters)
66 throws ApexEventException {
67 this.name = producerName;
69 // Check and get the producer Properties
70 if (!(producerParameters
71 .getCarrierTechnologyParameters() instanceof EventRequestorCarrierTechnologyParameters)) {
72 final String errorMessage =
73 "specified consumer properties are not applicable to event requestor producer (" + this.name + ")";
74 LOGGER.warn(errorMessage);
75 throw new ApexEventException(errorMessage);
78 // Check if we are in peered mode
79 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
80 final String errorMessage = "Event Requestor producer (" + this.name
81 + ") must run in peered requestor mode with a Event Requestor consumer";
82 LOGGER.warn(errorMessage);
83 throw new ApexEventException(errorMessage);
90 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getName()
93 public String getName() {
98 * Get the number of events sent to date.
100 * @return the number of events received
102 public int getEventsSent() {
110 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
111 return peerReferenceMap.get(peeredMode);
118 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
119 peerReferenceMap.put(peeredMode, peeredReference);
126 public void sendEvent(final long executionId, final Properties executorProperties, final String eventName,
127 final Object eventObject) {
128 // Check if this is a synchronized event, if so we have received a reply
129 final SynchronousEventCache synchronousEventCache =
130 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
131 if (synchronousEventCache != null) {
132 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
135 // Find the peered consumer for this producer
136 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
137 if (peeredRequestorReference != null) {
138 // Find the event Response Consumer that will handle this request
139 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
140 if (!(consumer instanceof EventRequestorConsumer)) {
141 final String errorMessage = "send of event to event consumer \""
142 + peeredRequestorReference.getPeeredConsumer() + "\" failed,"
143 + " event response consumer is not an instance of EventRequestorConsumer\n" + eventObject;
144 LOGGER.warn(errorMessage);
145 throw new ApexEventRuntimeException(errorMessage);
148 // Use the consumer to handle this event
149 final EventRequestorConsumer eventRequstConsumer = (EventRequestorConsumer) consumer;
150 eventRequstConsumer.processEvent(eventObject);
154 // No peered consumer defined
155 final String errorMessage = "send of event failed, event response consumer is not defined\n" + eventObject;
156 LOGGER.warn(errorMessage);
157 throw new ApexEventRuntimeException(errorMessage);
166 // For event requestor, all the implementation is in the consumer