Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restrequestor / src / main / java / org / onap / policy / apex / plugins / event / carrier / restrequestor / ApexRestRequestorProducer.java
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 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
26 import org.onap.policy.apex.service.engine.event.ApexEventException;
27 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
28 import org.onap.policy.apex.service.engine.event.ApexPluginsEventProducer;
29 import org.onap.policy.apex.service.engine.event.PeeredReference;
30 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
32
33 /**
34  * Concrete implementation of an Apex event requestor that manages the producer side of a REST request.
35  *
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  *
38  */
39 public class ApexRestRequestorProducer extends ApexPluginsEventProducer {
40     // The number of events sent
41     private int eventsSent = 0;
42
43     /**
44      * {@inheritDoc}.
45      */
46     @Override
47     public void init(final String producerName, final EventHandlerParameters producerParameters)
48         throws ApexEventException {
49         this.name = producerName;
50
51         // Check and get the REST Properties
52         if (!(producerParameters
53             .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) {
54             final String errorMessage =
55                 "specified producer properties are not applicable to REST requestor producer (" + this.name + ")";
56             throw new ApexEventException(errorMessage);
57         }
58         RestRequestorCarrierTechnologyParameters restProducerProperties =
59             (RestRequestorCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
60
61         // Check if we are in peered mode
62         if (!producerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) {
63             final String errorMessage = "REST Requestor producer (" + this.name
64                 + ") must run in peered requestor mode with a REST Requestor consumer";
65             throw new ApexEventException(errorMessage);
66         }
67
68         // Check if the HTTP URL has been set
69         if (restProducerProperties.getUrl() != null) {
70             final String errorMessage = "URL may not be specified on REST Requestor producer (" + this.name + ")";
71             throw new ApexEventException(errorMessage);
72         }
73
74         // Check if the HTTP method has been set
75         if (restProducerProperties.getHttpMethod() != null) {
76             final String errorMessage =
77                 "HTTP method may not be specified on REST Requestor producer (" + this.name + ")";
78             throw new ApexEventException(errorMessage);
79         }
80     }
81
82     /**
83      * Get the number of events sent to date.
84      *
85      * @return the number of events received
86      */
87     public int getEventsSent() {
88         return eventsSent;
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
96         final Object event) {
97         super.sendEvent(executionId, executionProperties, eventName, event);
98
99         // Find the peered consumer for this producer
100         final PeeredReference peeredRequestorReference = peerReferenceMap.get(EventHandlerPeeredMode.REQUESTOR);
101         if (peeredRequestorReference != null) {
102             // Find the REST Response Consumer that will handle this request
103             final ApexEventConsumer consumer = peeredRequestorReference.getPeeredConsumer();
104             if (!(consumer instanceof ApexRestRequestorConsumer)) {
105                 final String errorMessage = "send of event failed,"
106                     + " REST response consumer is not an instance of ApexRestRequestorConsumer\n" + event;
107                 throw new ApexEventRuntimeException(errorMessage);
108             }
109
110             // Use the consumer to handle this event
111             final ApexRestRequestorConsumer restRequstConsumer = (ApexRestRequestorConsumer) consumer;
112             restRequstConsumer
113                 .processRestRequest(new ApexRestRequest(executionId, executionProperties, eventName, event));
114
115             eventsSent++;
116         } else {
117             // No peered consumer defined
118             final String errorMessage = "send of event failed," + " REST response consumer is not defined\n" + event;
119             throw new ApexEventRuntimeException(errorMessage);
120         }
121     }
122
123     /**
124      * {@inheritDoc}.
125      */
126     @Override
127     public void stop() {
128         // For REST requestor, all the implementation is in the consumer
129     }
130 }