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