blob: eb6299211b54e0783eb92d43e7e284cb74325d35 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.drivers.huawei;
18
19import org.dom4j.Document;
20import org.dom4j.DocumentException;
21import org.dom4j.DocumentHelper;
22import org.dom4j.Element;
23import org.onosproject.net.DefaultAnnotations;
24import org.onosproject.net.device.DefaultPortDescription;
25import org.onosproject.net.device.PortDescription;
26
27import java.util.ArrayList;
28import java.util.Arrays;
29import java.util.Iterator;
30import java.util.List;
31
32import static org.onosproject.net.AnnotationKeys.PORT_NAME;
33import static org.onosproject.net.Port.Type.COPPER;
34import static org.onosproject.net.PortNumber.portNumber;
35
36/**
37 * Created by root1 on 27/3/17.
38 */
39public class HuaweiXmlParser {
40
41 private static final String DATA = "data";
42 private static final String IFM = "ifm";
43 private static final String IFS = "interfaces";
44 private static final String IF = "interface";
45 private static final String IF_TYPE = "ifPhyType";
46 private static final String IF_STAT = "ifPhyStatus";
47 private static final String IF_NUM = "ifNumber";
48 private static final String IF_SPEED = "ifOperSpeed";
49 private static final String IF_NAME = "ifName";
50 private static final String UP = "up";
51 private static final String DYN_INFO = "ifDynamicInfo";
52 private static final String DELIMITER = "/";
53 private static final String SYS = "system";
54 private static final String SYS_INFO = "systemInfo";
55 private static final String SYS_NAME = "sysName";
56 private static final String PDT_VER = "productVer";
57 private static final String PLATFORM_VER = "platformVer";
58 private static final String SYS_ID = "sysObjectId";
59
60 private static final String DEV_PARSE_ERR = "Unable to parse the received" +
61 " xml reply for system details from the huawei device";
62 private static final String INT_PARSE_ERR = "Unable to parse the received" +
63 " xml reply for interface details from the huawei device";
64 private static final String P_NAME_INVALID = "Invalid port name.";
65
66 //TODO: All type of interfaces has to be added.
67 private static final List INTERFACES = Arrays.asList(
68 "MEth", "LoopBack", "Ethernet", "POS", "GigabitEthernet");
69
70 private List<PortDescription> ports = new ArrayList<>();
71 private String xml;
72 private int portInc;
73 private String[] info = new String[4];
74
75 /**
76 * Constructs huawei XML parser with xml reply.
77 *
78 * @param xml xml reply
79 */
80 public HuaweiXmlParser(String xml) {
81 this.xml = xml;
82 }
83
84 /**
85 * Returns the system info.
86 *
87 * @return system info
88 */
89 String[] getInfo() {
90 return info;
91 }
92
93 /**
94 * Returns the port list.
95 *
96 * @return port list
97 */
98 List<PortDescription> getPorts() {
99 return ports;
100 }
101
102 /**
103 * Parses system info xml reply.
104 */
105 void parseSysInfo() {
106 Document doc;
107 try {
108 doc = DocumentHelper.parseText(xml);
109 } catch (DocumentException e) {
110 throw new IllegalArgumentException(DEV_PARSE_ERR);
111 }
112
113 Element root = doc.getRootElement();
114 Element parent = root.element(DATA).element(SYS).element(SYS_INFO);
115 info[0] = parent.element(SYS_NAME).getText();
116 info[1] = parent.element(PDT_VER).getText();
117 info[2] = parent.element(PLATFORM_VER).getText();
118 info[3] = parent.element(SYS_ID).getText();
119 }
120
121 /**
122 * Parses interface xml reply and creates ports to be updated.
123 */
124 void parseInterfaces() {
125 Document doc;
126 try {
127 doc = DocumentHelper.parseText(xml);
128 } catch (DocumentException e) {
129 throw new IllegalArgumentException(INT_PARSE_ERR);
130 }
131 Element root = doc.getRootElement();
132 Element parent = root.element(DATA).element(IFM).element(IFS);
133 Iterator itr = parent.elementIterator(IF);
134
135 while (itr.hasNext()) {
136 Element ifElement = (Element) itr.next();
137 addPorts(ifElement);
138 }
139 }
140
141 /**
142 * Adds port information to the port list from the xml reply.
143 *
144 * @param ifElement interface element
145 */
146 private void addPorts(Element ifElement) {
147 String ifType = ifElement.element(IF_TYPE).getText();
148
149 if (INTERFACES.contains(ifType)) {
150 Element info = ifElement.element(DYN_INFO);
151 String status = info.element(IF_STAT).getText();
152 String port = getPortNum(ifElement.element(IF_NUM).getText());
153 String speed = info.element(IF_SPEED).getText();
154 String ifName = ifElement.element(IF_NAME).getText();
155
156 boolean isEnabled = false;
157 if (status.equals(UP)) {
158 isEnabled = true;
159 }
160
161 Long portSpeed = 0L;
162 if (!speed.isEmpty()) {
163 portSpeed = Long.valueOf(speed);
164 }
165
166 DefaultAnnotations annotations = DefaultAnnotations.builder()
167 .set(PORT_NAME, ifName).build();
168 ports.add(new DefaultPortDescription(portNumber(port), isEnabled,
169 COPPER, portSpeed,
170 annotations));
171 }
172 }
173
174 /**
175 * Returns port number from the port name. As many type of port can have
176 * same port number, each port number will be prepended with a incrementing
177 * number to make it unique in the list.
178 *
179 * @param portName port name
180 * @return port number
181 */
182 private String getPortNum(String portName) {
183 String port;
184 if (!portName.contains(DELIMITER)) {
185 portInc++;
186 port = String.valueOf(portInc) + portName;
187 } else if (portName.indexOf(DELIMITER) > 0) {
188 try {
189 port = portName.substring(
190 portName.lastIndexOf(DELIMITER) + 1);
191 portInc++;
192 port = String.valueOf(portInc) + port;
193 } catch (StringIndexOutOfBoundsException e) {
194 throw new IllegalArgumentException(P_NAME_INVALID);
195 }
196 } else {
197 throw new IllegalArgumentException(P_NAME_INVALID);
198 }
199 return port;
200 }
201}