blob: c6909e4174e751b99da8d3f19075636196924a62 [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 com.google.common.collect.ImmutableList;
20import org.onosproject.net.Device;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.device.DefaultDeviceDescription;
23import org.onosproject.net.device.DeviceDescription;
janani bf7060cd2017-03-28 19:06:30 +053024import org.onosproject.net.device.DeviceService;
25import org.onosproject.net.device.PortDescription;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053026import org.onosproject.net.device.PortStatistics;
27import org.onosproject.net.device.PortStatisticsDiscovery;
janani bf7060cd2017-03-28 19:06:30 +053028import org.onosproject.net.driver.AbstractHandlerBehaviour;
29import org.onosproject.netconf.NetconfController;
30import org.onosproject.netconf.NetconfException;
31import org.onosproject.netconf.NetconfSession;
32import org.slf4j.Logger;
33
34import java.io.IOException;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053035import java.util.Collection;
janani bf7060cd2017-03-28 19:06:30 +053036import java.util.List;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.onosproject.drivers.huawei.DriverUtil.DEV_INFO_FAILURE;
40import static org.onosproject.drivers.huawei.DriverUtil.INT_INFO_FAILURE;
janani bf7060cd2017-03-28 19:06:30 +053041import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE;
42import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_FILTER;
43import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_GET;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053044import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_IFM;
janani bf7060cd2017-03-28 19:06:30 +053045import static org.onosproject.drivers.huawei.DriverUtil.RPC_FILTER;
46import static org.onosproject.drivers.huawei.DriverUtil.RPC_GET;
47import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFM;
Gaurav Agrawal9912f292017-03-30 01:22:40 +053048import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFS;
janani bf7060cd2017-03-28 19:06:30 +053049import static org.onosproject.drivers.huawei.DriverUtil.RPC_MSG;
50import static org.onosproject.drivers.huawei.DriverUtil.RPC_SYS;
51import static org.onosproject.net.Device.Type.ROUTER;
52import static org.slf4j.LoggerFactory.getLogger;
53
54/**
55 * Representation of device information and ports via NETCONF for huawei
56 * routers.
57 */
58public class HuaweiDeviceDescription extends AbstractHandlerBehaviour
Gaurav Agrawal9912f292017-03-30 01:22:40 +053059 implements PortStatisticsDiscovery {
janani bf7060cd2017-03-28 19:06:30 +053060
61 private final Logger log = getLogger(getClass());
62
63 /**
64 * Constructs huawei device description.
65 */
66 public HuaweiDeviceDescription() {
67 }
68
69 /**
70 * Discovers device details, for huawei device by getting the system
71 * information.
72 *
73 * @return device description
74 */
75 @Override
76 public DeviceDescription discoverDeviceDetails() {
77 NetconfSession session = getNetconfSession();
78 String sysInfo;
79 try {
80 sysInfo = session.get(getVersionReq());
81 } catch (IOException e) {
82 throw new IllegalArgumentException(
83 new NetconfException(DEV_INFO_FAILURE));
84 }
85
86 String[] details = parseSysInfoXml(sysInfo);
87 DeviceService devSvc = checkNotNull(handler().get(DeviceService.class));
88 DeviceId devId = handler().data().deviceId();
89 Device dev = devSvc.getDevice(devId);
90 return new DefaultDeviceDescription(dev.id().uri(), ROUTER,
91 details[0], details[1],
92 details[2], details[3],
93 dev.chassisId());
94 }
95
96 /**
97 * Discovers interface details, for huawei device.
98 *
99 * @return port list
100 */
101 @Override
102 public List<PortDescription> discoverPortDetails() {
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530103 return ImmutableList.copyOf(parseInterfaceXml(getInterfaces()));
janani bf7060cd2017-03-28 19:06:30 +0530104 }
105
106 /**
107 * Returns the NETCONF session of the device.
108 *
109 * @return session
110 */
111 private NetconfSession getNetconfSession() {
112 NetconfController controller = checkNotNull(
113 handler().get(NetconfController.class));
114 return controller.getDevicesMap().get(handler().data().deviceId())
115 .getSession();
116 }
117
118 /**
119 * Returns the rpc request message for fetching system details in huawei
120 * device.
121 *
122 * @return rpc request message
123 */
124 private String getVersionReq() {
125 StringBuilder rpc = new StringBuilder(RPC_MSG);
126 rpc.append(RPC_GET);
127 rpc.append(RPC_FILTER);
128 rpc.append(RPC_SYS);
129 rpc.append(RPC_CLOSE_FILTER);
130 rpc.append(RPC_CLOSE_GET);
131 rpc.append(RPC_CLOSE);
132 return rpc.toString();
133 }
134
135 /**
136 * Parses system info received from huawei device.
137 *
138 * @param sysInfo system info
139 * @return parsed values
140 */
141 private String[] parseSysInfoXml(String sysInfo) {
142 HuaweiXmlParser parser = new HuaweiXmlParser(sysInfo);
143 parser.parseSysInfo();
144 return parser.getInfo();
145 }
146
147 /**
148 * Returns the rpc request message for fetching interface details in
149 * huawei device.
150 *
151 * @return rpc request message
152 */
153 private String getInterfacesReq() {
154 StringBuilder rpc = new StringBuilder(RPC_MSG);
155 rpc.append(RPC_GET);
156 rpc.append(RPC_FILTER);
157 rpc.append(RPC_IFM);
158 rpc.append(RPC_IFS);
159 rpc.append(RPC_CLOSE_IFM);
160 rpc.append(RPC_CLOSE_FILTER);
161 rpc.append(RPC_CLOSE_GET);
162 rpc.append(RPC_CLOSE);
163 return rpc.toString();
164 }
165
166 /**
167 * Parses interfaces received from huawei device.
168 *
169 * @param interfaces interfaces
170 * @return port list
171 */
172 private List<PortDescription> parseInterfaceXml(String interfaces) {
173 HuaweiXmlParser parser = new HuaweiXmlParser(interfaces);
174 parser.parseInterfaces();
175 return parser.getPorts();
176 }
Gaurav Agrawal9912f292017-03-30 01:22:40 +0530177
178 @Override
179 public Collection<PortStatistics> discoverPortStatistics() {
180 return ImmutableList.copyOf(getPortStatistics(getInterfaces()));
181 }
182
183 private String getInterfaces() {
184 NetconfSession session = getNetconfSession();
185 String interfaces;
186 try {
187 return session.get(getInterfacesReq());
188 } catch (IOException e) {
189 throw new IllegalArgumentException(
190 new NetconfException(INT_INFO_FAILURE));
191 }
192 }
193
194 private Collection<PortStatistics> getPortStatistics(String ifs) {
195 HuaweiXmlParser parser = new HuaweiXmlParser(ifs);
196 return parser.parsePortsStatistics(handler().data().deviceId());
197 }
janani bf7060cd2017-03-28 19:06:30 +0530198}