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.plugins.event.carrier.restrequestor;
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 requestor that manages the producer side of a REST request.
42 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class ApexRestRequestorProducer implements ApexEventProducer {
46 private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestRequestorProducer.class);
48 // The REST carrier properties
49 private RestRequestorCarrierTechnologyParameters restProducerProperties;
51 // The name for this producer
52 private String name = null;
54 // The peer references for this event handler
55 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
57 // The number of events sent
58 private int eventsSent = 0;
64 public void init(final String producerName, final EventHandlerParameters producerParameters)
65 throws ApexEventException {
66 this.name = producerName;
68 // Check and get the REST Properties
69 if (!(producerParameters
70 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
71 final String errorMessage =
72 "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
73 LOGGER.warn(errorMessage);
74 throw new ApexEventException(errorMessage);
76 restProducerProperties =
77 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
79 // Check if we are in peered mode
80 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
81 final String errorMessage = "REST Requestor producer (" + this.name
82 + ") must run in peered requestor mode with a REST Requestor consumer";
83 LOGGER.warn(errorMessage);
84 throw new ApexEventException(errorMessage);
87 // Check if the HTTP URL has been set
88 if (restProducerProperties.getUrl() != null) {
89 final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
90 LOGGER.warn(errorMessage);
91 throw new ApexEventException(errorMessage);
94 // Check if the HTTP method has been set
95 if (restProducerProperties.getHttpMethod() != null) {
96 final String errorMessage =
97 "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
98 LOGGER.warn(errorMessage);
99 throw new ApexEventException(errorMessage);
107 public String getName() {
112 * Get the number of events sent to date.
114 * @return the number of events received
116 public int getEventsSent() {
124 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
125 return peerReferenceMap.get(peeredMode);
132 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
133 peerReferenceMap.put(peeredMode, peeredReference);
140 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
141 final Object event) {
142 // Check if this is a synchronized event, if so we have received a reply
143 final SynchronousEventCache synchronousEventCache =
144 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
145 if (synchronousEventCache != null) {
146 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
149 // Find the peered consumer for this producer
150 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
151 if (peeredRequestorReference != null) {
152 // Find the REST Response Consumer that will handle this request
153 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
154 if (!(consumer instanceof ApexRestRequestorConsumer)) {
155 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
156 + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
157 LOGGER.warn(errorMessage);
158 throw new ApexEventRuntimeException(errorMessage);
161 // Use the consumer to handle this event
162 final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
163 restRequstConsumer.processRestRequest(new ApexRestRequest(
164 executionId, executionProperties, eventName, event));
168 // No peered consumer defined
169 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
170 + " REST response consumer is not defined\n" + event;
171 LOGGER.warn(errorMessage);
172 throw new ApexEventRuntimeException(errorMessage);
181 // For REST requestor, all the implementation is in the consumer