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.StringReader;
\r
20 import java.lang.reflect.Field;
\r
21 import java.util.Date;
\r
22 import java.util.HashMap;
\r
23 import java.util.HashSet;
\r
24 import java.util.List;
\r
25 import java.util.Map;
\r
26 import java.util.Set;
\r
27 import lombok.Getter;
\r
28 import lombok.Setter;
\r
29 import org.jdom.Attribute;
\r
30 import org.jdom.Element;
\r
31 import org.jdom.input.SAXBuilder;
\r
32 import org.jdom.output.XMLOutputter;
\r
37 public class Alarm implements AplusData, Cloneable {
\r
39 public static final byte EVENT_CLEARED_ALARM = 3;
\r
41 public static final byte EVENT_CHANGED = 2;
\r
43 public static final byte EVENT_CLEARED = 1;
\r
45 public static final byte EVENT_RAISED = 0;
\r
47 private static final long serialVersionUID = 4520003737132012000L;
\r
49 private byte eventType = EVENT_RAISED;
\r
51 private long id = 0L;
\r
53 private String alarmKey = "";
\r
55 private String network = "";
\r
57 private String neType = "";
\r
59 private String equipType = "";
\r
61 private String position1 = "";
\r
63 private String subPosition1 = null;
\r
65 private String position2 = null;
\r
67 private String subPosition2 = null;
\r
69 private byte severity = -1;
\r
71 private byte alarmType = -1;
\r
73 private long probableCause = -1;
\r
75 private String specificProblem = null;
\r
77 private String additionalText = null;
\r
79 private Date raisedTime = new Date();
\r
81 private Date raisedServerTime = new Date();
\r
83 private Date clearedTime = null;
\r
85 private final Date clearedServerTime = null;
\r
87 private String region = null;
\r
89 private String site = null;
\r
91 private String aid = null;
\r
93 private short systemType = -1;
\r
95 private boolean rootAlarmFlag = false;
\r
97 private int linkId = -1;
\r
99 private int nodeIdx = -1;
\r
101 private Set<Integer> linkIds = new HashSet<Integer>();
\r
103 private HashMap<String, Integer> priorityMap = new HashMap<String, Integer>();
\r
105 private HashMap<String, Integer> rootAlarmTypeMap = new HashMap<String, Integer>();
\r
107 private int rootAlarmType = -1;
\r
109 private boolean keyAlarmFlag = false;
\r
111 private int keyAlarmType = -1;
\r
113 private int networkLevel = -1;
\r
115 private int linkType = -1;
\r
117 private int centerType;
\r
119 private final Map<Integer, Integer> linkIdNodeIdxMap = new HashMap<Integer, Integer>();
\r
121 public void addLinkIdNodeIdx(int linkId, int index) {
\r
122 linkIdNodeIdxMap.put(linkId, index);
\r
126 * Comparison of the location of different links: 999 the same link: the location of the node
\r
127 * number less than otherIdIdx in the location of the number (in the otherIdIdx upstream):-1,
\r
128 * the same link: the node position number is equal to the number of position in the otherIdIdx
\r
129 * (in the same site otherIdIdx):0, the same link: the node location number is greater than the
\r
130 * number of locations in otherIdIdx (otherIdIdx) with the site):1,
\r
132 public int CompareLinkPosition(Map<Integer, Integer> otherIdIdx) {
\r
133 Set<Integer> myIdSet = new HashSet<Integer>();
\r
134 myIdSet.addAll(this.linkIdNodeIdxMap.keySet());
\r
135 myIdSet.retainAll(otherIdIdx.keySet());
\r
137 if (myIdSet.isEmpty()) {
\r
141 for (int tempLinkId : myIdSet) {
\r
142 return this.linkIdNodeIdxMap.get(tempLinkId) - otherIdIdx.get(tempLinkId);
\r
147 public Map<Integer, Integer> getLinkIdNodeIdx() {
\r
148 return this.linkIdNodeIdxMap;
\r
151 public boolean containNode(int linkId, int index) {
\r
152 if (linkIdNodeIdxMap.containsKey(linkId) && linkIdNodeIdxMap.get(linkId) == index) {
\r
160 public byte getDataType() {
\r
161 return APLUS_EVENT;
\r
165 public String toString() {
\r
166 final String nullStr = "<null>";
\r
168 Element el = new Element("Alarm");
\r
169 el.setAttribute("id", String.valueOf(id));
\r
170 el.setAttribute("aid", String.valueOf(aid));
\r
171 el.setAttribute("alarmKey", String.valueOf(alarmKey));
\r
172 el.setAttribute("eventType", String.valueOf(eventType));
\r
173 el.setAttribute("region", region == null ? nullStr : region);
\r
174 el.setAttribute("site", site == null ? nullStr : site);
\r
175 el.setAttribute("network", network);
\r
176 el.setAttribute("neType", neType);
\r
177 el.setAttribute("equipType", equipType);
\r
178 el.setAttribute("position1", position1);
\r
179 el.setAttribute("subPosition1", subPosition1 == null ? nullStr : subPosition1);
\r
180 el.setAttribute("position2", position2 == null ? nullStr : position2);
\r
181 el.setAttribute("subPosition2", subPosition2 == null ? nullStr : subPosition2);
\r
182 el.setAttribute("severity", String.valueOf(severity));
\r
183 el.setAttribute("alarmType", String.valueOf(alarmType));
\r
184 el.setAttribute("probableCause", String.valueOf(probableCause));
\r
185 el.setAttribute("specificProblem", specificProblem == null ? nullStr : specificProblem);
\r
186 el.setAttribute("additionalText", additionalText == null ? nullStr : additionalText);
\r
187 el.setAttribute("raisedTime", String.valueOf(raisedTime.getTime()));
\r
188 el.setAttribute("raisedServerTime", String.valueOf(raisedServerTime.getTime()));
\r
189 if (clearedTime != null) {
\r
190 el.setAttribute("clearedTime", String.valueOf(clearedTime.getTime()));
\r
192 if (clearedServerTime != null) {
\r
193 el.setAttribute("clearedServerTime", String.valueOf(clearedServerTime.getTime()));
\r
196 return new XMLOutputter().outputString(el);
\r
199 public static Alarm valueOf(String xmlString) {
\r
200 Element element = null;
\r
202 StringReader sb = new StringReader(xmlString);
\r
203 element = new SAXBuilder().build(sb).getRootElement();
\r
205 } catch (Exception e) {
\r
206 throw new RuntimeException(e);
\r
209 Alarm alarm = new Alarm();
\r
210 @SuppressWarnings("unchecked")
\r
211 List<Attribute> list = element.getAttributes();
\r
212 for (Attribute attr : list) {
\r
213 String attrName = attr.getName();
\r
215 Field field = Alarm.class.getDeclaredField(attrName);
\r
216 if (!attrName.endsWith("Time")) {
\r
217 String type = field.getType().getSimpleName();
\r
218 if (type.equals("byte")) {
\r
219 field.set(alarm, Byte.parseByte(attr.getValue()));
\r
220 } else if (type.equals("long")) {
\r
221 field.set(alarm, Long.parseLong(attr.getValue()));
\r
222 } else if (type.equals("String")) {
\r
223 field.set(alarm, attr.getValue());
\r
225 throw new RuntimeException("unknow attr type: " + type.toString());
\r
228 Date date = new Date();
\r
229 date.setTime(Long.parseLong(attr.getValue()));
\r
230 field.set(alarm, date);
\r
232 } catch (Exception e) {
\r
233 throw new RuntimeException(e);
\r
241 public int hashCode() {
\r
242 return this.getAlarmKey().hashCode();
\r
246 public boolean equals(Object arg0) {
\r
247 if (arg0 == null) {
\r
250 return this.alarmKey.equals(((Alarm) arg0).getAlarmKey());
\r
254 public Object clone() throws CloneNotSupportedException {
\r
257 Alarm alarm = new Alarm();
\r
259 alarm.setEventType(this.getEventType());
\r
261 alarm.setAid(this.getAid());
\r
262 alarm.setId(this.getId());
\r
263 alarm.setAlarmKey(this.getAlarmKey());
\r
265 alarm.setNetwork(this.getNetwork());
\r
266 alarm.setEquipType(this.getEquipType());
\r
267 alarm.setNeType(this.getNeType());
\r
268 alarm.setPosition1(this.getPosition1());
\r
269 alarm.setSubPosition1(this.getSubPosition1());
\r
270 alarm.setPosition2(this.getPosition2());
\r
271 alarm.setSubPosition2(this.getSubPosition2());
\r
272 alarm.setRegion(this.getRegion());
\r
273 alarm.setSite(this.getSite());
\r
275 alarm.setSeverity(this.getSeverity());
\r
276 alarm.setAlarmType(this.getAlarmType());
\r
277 alarm.setSystemType(this.getSystemType());
\r
278 alarm.setSpecificProblem(this.getSpecificProblem());
\r
279 alarm.setAdditionalText(this.getAdditionalText());
\r
280 alarm.setProbableCause(this.getProbableCause());
\r
282 alarm.setRaisedTime(this.getRaisedTime());
\r
283 alarm.setRaisedServerTime(this.getRaisedServerTime());
\r
284 alarm.setClearedTime(this.getClearedTime());
\r
290 public String getObjectId() {
\r
291 return String.valueOf(id);
\r
294 public void addLinkIds(int linkId) {
\r
295 linkIds.add(linkId);
\r
299 public boolean containsPriority(String ruleId) {
\r
300 if (priorityMap.keySet().contains(ruleId)) {
\r
306 public int getPriority(String ruleId) {
\r
307 Integer priority = this.priorityMap.get(ruleId);
\r
308 if (priority == null) {
\r
314 public int getRootAlarmType(String ruleId) {
\r
315 Integer rootAlarmType = this.rootAlarmTypeMap.get(ruleId);
\r
316 if (rootAlarmType == null) {
\r
317 rootAlarmType = -1;
\r
319 return rootAlarmType;
\r