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