2 * Copyright 2017 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openo.holmes.common.api.stat;
\r
19 import java.io.Serializable;
\r
20 import java.io.StringReader;
\r
21 import java.lang.reflect.Field;
\r
22 import java.util.Date;
\r
23 import java.util.HashMap;
\r
24 import java.util.HashSet;
\r
25 import java.util.List;
\r
26 import java.util.Map;
\r
27 import java.util.Set;
\r
28 import lombok.Getter;
\r
29 import lombok.Setter;
\r
30 import org.jdom.Attribute;
\r
31 import org.jdom.Element;
\r
32 import org.jdom.input.SAXBuilder;
\r
33 import org.jdom.output.XMLOutputter;
\r
38 public class Alarm implements AplusData, Cloneable, Serializable {
\r
40 public static final byte EVENT_CLEARED_ALARM = 3;
\r
42 public static final byte EVENT_CHANGED = 2;
\r
44 public static final byte EVENT_CLEARED = 1;
\r
46 public static final byte EVENT_RAISED = 0;
\r
48 private static final long serialVersionUID = 4520003737132012000L;
\r
50 private byte eventType = EVENT_RAISED;
\r
52 private long id = 0L;
\r
54 private String alarmKey = "";
\r
56 private String network = "";
\r
58 private String neType = "";
\r
60 private String equipType = "";
\r
62 private String position1 = "";
\r
64 private String subPosition1 = null;
\r
66 private String position2 = null;
\r
68 private String subPosition2 = null;
\r
70 private byte severity = -1;
\r
72 private byte alarmType = -1;
\r
74 private long probableCause = -1;
\r
76 private String specificProblem = null;
\r
78 private String additionalText = null;
\r
80 private Date raisedTime = new Date();
\r
82 private Date raisedServerTime = new Date();
\r
84 private Date clearedTime = null;
\r
86 private final Date clearedServerTime = null;
\r
88 private String region = null;
\r
90 private String site = null;
\r
92 private String aid = null;
\r
94 private short systemType = -1;
\r
96 private boolean rootAlarmFlag = false;
\r
98 private int linkId = -1;
\r
100 private int nodeIdx = -1;
\r
102 private Set<Integer> linkIds = new HashSet<Integer>();
\r
104 private HashMap<String, Integer> priorityMap = new HashMap<String, Integer>();
\r
106 private HashMap<String, Integer> rootAlarmTypeMap = new HashMap<String, Integer>();
\r
108 private int rootAlarmType = -1;
\r
110 private boolean keyAlarmFlag = false;
\r
112 private int keyAlarmType = -1;
\r
114 private int networkLevel = -1;
\r
116 private int linkType = -1;
\r
118 private int centerType;
\r
120 private final Map<Integer, Integer> linkIdNodeIdxMap = new HashMap<Integer, Integer>();
\r
122 public void addLinkIdNodeIdx(int linkId, int index) {
\r
123 linkIdNodeIdxMap.put(linkId, index);
\r
127 * Comparison of the location of different links: 999 the same link: the location of the node
\r
128 * number less than otherIdIdx in the location of the number (in the otherIdIdx upstream):-1,
\r
129 * the same link: the node position number is equal to the number of position in the otherIdIdx
\r
130 * (in the same site otherIdIdx):0, the same link: the node location number is greater than the
\r
131 * number of locations in otherIdIdx (otherIdIdx) with the site):1,
\r
133 public int CompareLinkPosition(Map<Integer, Integer> otherIdIdx) {
\r
134 Set<Integer> myIdSet = new HashSet<Integer>();
\r
135 myIdSet.addAll(this.linkIdNodeIdxMap.keySet());
\r
136 myIdSet.retainAll(otherIdIdx.keySet());
\r
138 if (myIdSet.isEmpty()) {
\r
142 for (int tempLinkId : myIdSet) {
\r
143 return this.linkIdNodeIdxMap.get(tempLinkId) - otherIdIdx.get(tempLinkId);
\r
148 public boolean containNode(int linkId, int index) {
\r
149 if (linkIdNodeIdxMap.containsKey(linkId) && linkIdNodeIdxMap.get(linkId) == index) {
\r
157 public byte getDataType() {
\r
158 return APLUS_EVENT;
\r
162 public String toString() {
\r
163 final String nullStr = "<null>";
\r
165 Element el = new Element("Alarm");
\r
166 el.setAttribute("id", String.valueOf(id));
\r
167 el.setAttribute("aid", String.valueOf(aid));
\r
168 el.setAttribute("alarmKey", String.valueOf(alarmKey));
\r
169 el.setAttribute("eventType", String.valueOf(eventType));
\r
170 el.setAttribute("region", region == null ? nullStr : region);
\r
171 el.setAttribute("site", site == null ? nullStr : site);
\r
172 el.setAttribute("network", network);
\r
173 el.setAttribute("neType", neType);
\r
174 el.setAttribute("equipType", equipType);
\r
175 el.setAttribute("position1", position1);
\r
176 el.setAttribute("subPosition1", subPosition1 == null ? nullStr : subPosition1);
\r
177 el.setAttribute("position2", position2 == null ? nullStr : position2);
\r
178 el.setAttribute("subPosition2", subPosition2 == null ? nullStr : subPosition2);
\r
179 el.setAttribute("severity", String.valueOf(severity));
\r
180 el.setAttribute("alarmType", String.valueOf(alarmType));
\r
181 el.setAttribute("probableCause", String.valueOf(probableCause));
\r
182 el.setAttribute("specificProblem", specificProblem == null ? nullStr : specificProblem);
\r
183 el.setAttribute("additionalText", additionalText == null ? nullStr : additionalText);
\r
184 el.setAttribute("raisedTime", String.valueOf(raisedTime.getTime()));
\r
185 el.setAttribute("raisedServerTime", String.valueOf(raisedServerTime.getTime()));
\r
186 if (clearedTime != null) {
\r
187 el.setAttribute("clearedTime", String.valueOf(clearedTime.getTime()));
\r
189 if (clearedServerTime != null) {
\r
190 el.setAttribute("clearedServerTime", String.valueOf(clearedServerTime.getTime()));
\r
193 return new XMLOutputter().outputString(el);
\r
196 public static Alarm valueOf(String xmlString) {
\r
197 Element element = null;
\r
199 StringReader sb = new StringReader(xmlString);
\r
200 element = new SAXBuilder().build(sb).getRootElement();
\r
202 } catch (Exception e) {
\r
203 throw new RuntimeException(e);
\r
206 Alarm alarm = new Alarm();
\r
207 @SuppressWarnings("unchecked")
\r
208 List<Attribute> list = element.getAttributes();
\r
209 for (Attribute attr : list) {
\r
210 String attrName = attr.getName();
\r
212 Field field = Alarm.class.getDeclaredField(attrName);
\r
213 if (!attrName.endsWith("Time")) {
\r
214 String type = field.getType().getSimpleName();
\r
215 if (type.equals("byte")) {
\r
216 field.set(alarm, Byte.parseByte(attr.getValue()));
\r
217 } else if (type.equals("long")) {
\r
218 field.set(alarm, Long.parseLong(attr.getValue()));
\r
219 } else if (type.equals("String")) {
\r
220 field.set(alarm, attr.getValue());
\r
222 throw new RuntimeException("unknow attr type: " + type.toString());
\r
225 Date date = new Date();
\r
226 date.setTime(Long.parseLong(attr.getValue()));
\r
227 field.set(alarm, date);
\r
229 } catch (Exception e) {
\r
230 throw new RuntimeException(e);
\r
238 public int hashCode() {
\r
239 return this.getAlarmKey().hashCode();
\r
243 public boolean equals(Object arg0) {
\r
244 if (arg0 == null) {
\r
247 return this.alarmKey.equals(((Alarm) arg0).getAlarmKey());
\r
251 public Object clone() throws CloneNotSupportedException {
\r
254 Alarm alarm = new Alarm();
\r
256 alarm.setEventType(this.getEventType());
\r
258 alarm.setAid(this.getAid());
\r
259 alarm.setId(this.getId());
\r
260 alarm.setAlarmKey(this.getAlarmKey());
\r
262 alarm.setNetwork(this.getNetwork());
\r
263 alarm.setEquipType(this.getEquipType());
\r
264 alarm.setNeType(this.getNeType());
\r
265 alarm.setPosition1(this.getPosition1());
\r
266 alarm.setSubPosition1(this.getSubPosition1());
\r
267 alarm.setPosition2(this.getPosition2());
\r
268 alarm.setSubPosition2(this.getSubPosition2());
\r
269 alarm.setRegion(this.getRegion());
\r
270 alarm.setSite(this.getSite());
\r
272 alarm.setSeverity(this.getSeverity());
\r
273 alarm.setAlarmType(this.getAlarmType());
\r
274 alarm.setSystemType(this.getSystemType());
\r
275 alarm.setSpecificProblem(this.getSpecificProblem());
\r
276 alarm.setAdditionalText(this.getAdditionalText());
\r
277 alarm.setProbableCause(this.getProbableCause());
\r
279 alarm.setRaisedTime(this.getRaisedTime());
\r
280 alarm.setRaisedServerTime(this.getRaisedServerTime());
\r
281 alarm.setClearedTime(this.getClearedTime());
\r
287 public String getObjectId() {
\r
288 return String.valueOf(id);
\r
291 public void addLinkIds(int linkId) {
\r
292 linkIds.add(linkId);
\r
296 public boolean containsPriority(String ruleId) {
\r
297 if (priorityMap.keySet().contains(ruleId)) {
\r
303 public int getPriority(String ruleId) {
\r
304 Integer priority = this.priorityMap.get(ruleId);
\r
305 if (priority == null) {
\r
311 public int getRootAlarmType(String ruleId) {
\r
312 Integer rootAlarmType = this.rootAlarmTypeMap.get(ruleId);
\r
313 if (rootAlarmType == null) {
\r
314 rootAlarmType = -1;
\r
316 return rootAlarmType;
\r