Change the package name from openo to onap
[holmes/common.git] / holmes-actions / src / main / java / org / onap / holmes / common / api / stat / Alarm.java
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java b/holmes-actions/src/main/java/org/onap/holmes/common/api/stat/Alarm.java
new file mode 100644 (file)
index 0000000..be876e0
--- /dev/null
@@ -0,0 +1,279 @@
+/**\r
+ * Copyright 2017 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.holmes.common.api.stat;\r
+\r
+import java.io.Serializable;\r
+import java.io.StringReader;\r
+import java.lang.reflect.Field;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+import lombok.Getter;\r
+import lombok.Setter;\r
+import org.jdom.Attribute;\r
+import org.jdom.Element;\r
+import org.jdom.input.SAXBuilder;\r
+import org.jdom.output.XMLOutputter;\r
+\r
+\r
+@Getter\r
+@Setter\r
+public class Alarm implements AplusData, Cloneable, Serializable {\r
+\r
+    public static final byte EVENT_CLEARED_ALARM = 3;\r
+\r
+    public static final byte EVENT_CHANGED = 2;\r
+\r
+    public static final byte EVENT_CLEARED = 1;\r
+\r
+    public static final byte EVENT_RAISED = 0;\r
+\r
+    private static final long serialVersionUID = 4520003737132012000L;\r
+    private final static Date clearedServerTime = null;\r
+    private final Map<Integer, Integer> linkIdNodeIdxMap = new HashMap<Integer, Integer>();\r
+    private byte eventType = EVENT_RAISED;\r
+    private long id = 0L;\r
+    private String alarmKey = "";\r
+    private String network = "";\r
+    private String neType = "";\r
+    private String equipType = "";\r
+    private String position1 = "";\r
+    private String subPosition1 = null;\r
+    private String position2 = null;\r
+    private String subPosition2 = null;\r
+    private byte severity = -1;\r
+    private byte alarmType = -1;\r
+    private long probableCause = -1;\r
+    private String specificProblem = null;\r
+    private String additionalText = null;\r
+    private Date raisedTime = new Date();\r
+    private Date raisedServerTime = new Date();\r
+    private Date clearedTime = null;\r
+    private String region = null;\r
+    private String site = null;\r
+    private String aid = null;\r
+    private short systemType = -1;\r
+    private boolean rootAlarmFlag = false;\r
+    private int linkId = -1;\r
+    private int nodeIdx = -1;\r
+    private Set<Integer> linkIds = new HashSet<Integer>();\r
+    private HashMap<String, Integer> priorityMap = new HashMap<String, Integer>();\r
+    private HashMap<String, Integer> rootAlarmTypeMap = new HashMap<String, Integer>();\r
+    private int rootAlarmType = -1;\r
+    private boolean keyAlarmFlag = false;\r
+    private int keyAlarmType = -1;\r
+    private int networkLevel = -1;\r
+    private int linkType = -1;\r
+    private int centerType;\r
+\r
+    public static Alarm valueOf(String xmlString) {\r
+        Element element;\r
+        try {\r
+            StringReader sb = new StringReader(xmlString);\r
+            element = new SAXBuilder().build(sb).getRootElement();\r
+            sb.close();\r
+        } catch (Exception e) {\r
+            throw new RuntimeException(e);\r
+        }\r
+\r
+        Alarm alarm = new Alarm();\r
+        @SuppressWarnings("unchecked")\r
+        List<Attribute> list = element.getAttributes();\r
+        for (Attribute attr : list) {\r
+            String attrName = attr.getName();\r
+            try {\r
+                Field field = Alarm.class.getDeclaredField(attrName);\r
+                if (!attrName.endsWith("Time")) {\r
+                    String type = field.getType().getSimpleName();\r
+                    if ("byte".equals(type)) {\r
+                        field.set(alarm, Byte.parseByte(attr.getValue()));\r
+                    } else if ("long".equals(type)) {\r
+                        field.set(alarm, Long.parseLong(attr.getValue()));\r
+                    } else if ("String".equals(type)) {\r
+                        field.set(alarm, attr.getValue());\r
+                    } else {\r
+                        throw new RuntimeException("unknow attr type: " + type.toString());\r
+                    }\r
+                } else {\r
+                    Date date = new Date();\r
+                    date.setTime(Long.parseLong(attr.getValue()));\r
+                    field.set(alarm, date);\r
+                }\r
+            } catch (Exception e) {\r
+                throw new RuntimeException(e);\r
+            }\r
+        }\r
+\r
+        return alarm;\r
+    }\r
+\r
+    public void addLinkIdNodeIdx(int linkId, int index) {\r
+        linkIdNodeIdxMap.put(linkId, index);\r
+    }\r
+\r
+    /**\r
+     * Comparison of the location of different links: 999 the same link: the location of the node\r
+     * number less than otherIdIdx in the location of the number (in the otherIdIdx upstream):-1,\r
+     * the same link: the node position number is equal to the number of position in the otherIdIdx\r
+     * (in the same site otherIdIdx):0, the same link: the node location number is greater than the\r
+     * number of locations in otherIdIdx (otherIdIdx) with the site):1,\r
+     */\r
+    public int CompareLinkPosition(Map<Integer, Integer> otherIdIdx) {\r
+        Set<Integer> myIdSet = new HashSet<Integer>();\r
+        myIdSet.addAll(this.linkIdNodeIdxMap.keySet());\r
+        myIdSet.retainAll(otherIdIdx.keySet());\r
+\r
+        if (myIdSet.isEmpty()) {\r
+            return 999;\r
+        }\r
+\r
+        for (int tempLinkId : myIdSet) {\r
+            return this.linkIdNodeIdxMap.get(tempLinkId) - otherIdIdx.get(tempLinkId);\r
+        }\r
+        return 999;\r
+    }\r
+\r
+    public boolean containNode(int linkId, int index) {\r
+       return linkIdNodeIdxMap.containsKey(linkId) && linkIdNodeIdxMap.get(linkId) == index;\r
+    }\r
+\r
+    @Override\r
+    public byte getDataType() {\r
+        return APLUS_EVENT;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        final String nullStr = "<null>";\r
+\r
+        Element el = new Element("Alarm");\r
+        el.setAttribute("id", String.valueOf(id));\r
+        el.setAttribute("aid", String.valueOf(aid));\r
+        el.setAttribute("alarmKey", String.valueOf(alarmKey));\r
+        el.setAttribute("eventType", String.valueOf(eventType));\r
+        el.setAttribute("region", region == null ? nullStr : region);\r
+        el.setAttribute("site", site == null ? nullStr : site);\r
+        el.setAttribute("network", network);\r
+        el.setAttribute("neType", neType);\r
+        el.setAttribute("equipType", equipType);\r
+        el.setAttribute("position1", position1);\r
+        el.setAttribute("subPosition1", subPosition1 == null ? nullStr : subPosition1);\r
+        el.setAttribute("position2", position2 == null ? nullStr : position2);\r
+        el.setAttribute("subPosition2", subPosition2 == null ? nullStr : subPosition2);\r
+        el.setAttribute("severity", String.valueOf(severity));\r
+        el.setAttribute("alarmType", String.valueOf(alarmType));\r
+        el.setAttribute("probableCause", String.valueOf(probableCause));\r
+        el.setAttribute("specificProblem", specificProblem == null ? nullStr : specificProblem);\r
+        el.setAttribute("additionalText", additionalText == null ? nullStr : additionalText);\r
+        el.setAttribute("raisedTime", String.valueOf(raisedTime.getTime()));\r
+        el.setAttribute("raisedServerTime", String.valueOf(raisedServerTime.getTime()));\r
+        if (clearedTime != null) {\r
+            el.setAttribute("clearedTime", String.valueOf(clearedTime.getTime()));\r
+        }\r
+        if (clearedServerTime != null) {\r
+            el.setAttribute("clearedServerTime", String.valueOf(clearedServerTime.getTime()));\r
+        }\r
+\r
+        return new XMLOutputter().outputString(el);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return this.getAlarmKey().hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object arg0) {\r
+        if (arg0 == null || !(arg0 instanceof Alarm)) {\r
+            return false;\r
+        }\r
+        return this.alarmKey.equals(((Alarm) arg0).getAlarmKey());\r
+    }\r
+\r
+    @Override\r
+    public Object clone() throws CloneNotSupportedException {\r
+        super.clone();\r
+\r
+        Alarm alarm = new Alarm();\r
+\r
+        alarm.setEventType(this.getEventType());\r
+\r
+        alarm.setAid(this.getAid());\r
+        alarm.setId(this.getId());\r
+        alarm.setAlarmKey(this.getAlarmKey());\r
+\r
+        alarm.setNetwork(this.getNetwork());\r
+        alarm.setEquipType(this.getEquipType());\r
+        alarm.setNeType(this.getNeType());\r
+        alarm.setPosition1(this.getPosition1());\r
+        alarm.setSubPosition1(this.getSubPosition1());\r
+        alarm.setPosition2(this.getPosition2());\r
+        alarm.setSubPosition2(this.getSubPosition2());\r
+        alarm.setRegion(this.getRegion());\r
+        alarm.setSite(this.getSite());\r
+\r
+        alarm.setSeverity(this.getSeverity());\r
+        alarm.setAlarmType(this.getAlarmType());\r
+        alarm.setSystemType(this.getSystemType());\r
+        alarm.setSpecificProblem(this.getSpecificProblem());\r
+        alarm.setAdditionalText(this.getAdditionalText());\r
+        alarm.setProbableCause(this.getProbableCause());\r
+\r
+        alarm.setRaisedTime(this.getRaisedTime());\r
+        alarm.setRaisedServerTime(this.getRaisedServerTime());\r
+        alarm.setClearedTime(this.getClearedTime());\r
+\r
+        return alarm;\r
+    }\r
+\r
+    @Override\r
+    public String getObjectId() {\r
+        return String.valueOf(id);\r
+    }\r
+\r
+    public void addLinkIds(int linkId) {\r
+        linkIds.add(linkId);\r
+    }\r
+\r
+\r
+    public boolean containsPriority(String ruleId) {\r
+        if (priorityMap.keySet().contains(ruleId)) {\r
+            return true;\r
+        }\r
+        return false;\r
+    }\r
+\r
+    public int getPriority(String ruleId) {\r
+        Integer priority = this.priorityMap.get(ruleId);\r
+        if (priority == null) {\r
+            priority = 0;\r
+        }\r
+        return priority;\r
+    }\r
+\r
+    public int getRootAlarmType(String ruleId) {\r
+        Integer rootAlarmType = this.rootAlarmTypeMap.get(ruleId);\r
+        if (rootAlarmType == null) {\r
+            rootAlarmType = -1;\r
+        }\r
+        return rootAlarmType;\r
+    }\r
+\r
+}\r