dfa8dc28a0ccbe55b0e6d5a5e0ed379d4c767cfe
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.jms;
22
23 import java.util.Properties;
24
25 import javax.naming.Context;
26
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28
29 /**
30  * Apex parameters for JMS as an event carrier technology.
31  * <p>
32  * The parameters for this plugin are:
33  * <ol>
34  * <li>initialContextFactory: JMS uses a naming {@link Context} object to look up the locations of JMS servers and JMS
35  * topics. An Initial Context Factory is used to when creating a {@link Context} object that can be used for JMS
36  * lookups. The value of this parameter is passed to the {@link Context} with the label
37  * {@link Context#INITIAL_CONTEXT_FACTORY}. Its value must be the full canonical path to a class that implements the
38  * {@code javax.naming.spi.InitialContextFactory} interface. The parameter defaults to the string value
39  * {@code org.jboss.naming.remote.client.InitialContextFactory}.
40  * <li>providerURL: The location of the server to use for naming context lookups. The value of this parameter is passed
41  * to the {@link Context} with the label {@link Context#PROVIDER_URL}. Its value must be a URL that identifies the JMS
42  * naming server. The parameter defaults to the string value {@code remote://localhost:4447}.
43  * <li>securityPrincipal: The user name to use for JMS access. The value of this parameter is passed to the
44  * {@link Context} with the label {@link Context#SECURITY_PRINCIPAL}. Its value must be the user name of a user defined
45  * on the JMS server. The parameter defaults to the string value {@code userid}.
46  * <li>securityCredentials:The password to use for JMS access. The value of this parameter is passed to the
47  * {@link Context} with the label {@link Context#SECURITY_CREDENTIALS}. Its value must be the password of a suer defined
48  * on the JMS server. The parameter defaults to the string value {@code password}.
49  * <li>connectionFactory: JMS uses a {@link javax.jms.ConnectionFactory} instance to create connections towards a JMS
50  * server. The connection factory to use is held in the JMS {@link Context} object. This parameter specifies the label
51  * to use to look up the {@link javax.jms.ConnectionFactory} instance from the JMS {@link Context}.
52  * <li>producerTopic: JMS uses a {@link javax.jms.Topic} instance to for sending and receiving messages. The topic to
53  * use for sending events to JMS from an Apex producer is held in the JMS {@link Context} object. This parameter
54  * specifies the label to use to look up the {@link javax.jms.Topic} instance in the JMS {@link Context} for the JMS
55  * server. The topic must, of course, also be defined on the JMS server. The parameter defaults to the string value
56  * {@code apex-out}.
57  * <li>consumerTopic: The topic to use for receiving events from JMS in an Apex consumer is held in the JMS
58  * {@link Context} object. This parameter specifies the label to use to look up the {@link javax.jms.Topic} instance in
59  * the JMS {@link Context} for the JMS server. The topic must, of course, also be defined on the JMS server. The
60  * parameter defaults to the string value {@code apex-in}.
61  * <li>consumerWaitTime: The amount of milliseconds a JMS consumer should wait between checks of its thread execution
62  * status. The parameter defaults to the long value {@code 100}.
63  * <li>objectMessageSending: A flag that indicates whether Apex producers should send JMS messages as
64  * {@link javax.jms.ObjectMessage} instances for java objects (value {@code true}) or as {@link javax.jms.TextMessage}
65  * instances for strings (value {@code false}) . The parameter defaults to the boolean value {@code true}.
66  * </ol>
67  *
68  * @author Liam Fallon (liam.fallon@ericsson.com)
69  */
70 public class JMSCarrierTechnologyParameters extends CarrierTechnologyParameters {
71     /** The label of this carrier technology. */
72     public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
73
74     /** The producer plugin class for the JMS carrier technology. */
75     public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJMSProducer.class.getCanonicalName();
76
77     /** The consumer plugin class for the JMS carrier technology. */
78     public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJMSConsumer.class.getCanonicalName();
79
80     // @formatter:off
81
82     // Default parameter values
83     private static final String  DEFAULT_CONNECTION_FACTORY        = "jms/RemoteConnectionFactory";
84     private static final String  DEFAULT_INITIAL_CONTEXT_FACTORY   = "org.jboss.naming.remote.client.InitialContextFactory";
85     private static final String  DEFAULT_PROVIDER_URL              = "remote://localhost:4447";
86     private static final String  DEFAULT_SECURITY_PRINCIPAL        = "userid";
87     private static final String  DEFAULT_SECURITY_CREDENTIALS      = "password";
88     private static final String  DEFAULT_CONSUMER_TOPIC            = "apex-in";
89     private static final String  DEFAULT_PRODUCER_TOPIC            = "apex-out";
90     private static final int     DEFAULT_CONSUMER_WAIT_TIME        = 100;
91     private static final boolean DEFAULT_TO_OBJECT_MESSAGE_SENDING = true;
92
93     // Parameter property map tokens
94     private static final String PROPERTY_INITIAL_CONTEXT_FACTORY  = Context.INITIAL_CONTEXT_FACTORY;
95     private static final String PROPERTY_PROVIDER_URL             = Context.PROVIDER_URL;
96     private static final String PROPERTY_SECURITY_PRINCIPAL       = Context.SECURITY_PRINCIPAL;
97     private static final String PROPERTY_SECURITY_CREDENTIALS     = Context.SECURITY_CREDENTIALS;
98
99     // JMS carrier parameters
100     private String  connectionFactory     = DEFAULT_CONNECTION_FACTORY;
101     private String  initialContextFactory = DEFAULT_INITIAL_CONTEXT_FACTORY;
102     private String  providerURL           = DEFAULT_PROVIDER_URL;
103     private String  securityPrincipal     = DEFAULT_SECURITY_PRINCIPAL;
104     private String  securityCredentials   = DEFAULT_SECURITY_CREDENTIALS;
105     private String  producerTopic         = DEFAULT_PRODUCER_TOPIC;
106     private String  consumerTopic         = DEFAULT_CONSUMER_TOPIC;
107     private int     consumerWaitTime      = DEFAULT_CONSUMER_WAIT_TIME;
108     private boolean objectMessageSending  = DEFAULT_TO_OBJECT_MESSAGE_SENDING;
109     // @formatter:on
110
111     /**
112      * Constructor to create a jms carrier technology parameters instance and register the instance with the parameter
113      * service.
114      */
115     public JMSCarrierTechnologyParameters() {
116         super(JMSCarrierTechnologyParameters.class.getCanonicalName());
117
118         // Set the carrier technology properties for the JMS carrier technology
119         this.setLabel(JMS_CARRIER_TECHNOLOGY_LABEL);
120         this.setEventProducerPluginClass(JMS_EVENT_PRODUCER_PLUGIN_CLASS);
121         this.setEventConsumerPluginClass(JMS_EVENT_CONSUMER_PLUGIN_CLASS);
122     }
123
124     /**
125      * Gets the JMS producer properties.
126      *
127      * @return the JMS producer properties
128      */
129     public Properties getJMSProducerProperties() {
130         final Properties jmsProperties = new Properties();
131
132         jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
133         jmsProperties.put(PROPERTY_PROVIDER_URL, providerURL);
134         jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
135         jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
136
137         return jmsProperties;
138     }
139
140     /**
141      * Gets the jms consumer properties.
142      *
143      * @return the jms consumer properties
144      */
145     public Properties getJMSConsumerProperties() {
146         final Properties jmsProperties = new Properties();
147
148         jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
149         jmsProperties.put(PROPERTY_PROVIDER_URL, providerURL);
150         jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
151         jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
152
153         return jmsProperties;
154     }
155
156     /**
157      * Gets the connection factory.
158      *
159      * @return the connection factory
160      */
161     public String getConnectionFactory() {
162         return connectionFactory;
163     }
164
165     /**
166      * Gets the initial context factory.
167      *
168      * @return the initial context factory
169      */
170     public String getInitialContextFactory() {
171         return initialContextFactory;
172     }
173
174     /**
175      * Gets the provider URL.
176      *
177      * @return the provider URL
178      */
179     public String getProviderURL() {
180         return       providerURL;
181     }
182
183     /**
184      * Gets the security principal.
185      *
186      * @return the security principal
187      */
188     public String getSecurityPrincipal() {
189         return securityPrincipal;
190     }
191
192     /**
193      * Gets the security credentials.
194      *
195      * @return the security credentials
196      */
197     public String getSecurityCredentials() {
198         return securityCredentials;
199     }
200
201     /**
202      * Gets the producer topic.
203      *
204      * @return the producer topic
205      */
206     public String getProducerTopic() {
207         return producerTopic;
208     }
209
210     /**
211      * Gets the consumer topic.
212      *
213      * @return the consumer topic
214      */
215     public String getConsumerTopic() {
216         return consumerTopic;
217     }
218
219     /**
220      * Gets the consumer wait time.
221      *
222      * @return the consumer wait time
223      */
224     public long getConsumerWaitTime() {
225         return consumerWaitTime;
226     }
227
228     /**
229      * Sets the connection factory.
230      *
231      * @param connectionFactory the connection factory
232      */
233     public void setConnectionFactory(final String connectionFactory) {
234         this.connectionFactory = connectionFactory;
235     }
236
237     /**
238      * Sets the initial context factory.
239      *
240      * @param initialContextFactory the initial context factory
241      */
242     public void setInitialContextFactory(final String initialContextFactory) {
243         this.initialContextFactory = initialContextFactory;
244     }
245
246     /**
247      * Sets the provider URL.
248      *
249      * @param providerURL the provider URL
250      */
251     public void setProviderURL(final String providerURL) {
252         this.providerURL = providerURL;
253     }
254
255     /**
256      * Sets the security principal.
257      *
258      * @param securityPrincipal the security principal
259      */
260     public void setSecurityPrincipal(final String securityPrincipal) {
261         this.securityPrincipal = securityPrincipal;
262     }
263
264     /**
265      * Sets the security credentials.
266      *
267      * @param securityCredentials the security credentials
268      */
269     public void setSecurityCredentials(final String securityCredentials) {
270         this.securityCredentials = securityCredentials;
271     }
272
273     /**
274      * Sets the producer topic.
275      *
276      * @param producerTopic the producer topic
277      */
278     public void setProducerTopic(final String producerTopic) {
279         this.producerTopic = producerTopic;
280     }
281
282     /**
283      * Sets the consumer topic.
284      *
285      * @param consumerTopic the consumer topic
286      */
287     public void setConsumerTopic(final String consumerTopic) {
288         this.consumerTopic = consumerTopic;
289     }
290
291     /**
292      * Sets the consumer wait time.
293      *
294      * @param consumerWaitTime the consumer wait time
295      */
296     public void setConsumerWaitTime(final int consumerWaitTime) {
297         this.consumerWaitTime = consumerWaitTime;
298     }
299
300     /**
301      * Checks if is object message sending.
302      *
303      * @return true, if checks if is object message sending
304      */
305     public boolean isObjectMessageSending() {
306         return objectMessageSending;
307     }
308
309     /**
310      * Sets the object message sending.
311      *
312      * @param objectMessageSending the object message sending
313      */
314     public void setObjectMessageSending(final boolean objectMessageSending) {
315         this.objectMessageSending = objectMessageSending;
316     }
317
318     /*
319      * (non-Javadoc)
320      *
321      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
322      */
323     @Override
324     public String validate() {
325         final StringBuilder errorMessageBuilder = new StringBuilder();
326
327         errorMessageBuilder.append(super.validate());
328
329         if (initialContextFactory == null || initialContextFactory.trim().length() == 0) {
330             errorMessageBuilder
331                     .append("  initialContextFactory not specified, must be specified as a string that is a class"
332                             + " that implements the interface org.jboss.naming.remote.client.InitialContextFactory\n");
333         }
334
335         if (providerURL == null || providerURL.trim().length() == 0) {
336             errorMessageBuilder.append(
337                     "  providerURL not specified, must be specified as a URL string that specifies the location of "
338                             + "configuration information for the service provider to use such as remote://localhost:4447\n");
339         }
340
341         if (securityPrincipal == null || securityPrincipal.trim().length() == 0) {
342             errorMessageBuilder.append(
343                     "  securityPrincipal not specified, must be specified the identity of the principal for authenticating the caller to the service\n");
344         }
345
346         if (securityCredentials == null || securityCredentials.trim().length() == 0) {
347             errorMessageBuilder.append("  securityCredentials not specified, must be specified as "
348                     + "the credentials of the principal for authenticating the caller to the service\n");
349         }
350
351         if (producerTopic == null || producerTopic.trim().length() == 0) {
352             errorMessageBuilder.append(
353                     "  producerTopic not specified, must be a string that identifies the JMS topic on which Apex will send events\n");
354         }
355
356         if (consumerTopic == null || consumerTopic.trim().length() == 0) {
357             errorMessageBuilder.append(
358                     "  consumerTopic not specified, must be a string that identifies the JMS topic on which Apex will recieve events\n");
359         }
360
361         if (consumerWaitTime < 0) {
362             errorMessageBuilder.append("  consumerWaitTime [" + consumerWaitTime
363                     + "] invalid, must be specified as consumerWaitTime >= 0\n");
364         }
365
366         return errorMessageBuilder.toString();
367     }
368 }