2 * ============LICENSE_START========================================================================
\r
3 * ONAP : tr-069-adapter
\r
4 * =================================================================================================
\r
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
\r
6 * =================================================================================================
\r
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
\r
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
\r
9 * may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
\r
14 * either express or implied. See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
16 * ===============LICENSE_END=======================================================================
\r
19 package org.commscope.tr069adapter.acs.cpe.utils;
\r
21 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.DEFAULT_CONNECTION_TIMEOUT;
\r
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_CONNECTION_IDLE_TIMEOUT;
\r
23 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_OP_FAILED;
\r
24 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_OP_SUCCESS;
\r
25 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_STATUS_OK;
\r
26 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.HTTP_STATUS_OK_WITH_NO_CONTENT;
\r
28 import java.io.IOException;
\r
30 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
\r
31 import org.apache.commons.httpclient.HttpClient;
\r
32 import org.apache.commons.httpclient.HttpConnectionManager;
\r
33 import org.apache.commons.httpclient.HttpException;
\r
34 import org.apache.commons.httpclient.UsernamePasswordCredentials;
\r
35 import org.apache.commons.httpclient.auth.AuthScope;
\r
36 import org.apache.commons.httpclient.methods.GetMethod;
\r
37 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
\r
38 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
\r
39 import org.commscope.tr069adapter.acs.common.OperationResponse;
\r
40 import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;
\r
41 import org.commscope.tr069adapter.acs.common.dto.TR069OperationCode;
\r
42 import org.slf4j.Logger;
\r
43 import org.slf4j.LoggerFactory;
\r
44 import org.springframework.stereotype.Component;
\r
47 public class DeviceConnector {
\r
49 private static final Logger logger = LoggerFactory.getLogger(DeviceConnector.class);
\r
52 * Performs HTTP get on the connection request URL for the device.
\r
54 * @param deviceDetails
\r
56 * @throws IOException
\r
59 public DeviceRPCResponse requestConnectionHttp(TR069DeviceDetails deviceDetails)
\r
60 throws IOException {
\r
62 HttpClient client = new HttpClient();
\r
63 HttpConnectionManager hcm = client.getHttpConnectionManager();
\r
64 HttpConnectionManagerParams hcmParam = hcm.getParams();
\r
65 hcmParam.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);
\r
66 hcmParam.setSoTimeout(DEFAULT_CONNECTION_TIMEOUT);
\r
67 hcm.setParams(hcmParam);
\r
68 client.setHttpConnectionManager(hcm);
\r
70 if (deviceDetails.getUsername() != null && deviceDetails.getPassword() != null) {
\r
71 client.getState().setCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(
\r
72 deviceDetails.getUsername(), deviceDetails.getPassword()));
\r
74 logger.error("Insufficient HTTP arguments: UserName or password is null");
\r
75 throw new HttpException("Insufficient HTTP arguments: UserName or password is null");
\r
78 DefaultHttpMethodRetryHandler retryhandler = new DefaultHttpMethodRetryHandler(1, true);
\r
79 client.getParams().setParameter("http.method.retry-handler", retryhandler);
\r
80 GetMethod get = new GetMethod(deviceDetails.getConnectionRequestURL());
\r
81 get.setDoAuthentication(true);
\r
84 int status = client.executeMethod(get);
\r
85 return populateOperationResult(deviceDetails, get.getResponseBodyAsString(), status);
\r
87 get.releaseConnection();
\r
88 hcm.closeIdleConnections(HTTP_CONNECTION_IDLE_TIMEOUT);
\r
93 * @param deviceDtails
\r
98 private DeviceRPCResponse populateOperationResult(TR069DeviceDetails deviceDtails, String message,
\r
101 DeviceRPCResponse deviceRPCResponse = new DeviceRPCResponse();
\r
102 OperationResponse operationResponse = new OperationResponse();
\r
104 operationResponse.setOperationCode(TR069OperationCode.INITIATE_CR);
\r
106 // HTTP status for successful connection can be 200=Ok, 204=No Content)
\r
107 if (httpStatus != HTTP_STATUS_OK && httpStatus != HTTP_STATUS_OK_WITH_NO_CONTENT) {
\r
108 operationResponse.setStatus(HTTP_OP_FAILED);
\r
109 deviceRPCResponse.setFaultKey(httpStatus + "");
\r
110 deviceRPCResponse.setFaultString(message);
\r
112 operationResponse.setStatus(HTTP_OP_SUCCESS);
\r
115 deviceRPCResponse.setOperationResponse(operationResponse);
\r
116 deviceRPCResponse.setDeviceDetails(deviceDtails);
\r
118 // nBIOperationResult.setOperationId(operationId);// why operation id is here ?
\r
120 return deviceRPCResponse;
\r