2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.plugins.event.carrier.restserver;
25 import java.util.EnumMap;
27 import java.util.Properties;
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.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 events using REST.
41 * @author Liam Fallon (liam.fallon@ericsson.com)
44 public class ApexRestServerProducer implements ApexEventProducer {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestServerProducer.class);
47 // The name for this producer
49 private String name = null;
51 // The peer references for this event handler
52 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
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.getCarrierTechnologyParameters() instanceof RestServerCarrierTechnologyParameters)) {
64 final String errorMessage =
65 "specified producer properties are not applicable to REST Server producer (" + this.name + ")";
66 LOGGER.warn(errorMessage);
67 throw new ApexEventException(errorMessage);
70 // The REST carrier properties
71 RestServerCarrierTechnologyParameters restProducerProperties =
72 (RestServerCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
74 // Check if host and port are defined
75 if (restProducerProperties.getHost() != null || restProducerProperties.getPort() != -1
76 || restProducerProperties.isStandalone()) {
77 final String errorMessage =
78 "the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer ("
80 LOGGER.warn(errorMessage);
81 throw new ApexEventException(errorMessage);
84 // Check if we are in synchronous mode
85 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)) {
86 final String errorMessage =
87 "REST Server producer (" + this.name + ") must run in synchronous mode with a REST Server consumer";
88 LOGGER.warn(errorMessage);
89 throw new ApexEventException(errorMessage);
97 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
98 return peerReferenceMap.get(peeredMode);
105 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
106 peerReferenceMap.put(peeredMode, peeredReference);
113 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
114 final Object event) {
115 if (LOGGER.isDebugEnabled()) {
116 String message = name + ": event " + executionId + ':' + eventName + " recevied from Apex, event=" + event;
117 LOGGER.debug(message);
120 // If we are not synchronized, then exit
121 final SynchronousEventCache synchronousEventCache =
122 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
123 if (synchronousEventCache == null) {
127 // We see all events on the receiver, even those that are not replies to events sent by the synchronized
128 // consumer of this producer, ignore those
130 if (!synchronousEventCache.existsEventToApex(executionId)) {
134 if (LOGGER.isDebugEnabled()) {
135 LOGGER.debug("{}: event {}:{} is a reply to a REST server call from {}",
136 name, executionId, eventName, name);
139 // Add the event to the received event cache
140 synchronousEventCache.cacheSynchronizedEventFromApex(executionId, event);
148 // Implementation not required on this class