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