2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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.ApexPluginsEventProducer;
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 extends ApexPluginsEventProducer {
46 private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestRequestorProducer.class);
48 // The REST carrier properties
49 private RestRequestorCarrierTechnologyParameters restProducerProperties;
51 // The number of events sent
52 private int eventsSent = 0;
58 public void init(final String producerName, final EventHandlerParameters producerParameters)
59 throws ApexEventException {
60 this.name = producerName;
62 // Check and get the REST Properties
63 if (!(producerParameters
64 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
65 final String errorMessage =
66 "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
67 LOGGER.warn(errorMessage);
68 throw new ApexEventException(errorMessage);
70 restProducerProperties =
71 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
73 // Check if we are in peered mode
74 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
75 final String errorMessage = "REST Requestor producer (" + this.name
76 + ") must run in peered requestor mode with a REST Requestor consumer";
77 LOGGER.warn(errorMessage);
78 throw new ApexEventException(errorMessage);
81 // Check if the HTTP URL has been set
82 if (restProducerProperties.getUrl() != null) {
83 final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
84 LOGGER.warn(errorMessage);
85 throw new ApexEventException(errorMessage);
88 // Check if the HTTP method has been set
89 if (restProducerProperties.getHttpMethod() != null) {
90 final String errorMessage =
91 "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
92 LOGGER.warn(errorMessage);
93 throw new ApexEventException(errorMessage);
98 * Get the number of events sent to date.
100 * @return the number of events received
102 public int getEventsSent() {
110 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
111 final Object event) {
112 super.sendEvent(executionId, executionProperties, eventName, event);
114 // Find the peered consumer for this producer
115 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
116 if (peeredRequestorReference != null) {
117 // Find the REST Response Consumer that will handle this request
118 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
119 if (!(consumer instanceof ApexRestRequestorConsumer)) {
120 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
121 + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
122 LOGGER.warn(errorMessage);
123 throw new ApexEventRuntimeException(errorMessage);
126 // Use the consumer to handle this event
127 final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
128 restRequstConsumer.processRestRequest(new ApexRestRequest(
129 executionId, executionProperties, eventName, event));
133 // No peered consumer defined
134 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
135 + " REST response consumer is not defined\n" + event;
136 LOGGER.warn(errorMessage);
137 throw new ApexEventRuntimeException(errorMessage);
146 // For REST requestor, all the implementation is in the consumer