Fix: Update Sonar needed parameters
[oam/tr069-adapter.git] / ves-agent / src / main / java / org / commscope / tr069adapter / vesagent / fault / Parser.java
1 /*\r
2  * ============LICENSE_START========================================================================\r
3  * ONAP : tr-069-adapter\r
4  * =================================================================================================\r
5  * Copyright (C) 2020 CommScope Inc Intellectual Property.\r
6  * =================================================================================================\r
7  * This tr-069-adapter software file is distributed by CommScope Inc under the Apache License,\r
8  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You\r
9  * may obtain a copy of the License at\r
10  *\r
11  * http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,\r
14  * either express or implied. See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ===============LICENSE_END=======================================================================\r
17  */\r
18 \r
19 package org.commscope.tr069adapter.vesagent.fault;\r
20 \r
21 import java.util.HashMap;\r
22 import java.util.List;\r
23 import java.util.Map;\r
24 \r
25 import org.apache.commons.logging.Log;\r
26 import org.apache.commons.logging.LogFactory;\r
27 import org.commscope.tr069adapter.acs.common.ParameterDTO;\r
28 import org.commscope.tr069adapter.vesagent.InformEventData;\r
29 import org.commscope.tr069adapter.vesagent.exception.InvalidFaultOperationException;\r
30 import org.commscope.tr069adapter.vesagent.model.PnfRegEventFields;\r
31 \r
32 public class Parser {\r
33 \r
34   private Log logger = LogFactory.getLog(Parser.class);\r
35 \r
36   public Map<String, ExpeditedEvent> parseFaultParams(List<ParameterDTO> parameters)\r
37       throws InvalidFaultOperationException {\r
38     Map<String, ExpeditedEvent> dmAlarmParameters = new HashMap<>();\r
39 \r
40     try {\r
41       for (ParameterDTO parameter : parameters) {\r
42         logger.debug("PARAM NAME " + parameter.getParamName());\r
43         if (parameter.getParamName() == null || !isAlarmEvent(parameter.getParamName())) {\r
44           continue;\r
45         }\r
46 \r
47         String alarmParamMO = parameter.getParamName();\r
48         String alarmParam = null;\r
49         String alarmParamParentMoWithIndex = null;\r
50 \r
51         if (null != alarmParamMO) {\r
52           alarmParam = alarmParamMO.substring(alarmParamMO.lastIndexOf('.') + 1);\r
53           alarmParamParentMoWithIndex = alarmParamMO.substring(0, alarmParamMO.lastIndexOf('.'));\r
54         }\r
55 \r
56         ExpeditedEvent expeditedEvent = null;\r
57         if (dmAlarmParameters.containsKey(alarmParamParentMoWithIndex)) {\r
58           expeditedEvent = dmAlarmParameters.get(alarmParamParentMoWithIndex);\r
59         } else {\r
60           expeditedEvent = new ExpeditedEvent();\r
61           logger.debug("ADDING " + alarmParamParentMoWithIndex);\r
62           dmAlarmParameters.put(alarmParamParentMoWithIndex, expeditedEvent);\r
63         }\r
64 \r
65         expeditedEvent.parse(parameter, alarmParam);\r
66       }\r
67     } catch (NumberFormatException ex) {\r
68       logger.error("Error while parsing alarm event parameter {}", ex);\r
69       throw new InvalidFaultOperationException(ex.getMessage());\r
70     } catch (Exception e) {\r
71       logger.error("Error occurred while parsing alarm event notification");\r
72       throw new InvalidFaultOperationException(e.getMessage());\r
73     }\r
74 \r
75     return dmAlarmParameters;\r
76   }\r
77 \r
78   public PnfRegEventFields parseNotificationParams(List<ParameterDTO> parameters) {\r
79     InformEventData feild = new InformEventData();\r
80     try {\r
81       for (ParameterDTO parameter : parameters) {\r
82 \r
83         if (isAlarmEvent(parameter.getParamName())) {\r
84           continue;\r
85         }\r
86 \r
87         feild.parse(parameter, parameter.getParamName());\r
88       }\r
89     } catch (Exception e) {\r
90       logger.error("Error occurred while parsing alarm event notification");\r
91     }\r
92 \r
93     return feild.getFeilds();\r
94   }\r
95 \r
96   public static boolean isAlarmEvent(String str) {\r
97     return (str.contains(".FaultMgmt.ExpeditedEvent.")\r
98         || str.startsWith("InternetGatewayDevice.X_0005B9_FaultStatus"));\r
99   }\r
100 }\r