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.restserver;
23 import java.util.EnumMap;
26 import org.onap.policy.apex.service.engine.event.ApexEventException;
27 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
28 import org.onap.policy.apex.service.engine.event.PeeredReference;
29 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
30 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Concrete implementation of an Apex event producer that sends events using REST.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ApexRestServerProducer implements ApexEventProducer {
42 private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestServerProducer.class);
44 // The name for this producer
45 private String name = null;
47 // The peer references for this event handler
48 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
53 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#init(java.lang.String,
54 * org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters)
57 public void init(final String producerName, final EventHandlerParameters producerParameters)
58 throws ApexEventException {
59 this.name = producerName;
61 // Check and get the REST Properties
62 if (!(producerParameters.getCarrierTechnologyParameters() instanceof RestServerCarrierTechnologyParameters)) {
63 final String errorMessage =
64 "specified producer properties are not applicable to REST Server producer (" + this.name + ")";
65 LOGGER.warn(errorMessage);
66 throw new ApexEventException(errorMessage);
69 // The REST carrier properties
70 RestServerCarrierTechnologyParameters restProducerProperties =
71 (RestServerCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
73 // Check if host and port are defined
74 if (restProducerProperties.getHost() != null || restProducerProperties.getPort() != -1
75 || restProducerProperties.isStandalone()) {
76 final String errorMessage =
77 "the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer ("
79 LOGGER.warn(errorMessage);
80 throw new ApexEventException(errorMessage);
83 // Check if we are in synchronous mode
84 if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)) {
85 final String errorMessage =
86 "REST Server producer (" + this.name + ") must run in synchronous mode with a REST Server consumer";
87 LOGGER.warn(errorMessage);
88 throw new ApexEventException(errorMessage);
95 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getName()
98 public String getName() {
105 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getPeeredReference(org.onap.policy.apex.service.
106 * parameters. eventhandler.EventHandlerPeeredMode)
109 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
110 return peerReferenceMap.get(peeredMode);
116 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#setPeeredReference(org.onap.policy.apex.service.
117 * parameters. eventhandler.EventHandlerPeeredMode, org.onap.policy.apex.service.engine.event.PeeredReference)
120 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
121 peerReferenceMap.put(peeredMode, peeredReference);
127 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#sendEvent(long, java.lang. String,
131 public void sendEvent(final long executionId, final String eventName, final Object event) {
132 if (LOGGER.isDebugEnabled()) {
133 String message = name + ": event " + executionId + ':' + eventName + " recevied from Apex, event=" + event;
134 LOGGER.debug(message);
137 // If we are not synchronized, then exit
138 final SynchronousEventCache synchronousEventCache =
139 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
140 if (synchronousEventCache == null) {
144 // We see all events on the receiver, even those that are not replies to events sent by the synchronized
145 // consumer of this producer, ignore those
147 if (!synchronousEventCache.existsEventToApex(executionId)) {
151 if (LOGGER.isDebugEnabled()) {
152 LOGGER.debug(name + ": event " + executionId + ':' + eventName + " is a reply to a REST server call from "
156 // Add the event to the received event cache
157 synchronousEventCache.cacheSynchronizedEventFromApex(executionId, event);
163 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#stop()
167 // Implementation not required on this class