bf24d2260f49257571402a1206b0af5a4eb8caf4
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restserver / src / main / java / org / onap / policy / apex / plugins / event / carrier / restserver / RestServerCarrierTechnologyParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.restserver;
24
25 import lombok.AccessLevel;
26 import lombok.Getter;
27 import lombok.Setter;
28 import org.apache.commons.lang3.StringUtils;
29 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
30 import org.onap.policy.common.parameters.BeanValidationResult;
31 import org.onap.policy.common.parameters.ValidationStatus;
32 import org.onap.policy.models.base.Validated;
33
34 /**
35  * Apex parameters for REST as an event carrier technology with Apex as a REST client.
36  *
37  * <p>The parameters for this plugin are:
38  * <ol>
39  * <li>standalone: A flag indicating if APEX should start a standalone HTTP server to process REST requests (true) or
40  * whether it should use an underlying servlet infrastructure such as Apache Tomcat (False). This parameter is legal
41  * only on REST server event inputs.
42  * <li>host: The host name to use when setting up a standalone HTTP server. This parameter is legal only on REST server
43  * event inputs in standalone mode.
44  * <li>port: The port to use when setting up a standalone HTTP server. This parameter is legal only on REST server event
45  * inputs in standalone mode.
46  * </ol>
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  */
50 @Getter
51 public class RestServerCarrierTechnologyParameters extends CarrierTechnologyParameters {
52     // @formatter:off
53     private static final int MIN_USER_PORT =  1024;
54     private static final int MAX_USER_PORT = 65535;
55
56     /** The label of this carrier technology. */
57     public static final String RESTSERVER_CARRIER_TECHNOLOGY_LABEL = "RESTSERVER";
58
59     /** The producer plugin class for the REST carrier technology. */
60     public static final String RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestServerProducer.class.getName();
61
62     /** The consumer plugin class for the REST carrier technology. */
63     public static final String RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestServerConsumer.class.getName();
64
65     // REST server parameters
66     @Setter(AccessLevel.PACKAGE)
67     private boolean standalone = false;
68     @Setter(AccessLevel.PACKAGE)
69     private String  host       = null;
70     @Setter(AccessLevel.PACKAGE)
71     private int     port       = -1;
72     private String userName;
73     private String password;
74     private boolean https;
75     private boolean aaf;
76     // @formatter:on
77
78     /**
79      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
80      * service.
81      */
82     public RestServerCarrierTechnologyParameters() {
83         super();
84
85         // Set the carrier technology properties for the web socket carrier technology
86         this.setLabel(RESTSERVER_CARRIER_TECHNOLOGY_LABEL);
87         this.setEventProducerPluginClass(RESTSERVER_EVENT_PRODUCER_PLUGIN_CLASS);
88         this.setEventConsumerPluginClass(RESTSERVER_EVENT_CONSUMER_PLUGIN_CLASS);
89     }
90
91     /**
92      * {@inheritDoc}.
93      */
94     @Override
95     public BeanValidationResult validate() {
96         final BeanValidationResult result = super.validate();
97
98         // Check if host is defined, it is only defined on REST server consumers
99         if (standalone) {
100             if (StringUtils.isBlank(host)) {
101                 result.addResult("host", host, ValidationStatus.INVALID, Validated.IS_BLANK);
102             }
103
104             // Check if port is defined, it is only defined on REST server consumers
105             if (port != -1 && port < MIN_USER_PORT || port > MAX_USER_PORT) {
106                 result.addResult("port", port, ValidationStatus.INVALID, "must be between 1024 and 65535");
107             }
108         } else {
109             if (host != null) {
110                 result.addResult("host", host, ValidationStatus.INVALID, "should be specified only in standalone mode");
111             }
112             if (port != -1) {
113                 result.addResult("port", port, ValidationStatus.INVALID, "should be specified only in standalone mode");
114             }
115         }
116
117         return result;
118     }
119 }