2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restclient;
23 import java.util.EnumMap;
25 import java.util.Properties;
27 import javax.ws.rs.client.Client;
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Response;
32 import org.onap.policy.apex.service.engine.event.ApexEventException;
33 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
34 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
35 import org.onap.policy.apex.service.engine.event.PeeredReference;
36 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
37 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
38 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Concrete implementation of an Apex event producer that sends events using REST.
45 * @author Joss Armstrong (joss.armstrong@ericsson.com)
48 public class ApexRestClientProducer implements ApexEventProducer {
49 private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestClientProducer.class);
51 // The HTTP client that makes a REST call with an event from Apex
52 private Client client;
54 // The REST carrier properties
55 private RestClientCarrierTechnologyParameters restProducerProperties;
57 // The name for this producer
58 private String name = null;
60 // The peer references for this event handler
61 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
66 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#init(java.lang.String,
67 * org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters)
70 public void init(final String producerName, final EventHandlerParameters producerParameters)
71 throws ApexEventException {
72 this.name = producerName;
74 // Check and get the REST Properties
75 if (!(producerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) {
76 final String errorMessage =
77 "specified producer properties are not applicable to REST client producer (" + this.name + ")";
78 LOGGER.warn(errorMessage);
79 throw new ApexEventException(errorMessage);
81 restProducerProperties =
82 (RestClientCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
84 // Check if the HTTP method has been set
85 if (restProducerProperties.getHttpMethod() == null) {
86 restProducerProperties.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
89 if (!RestClientCarrierTechnologyParameters.HttpMethod.POST.equals(restProducerProperties.getHttpMethod())
90 && !RestClientCarrierTechnologyParameters.HttpMethod.PUT
91 .equals(restProducerProperties.getHttpMethod())) {
92 final String errorMessage = "specified HTTP method of \"" + restProducerProperties.getHttpMethod()
93 + "\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supproted "
94 + "for event sending on REST client producer (" + this.name + ")";
95 LOGGER.warn(errorMessage);
96 throw new ApexEventException(errorMessage);
99 // Initialize the HTTP client
100 client = ClientBuilder.newClient();
106 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getName()
109 public String getName() {
116 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#getPeeredReference(org.onap.policy.apex.service.
117 * parameters. eventhandler.EventHandlerPeeredMode)
120 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
121 return peerReferenceMap.get(peeredMode);
127 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#setPeeredReference(org.onap.policy.apex.service.
128 * parameters. eventhandler.EventHandlerPeeredMode, org.onap.policy.apex.service.engine.event.PeeredReference)
131 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
132 peerReferenceMap.put(peeredMode, peeredReference);
138 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#sendEvent(long, java.lang. String,
142 public void sendEvent(final long executionId, final Properties executionProperties, final String eventName,
143 final Object event) {
144 // Check if this is a synchronized event, if so we have received a reply
145 final SynchronousEventCache synchronousEventCache =
146 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
147 if (synchronousEventCache != null) {
148 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
151 // Send the event as a REST request
152 final Response response = sendEventAsRestRequest((String) event);
154 // Check that the request worked
155 if (response.getStatus() != Response.Status.OK.getStatusCode()) {
156 final String errorMessage = "send of event to URL \"" + restProducerProperties.getUrl() + "\" using HTTP \""
157 + restProducerProperties.getHttpMethod() + "\" failed with status code " + response.getStatus()
158 + " and message \"" + response.readEntity(String.class) + "\", event:\n" + event;
159 LOGGER.warn(errorMessage);
160 throw new ApexEventRuntimeException(errorMessage);
163 if (LOGGER.isTraceEnabled()) {
164 LOGGER.trace("event sent from engine using {} to URL {} with HTTP {} : {} and response {} ", this.name,
165 restProducerProperties.getUrl(), restProducerProperties.getHttpMethod(), event, response);
172 * @see org.onap.policy.apex.service.engine.event.ApexEventProducer#stop()
176 // Close the HTTP session
181 * Send the event as a JSON string as a REST request.
183 * @param event the event to send
184 * @return the response to the JSON request
186 private Response sendEventAsRestRequest(final String event) {
187 // We have already checked that it is a PUT or POST request
188 if (RestClientCarrierTechnologyParameters.HttpMethod.POST.equals(restProducerProperties.getHttpMethod())) {
189 return client.target(restProducerProperties.getUrl()).request("application/json")
190 .headers(restProducerProperties.getHttpHeadersAsMultivaluedMap()).post(Entity.json(event));
192 return client.target(restProducerProperties.getUrl()).request("application/json")
193 .headers(restProducerProperties.getHttpHeadersAsMultivaluedMap()).put(Entity.json(event));
198 * Hook for unit test mocking of HTTP client.
200 * @param client the mocked client
202 protected void setClient(final Client client) {
203 this.client = client;