03913e0810970d851fcc447333bc7c275434c050
[policy/apex-pdp.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.carrier.restserver;
24
25 import java.util.EnumMap;
26 import java.util.Map;
27 import java.util.Properties;
28 import lombok.Getter;
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;
37
38 /**
39  * Concrete implementation of an Apex event producer that sends events using REST.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  *
43  */
44 public class ApexRestServerProducer implements ApexEventProducer {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestServerProducer.class);
46
47     // The name for this producer
48     @Getter
49     private String name = null;
50
51     // The peer references for this event handler
52     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
53
54     /**
55      * {@inheritDoc}.
56      */
57     @Override
58     public void init(final String producerName, final EventHandlerParameters producerParameters)
59             throws ApexEventException {
60         this.name = producerName;
61
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);
68         }
69
70         // The REST carrier properties
71         RestServerCarrierTechnologyParameters restProducerProperties =
72                 (RestServerCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
73
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 ("
79                             + this.name + ")";
80             LOGGER.warn(errorMessage);
81             throw new ApexEventException(errorMessage);
82         }
83
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);
90         }
91     }
92
93     /**
94      * {@inheritDoc}.
95      */
96     @Override
97     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
98         return peerReferenceMap.get(peeredMode);
99     }
100
101     /**
102      * {@inheritDoc}.
103      */
104     @Override
105     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
106         peerReferenceMap.put(peeredMode, peeredReference);
107     }
108
109     /**
110      * {@inheritDoc}.
111      */
112     @Override
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);
118         }
119
120         // If we are not synchronized, then exit
121         final SynchronousEventCache synchronousEventCache =
122                 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
123         if (synchronousEventCache == null) {
124             return;
125         }
126
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
129         // events
130         if (!synchronousEventCache.existsEventToApex(executionId)) {
131             return;
132         }
133
134         if (LOGGER.isDebugEnabled()) {
135             LOGGER.debug("{}: event {}:{} is a reply to a REST server call from {}",
136                     name, executionId, eventName, name);
137         }
138
139         // Add the event to the received event cache
140         synchronousEventCache.cacheSynchronizedEventFromApex(executionId, event);
141     }
142
143     /**
144      * {@inheritDoc}.
145      */
146     @Override
147     public void stop() {
148         // Implementation not required on this class
149     }
150 }