e166bdc1ff9f574599b3f1dfe5a7f00fe16b8306
[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.Properties;
25
26 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
27 import org.onap.policy.apex.service.engine.event.ApexEventException;
28 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
29 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
30 import org.onap.policy.apex.service.engine.event.PeeredReference;
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 requestor that manages the producer side of a REST request.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  *
41  */
42 public class ApexRestRequestorProducer extends ApexPluginsEventProducer {
43     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestRequestorProducer.class);
44
45     // The REST carrier properties
46     private RestRequestorCarrierTechnologyParameters restProducerProperties;
47
48     // The number of events sent
49     private int eventsSent = 0;
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
61                 .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
62             final String errorMessage =
63                     "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
64             LOGGER.warn(errorMessage);
65             throw new ApexEventException(errorMessage);
66         }
67         restProducerProperties =
68                 (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
69
70         // Check if we are in peered mode
71         if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
72             final String errorMessage = "REST Requestor producer (" + this.name
73                     + ") must run in peered requestor mode with a REST Requestor consumer";
74             LOGGER.warn(errorMessage);
75             throw new ApexEventException(errorMessage);
76         }
77
78         // Check if the HTTP URL has been set
79         if (restProducerProperties.getUrl() != null) {
80             final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
81             LOGGER.warn(errorMessage);
82             throw new ApexEventException(errorMessage);
83         }
84
85         // Check if the HTTP method has been set
86         if (restProducerProperties.getHttpMethod() != null) {
87             final String errorMessage =
88                     "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
89             LOGGER.warn(errorMessage);
90             throw new ApexEventException(errorMessage);
91         }
92     }
93
94     /**
95      * Get the number of events sent to date.
96      *
97      * @return the number of events received
98      */
99     public int getEventsSent() {
100         return eventsSent;
101     }
102
103     /**
104      * {@inheritDoc}.
105      */
106     @Override
107     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
108             final Object event) {
109         super.sendEvent(executionId, executionProperties, eventName, event);
110
111         // Find the peered consumer for this producer
112         final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
113         if (peeredRequestorReference != null) {
114             // Find the REST Response Consumer that will handle this request
115             final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
116             if (!(consumer instanceof ApexRestRequestorConsumer)) {
117                 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
118                         + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
119                 LOGGER.warn(errorMessage);
120                 throw new ApexEventRuntimeException(errorMessage);
121             }
122
123             // Use the consumer to handle this event
124             final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
125             restRequstConsumer
126                     .processRestRequest(new ApexRestRequest(executionId, executionProperties, eventName, event));
127
128             eventsSent++;
129         } else {
130             // No peered consumer defined
131             final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" failed,"
132                     + " REST response consumer is not defined\n" + event;
133             LOGGER.warn(errorMessage);
134             throw new ApexEventRuntimeException(errorMessage);
135         }
136     }
137
138     /**
139      * {@inheritDoc}.
140      */
141     @Override
142     public void stop() {
143         // For REST requestor, all the implementation is in the consumer
144     }
145 }