258c01d3f40bf70df147dd9d74dd6c143173068d
[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.restclient;
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 /**
34  * Apex parameters for REST as an event carrier technology with Apex as a REST client.
35  *
36  * <p>The parameters for this plugin are:
37  * <ol>
38  * <li>url: The URL that the Apex Rest client will connect to over REST for event reception or event sending. This
39  * parameter is mandatory.
40  * <li>httpMethod: The HTTP method to use when sending events over REST, legal values are POST (default) and PUT. When
41  * receiving events, the REST client plugin always uses the HTTP GET method.
42  * </ol>
43  *
44  * @author Joss Armstrong (joss.armstrong@ericsson.com)
45  */
46 public class RestClientCarrierTechnologyParameters extends CarrierTechnologyParameters {
47     /** The supported HTTP methods. */
48     public enum HttpMethod {
49         GET, PUT, POST, DELETE
50     }
51
52     /** The label of this carrier technology. */
53     public static final String RESTCLIENT_CARRIER_TECHNOLOGY_LABEL = "RESTCLIENT";
54
55     /** The producer plugin class for the REST carrier technology. */
56     public static final String RESTCLIENT_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestClientProducer.class.getName();
57
58     /** The consumer plugin class for the REST carrier technology. */
59     public static final String RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestClientConsumer.class.getName();
60
61     // Commonly occurring strings
62     private static final String HTTP_HEADERS = "httpHeaders";
63
64     private String url = null;
65     private HttpMethod httpMethod = null;
66     private String[][] httpHeaders = null;
67
68     /**
69      * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter
70      * service.
71      */
72     public RestClientCarrierTechnologyParameters() {
73         super();
74
75         // Set the carrier technology properties for the web socket carrier technology
76         this.setLabel(RESTCLIENT_CARRIER_TECHNOLOGY_LABEL);
77         this.setEventProducerPluginClass(RESTCLIENT_EVENT_PRODUCER_PLUGIN_CLASS);
78         this.setEventConsumerPluginClass(RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS);
79
80     }
81
82     /**
83      * Gets the URL for the REST request.
84      *
85      * @return the URL
86      */
87     public String getUrl() {
88         return url;
89     }
90
91     /**
92      * Sets the URL for the REST request.
93      *
94      * @param incomingUrl the URL
95      */
96     public void setUrl(final String incomingUrl) {
97         this.url = incomingUrl;
98     }
99
100     /**
101      * Gets the HTTP method to use for the REST request.
102      *
103      * @return the HTTP method
104      */
105     public HttpMethod getHttpMethod() {
106         return httpMethod;
107     }
108
109     /**
110      * Sets the HTTP method to use for the REST request.
111      *
112      * @param httpMethod the HTTP method
113      */
114     public void setHttpMethod(final HttpMethod httpMethod) {
115         this.httpMethod = httpMethod;
116     }
117
118     /**
119      * Check if http headers have been set for the REST request.
120      *
121      * @return true if headers have been set
122      */
123     public boolean checkHttpHeadersSet() {
124         return httpHeaders != null && httpHeaders.length > 0;
125     }
126
127     /**
128      * Gets the http headers for the REST request.
129      *
130      * @return the headers
131      */
132     public String[][] getHttpHeaders() {
133         return httpHeaders;
134     }
135
136     /**
137      * Gets the http headers for the REST request as a multivalued map.
138      *
139      * @return the headers
140      */
141     public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() {
142         if (httpHeaders == null) {
143             return null;
144         }
145
146         // Load the HTTP headers into the map
147         MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>();
148
149         for (String[] httpHeader : httpHeaders) {
150             httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]);
151         }
152
153         return httpHeaderMap;
154     }
155
156     /**
157      * Sets the header for the REST request.
158      *
159      * @param httpHeaders the incoming HTTP headers
160      */
161     public void setHttpHeaders(final String[][] httpHeaders) {
162         this.httpHeaders = httpHeaders;
163     }
164
165     /**
166      * {@inheritDoc}.
167      */
168     @Override
169     public GroupValidationResult validate() {
170         final GroupValidationResult result = super.validate();
171
172         // Check if the URL has been set for event output
173         if (getUrl() == null) {
174             result.setResult("url", ValidationStatus.INVALID, "no URL has been set for event sending on REST client");
175         }
176
177         if (httpHeaders == null) {
178             return result;
179         }
180
181         for (String[] httpHeader : httpHeaders) {
182             if (httpHeader == null) {
183                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null");
184             } else if (httpHeader.length != 2) {
185                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
186                                 "HTTP header array entries must have one key and one value: "
187                                                 + Arrays.deepToString(httpHeader));
188             } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) {
189                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
190                                 "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader));
191             } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) {
192                 result.setResult(HTTP_HEADERS, ValidationStatus.INVALID,
193                                 "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader));
194             }
195         }
196
197         return result;
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public String toString() {
205         return "RestClientCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders="
206                         + Arrays.deepToString(httpHeaders) + "]";
207     }
208 }