2f3f9df96c384efb1062f743cc09b07782560f08
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / service / VesAgentServiceHelper.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.service;\r
20 \r
21 import com.google.gson.Gson;\r
22 \r
23 import java.util.ArrayList;\r
24 import java.util.HashMap;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 import java.util.concurrent.TimeUnit;\r
28 import java.util.function.Function;\r
29 \r
30 import org.commscope.tr069adapter.acs.common.DeviceDetails;\r
31 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;\r
32 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
33 import org.commscope.tr069adapter.mapper.model.VESNotification;\r
34 import org.commscope.tr069adapter.vesagent.async.WaitForNotifications;\r
35 import org.commscope.tr069adapter.vesagent.entity.DeviceDataEntity;\r
36 import org.commscope.tr069adapter.vesagent.exception.VesAgentException;\r
37 import org.commscope.tr069adapter.vesagent.repository.VesDataRepository;\r
38 import org.commscope.tr069adapter.vesagent.timer.HeartBeatTimeoutTask;\r
39 import org.commscope.tr069adapter.vesagent.timer.ScheduleInfo;\r
40 import org.commscope.tr069adapter.vesagent.timer.ScheduleTaskService;\r
41 import org.commscope.tr069adapter.vesagent.util.VesAgentConstants;\r
42 import org.commscope.tr069adapter.vesagent.util.VesAgentUtils;\r
43 import org.slf4j.Logger;\r
44 import org.slf4j.LoggerFactory;\r
45 import org.springframework.beans.factory.annotation.Autowired;\r
46 import org.springframework.stereotype.Component;\r
47 \r
48 @Component\r
49 public class VesAgentServiceHelper {\r
50   private final Logger logger = LoggerFactory.getLogger(this.getClass());\r
51 \r
52   @Autowired\r
53   private Function<String, HeartBeatTimeoutTask> beanFactory;\r
54 \r
55   public HeartBeatTimeoutTask getBeanInstance(String name) {\r
56     return beanFactory.apply(name);\r
57   }\r
58 \r
59   @Autowired\r
60   VesDataRepository vesDataRepository;\r
61 \r
62   @Autowired\r
63   WaitForNotifications waitForNotifications;\r
64 \r
65   @Autowired\r
66   ScheduleTaskService timerService;\r
67 \r
68   private boolean saveDeviceDataEntity(DeviceDetails deviceDetails, String eNodeBName,\r
69       String heartBeatPeriod) throws VesAgentException {\r
70 \r
71     List<DeviceDataEntity> deviceDataEntityList = vesDataRepository\r
72         .findByDeviceIdAndAttrGroup(deviceDetails.getDeviceId(), VesAgentConstants.HEART_BEAT);\r
73 \r
74     DeviceDataEntity deviceDataEntity = null;\r
75     Map<String, String> attrJsonMap = null;\r
76 \r
77     if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {\r
78       deviceDataEntity = new DeviceDataEntity();\r
79 \r
80       deviceDataEntity.setDeviceId(deviceDetails.getDeviceId());\r
81       deviceDataEntity.seteNodeBName(eNodeBName);\r
82       deviceDataEntity.setOui(deviceDetails.getOui());\r
83       deviceDataEntity.setProductClass(deviceDetails.getProductClass());\r
84       deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);\r
85 \r
86       attrJsonMap = new HashMap<>();\r
87     } else {\r
88       deviceDataEntity = deviceDataEntityList.get(0);\r
89       attrJsonMap = deviceDataEntity.getAttributesMap();\r
90     }\r
91 \r
92     String existingHeartBeatPeriod = attrJsonMap.get(VesAgentConstants.HEART_BEAT_PERIOD);\r
93 \r
94     if (null == heartBeatPeriod\r
95         && (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(existingHeartBeatPeriod))\r
96             || existingHeartBeatPeriod\r
97                 .equalsIgnoreCase(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL))) {\r
98       return false;\r
99     }\r
100 \r
101     if (!VesAgentUtils.isNullOrEmpty(heartBeatPeriod)) {\r
102       attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, heartBeatPeriod);\r
103     }\r
104 \r
105     deviceDataEntity.setAttributesMap(attrJsonMap);\r
106 \r
107     vesDataRepository.save(deviceDataEntity);\r
108 \r
109     return true;\r
110   }\r
111 \r
112   public void processHeartBeatSetRequest(DeviceRPCRequest deviceRPCRequest, String heartBeatPeriod,\r
113       String countDownTimer) throws VesAgentException {\r
114 \r
115     String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();\r
116 \r
117     VesAgentUtils.validateDeviceId(deviceId);\r
118 \r
119     if (VesAgentUtils.isNullOrEmpty(heartBeatPeriod)\r
120         && VesAgentUtils.isNullOrEmpty(countDownTimer)) {\r
121       String errorMsg =\r
122           "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;\r
123       logger.error(errorMsg);\r
124       throw new VesAgentException(VesAgentConstants.INVALID_PARAMETER_VALUE, errorMsg);\r
125     }\r
126 \r
127     Object eNodeBNameObj = deviceRPCRequest.getContext().get(VesAgentConstants.ENODEB_NAME);\r
128 \r
129     String eNodeBName = null;\r
130     if (null != eNodeBNameObj) {\r
131       eNodeBName = (String) eNodeBNameObj;\r
132     }\r
133 \r
134     boolean resetTimerJob =\r
135         saveDeviceDataEntity(deviceRPCRequest.getDeviceDetails(), eNodeBName, heartBeatPeriod);\r
136 \r
137     if (resetTimerJob) {\r
138       resetTimerJob(deviceId, heartBeatPeriod, countDownTimer);\r
139       abortRunningDeviceConnectivityCheck(deviceRPCRequest);\r
140     }\r
141 \r
142   }\r
143 \r
144   public void processHeartBeatGetRequest(DeviceRPCRequest deviceRPCRequest) {\r
145 \r
146     String deviceId = deviceRPCRequest.getDeviceDetails().getDeviceId();\r
147     List<DeviceDataEntity> deviceDataEntityList =\r
148         vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);\r
149 \r
150     if (VesAgentUtils.isNullOrEmpty(deviceDataEntityList)\r
151         || VesAgentUtils.isNullOrEmpty(deviceDataEntityList.get(0).getAttributesMap())) {\r
152       return;\r
153     }\r
154 \r
155     DeviceDataEntity deviceDataEntity = deviceDataEntityList.get(0);\r
156 \r
157     List<ParameterDTO> resultparamDTOList = null;\r
158     List<ParameterDTO> paramDTOList = deviceRPCRequest.getOpDetails().getParmeters();\r
159 \r
160     for (ParameterDTO paramDTO : paramDTOList) {\r
161       resultparamDTOList = ifDataTypeObject(paramDTO, deviceDataEntity);\r
162 \r
163       if (!resultparamDTOList.isEmpty()) {\r
164         break;\r
165       }\r
166 \r
167       if (paramDTO.getParamName().equalsIgnoreCase(VesAgentConstants.COUNT_DOWN_TIMER)) {\r
168         paramDTO.setParamValue(getCountDownTimerParam(deviceDataEntity).getParamValue());\r
169       } else {\r
170         paramDTO.setParamValue(deviceDataEntity.getAttributesMap().get(paramDTO.getParamName()));\r
171       }\r
172     }\r
173 \r
174     if (null != resultparamDTOList && !resultparamDTOList.isEmpty()) {\r
175       deviceRPCRequest.getOpDetails().setParmeters(resultparamDTOList);\r
176     }\r
177   }\r
178 \r
179   public void processHeartBeatDeleteRequest(VESNotification vesNotification) {\r
180     List<ParameterDTO> paramDTOList = vesNotification.getOperationDetails().getParmeters();\r
181 \r
182     for (ParameterDTO paramDTO : paramDTOList) {\r
183       if (Boolean.TRUE.equals(VesAgentUtils.isVesNotificationRequest(paramDTO))) {\r
184         List<DeviceDataEntity> deviceDataEntityList = vesDataRepository.findByDeviceIdAndAttrGroup(\r
185             vesNotification.geteNodeBName(), VesAgentConstants.HEART_BEAT);\r
186 \r
187         if (Boolean.TRUE.equals(VesAgentUtils.isNullOrEmpty(deviceDataEntityList))) {\r
188           return;\r
189         }\r
190         vesDataRepository.delete(deviceDataEntityList.get(0));\r
191         timerService.cancelSchedule(vesNotification.geteNodeBName());\r
192         break;\r
193       }\r
194     }\r
195   }\r
196 \r
197   private List<ParameterDTO> ifDataTypeObject(ParameterDTO paramDTO,\r
198       DeviceDataEntity deviceDataEntity) {\r
199     List<ParameterDTO> paramDTOList = new ArrayList<>();\r
200 \r
201     if (null != paramDTO.getDataType()\r
202         && paramDTO.getDataType().equalsIgnoreCase(VesAgentConstants.OBJECT_DATA_TYPE.toLowerCase())\r
203         && paramDTO.getParamName().toLowerCase()\r
204             .contains(VesAgentConstants.HEART_BEAT.toLowerCase())) {\r
205 \r
206       Map<String, String> attrMap = deviceDataEntity.getAttributesMap();\r
207 \r
208       for (Map.Entry<String, String> entry : attrMap.entrySet()) {\r
209         ParameterDTO param = new ParameterDTO();\r
210         param.setParamName(entry.getKey());\r
211         param.setParamValue(entry.getValue());\r
212 \r
213         paramDTOList.add(param);\r
214       }\r
215 \r
216       ParameterDTO countDownParam = getCountDownTimerParam(deviceDataEntity);\r
217       paramDTOList.add(countDownParam);\r
218     }\r
219 \r
220     return paramDTOList;\r
221   }\r
222 \r
223   private ParameterDTO getCountDownTimerParam(DeviceDataEntity deviceDataEntity) {\r
224     Long countDownTimerVal = timerService\r
225         .getTimeRemainingTillNextExecution(deviceDataEntity.getDeviceId(), TimeUnit.MINUTES);\r
226 \r
227     ParameterDTO param = new ParameterDTO();\r
228     param.setParamName(VesAgentConstants.COUNT_DOWN_TIMER);\r
229 \r
230     if (null != countDownTimerVal) {\r
231       param.setParamValue(countDownTimerVal.toString());\r
232     }\r
233 \r
234     return param;\r
235   }\r
236 \r
237 \r
238   public void processHeartBeatGetRequest(String deviceId, Integer HeartBeatPeriod,\r
239       Integer countDownTimer) throws VesAgentException {\r
240     VesAgentUtils.validateDeviceId(deviceId);\r
241 \r
242 \r
243     if (null == HeartBeatPeriod && null == countDownTimer) {// this should just check if heartbeat\r
244                                                             // is null\r
245       String errorMsg =\r
246           "Invalid input: HeartBeatPeriod and countDownTimer both are null for device " + deviceId;\r
247       logger.error(errorMsg);\r
248       throw new VesAgentException(errorMsg);\r
249     }\r
250 \r
251     List<DeviceDataEntity> deviceDataEntityList =\r
252         vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, VesAgentConstants.HEART_BEAT);\r
253 \r
254     DeviceDataEntity deviceDataEntity = null;\r
255     Map<String, String> attrJsonMap = null;\r
256 \r
257     if (null == deviceDataEntityList || deviceDataEntityList.isEmpty()) {\r
258       deviceDataEntity = new DeviceDataEntity();\r
259       deviceDataEntity.setDeviceId(deviceId);\r
260       deviceDataEntity.setAttrGroup(VesAgentConstants.HEART_BEAT);\r
261 \r
262       attrJsonMap = new HashMap<String, String>();\r
263     } else {\r
264       deviceDataEntity = deviceDataEntityList.get(0);\r
265       attrJsonMap = new Gson().fromJson(deviceDataEntity.getAttrJson(), Map.class);\r
266     }\r
267 \r
268 \r
269     if (null != HeartBeatPeriod) {\r
270       attrJsonMap.put(VesAgentConstants.HEART_BEAT_PERIOD, HeartBeatPeriod.toString());\r
271     }\r
272 \r
273     if (null != countDownTimer) {\r
274       attrJsonMap.put(VesAgentConstants.COUNT_DOWN_TIMER, countDownTimer.toString());\r
275     }\r
276 \r
277     String attrJson = new Gson().toJson(attrJsonMap);\r
278     deviceDataEntity.setAttrJson(attrJson);\r
279 \r
280     vesDataRepository.save(deviceDataEntity);\r
281   }\r
282 \r
283   private void resetTimerJob(String deviceId, String heartBeatPeriod, String countDownTimer) {\r
284     if (null == heartBeatPeriod || heartBeatPeriod.isEmpty()) {\r
285       scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));\r
286     } else if (heartBeatPeriod.equals(VesAgentConstants.REMOVE_HEART_BEAT_TIMER_VAL)) {\r
287       timerService.cancelSchedule(deviceId);\r
288     } else {\r
289       if (Boolean.FALSE.equals(VesAgentUtils.isNullOrEmpty(countDownTimer))) {\r
290         scheduleTimerJob(deviceId, Integer.parseInt(countDownTimer));\r
291       } else {\r
292         scheduleTimerJob(deviceId, Integer.parseInt(heartBeatPeriod));\r
293       }\r
294     }\r
295   }\r
296 \r
297   private void scheduleTimerJob(String deviceId, Integer timeoutInterval) {\r
298     ScheduleInfo scheduleInfo = new ScheduleInfo();\r
299     scheduleInfo.setInterval(timeoutInterval);\r
300     scheduleInfo.setTimeUnit(TimeUnit.MINUTES);\r
301 \r
302     HeartBeatTimeoutTask callbackTask = getBeanInstance(deviceId);\r
303 \r
304     timerService.schedule(deviceId, scheduleInfo, callbackTask);\r
305   }\r
306 \r
307   private void abortRunningDeviceConnectivityCheck(DeviceRPCRequest deviceRPCRequest) {\r
308     waitForNotifications.notifyResult(VesAgentUtils.getErrorResponse(deviceRPCRequest, null, null));\r
309   }\r
310 \r
311   public List<DeviceDataEntity> getAllDeviceDataEntity() {\r
312     return (List<DeviceDataEntity>) vesDataRepository.findAll();\r
313   }\r
314 \r
315   public List<DeviceDataEntity> findByDeviceIdAndGroup(String deviceId, String attrGroup) {\r
316     return vesDataRepository.findByDeviceIdAndAttrGroup(deviceId, attrGroup);\r
317   }\r
318 \r
319 \r
320 \r
321 }\r