4296331e4de48e1f6b7d62a786208fb915b2b8a1
[policy/apex-pdp.git] / plugins / plugins-event / plugins-event-carrier / plugins-event-carrier-restrequestor / src / main / java / org / onap / policy / apex / plugins / event / carrier / restrequestor / RestRequestorCarrierTechnologyParameters.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 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.restrequestor;
23
24 import java.util.regex.Matcher;
25
26 import lombok.Getter;
27 import lombok.Setter;
28
29 import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters;
30 import org.onap.policy.common.parameters.GroupValidationResult;
31 import org.onap.policy.common.parameters.ValidationStatus;
32
33 // @formatter:off
34 /**
35  * Apex parameters for REST as an event carrier technology with Apex issuing a REST request and receiving a REST
36  * response.
37  *
38  * <p>The parameters for this plugin are:
39  * <ol>
40  * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending.
41  * This parameter is mandatory.
42  * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default),
43  *  POST, PUT, and DELETE.
44  * <li>httpHeaders, the HTTP headers to send on REST requests, optional parameter, defaults to none.
45  * <li>httpCodeFilter: a regular expression filter for returned HTTP codes, if the returned HTTP code passes this
46  * filter, then the request is assumed to have succeeded by the plugin, optional, defaults to allowing 2xx codes
47  * through, that is a regular expression of "[2][0-9][0-9]"
48  * </ol>
49  *
50  * @author Liam Fallon (liam.fallon@ericsson.com)
51  */
52 //@formatter:on
53 @Getter
54 @Setter
55 public class RestRequestorCarrierTechnologyParameters extends RestPluginCarrierTechnologyParameters {
56
57     /** The default HTTP method for request events. */
58     public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET;
59
60     /** The default timeout for REST requests. */
61     public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500;
62
63     /**
64      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
65      * service.
66      */
67     public RestRequestorCarrierTechnologyParameters() {
68         super();
69
70         // Set the carrier technology properties for the REST requestor carrier technology
71         this.setLabel("RESTREQUESTOR");
72         this.setEventProducerPluginClass(ApexRestRequestorProducer.class.getName());
73         this.setEventConsumerPluginClass(ApexRestRequestorConsumer.class.getName());
74     }
75
76     // @formatter:off
77     /**
78      * Validate the URL.
79      *
80      * <p>Checks:
81      * http://www.blah.com/{par1/somethingelse (Missing end tag) use  {[^\\{}]*$
82      * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{
83      * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.}
84      * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*}
85      * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*}
86      * @param result the result of the validation
87      */
88     // @formatter:on
89     @Override
90     public GroupValidationResult validateUrl(final GroupValidationResult result) {
91         // URL is only set on Requestor consumers
92         if (getUrl() == null) {
93             return result;
94         }
95
96         Matcher matcher = patternErrorKey.matcher(getUrl());
97         if (matcher.find()) {
98             result.setResult("url", ValidationStatus.INVALID,
99                     "no proper URL has been set for event sending on REST requestor");
100         }
101
102         return result;
103     }
104 }