42613bcf8b70b6a788f0978c2da70ef1d647e1a0
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.event.carrier.restserver;
23
24 import lombok.Getter;
25 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
28
29 /**
30  * Apex parameters for REST as an event carrier technology with Apex as a REST client.
31  *
32  * <p>The parameters for this plugin are:
33  * <ol>
34  * <li>standalone: A flag indicating if APEX should start a standalone HTTP server to process REST requests (true) or
35  * whether it should use an underlying servlet infrastructure such as Apache Tomcat (False). This parameter is legal
36  * only on REST server event inputs.
37  * <li>host: The host name to use when setting up a standalone HTTP server. This parameter is legal only on REST server
38  * event inputs in standalone mode.
39  * <li>port: The port to use when setting up a standalone HTTP server. This parameter is legal only on REST server event
40  * inputs in standalone mode.
41  * </ol>
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 @Getter
46 public class RestServerCarrierTechnologyParameters extends CarrierTechnologyParameters {
47     // @formatter:off
48     private static final int MIN_USER_PORT =  1024;
49     private static final int MAX_USER_PORT = 65535;
50
51     /** The label of this carrier technology. */
52     public static final String RESTSERVER_CARRIER_TECHNOLOGY_LABEL = "RESTSERVER";
53
54     /** The producer plugin class for the REST carrier technology. */
55     public static final String RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestServerProducer.class.getName();
56
57     /** The consumer plugin class for the REST carrier technology. */
58     public static final String RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestServerConsumer.class.getName();
59
60     // REST server parameters
61     private boolean standalone = false;
62     private String  host       = null;
63     private int     port       = -1;
64     private String userName;
65     private String password;
66     private boolean https;
67     private boolean aaf;
68     // @formatter:on
69
70     /**
71      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
72      * service.
73      */
74     public RestServerCarrierTechnologyParameters() {
75         super();
76
77         // Set the carrier technology properties for the web socket carrier technology
78         this.setLabel(RESTSERVER_CARRIER_TECHNOLOGY_LABEL);
79         this.setEventProducerPluginClass(RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS);
80         this.setEventConsumerPluginClass(RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS);
81     }
82
83     /**
84      * {@inheritDoc}.
85      */
86     @Override
87     public GroupValidationResult validate() {
88         final GroupValidationResult result = super.validate();
89
90         // Check if host is defined, it is only defined on REST server consumers
91         if (standalone) {
92             if (host != null && host.trim().length() == 0) {
93                 result.setResult("host", ValidationStatus.INVALID,
94                                 "host not specified, host must be specified as a string");
95             }
96
97             // Check if port is defined, it is only defined on REST server consumers
98             if (port != -1 && port < MIN_USER_PORT || port > MAX_USER_PORT) {
99                 result.setResult("port", ValidationStatus.INVALID,
100                                 "[" + port + "] invalid, must be specified as 1024 <= port <= 65535");
101             }
102         } else {
103             if (host != null) {
104                 result.setResult("host", ValidationStatus.INVALID, "host is specified only in standalone mode");
105             }
106             if (port != -1) {
107                 result.setResult("port", ValidationStatus.INVALID, "port is specified only in standalone mode");
108             }
109         }
110
111         return result;
112     }
113 }