3e2f239dfbac7ab83a7af3b6c139d650cb76a86f
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019,2021 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.plugins.event.carrier.jms;
24
25 import java.util.Properties;
26 import javax.naming.Context;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
30 import org.onap.policy.common.parameters.annotations.Min;
31 import org.onap.policy.common.parameters.annotations.NotBlank;
32 import org.onap.policy.common.parameters.annotations.NotNull;
33
34 /**
35  * Apex parameters for JMS as an event carrier technology.
36  *
37  * <p>The parameters for this plugin are:
38  * <ol>
39  * <li>initialContextFactory: JMS uses a naming {@link Context} object to look up the locations of JMS servers and JMS
40  * topics. An Initial Context Factory is used to when creating a {@link Context} object that can be used for JMS
41  * lookups. The value of this parameter is passed to the {@link Context} with the label
42  * {@link Context#INITIAL_CONTEXT_FACTORY}. Its value must be the full canonical path to a class that implements the
43  * {@code javax.naming.spi.InitialContextFactory} interface. The parameter defaults to the string value
44  * {@code org.jboss.naming.remote.client.InitialContextFactory}.
45  * <li>providerURL: The location of the server to use for naming context lookups. The value of this parameter is passed
46  * to the {@link Context} with the label {@link Context#PROVIDER_URL}. Its value must be a URL that identifies the JMS
47  * naming server. The parameter defaults to the string value {@code remote://localhost:4447}.
48  * <li>securityPrincipal: The user name to use for JMS access. The value of this parameter is passed to the
49  * {@link Context} with the label {@link Context#SECURITY_PRINCIPAL}. Its value must be the user name of a user defined
50  * on the JMS server. The parameter defaults to the string value {@code userid}.
51  * <li>securityCredentials:The password to use for JMS access. The value of this parameter is passed to the
52  * {@link Context} with the label {@link Context#SECURITY_CREDENTIALS}. Its value must be the password of a suer defined
53  * on the JMS server. The parameter defaults to the string value {@code password}.
54  * <li>connectionFactory: JMS uses a {@link javax.jms.ConnectionFactory} instance to create connections towards a JMS
55  * server. The connection factory to use is held in the JMS {@link Context} object. This parameter specifies the label
56  * to use to look up the {@link javax.jms.ConnectionFactory} instance from the JMS {@link Context}.
57  * <li>producerTopic: JMS uses a {@link javax.jms.Topic} instance to for sending and receiving messages. The topic to
58  * use for sending events to JMS from an Apex producer is held in the JMS {@link Context} object. This parameter
59  * specifies the label to use to look up the {@link javax.jms.Topic} instance in the JMS {@link Context} for the JMS
60  * server. The topic must, of course, also be defined on the JMS server. The parameter defaults to the string value
61  * {@code apex-out}.
62  * <li>consumerTopic: The topic to use for receiving events from JMS in an Apex consumer is held in the JMS
63  * {@link Context} object. This parameter specifies the label to use to look up the {@link javax.jms.Topic} instance in
64  * the JMS {@link Context} for the JMS server. The topic must, of course, also be defined on the JMS server. The
65  * parameter defaults to the string value {@code apex-in}.
66  * <li>consumerWaitTime: The amount of milliseconds a JMS consumer should wait between checks of its thread execution
67  * status. The parameter defaults to the long value {@code 100}.
68  * <li>objectMessageSending: A flag that indicates whether Apex producers should send JMS messages as
69  * {@link javax.jms.ObjectMessage} instances for java objects (value {@code true}) or as {@link javax.jms.TextMessage}
70  * instances for strings (value {@code false}) . The parameter defaults to the boolean value {@code true}.
71  * </ol>
72  *
73  * @author Liam Fallon (liam.fallon@ericsson.com)
74  */
75 @Getter
76 @Setter
77 public class JmsCarrierTechnologyParameters extends CarrierTechnologyParameters {
78     /** The label of this carrier technology. */
79     public static final String JMS_CARRIER_TECHNOLOGY_LABEL = "JMS";
80
81     /** The producer plugin class for the JMS carrier technology. */
82     public static final String JMS_EVENT_PRODUCER_PLUGIN_CLASS = ApexJmsProducer.class.getName();
83
84     /** The consumer plugin class for the JMS carrier technology. */
85     public static final String JMS_EVENT_CONSUMER_PLUGIN_CLASS = ApexJmsConsumer.class.getName();
86
87     // @formatter:off
88
89     // Default parameter values
90     private static final String  DEFAULT_CONNECTION_FACTORY    = "jms/RemoteConnectionFactory";
91     private static final String  DEFAULT_INITIAL_CTXT_FACTORY  = "org.jboss.naming.remote.client.InitialContextFactory";
92     private static final String  DEFAULT_PROVIDER_URL          = null;
93     private static final String  DEFAULT_SECURITY_PRINCIPAL    = null;
94     private static final String  DEFAULT_SECURITY_CREDENTIALS  = null;
95     private static final String  DEFAULT_CONSUMER_TOPIC        = "apex-in";
96     private static final String  DEFAULT_PRODUCER_TOPIC        = "apex-out";
97     private static final int     DEFAULT_CONSUMER_WAIT_TIME    = 100;
98     private static final boolean DEFAULT_TO_OBJECT_MSG_SENDING = true;
99
100     // Parameter property map tokens
101     private static final String PROPERTY_INITIAL_CONTEXT_FACTORY  = Context.INITIAL_CONTEXT_FACTORY;
102     private static final String PROPERTY_PROVIDER_URL             = Context.PROVIDER_URL;
103     private static final String PROPERTY_SECURITY_PRINCIPAL       = Context.SECURITY_PRINCIPAL;
104     private static final String PROPERTY_SECURITY_CREDENTIALS     = Context.SECURITY_CREDENTIALS;
105
106     // JMS carrier parameters
107     private String  connectionFactory     = DEFAULT_CONNECTION_FACTORY;
108     @NotNull @NotBlank
109     private String  initialContextFactory = DEFAULT_INITIAL_CTXT_FACTORY;
110     @NotNull @NotBlank
111     private String  providerUrl           = DEFAULT_PROVIDER_URL;
112     @NotNull @NotBlank
113     private String  securityPrincipal     = DEFAULT_SECURITY_PRINCIPAL;
114     @NotNull @NotBlank
115     private String  securityCredentials   = DEFAULT_SECURITY_CREDENTIALS;
116     @NotNull @NotBlank
117     private String  producerTopic         = DEFAULT_PRODUCER_TOPIC;
118     @NotNull @NotBlank
119     private String  consumerTopic         = DEFAULT_CONSUMER_TOPIC;
120     @Min(0)
121     private int     consumerWaitTime      = DEFAULT_CONSUMER_WAIT_TIME;
122     private boolean objectMessageSending  = DEFAULT_TO_OBJECT_MSG_SENDING;
123     // @formatter:on
124
125     /**
126      * Constructor to create a jms carrier technology parameters instance and register the instance with the parameter
127      * service.
128      */
129     public JmsCarrierTechnologyParameters() {
130         super();
131
132         // Set the carrier technology properties for the JMS carrier technology
133         this.setLabel(JMS_CARRIER_TECHNOLOGY_LABEL);
134         this.setEventProducerPluginClass(JMS_EVENT_PRODUCER_PLUGIN_CLASS);
135         this.setEventConsumerPluginClass(JMS_EVENT_CONSUMER_PLUGIN_CLASS);
136     }
137
138     /**
139      * Gets the JMS producer properties.
140      *
141      * @return the JMS producer properties
142      */
143     public Properties getJmsProducerProperties() {
144         return getJmsProperties();
145     }
146
147     /**
148      * Gets the jms consumer properties.
149      *
150      * @return the jms consumer properties
151      */
152     public Properties getJmsConsumerProperties() {
153         return getJmsProperties();
154     }
155
156     /**
157      * Gets the JMS consumer properties.
158      *
159      * @return the jms consumer properties
160      */
161     private Properties getJmsProperties() {
162         final Properties jmsProperties = new Properties();
163
164         jmsProperties.put(PROPERTY_INITIAL_CONTEXT_FACTORY, initialContextFactory);
165
166         if (providerUrl != null) {
167             jmsProperties.put(PROPERTY_PROVIDER_URL, providerUrl);
168         }
169
170         if (securityPrincipal != null) {
171             jmsProperties.put(PROPERTY_SECURITY_PRINCIPAL, securityPrincipal);
172         }
173
174         if (securityCredentials != null) {
175             jmsProperties.put(PROPERTY_SECURITY_CREDENTIALS, securityCredentials);
176         }
177
178         return jmsProperties;
179     }
180 }