aabf1664198d1ddeb40598bb12d46ec63eccfe0d
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restserver / src / main / java / org / onap / policy / apex / plugins / event / carrier / restserver / ApexRestServerProducer.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restserver;
22
23 import java.util.EnumMap;
24 import java.util.Map;
25 import java.util.Properties;
26
27 import org.onap.policy.apex.service.engine.event.ApexEventException;
28 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
29 import org.onap.policy.apex.service.engine.event.PeeredReference;
30 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
32 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Concrete implementation of an Apex event producer that sends events using REST.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  *
41  */
42 public class ApexRestServerProducer implements ApexEventProducer {
43     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestServerProducer.class);
44
45     // The name for this producer
46     private String name = null;
47
48     // The peer references for this event handler
49     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
50
51     /**
52      * {@inheritDoc}.
53      */
54     @Override
55     public void init(final String producerName, final EventHandlerParameters producerParameters)
56             throws ApexEventException {
57         this.name = producerName;
58
59         // Check and get the REST Properties
60         if (!(producerParameters.getCarrierTechnologyParameters() instanceof RestServerCarrierTechnologyParameters)) {
61             final String errorMessage =
62                     "specified producer properties are not applicable to REST Server producer (" + this.name + ")";
63             LOGGER.warn(errorMessage);
64             throw new ApexEventException(errorMessage);
65         }
66
67         // The REST carrier properties
68         RestServerCarrierTechnologyParameters restProducerProperties =
69                 (RestServerCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
70
71         // Check if host and port are defined
72         if (restProducerProperties.getHost() != null || restProducerProperties.getPort() != -1
73                 || restProducerProperties.isStandalone()) {
74             final String errorMessage =
75                     "the parameters \"host\", \"port\", and \"standalone\" are illegal on REST Server producer ("
76                             + this.name + ")";
77             LOGGER.warn(errorMessage);
78             throw new ApexEventException(errorMessage);
79         }
80
81         // Check if we are in synchronous mode
82         if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.SYNCHRONOUS)) {
83             final String errorMessage =
84                     "REST Server producer (" + this.name + ") must run in synchronous mode with a REST Server consumer";
85             LOGGER.warn(errorMessage);
86             throw new ApexEventException(errorMessage);
87         }
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
94     public String getName() {
95         return name;
96     }
97
98     /**
99      * {@inheritDoc}.
100      */
101     @Override
102     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
103         return peerReferenceMap.get(peeredMode);
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
111         peerReferenceMap.put(peeredMode, peeredReference);
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
119             final Object event) {
120         if (LOGGER.isDebugEnabled()) {
121             String message = name + ": event " + executionId + ':' + eventName + " recevied from Apex, event=" + event;
122             LOGGER.debug(message);
123         }
124
125         // If we are not synchronized, then exit
126         final SynchronousEventCache synchronousEventCache =
127                 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
128         if (synchronousEventCache == null) {
129             return;
130         }
131
132         // We see all events on the receiver, even those that are not replies to events sent by the synchronized
133         // consumer of this producer, ignore those
134         // events
135         if (!synchronousEventCache.existsEventToApex(executionId)) {
136             return;
137         }
138
139         if (LOGGER.isDebugEnabled()) {
140             LOGGER.debug(name + ": event " + executionId + ':' + eventName + " is a reply to a REST server call from "
141                     + name);
142         }
143
144         // Add the event to the received event cache
145         synchronousEventCache.cacheSynchronizedEventFromApex(executionId, event);
146     }
147
148     /**
149      * {@inheritDoc}.
150      */
151     @Override
152     public void stop() {
153         // Implementation not required on this class
154     }
155 }