Development of NETCONF RPCs for tr-069 adapter to
[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
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
28 import org.commscope.tr069adapter.acs.common.DeviceRPCResponse;
29 import org.commscope.tr069adapter.acs.common.OperationCode;
30 import org.commscope.tr069adapter.acs.common.OperationResponse;
31 import org.commscope.tr069adapter.acs.common.ParameterDTO;
32 import org.commscope.tr069adapter.mapper.model.VESNotification;
33 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;
34
35
36 public class VesAgentUtils {
37   private VesAgentUtils() {}
38
39   private static final Log logger = LogFactory.getLog(VesAgentUtils.class);
40
41   private static String errorMsg = null;
42
43   public static boolean isNullOrEmpty(String object) {
44     return (null == object || object.isEmpty());
45   }
46
47   public static Boolean isNullOrEmpty(List list) {
48     return (null == list || list.isEmpty());
49   }
50
51   public static Boolean isNullOrEmpty(Map map) {
52     return (null == map || map.isEmpty());
53   }
54
55   public static void validateDeviceId(String deviceId) throws VesAgentException {
56     if (null == deviceId || deviceId.isEmpty()) {
57       errorMsg = "Error: deviceId in request is null or empty";
58       logger.error(errorMsg);
59       throw new VesAgentException(errorMsg);
60     }
61   }
62
63   public static void validateHeartBeatPeriod(Integer heartBeatPeriod) throws VesAgentException {
64     if (null == heartBeatPeriod) {
65       errorMsg = "Error: heartBeatPeriod in request is null or empty";
66       logger.error(errorMsg);
67       throw new VesAgentException(errorMsg);
68     }
69   }
70
71
72   public static void validateCountDownTimer(Integer countDownTimer) throws VesAgentException {
73     if (null == countDownTimer) {
74       errorMsg = "Error: countDownTimer in request is null or empty";
75       logger.error(errorMsg);
76       throw new VesAgentException(errorMsg);
77     }
78   }
79
80   public static void validateDeviceRPCRequest(DeviceRPCRequest deviceRPCRequest)
81       throws VesAgentException {
82     if (null == deviceRPCRequest || null == deviceRPCRequest.getOpDetails()
83         || null == deviceRPCRequest.getOpDetails().getParmeters()
84         || deviceRPCRequest.getOpDetails().getParmeters().isEmpty()) {
85       errorMsg = "Error: Input parameter list is null or empty";
86       logger.error(errorMsg);
87       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
88     }
89
90     if (null == deviceRPCRequest.getDeviceDetails()
91         || null == deviceRPCRequest.getDeviceDetails().getDeviceId()
92         || deviceRPCRequest.getDeviceDetails().getDeviceId().isEmpty()) {
93       errorMsg = "Error: Input deviceId is null or empty";
94       logger.error(errorMsg);
95       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
96     }
97   }
98
99   public static void validateVESNotification(VESNotification notification)
100       throws VesAgentException {
101     if (null == notification || null == notification.getDevnotification()
102         || null == notification.getDevnotification().getDeviceDetails()
103         || null == notification.getDevnotification().getDeviceDetails().getDeviceId()
104         || notification.getDevnotification().getDeviceDetails().getDeviceId().isEmpty()) {
105
106       errorMsg = "Error: Input device details is null or empty";
107       logger.error(errorMsg);
108       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
109     }
110   }
111
112   public static void validateDelVESNotification(VESNotification notification)
113       throws VesAgentException {
114     if (null == notification || null == notification.getOperationDetails()
115         || null == notification.getOperationDetails().getParmeters()
116         || notification.getOperationDetails().getParmeters().isEmpty()) {
117       errorMsg = "Error: Input parameter list is null or empty";
118       logger.error(errorMsg);
119       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
120     }
121
122     if (null == notification.geteNodeBName() || notification.geteNodeBName().isEmpty()) {
123       errorMsg = "Error: Input deviceId/enodeBName is null or empty";
124       logger.error(errorMsg);
125       throw new VesAgentException(VesAgentConstants.INVALID_ARGUMENTS, errorMsg);
126     }
127   }
128
129
130   public static DeviceRPCResponse getErrorResponse(DeviceRPCRequest deviceRPCRequest,
131       String faultCode, String faultMessage) {
132     DeviceRPCResponse errorResponse = new DeviceRPCResponse();
133
134     errorResponse.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
135
136     OperationResponse operationResponse = new OperationResponse();
137     operationResponse.setStatus(VesAgentConstants.RPC_FAILED);// device reachable...change value 1
138                                                               // to some constant or enum
139     operationResponse.setOperationCode(deviceRPCRequest.getOpDetails().getOpCode());
140
141     errorResponse.setOperationResponse(operationResponse);
142     errorResponse.setFaultKey(faultCode);
143     errorResponse.setFaultString(faultMessage);
144
145     return errorResponse;
146   }
147
148   public static DeviceRPCResponse getSuccessResponse(DeviceRPCRequest deviceRPCRequest) {
149     DeviceRPCResponse response = new DeviceRPCResponse();
150
151     response.setDeviceDetails(deviceRPCRequest.getDeviceDetails());
152
153     OperationResponse operationResponse = new OperationResponse();
154     operationResponse.setStatus(VesAgentConstants.RPC_SUCCESS);
155     operationResponse.setOperationCode(deviceRPCRequest.getOpDetails().getOpCode());
156     operationResponse.setParameterDTOs(deviceRPCRequest.getOpDetails().getParmeters());
157
158     response.setOperationResponse(operationResponse);
159     return response;
160   }
161
162   public static boolean isDeviceReachable(DeviceRPCResponse deviceRPCResponse) {
163     if (null == deviceRPCResponse || null == deviceRPCResponse.getOperationResponse()) {
164       return false;
165     }
166
167     if (deviceRPCResponse.getOperationResponse()
168         .getStatus() == VesAgentConstants.DEVICE_IS_REACHABLE) {
169       return true;
170     }
171
172     return (null != deviceRPCResponse.getFaultKey() && deviceRPCResponse.getFaultKey()
173         .equalsIgnoreCase(VesAgentConstants.ABORTED_BY_BOOT_BOOTSTRAP));
174   }
175
176   public static Boolean isVesNotificationRequest(ParameterDTO param) {
177     if (null == param.getParamName() || param.getParamName().isEmpty()) {
178       return false;
179     }
180
181     return param.getParamName().toLowerCase().contains(VesAgentConstants.HEART_BEAT.toLowerCase());
182   }
183
184
185   public static String getDeviceOperationKey(String deviceId, OperationCode opCode) {
186     return deviceId + "-" + opCode;
187   }
188
189   public static long getStartEpochTime() {
190     Calendar calendar = Calendar.getInstance();
191
192     long minuteEquivalentWithoutSec = calendar.getTimeInMillis();
193     minuteEquivalentWithoutSec = (minuteEquivalentWithoutSec / 60000);
194     minuteEquivalentWithoutSec = minuteEquivalentWithoutSec * 60 * 1000;
195
196     return minuteEquivalentWithoutSec;
197   }
198
199 }