47e31fffa583aae3104f9058fd9146c4568be6f6
[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.restserver;
22
23 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
24
25 /**
26  * Apex parameters for REST as an event carrier technology with Apex as a REST client.
27  *
28  * The parameters for this plugin are:
29  * <ol>
30  * <li>standalone: A flag indicating if APEX should start a standalone HTTP server to process REST requests (true) or
31  * whether it should use an underlying servlet infrastructure such as Apache Tomcat (False). This parameter is legal
32  * only on REST server event inputs.
33  * <li>host: The host name to use when setting up a standalone HTTP server. This parameter is legal only on REST server
34  * event inputs in standalone mode.
35  * <li>port: The port to use when setting up a standalone HTTP server. This parameter is legal only on REST server event
36  * inputs in standalone mode.
37  * </ol>
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class RESTServerCarrierTechnologyParameters extends CarrierTechnologyParameters {
42     // @formatter:off
43     private static final int MIN_USER_PORT =  1024;
44     private static final int MAX_USER_PORT = 65535;
45
46     /** The label of this carrier technology. */
47     public static final String RESTSERVER_CARRIER_TECHNOLOGY_LABEL = "RESTSERVER";
48
49     /** The producer plugin class for the REST carrier technology. */
50     public static final String RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestServerProducer.class.getCanonicalName();
51
52     /** The consumer plugin class for the REST carrier technology. */
53     public static final String RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestServerConsumer.class.getCanonicalName();
54
55     // REST server parameters
56     private boolean standalone = false;
57     private String  host       = null;
58     private int     port       = -1;
59     // @formatter:on
60
61     /**
62      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
63      * service.
64      */
65     public RESTServerCarrierTechnologyParameters() {
66         super(RESTServerCarrierTechnologyParameters.class.getCanonicalName());
67
68         // Set the carrier technology properties for the web socket carrier technology
69         this.setLabel(RESTSERVER_CARRIER_TECHNOLOGY_LABEL);
70         this.setEventProducerPluginClass(RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS);
71         this.setEventConsumerPluginClass(RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS);
72     }
73
74     /**
75      * Check if the REST server is running in standalone mode or is using an underlying servlet infrastructure to manage
76      * requests.
77      *
78      * @return true if in standalone mode
79      */
80     public boolean isStandalone() {
81         return standalone;
82     }
83
84     /**
85      * Gets the host.
86      *
87      * @return the host
88      */
89     public String getHost() {
90         return host;
91     }
92
93     /**
94      * Gets the port.
95      *
96      * @return the port
97      */
98     public int getPort() {
99         return port;
100     }
101
102     /*
103      * (non-Javadoc)
104      *
105      * @see org.onap.policy.apex.apps.uservice.parameters.ApexParameterValidator#validate()
106      */
107     @Override
108     public String validate() {
109         final StringBuilder errorMessageBuilder = new StringBuilder();
110
111         errorMessageBuilder.append(super.validate());
112
113         // Check if host is defined, it is only defined on REST server consumers
114         if (standalone) {
115             if (host != null) {
116                 if (host.trim().length() == 0) {
117                     errorMessageBuilder.append("  host not specified, must be host as a string\n");
118                 }
119             }
120
121             // Check if port is defined, it is only defined on REST server consumers
122             if (port != -1) {
123                 if (port < MIN_USER_PORT || port > MAX_USER_PORT) {
124                     errorMessageBuilder
125                             .append("  port [" + port + "] invalid, must be specified as 1024 <= port <= 6535\n");
126                 }
127             }
128         } else {
129             if (host != null || port != -1) {
130                 errorMessageBuilder.append("  host and port are specified only in standalone mode\n");
131             }
132         }
133
134         return errorMessageBuilder.toString();
135     }
136 }