Development of NETCONF RPCs for tr-069 adapter to
[oam/tr069-adapter.git] / acs / requestprocessor / src / main / java / org / commscope / tr069adapter / acs / requestprocessor / util / TR069RequestProcessorUtility.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
20 package org.commscope.tr069adapter.acs.requestprocessor.util;
21
22 import static org.commscope.tr069adapter.acs.common.utils.AcsConstants.DEVICE_RPC_EXECUTION_TIMEOUT_SECONDS;
23
24 import com.google.gson.Gson;
25 import com.google.gson.reflect.TypeToken;
26
27 import java.lang.reflect.Type;
28 import java.util.ArrayList;
29 import java.util.Date;
30 import java.util.List;
31
32 import org.commscope.tr069adapter.acs.common.DeviceRPCRequest;
33 import org.commscope.tr069adapter.acs.common.OperationCode;
34 import org.commscope.tr069adapter.acs.common.OperationDetails;
35 import org.commscope.tr069adapter.acs.common.OperationOptions;
36 import org.commscope.tr069adapter.acs.common.ParameterDTO;
37 import org.commscope.tr069adapter.acs.common.dto.CustomOperationCode;
38 import org.commscope.tr069adapter.acs.common.dto.ParameterAttributeDTO;
39 import org.commscope.tr069adapter.acs.common.dto.TR069DeviceDetails;
40 import org.commscope.tr069adapter.acs.common.dto.TR069OperationCode;
41 import org.commscope.tr069adapter.acs.common.dto.TR069OperationDetails;
42 import org.commscope.tr069adapter.acs.common.exception.TR069EventProcessingException;
43 import org.commscope.tr069adapter.acs.common.utils.ErrorCode;
44 import org.commscope.tr069adapter.acs.requestprocessor.entity.TR069DeviceRPCRequestEntity;
45
46 public class TR069RequestProcessorUtility {
47
48   protected static final int BLOCK_SIZE = 3999;
49   protected static final int MAX_SIZE = 11998;
50   public static final int DEVICE_REACHABLE_STATUS_CODE = 100;
51   public static final String ADMIN_STATE = "FAPControl.LTE.AdminState";
52   public static final String ADMIN_STATUS = "FAPControl.LTE.AdminStatus";
53
54
55   private TR069RequestProcessorUtility() {
56     super();
57   }
58
59   /**
60    * Converts entity object to DTO
61    * 
62    * @param entityList
63    * @return
64    */
65   public static DeviceRPCRequest convertToDTO(List<TR069DeviceRPCRequestEntity> entityList) {
66     DeviceRPCRequest deviceRPCRequest = new DeviceRPCRequest();
67     OperationDetails operationDetails = new TR069OperationDetails();
68
69     boolean isCustomOperation = true;
70     OperationCode opCode = null;
71
72     TR069DeviceRPCRequestEntity entity = entityList.get(0);
73     if (CustomOperationCode.getByOperationCode(entity.getOpCode()) != null) {
74       opCode = CustomOperationCode.getByOperationCode(entity.getOpCode());
75     } else {
76       opCode = TR069OperationCode.getByOperationCode(entity.getOpCode());
77       isCustomOperation = false;
78     }
79
80     operationDetails.setOpCode(opCode);
81     dtoFromEntityJson(operationDetails, entityList, isCustomOperation);
82     deviceRPCRequest.setOpDetails(operationDetails);
83
84     OperationOptions opOptions = new OperationOptions();
85     opOptions.setExecutionTimeout(entity.getRequestTimeOut());
86     deviceRPCRequest.setOptions(opOptions);
87
88     TR069DeviceDetails deviceDetails = new TR069DeviceDetails();
89     deviceDetails.setDeviceId(entity.getDeviceId());
90
91     deviceRPCRequest.setDeviceDetails(deviceDetails);
92     deviceRPCRequest.setOperationId(entity.getOperationId());
93     return deviceRPCRequest;
94   }
95
96   /**
97    * Converts DTO to entity object
98    * 
99    * @param deviceRPCRequest
100    * @return
101    * @throws TR069EventProcessingException
102    */
103   public static List<TR069DeviceRPCRequestEntity> convertToEntity(DeviceRPCRequest deviceRPCRequest)
104       throws TR069EventProcessingException {
105
106     if (deviceRPCRequest.getOpDetails() == null
107         || deviceRPCRequest.getOpDetails().getOpCode() == null) {
108       throw new TR069EventProcessingException(ErrorCode.MISSING_OPERATION_DETAILS);
109     }
110
111     Integer opCode = null;
112     boolean isCustomOperation = true;
113     OperationCode operationCode = deviceRPCRequest.getOpDetails().getOpCode();
114     if (CustomOperationCode.getByOperationCode(operationCode.getOperationCode()) != null) {
115       CustomOperationCode customOperationCode =
116           CustomOperationCode.getByOperationCode(operationCode.getOperationCode());
117       opCode = customOperationCode.getOperationCode();
118     } else {
119       TR069OperationCode tr069OperationCode =
120           (TR069OperationCode) deviceRPCRequest.getOpDetails().getOpCode();
121       opCode = tr069OperationCode.getOperationCode();
122       isCustomOperation = false;
123     }
124
125     String paramListJSON = paramListToJson(deviceRPCRequest, isCustomOperation);
126     List<TR069DeviceRPCRequestEntity> tr069DeviceRPCRequestEntities =
127         getTR069DeviceRPCRequestEntities(paramListJSON);
128
129     for (TR069DeviceRPCRequestEntity entity : tr069DeviceRPCRequestEntities) {
130       entity.setOpCode(opCode);
131       entity.setCreateTime(new Date());
132       entity.setDeviceId(deviceRPCRequest.getDeviceDetails().getDeviceId());
133       entity.setOperationId(deviceRPCRequest.getOperationId());
134       entity.setIsProcessed(0);
135       if (deviceRPCRequest.getOptions() != null
136           && deviceRPCRequest.getOptions().getExecutionTimeout() != 0L) {
137         entity.setRequestTimeOut(deviceRPCRequest.getOptions().getExecutionTimeout());
138       } else {
139         entity.setRequestTimeOut(DEVICE_RPC_EXECUTION_TIMEOUT_SECONDS);
140         OperationOptions options = new OperationOptions();
141         options.setExecutionTimeout(DEVICE_RPC_EXECUTION_TIMEOUT_SECONDS);
142         deviceRPCRequest.setOptions(options);
143       }
144     }
145
146     return tr069DeviceRPCRequestEntities;
147   }
148
149   /**
150    * @param entity
151    * @param dto
152    * @param isCustomOperation
153    */
154   private static String paramListToJson(DeviceRPCRequest dto, boolean isCustomOperation) {
155     String attrJsonString = null;
156     if (isCustomOperation) {
157       TR069OperationDetails operationDetails = (TR069OperationDetails) dto.getOpDetails();
158       StringBuilder buffer = new StringBuilder();
159
160       List<ParameterDTO> deletParamList = operationDetails.getDeleteParamList();
161       List<ParameterDTO> modifyParamList = operationDetails.getModifyParamList();
162       List<ParameterDTO> setParamList = operationDetails.getSetParamList();
163
164       if (deletParamList != null && !deletParamList.isEmpty()) {
165         convertBooleanValues(deletParamList);
166         buffer.append(toJson(deletParamList));
167       } else {
168         buffer.append(toJson(new ArrayList<>()));
169       }
170
171       if (modifyParamList != null && !modifyParamList.isEmpty()) {
172         convertBooleanValues(modifyParamList);
173         buffer.append(toJson(modifyParamList));
174       } else {
175         buffer.append(toJson(new ArrayList<>()));
176       }
177
178       if (setParamList != null && !setParamList.isEmpty()) {
179         convertBooleanValues(setParamList);
180         buffer.append(toJson(setParamList));
181       } else {
182         buffer.append(toJson(new ArrayList<>()));
183       }
184
185       attrJsonString = buffer.toString();
186     } else {
187       List<ParameterDTO> parameterDTOs = dto.getOpDetails().getParmeters();
188       attrJsonString = toJson(parameterDTOs);
189     }
190
191     return attrJsonString;
192   }
193
194   /**
195    * @param paramListJSON
196    * @return
197    */
198   private static List<TR069DeviceRPCRequestEntity> getTR069DeviceRPCRequestEntities(
199       String paramListJSON) {
200     List<TR069DeviceRPCRequestEntity> tr069DeviceRPCRequestEntities = new ArrayList<>();
201
202     if (paramListJSON.length() < MAX_SIZE) {
203       TR069DeviceRPCRequestEntity entity = new TR069DeviceRPCRequestEntity();
204       entity.setAttributeJson1(getAttriuteJsonData(paramListJSON, 0));
205       entity.setAttributeJson2(getAttriuteJsonData(paramListJSON, 1));
206       entity.setAttributeJson3(getAttriuteJsonData(paramListJSON, 2));
207
208       tr069DeviceRPCRequestEntities.add(entity);
209     } else {
210       int noOfEntities = 0;
211       while (true) {
212         int i = 3 * noOfEntities;
213         TR069DeviceRPCRequestEntity entity = new TR069DeviceRPCRequestEntity();
214         entity.setAttributeJson1(getAttriuteJsonData(paramListJSON, i));
215         entity.setAttributeJson2(getAttriuteJsonData(paramListJSON, ++i));
216         entity.setAttributeJson3(getAttriuteJsonData(paramListJSON, ++i));
217
218         if (entity.getAttributeJson1() != null) {
219           tr069DeviceRPCRequestEntities.add(entity);
220         }
221
222         if (entity.getAttributeJson1() == null || entity.getAttributeJson2() == null
223             || entity.getAttributeJson3() == null) {
224           break;
225         }
226
227         noOfEntities++;
228       }
229
230     }
231     return tr069DeviceRPCRequestEntities;
232   }
233
234   /**
235    * @param eventData
236    * @param blockNum
237    * @return
238    */
239   private static String getAttriuteJsonData(String eventData, int blockNum) {
240     int eventDataSize = eventData.length();
241     int startIndex = blockNum * BLOCK_SIZE;
242     if (startIndex > eventDataSize) {
243       return null;
244     }
245     int endIndex = startIndex + BLOCK_SIZE;
246     endIndex = endIndex < eventDataSize ? endIndex : eventDataSize;
247     return eventData.substring(startIndex, endIndex);
248   }
249
250   /**
251    * @param opDetails
252    * @param entity
253    * @param isCustomOperation
254    */
255   private static void dtoFromEntityJson(OperationDetails opDetails,
256       List<TR069DeviceRPCRequestEntity> entityList, boolean isCustomOperation) {
257     StringBuilder sb = new StringBuilder();
258     for (TR069DeviceRPCRequestEntity entity : entityList) {
259       append(sb, entity.getAttributeJson1());
260       append(sb, entity.getAttributeJson2());
261       append(sb, entity.getAttributeJson3());
262     }
263
264     if (isCustomOperation) {
265       String[] splitStringArray = sb.toString().split("]\\[");
266       List<ParameterDTO> deleteParamList = new ArrayList<>();
267       List<ParameterDTO> modifyParamList = new ArrayList<>();
268       List<ParameterDTO> setParamList = new ArrayList<>();
269       for (int i = 0; i < splitStringArray.length; i++) {
270         String data = splitStringArray[i];
271         if (i == 0) {
272           data = data + "]";
273           deleteParamList.addAll(fromJson(data));
274         } else if ((i + 1) == splitStringArray.length) {
275           data = "[" + data;
276           setParamList.addAll(fromJson(data));
277         } else {
278           data = "[" + data + "]";
279           modifyParamList.addAll(fromJson(data));
280         }
281       }
282       TR069OperationDetails tr069OperationDetails = (TR069OperationDetails) opDetails;
283       tr069OperationDetails.setDeleteParamList(deleteParamList);
284       tr069OperationDetails.setModifyParamList(modifyParamList);
285       tr069OperationDetails.setSetParamList(setParamList);
286     } else {
287       if (TR069OperationCode.SET_PARAMETER_ATTRIBUTES.equals(opDetails.getOpCode())) {
288         List<ParameterDTO> list = fromJsonToParameterAttribute(sb.toString());
289         opDetails.setParmeters(list);
290       } else {
291         List<ParameterDTO> list = fromJson(sb.toString());
292         opDetails.setParmeters(list);
293       }
294     }
295   }
296
297   /**
298    * @param sb
299    * @param temp
300    */
301   private static void append(StringBuilder sb, String temp) {
302     if (temp != null && !temp.isEmpty()) {
303       sb.append(temp);
304     }
305   }
306
307   /**
308    * @param jsonString
309    * @return
310    */
311   private static List<ParameterDTO> fromJson(String jsonString) {
312     Gson gson = new Gson();
313     Type collectionType = new TypeToken<List<ParameterDTO>>() {}.getType();
314     return gson.fromJson(jsonString, collectionType);
315   }
316
317   /**
318    * @param jsonString
319    * @return
320    */
321   private static List<ParameterDTO> fromJsonToParameterAttribute(String jsonString) {
322     Gson gson = new Gson();
323     Type collectionType = new TypeToken<List<ParameterAttributeDTO>>() {}.getType();
324     return gson.fromJson(jsonString, collectionType);
325   }
326
327   /**
328    * @param entity
329    * @return
330    */
331   private static <T> String toJson(T entity) {
332     Gson gson = new Gson();
333     return gson.toJson(entity);
334   }
335
336   /**
337    * @param parameterDTOs
338    */
339   private static void convertBooleanValues(List<ParameterDTO> parameterDTOs) {
340     for (ParameterDTO param : parameterDTOs) {
341       if (param.getDataType() != null && param.getDataType().equalsIgnoreCase("boolean")) {
342         if (param.getParamValue() != null && (param.getParamValue().equalsIgnoreCase("true")
343             || param.getParamValue().equalsIgnoreCase("1"))) {
344           param.setParamValue("1");
345         } else {
346           param.setParamValue("0");
347         }
348       }
349     }
350   }
351
352 }