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