5e299229c7d3e044375a0555df882bc552a44534
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
22
23 import java.util.Arrays;
24
25 import javax.ws.rs.core.MultivaluedHashMap;
26 import javax.ws.rs.core.MultivaluedMap;
27
28 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
29 import org.onap.policy.common.parameters.GroupValidationResult;
30 import org.onap.policy.common.parameters.ValidationStatus;
31 import org.onap.policy.common.utils.validation.ParameterValidationUtils;
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>restRequestTimeout: The time in milliseconds to wait for a REST request to complete.
45  * <li>restRequestHeader: The necessary header needed
46  * </ol>
47  *
48  * @author Liam Fallon (liam.fallon@ericsson.com)
49  */
50 //@formatter:on
51 public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyParameters {
52     /** The supported HTTP methods. */
53     public enum HttpMethod {
54         GET, PUT, POST, DELETE
55     }
56
57     /** The label of this carrier technology. */
58     public static final String RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL = "RESTREQUESTOR";
59
60     /** The producer plugin class for the REST carrier technology. */
61     public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestRequestorProducer.class
62                     .getCanonicalName();
63
64     /** The consumer plugin class for the REST carrier technology. */
65     public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestRequestorConsumer.class
66                     .getCanonicalName();
67
68     /** The default HTTP method for request events. */
69     public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET;
70
71     /** The default timeout for REST requests. */
72     public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500;
73
74     // Commonly occurring strings
75     private static final String HTTP_HEADERS = "httpHeaders";
76
77     private String url = null;
78     private HttpMethod httpMethod = null;
79     private String[][] httpHeaders = null;
80
81     /**
82      * Constructor to create a REST carrier technology parameters instance and regiaaaster the instance with the
83      * parameter service.
84      */
85     public RestRequestorCarrierTechnologyParameters() {
86         super();
87
88         // Set the carrier technology properties for the web socket carrier technology
89         this.setLabel(RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL);
90         this.setEventProducerPluginClass(RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS);
91         this.setEventConsumerPluginClass(RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS);
92     }
93
94     /**
95      * Gets the URL for the REST request.
96      *
97      * @return the URL
98      */
99     public String getUrl() {
100         return url;
101     }
102
103     /**
104      * Sets the URL for the REST request.
105      *
106      * @param incomingUrl the URL
107      */
108     public void setUrl(final String incomingUrl) {
109         this.url = incomingUrl;
110     }
111
112     /**
113      * Gets the HTTP method to use for the REST request.
114      *
115      * @return the HTTP method
116      */
117     public HttpMethod getHttpMethod() {
118         return httpMethod;
119     }
120
121     /**
122      * Sets the HTTP method to use for the REST request.
123      *
124      * @param httpMethod the HTTP method
125      */
126     public void setHttpMethod(final HttpMethod httpMethod) {
127         this.httpMethod = httpMethod;
128     }
129
130     /**
131      * Check if http headers have been set for the REST request.
132      *
133      * @return true if headers have beenset
134      */
135     public boolean checkHttpHeadersSet() {
136         return httpHeaders != null && httpHeaders.length > 0;
137     }
138
139     /**
140      * Gets the http headers for the REST request.
141      *
142      * @return the headers
143      */
144     public String[][] getHttpHeaders() {
145         return httpHeaders;
146     }
147
148     /**
149      * Gets the http headers for the REST request as a multivalued map.
150      *
151      * @return the headers
152      */
153     public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
154         if (httpHeaders == null) {
155             return null;
156         }
157
158         // Load the HTTP headers into the map
159         MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
160
161         for (String[] httpHeader : httpHeaders) {
162             httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
163         }
164
165         return httpHeaderMap;
166     }
167
168     /**
169      * Sets the header for the REST request.
170      *
171      * @param httpHeaders the incoming HTTP headers
172      */
173     public void setHttpHeaders(final String[][] httpHeaders) {
174         this.httpHeaders = httpHeaders;
175     }
176
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public GroupValidationResult validate() {
182         final GroupValidationResult result = super.validate();
183
184         if (httpHeaders == null) {
185             return result;
186         }
187
188         for (String[] httpHeader : httpHeaders) {
189             if (httpHeader == null) {
190                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
191             } else if (httpHeader.length != 2) {
192                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
193                                 "HTTP header array entries must have one key and one value: "
194                                                 + Arrays.deepToString(httpHeader));
195             } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
196                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
197                                 "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
198             } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
199                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
200                                 "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
201             }
202         }
203
204         return result;
205     }
206
207     /**
208      * {@inheritDoc}.
209      */
210     @Override
211     public String toString() {
212         return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
213                         + Arrays.deepToString(httpHeaders) + "]";
214     }
215 }