janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.drivers.huawei; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 20 | import org.apache.commons.lang.StringUtils; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.device.DefaultDeviceDescription; |
| 24 | import org.onosproject.net.device.DeviceDescription; |
Andrea Campanella | 527f8d0 | 2017-06-27 15:22:23 +0200 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceService; |
| 27 | import org.onosproject.net.device.PortDescription; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 28 | import org.onosproject.net.device.PortStatistics; |
| 29 | import org.onosproject.net.device.PortStatisticsDiscovery; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 30 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 31 | import org.onosproject.netconf.NetconfController; |
| 32 | import org.onosproject.netconf.NetconfException; |
| 33 | import org.onosproject.netconf.NetconfSession; |
| 34 | import org.slf4j.Logger; |
| 35 | |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 36 | import java.util.Collection; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 37 | import java.util.List; |
| 38 | |
| 39 | import static com.google.common.base.Preconditions.checkNotNull; |
| 40 | import static org.onosproject.drivers.huawei.DriverUtil.DEV_INFO_FAILURE; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 41 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE; |
| 42 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_FILTER; |
| 43 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_GET; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 44 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_IFM; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 45 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_FILTER; |
| 46 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_GET; |
| 47 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFM; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 48 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFS; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 49 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_MSG; |
| 50 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_SYS; |
| 51 | import static org.onosproject.net.Device.Type.ROUTER; |
| 52 | import static org.slf4j.LoggerFactory.getLogger; |
| 53 | |
| 54 | /** |
| 55 | * Representation of device information and ports via NETCONF for huawei |
| 56 | * routers. |
| 57 | */ |
| 58 | public class HuaweiDeviceDescription extends AbstractHandlerBehaviour |
Andrea Campanella | 527f8d0 | 2017-06-27 15:22:23 +0200 | [diff] [blame] | 59 | implements PortStatisticsDiscovery, DeviceDescriptionDiscovery { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 60 | |
| 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()); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 81 | } catch (NetconfException e) { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 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 Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 103 | return ImmutableList.copyOf(parseInterfaceXml(getInterfaces())); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 104 | } |
| 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 Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 177 | |
| 178 | @Override |
| 179 | public Collection<PortStatistics> discoverPortStatistics() { |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 180 | String interfaces = getInterfaces(); |
| 181 | if (StringUtils.isNotBlank(interfaces)) { |
| 182 | Collection<PortStatistics> portStats = getPortStatistics(interfaces); |
| 183 | return ImmutableList.copyOf(portStats); |
| 184 | } |
| 185 | return null; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | private String getInterfaces() { |
| 189 | NetconfSession session = getNetconfSession(); |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 190 | String interfaces = null; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 191 | try { |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 192 | interfaces = session.get(getInterfacesReq()); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 193 | } catch (NetconfException e) { |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 194 | log.info("Failed to retrive interface {} ", e.getMessage()); |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 195 | } |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 196 | return interfaces; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private Collection<PortStatistics> getPortStatistics(String ifs) { |
| 200 | HuaweiXmlParser parser = new HuaweiXmlParser(ifs); |
| 201 | return parser.parsePortsStatistics(handler().data().deviceId()); |
| 202 | } |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 203 | } |