3e2cd5a94cad6423730a4c04a39d7995840f63b3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
23
24 import java.util.EnumMap;
25 import java.util.Map;
26 import java.util.Properties;
27
28 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
31 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
32 import org.onap.policy.apex.service.engine.event.PeeredReference;
33 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
35 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Concrete implementation of an Apex event requestor that manages the producer side of a REST request.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  *
44  */
45 public class ApexRestRequestorProducer extends ApexPluginsEventProducer {
46     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestRequestorProducer.class);
47
48     // The REST carrier properties
49     private RestRequestorCarrierTechnologyParameters restProducerProperties;
50
51     // The number of events sent
52     private int eventsSent = 0;
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
64                 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
65             final String errorMessage =
66                     "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
67             LOGGER.warn(errorMessage);
68             throw new ApexEventException(errorMessage);
69         }
70         restProducerProperties =
71                 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
72
73         // Check if we are in peered mode
74         if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
75             final String errorMessage = "REST Requestor producer (" + this.name
76                     + ") must run in peered requestor mode with a REST Requestor consumer";
77             LOGGER.warn(errorMessage);
78             throw new ApexEventException(errorMessage);
79         }
80
81         // Check if the HTTP URL has been set
82         if (restProducerProperties.getUrl() != null) {
83             final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
84             LOGGER.warn(errorMessage);
85             throw new ApexEventException(errorMessage);
86         }
87
88         // Check if the HTTP method has been set
89         if (restProducerProperties.getHttpMethod() != null) {
90             final String errorMessage =
91                     "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
92             LOGGER.warn(errorMessage);
93             throw new ApexEventException(errorMessage);
94         }
95     }
96
97     /**
98      * Get the number of events sent to date.
99      *
100      * @return the number of events received
101      */
102     public int getEventsSent() {
103         return eventsSent;
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
111             final Object event) {
112         super.sendEvent(executionId, executionProperties, eventName, event);
113
114         // Find the peered consumer for this producer
115         final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
116         if (peeredRequestorReference != null) {
117             // Find the REST Response Consumer that will handle this request
118             final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
119             if (!(consumer instanceof ApexRestRequestorConsumer)) {
120                 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
121                         + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
122                 LOGGER.warn(errorMessage);
123                 throw new ApexEventRuntimeException(errorMessage);
124             }
125
126             // Use the consumer to handle this event
127             final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
128             restRequstConsumer.processRestRequest(new ApexRestRequest(
129                 executionId, executionProperties, eventName, event));
130
131             eventsSent++;
132         } else {
133             // No peered consumer defined
134             final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
135                     + " REST response consumer is not defined\n" + event;
136             LOGGER.warn(errorMessage);
137             throw new ApexEventRuntimeException(errorMessage);
138         }
139     }
140
141     /**
142      * {@inheritDoc}.
143      */
144     @Override
145     public void stop() {
146         // For REST requestor, all the implementation is in the consumer
147     }
148 }