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