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;
62 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#init(java.lang.String,
63 * org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters)
66 public void init(final String producerName, final EventHandlerParameters producerParameters)
67 throws ApexEventException {
68 this.name = producerName;
70 // Check and get the REST Properties
71 if (!(producerParameters
72 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
73 final String errorMessage =
74 "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
75 LOGGER.warn(errorMessage);
76 throw new ApexEventException(errorMessage);
78 restProducerProperties =
79 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
81 // Check if we are in peered mode
82 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
83 final String errorMessage = "REST Requestor producer (" + this.name
84 + ") must run in peered requestor mode with a REST Requestor consumer";
85 LOGGER.warn(errorMessage);
86 throw new ApexEventException(errorMessage);
89 // Check if the HTTP URL has been set
90 if (restProducerProperties.getUrl() != null) {
91 final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
92 LOGGER.warn(errorMessage);
93 throw new ApexEventException(errorMessage);
96 // Check if the HTTP method has been set
97 if (restProducerProperties.getHttpMethod() != null) {
98 final String errorMessage =
99 "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
100 LOGGER.warn(errorMessage);
101 throw new ApexEventException(errorMessage);
108 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getName()
111 public String getName() {
116 * Get the number of events sent to date.
118 * @return the number of events received
120 public int getEventsSent() {
127 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getPeeredReference(org.onap.policy.apex.service.
128 * parameters.eventhandler.EventHandlerPeeredMode)
131 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
132 return peerReferenceMap.get(peeredMode);
138 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#setPeeredReference(org.onap.policy.apex.service.
139 * parameters.eventhandler.EventHandlerPeeredMode, org.onap.policy.apex.service.engine.event.PeeredReference)
142 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
143 peerReferenceMap.put(peeredMode, peeredReference);
149 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#sendEvent(long, java.lang. String,
153 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
154 final Object event) {
155 // Check if this is a synchronized event, if so we have received a reply
156 final SynchronousEventCache synchronousEventCache =
157 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
158 if (synchronousEventCache != null) {
159 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
162 // Find the peered consumer for this producer
163 final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
164 if (peeredRequestorReference != null) {
165 // Find the REST Response Consumer that will handle this request
166 final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
167 if (!(consumer instanceof ApexRestRequestorConsumer)) {
168 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
169 + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
170 LOGGER.warn(errorMessage);
171 throw new ApexEventRuntimeException(errorMessage);
174 // Use the consumer to handle this event
175 final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
176 restRequstConsumer.processRestRequest(new ApexRestRequest(executionId, eventName, event));
180 // No peered consumer defined
181 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
182 + " REST response consumer is not defined\n" + event;
183 LOGGER.warn(errorMessage);
184 throw new ApexEventRuntimeException(errorMessage);
191 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#stop()
195 // For REST requestor, all the implementation is in the consumer