bbe61f3fead53790f1f47b6b4d287d40bfbbae0e
[policy/apex-pdp.git] /
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.restrequestor;
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.ApexEventConsumer;
28 import org.onap.policy.apex.service.engine.event.ApexEventException;
29 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
30 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
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 requestor that manages the producer side of a REST request.
40  *
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  *
43  */
44 public class ApexRestRequestorProducer implements ApexEventProducer {
45     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestRequestorProducer.class);
46
47     // The REST carrier properties
48     private RestRequestorCarrierTechnologyParameters restProducerProperties;
49
50     // The name for this producer
51     private String name = null;
52
53     // The peer references for this event handler
54     private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
55
56     // The number of events sent
57     private int eventsSent = 0;
58
59     /**
60      * {@inheritDoc}.
61      */
62     @Override
63     public void init(final String producerName, final EventHandlerParameters producerParameters)
64             throws ApexEventException {
65         this.name = producerName;
66
67         // Check and get the REST Properties
68         if (!(producerParameters
69                 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
70             final String errorMessage =
71                     "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
72             LOGGER.warn(errorMessage);
73             throw new ApexEventException(errorMessage);
74         }
75         restProducerProperties =
76                 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
77
78         // Check if we are in peered mode
79         if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
80             final String errorMessage = "REST Requestor producer (" + this.name
81                     + ") must run in peered requestor mode with a REST Requestor consumer";
82             LOGGER.warn(errorMessage);
83             throw new ApexEventException(errorMessage);
84         }
85
86         // Check if the HTTP URL has been set
87         if (restProducerProperties.getUrl() != null) {
88             final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
89             LOGGER.warn(errorMessage);
90             throw new ApexEventException(errorMessage);
91         }
92
93         // Check if the HTTP method has been set
94         if (restProducerProperties.getHttpMethod() != null) {
95             final String errorMessage =
96                     "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
97             LOGGER.warn(errorMessage);
98             throw new ApexEventException(errorMessage);
99         }
100     }
101
102     /**
103      * {@inheritDoc}.
104      */
105     @Override
106     public String getName() {
107         return name;
108     }
109
110     /**
111      * Get the number of events sent to date.
112      *
113      * @return the number of events received
114      */
115     public int getEventsSent() {
116         return eventsSent;
117     }
118
119     /**
120      * {@inheritDoc}.
121      */
122     @Override
123     public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
124         return peerReferenceMap.get(peeredMode);
125     }
126
127     /**
128      * {@inheritDoc}.
129      */
130     @Override
131     public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
132         peerReferenceMap.put(peeredMode, peeredReference);
133     }
134
135     /**
136      * {@inheritDoc}.
137      */
138     @Override
139     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
140             final Object event) {
141         // Check if this is a synchronized event, if so we have received a reply
142         final SynchronousEventCache synchronousEventCache =
143                 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
144         if (synchronousEventCache != null) {
145             synchronousEventCache.removeCachedEventToApexIfExists(executionId);
146         }
147
148         // Find the peered consumer for this producer
149         final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
150         if (peeredRequestorReference != null) {
151             // Find the REST Response Consumer that will handle this request
152             final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
153             if (!(consumer instanceof ApexRestRequestorConsumer)) {
154                 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
155                         + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
156                 LOGGER.warn(errorMessage);
157                 throw new ApexEventRuntimeException(errorMessage);
158             }
159
160             // Use the consumer to handle this event
161             final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
162             restRequstConsumer.processRestRequest(new ApexRestRequest(executionId, eventName, event));
163
164             eventsSent++;
165         } else {
166             // No peered consumer defined
167             final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
168                     + " REST response consumer is not defined\n" + event;
169             LOGGER.warn(errorMessage);
170             throw new ApexEventRuntimeException(errorMessage);
171         }
172     }
173
174     /**
175      * {@inheritDoc}.
176      */
177     @Override
178     public void stop() {
179         // For REST requestor, all the implementation is in the consumer
180     }
181 }