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