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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.event.carrier.jms;
24 import java.util.Base64;
25 import java.util.Properties;
26 import javax.naming.Context;
27 import org.apache.commons.lang3.StringUtils;
28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.parameters.ValidationStatus;
33 * Apex parameters for JMS as an event carrier technology.
35 * <p>The parameters for this plugin are:
37 * <li>initialContextFactory: JMS uses a naming {@link Context} object to look up the locations of JMS servers and JMS
38 * topics. An Initial Context Factory is used to when creating a {@link Context} object that can be used for JMS
39 * lookups. The value of this parameter is passed to the {@link Context} with the label
40 * {@link Context#INITIAL_CONTEXT_FACTORY}. Its value must be the full canonical path to a class that implements the
41 * {@code javax.naming.spi.InitialContextFactory} interface. The parameter defaults to the string value
42 * {@code org.jboss.naming.remote.client.InitialContextFactory}.
43 * <li>providerURL: The location of the server to use for naming context lookups. The value of this parameter is passed
44 * to the {@link Context} with the label {@link Context#PROVIDER_URL}. Its value must be a URL that identifies the JMS
45 * naming server. The parameter defaults to the string value {@code remote://localhost:4447}.
46 * <li>securityPrincipal: The user name to use for JMS access. The value of this parameter is passed to the
47 * {@link Context} with the label {@link Context#SECURITY_PRINCIPAL}. Its value must be the user name of a user defined
48 * on the JMS server. The parameter defaults to the string value {@code userid}.
49 * <li>securityCredentials:The password to use for JMS access. The value of this parameter is passed to the
50 * {@link Context} with the label {@link Context#SECURITY_CREDENTIALS}. Its value must be the password of a suer defined
51 * on the JMS server. The parameter defaults to the string value {@code password}.
52 * <li>connectionFactory: JMS uses a {@link javax.jms.ConnectionFactory} instance to create connections towards a JMS
53 * server. The connection factory to use is held in the JMS {@link Context} object. This parameter specifies the label
54 * to use to look up the {@link javax.jms.ConnectionFactory} instance from the JMS {@link Context}.
55 * <li>producerTopic: JMS uses a {@link javax.jms.Topic} instance to for sending and receiving messages. The topic to
56 * use for sending events to JMS from an Apex producer is held in the JMS {@link Context} object. This parameter
57 * specifies the label to use to look up the {@link javax.jms.Topic} instance in the JMS {@link Context} for the JMS
58 * server. The topic must, of course, also be defined on the JMS server. The parameter defaults to the string value
60 * <li>consumerTopic: The topic to use for receiving events from JMS in an Apex consumer is held in the JMS
61 * {@link Context} object. This parameter specifies the label to use to look up the {@link javax.jms.Topic} instance in
62 * the JMS {@link Context} for the JMS server. The topic must, of course, also be defined on the JMS server. The
63 * parameter defaults to the string value {@code apex-in}.
64 * <li>consumerWaitTime: The amount of milliseconds a JMS consumer should wait between checks of its thread execution
65 * status. The parameter defaults to the long value {@code 100}.
66 * <li>objectMessageSending: A flag that indicates whether Apex producers should send JMS messages as
67 * {@link javax.jms.ObjectMessage} instances for java objects (value {@code true}) or as {@link javax.jms.TextMessage}
68 * instances for strings (value {@code false}) . The parameter defaults to the boolean value {@code true}.
71 * @author Liam Fallon (liam.fallon@ericsson.com)
73 public class JmsCarrierTechnologyParameters extends CarrierTechnologyParameters {
74 /** The label of this carrier technology. */
75 public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
77 /** The producer plugin class for the JMS carrier technology. */
78 public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName();
80 /** The consumer plugin class for the JMS carrier technology. */
81 public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName();
85 // Default parameter values
86 private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
87 private static final String DEFAULT_INITIAL_CTXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
88 private static final String DEFAULT_PROVIDER_URL = "remote://localhost:4447";
89 private static final String DEFAULT_SECURITY_PRINCIPAL = "userid";
90 private static final String DEFAULT_SECURITY_CREDENTIALS = "cGFzc3dvcmQ=";
91 private static final String DEFAULT_CONSUMER_TOPIC = "apex-in";
92 private static final String DEFAULT_PRODUCER_TOPIC = "apex-out";
93 private static final int DEFAULT_CONSUMER_WAIT_TIME = 100;
94 private static final boolean DEFAULT_TO_OBJECT_MSG_SENDING = true;
96 // Parameter property map tokens
97 private static final String PROPERTY_INITIAL_CONTEXT_FACTORY = Context.INITIAL_CONTEXT_FACTORY;
98 private static final String PROPERTY_PROVIDER_URL = Context.PROVIDER_URL;
99 private static final String PROPERTY_SECURITY_PRINCIPAL = Context.SECURITY_PRINCIPAL;
100 private static final String PROPERTY_SECURITY_CREDENTIALS = Context.SECURITY_CREDENTIALS;
102 // JMS carrier parameters
103 private String connectionFactory = DEFAULT_CONNECTION_FACTORY;
104 private String initialContextFactory = DEFAULT_INITIAL_CTXT_FACTORY;
105 private String providerUrl = DEFAULT_PROVIDER_URL;
106 private String securityPrincipal = DEFAULT_SECURITY_PRINCIPAL;
107 private String securityCredentials = getDefaultCredential();
108 private String producerTopic = DEFAULT_PRODUCER_TOPIC;
109 private String consumerTopic = DEFAULT_CONSUMER_TOPIC;
110 private int consumerWaitTime = DEFAULT_CONSUMER_WAIT_TIME;
111 private boolean objectMessageSending = DEFAULT_TO_OBJECT_MSG_SENDING;
115 * Constructor to create a jms carrier technology parameters instance and register the instance with the parameter
118 public JmsCarrierTechnologyParameters() {
121 // Set the carrier technology properties for the JMS carrier technology
122 this.setLabel(JMS_CARRIER_TECHNOLOGY_LABEL);
123 this.setEventProducerPluginClass(JMS_EVENT_PRODUCER_PLUGIN_CLASS);
124 this.setEventConsumerPluginClass(JMS_EVENT_CONSUMER_PLUGIN_CLASS);
128 * Gets the JMS producer properties.
130 * @return the JMS producer properties
132 public Properties getJmsProducerProperties() {
133 return getJmsProperties();
137 * Gets the jms consumer properties.
139 * @return the jms consumer properties
141 public Properties getJmsConsumerProperties() {
142 return getJmsProperties();
146 * Gets the JMS consumer properties.
148 * @return the jms consumer properties
150 private Properties getJmsProperties() {
151 final Properties jmsProperties = new Properties();
153 jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
154 jmsProperties.put(PROPERTY_PROVIDER_URL, providerUrl);
155 jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
156 jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
158 return jmsProperties;
162 * Gets the connection factory.
164 * @return the connection factory
166 public String getConnectionFactory() {
167 return connectionFactory;
171 * Gets the initial context factory.
173 * @return the initial context factory
175 public String getInitialContextFactory() {
176 return initialContextFactory;
180 * Gets the provider URL.
182 * @return the provider URL
184 public String getProviderUrl() {
189 * Gets the security principal.
191 * @return the security principal
193 public String getSecurityPrincipal() {
194 return securityPrincipal;
198 * Gets the security credentials.
200 * @return the security credentials
202 public String getSecurityCredentials() {
203 return securityCredentials;
207 * Gets the producer topic.
209 * @return the producer topic
211 public String getProducerTopic() {
212 return producerTopic;
216 * Gets the consumer topic.
218 * @return the consumer topic
220 public String getConsumerTopic() {
221 return consumerTopic;
225 * Gets the consumer wait time.
227 * @return the consumer wait time
229 public long getConsumerWaitTime() {
230 return consumerWaitTime;
234 * Sets the connection factory.
236 * @param connectionFactory the connection factory
238 public void setConnectionFactory(final String connectionFactory) {
239 this.connectionFactory = connectionFactory;
243 * Sets the initial context factory.
245 * @param initialContextFactory the initial context factory
247 public void setInitialContextFactory(final String initialContextFactory) {
248 this.initialContextFactory = initialContextFactory;
252 * Sets the provider URL.
254 * @param providerUrl the provider URL
256 public void setProviderUrl(final String providerUrl) {
257 this.providerUrl = providerUrl;
261 * Sets the security principal.
263 * @param securityPrincipal the security principal
265 public void setSecurityPrincipal(final String securityPrincipal) {
266 this.securityPrincipal = securityPrincipal;
270 * Sets the security credentials.
272 * @param securityCredentials the security credentials
274 public void setSecurityCredentials(final String securityCredentials) {
275 this.securityCredentials = securityCredentials;
279 * Sets the producer topic.
281 * @param producerTopic the producer topic
283 public void setProducerTopic(final String producerTopic) {
284 this.producerTopic = producerTopic;
288 * Sets the consumer topic.
290 * @param consumerTopic the consumer topic
292 public void setConsumerTopic(final String consumerTopic) {
293 this.consumerTopic = consumerTopic;
297 * Sets the consumer wait time.
299 * @param consumerWaitTime the consumer wait time
301 public void setConsumerWaitTime(final int consumerWaitTime) {
302 this.consumerWaitTime = consumerWaitTime;
306 * Checks if is object message sending.
308 * @return true, if checks if is object message sending
310 public boolean isObjectMessageSending() {
311 return objectMessageSending;
315 * Sets the object message sending.
317 * @param objectMessageSending the object message sending
319 public void setObjectMessageSending(final boolean objectMessageSending) {
320 this.objectMessageSending = objectMessageSending;
326 * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
329 public GroupValidationResult validate() {
330 final GroupValidationResult result = super.validate();
332 if (StringUtils.isBlank(initialContextFactory)) {
333 result.setResult("initialContextFactory", ValidationStatus.INVALID,
334 "initialContextFactory must be specified as a string that is a class that implements the "
335 + "interface org.jboss.naming.remote.client.InitialContextFactory");
338 if (StringUtils.isBlank(providerUrl)) {
339 result.setResult("providerUrl", ValidationStatus.INVALID,
340 "providerUrl must be specified as a URL string that specifies the location of "
341 + "configuration information for the service provider to use "
342 + "such as remote://localhost:4447");
345 if (StringUtils.isBlank(securityPrincipal)) {
346 result.setResult("securityPrincipal", ValidationStatus.INVALID,
347 "securityPrincipal must be specified the identity of the principal for authenticating "
348 + "the caller to the service");
351 if (StringUtils.isBlank(securityCredentials)) {
352 result.setResult("securityCredentials", ValidationStatus.INVALID,
353 " securityCredentials must be specified as the credentials of the "
354 + "principal for authenticating the caller to the service");
357 if (StringUtils.isBlank(producerTopic)) {
358 result.setResult("producerTopic", ValidationStatus.INVALID,
359 " producerTopic must be a string that identifies the JMS topic "
360 + "on which Apex will send events");
363 if (StringUtils.isBlank(consumerTopic)) {
364 result.setResult("consumerTopic", ValidationStatus.INVALID,
365 " consumerTopic must be a string that identifies the JMS topic "
366 + "on which Apex will recieve events");
369 if (consumerWaitTime < 0) {
370 result.setResult("consumerWaitTime", ValidationStatus.INVALID,
371 "[" + consumerWaitTime + "] invalid, must be specified as consumerWaitTime >= 0");
377 private String getDefaultCredential() {
378 return new String(Base64.getDecoder().decode(DEFAULT_SECURITY_CREDENTIALS.getBytes()));