2 * ============LICENSE_START========================================================================
3 * ONAP : tr-069-adapter
4 * =================================================================================================
5 * Copyright (C) 2020 CommScope Inc Intellectual Property.
6 * =================================================================================================
7 * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You
9 * may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14 * either express or implied. See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ===============LICENSE_END=======================================================================
19 package org.commscope.tr069adapter.vesagent.util;
21 import java.util.Calendar;
22 import java.util.List;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
27 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
28 import org.commscope.tr069adapter.acs.common.OperationCode;
29 import org.commscope.tr069adapter.acs.common.OperationResponse;
30 import org.commscope.tr069adapter.acs.common.ParameterDTO;
31 import org.commscope.tr069adapter.mapper.model.VESNotification;
32 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
35 public class VesAgentUtils {
36 private static final Log logger = LogFactory.getLog(VesAgentUtils.class);
38 private static String errorMsg = null;
40 public static boolean isNullOrEmpty(String object) {
41 return (null == object || object.isEmpty());
44 public static Boolean isNullOrEmpty(List list) {
45 return (null == list || list.isEmpty());
48 public static Boolean isNullOrEmpty(Map map) {
49 return (null == map || map.isEmpty());
52 public static void validateDeviceId(String deviceId) throws VesAgentException {
53 if (null == deviceId || deviceId.isEmpty()) {
54 errorMsg = "Error: deviceId in request is null or empty";
55 logger.error(errorMsg);
56 throw new VesAgentException(errorMsg);
60 public static void validateHeartBeatPeriod(Integer heartBeatPeriod) throws VesAgentException {
61 if (null == heartBeatPeriod) {
62 errorMsg = "Error: heartBeatPeriod in request is null or empty";
63 logger.error(errorMsg);
64 throw new VesAgentException(errorMsg);
69 public static void validateCountDownTimer(Integer countDownTimer) throws VesAgentException {
70 if (null == countDownTimer) {
71 errorMsg = "Error: countDownTimer in request is null or empty";
72 logger.error(errorMsg);
73 throw new VesAgentException(errorMsg);
77 public static void validateDeviceRPCRequest(DeviceRPCRequest deviceRPCRequest)
78 throws VesAgentException {
79 if (null == deviceRPCRequest || null == deviceRPCRequest.getOpDetails()
80 || null == deviceRPCRequest.getOpDetails().getParmeters()
81 || deviceRPCRequest.getOpDetails().getParmeters().isEmpty()) {
82 errorMsg = "Error: Input parameter list is null or empty";
83 logger.error(errorMsg);
84 throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
87 if (null == deviceRPCRequest.getDeviceDetails()
88 || null == deviceRPCRequest.getDeviceDetails().getDeviceId()
89 || deviceRPCRequest.getDeviceDetails().getDeviceId().isEmpty()) {
90 errorMsg = "Error: Input deviceId is null or empty";
91 logger.error(errorMsg);
92 throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
96 public static void validateVESNotification(VESNotification notification)
97 throws VesAgentException {
98 if (null == notification || null == notification.getDevnotification()
99 || null == notification.getDevnotification().getDeviceDetails()
100 || null == notification.getDevnotification().getDeviceDetails().getDeviceId()
101 || notification.getDevnotification().getDeviceDetails().getDeviceId().isEmpty()) {
103 errorMsg = "Error: Input device details is null or empty";
104 logger.error(errorMsg);
105 throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
109 public static void validateDelVESNotification(VESNotification notification)
110 throws VesAgentException {
111 if (null == notification || null == notification.getOperationDetails()
112 || null == notification.getOperationDetails().getParmeters()
113 || notification.getOperationDetails().getParmeters().isEmpty()) {
114 errorMsg = "Error: Input parameter list is null or empty";
115 logger.error(errorMsg);
116 throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
119 if (null == notification.geteNodeBName() || notification.geteNodeBName().isEmpty()) {
120 errorMsg = "Error: Input deviceId/enodeBName is null or empty";
121 logger.error(errorMsg);
122 throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
127 public static DeviceRPCResponse getErrorResponse(DeviceRPCRequest deviceRPCRequest,
128 String faultCode, String faultMessage) {
129 DeviceRPCResponse errorResponse = new DeviceRPCResponse();
131 errorResponse.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
133 OperationResponse operationResponse = new OperationResponse();
134 operationResponse.setStatus(VesAgentConstants.RPC_FAILED);// device reachable...change value 1
135 // to some constant or enum
136 operationResponse.setOperationCode(deviceRPCRequest.getOpDetails().getOpCode());
138 errorResponse.setOperationResponse(operationResponse);
139 errorResponse.setFaultKey(faultCode);
140 errorResponse.setFaultString(faultMessage);
142 return errorResponse;
145 public static DeviceRPCResponse getSuccessResponse(DeviceRPCRequest deviceRPCRequest) {
146 DeviceRPCResponse response = new DeviceRPCResponse();
148 response.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
150 OperationResponse operationResponse = new OperationResponse();
151 operationResponse.setStatus(VesAgentConstants.RPC_SUCCESS);
152 operationResponse.setOperationCode(deviceRPCRequest.getOpDetails().getOpCode());
153 operationResponse.setParameterDTOs(deviceRPCRequest.getOpDetails().getParmeters());
155 response.setOperationResponse(operationResponse);
159 public static boolean isDeviceReachable(DeviceRPCResponse deviceRPCResponse) {
160 if (null == deviceRPCResponse || null == deviceRPCResponse.getOperationResponse()) {
164 if (deviceRPCResponse.getOperationResponse()
165 .getStatus() == VesAgentConstants.DEVICE_IS_REACHABLE) {
169 return (null != deviceRPCResponse.getFaultKey() && deviceRPCResponse.getFaultKey()
170 .equalsIgnoreCase(VesAgentConstants.ABORTED_BY_BOOT_BOOTSTRAP));
173 public static Boolean isVesNotificationRequest(ParameterDTO param) {
174 if (null == param.getParamName() || param.getParamName().isEmpty()) {
178 return param.getParamName().toLowerCase().contains(VesAgentConstants.HEART_BEAT.toLowerCase());
182 public static String getDeviceOperationKey(String deviceId, OperationCode opCode) {
183 return deviceId + "-" + opCode;
186 public static long getStartEpochTime() {
187 Calendar calendar = Calendar.getInstance();
189 long minuteEquivalentWithoutSec = calendar.getTimeInMillis();
190 minuteEquivalentWithoutSec = (minuteEquivalentWithoutSec / 60000);
191 minuteEquivalentWithoutSec = minuteEquivalentWithoutSec * 60 * 1000;
193 return minuteEquivalentWithoutSec;