ae43e1fd0d06166e00b9360206a0328f803f9eb2
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / util / VesAgentUtils.java
1 /*
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=======================================================================
17  */
18
19 package org.commscope.tr069adapter.vesagent.util;
20
21 import java.util.Calendar;
22 import java.util.List;
23 import java.util.Map;
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;
33
34
35 public class VesAgentUtils {
36   private static final Log logger = LogFactory.getLog(VesAgentUtils.class);
37
38   private static String errorMsg = null;
39
40   public static boolean isNullOrEmpty(String object) {
41     return (null == object || object.isEmpty());
42   }
43
44   public static Boolean isNullOrEmpty(List list) {
45     return (null == list || list.isEmpty());
46   }
47
48   public static Boolean isNullOrEmpty(Map map) {
49     return (null == map || map.isEmpty());
50   }
51
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);
57     }
58   }
59
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);
65     }
66   }
67
68
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);
74     }
75   }
76
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);
85     }
86
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);
93     }
94   }
95
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()) {
102
103       errorMsg = "Error: Input device details is null or empty";
104       logger.error(errorMsg);
105       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
106     }
107   }
108
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);
117     }
118
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);
123     }
124   }
125
126
127   public static DeviceRPCResponse getErrorResponse(DeviceRPCRequest deviceRPCRequest,
128       String faultCode, String faultMessage) {
129     DeviceRPCResponse errorResponse = new DeviceRPCResponse();
130
131     errorResponse.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
132
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());
137
138     errorResponse.setOperationResponse(operationResponse);
139     errorResponse.setFaultKey(faultCode);
140     errorResponse.setFaultString(faultMessage);
141
142     return errorResponse;
143   }
144
145   public static DeviceRPCResponse getSuccessResponse(DeviceRPCRequest deviceRPCRequest) {
146     DeviceRPCResponse response = new DeviceRPCResponse();
147
148     response.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
149
150     OperationResponse operationResponse = new OperationResponse();
151     operationResponse.setStatus(VesAgentConstants.RPC_SUCCESS);
152     operationResponse.setOperationCode(deviceRPCRequest.getOpDetails().getOpCode());
153     operationResponse.setParameterDTOs(deviceRPCRequest.getOpDetails().getParmeters());
154
155     response.setOperationResponse(operationResponse);
156     return response;
157   }
158
159   public static boolean isDeviceReachable(DeviceRPCResponse deviceRPCResponse) {
160     if (null == deviceRPCResponse || null == deviceRPCResponse.getOperationResponse()) {
161       return false;
162     }
163
164     if (deviceRPCResponse.getOperationResponse()
165         .getStatus() == VesAgentConstants.DEVICE_IS_REACHABLE) {
166       return true;
167     }
168
169     return (null != deviceRPCResponse.getFaultKey() && deviceRPCResponse.getFaultKey()
170         .equalsIgnoreCase(VesAgentConstants.ABORTED_BY_BOOT_BOOTSTRAP));
171   }
172
173   public static Boolean isVesNotificationRequest(ParameterDTO param) {
174     if (null == param.getParamName() || param.getParamName().isEmpty()) {
175       return false;
176     }
177
178     return param.getParamName().toLowerCase().contains(VesAgentConstants.HEART_BEAT.toLowerCase());
179   }
180
181
182   public static String getDeviceOperationKey(String deviceId, OperationCode opCode) {
183     return deviceId + "-" + opCode;
184   }
185
186   public static long getStartEpochTime() {
187     Calendar calendar = Calendar.getInstance();
188
189     long minuteEquivalentWithoutSec = calendar.getTimeInMillis();
190     minuteEquivalentWithoutSec = (minuteEquivalentWithoutSec / 60000);
191     minuteEquivalentWithoutSec = minuteEquivalentWithoutSec * 60 * 1000;
192
193     return minuteEquivalentWithoutSec;
194   }
195
196 }