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