0d122c67b1d39727c8b9f72046e7dcd8cd99cd79
[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.restrequestor;
23
24 import java.util.Arrays;
25 import java.util.HashSet;
26 import java.util.Set;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29 import java.util.regex.PatternSyntaxException;
30
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33
34 import lombok.Getter;
35 import lombok.Setter;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters;
38 import org.onap.policy.common.parameters.GroupValidationResult;
39 import org.onap.policy.common.parameters.ValidationStatus;
40 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 // @formatter:off
45 /**
46  * Apex parameters for REST as an event carrier technology with Apex issuing a REST request and receiving a REST
47  * response.
48  *
49  * <p>The parameters for this plugin are:
50  * <ol>
51  * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending.
52  * This parameter is mandatory.
53  * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default),
54  *  POST, PUT, and DELETE.
55  * <li>httpHeaders, the HTTP headers to send on REST requests, optional parameter, defaults to none.
56  * <li>httpCodeFilter: a regular expression filter for returned HTTP codes, if the returned HTTP code passes this
57  * filter, then the request is assumed to have succeeded by the plugin, optional, defaults to allowing 2xx codes
58  * through, that is a regular expression of "[2][0-9][0-9]"
59  * </ol>
60  *
61  * @author Liam Fallon (liam.fallon@ericsson.com)
62  */
63 //@formatter:on
64 @Getter
65 @Setter
66 public class RestRequestorCarrierTechnologyParameters extends RestPluginCarrierTechnologyParameters {
67     // Get a reference to the logger
68     private static final Logger LOGGER = LoggerFactory.getLogger(RestRequestorCarrierTechnologyParameters.class);
69
70
71
72     /** The default HTTP method for request events. */
73     public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET;
74
75     /** The default timeout for REST requests. */
76     public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500;
77
78     /**
79      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
80      * service.
81      */
82     public RestRequestorCarrierTechnologyParameters() {
83         super();
84
85         // Set the carrier technology properties for the web socket carrier technology
86         CARRIER_TECHNOLOGY_LABEL = "RESTREQUESTOR";
87         EVENT_PRODUCER_PLUGIN_CLASS = ApexRestRequestorProducer.class.getName();
88         EVENT_CONSUMER_PLUGIN_CLASS = ApexRestRequestorConsumer.class.getName();
89         // Set the carrier technology properties for the web socket carrier technology
90         this.setLabel(CARRIER_TECHNOLOGY_LABEL);
91         this.setEventProducerPluginClass(EVENT_PRODUCER_PLUGIN_CLASS);
92         this.setEventConsumerPluginClass(EVENT_CONSUMER_PLUGIN_CLASS);
93     }
94
95     // @formatter:off
96     /**
97      * Validate the URL.
98      *
99      * <p>Checks:
100      * http://www.blah.com/{par1/somethingelse (Missing end tag) use  {[^\\{}]*$
101      * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{
102      * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.}
103      * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*}
104      * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*}
105      * @param result the result of the validation
106      */
107     // @formatter:on
108     @Override
109     public GroupValidationResult validateUrl(final GroupValidationResult result) {
110         // URL is only set on Requestor consumers
111         if (getUrl() == null) {
112             return result;
113         }
114
115         Matcher matcher = patternErrorKey.matcher(getUrl());
116         if (matcher.find()) {
117             result.setResult("url", ValidationStatus.INVALID,
118                     "no proper URL has been set for event sending on REST requestor");
119         }
120
121         return result;
122     }
123 }