janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | |
| 36 | import java.io.IOException; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 37 | import java.util.Collection; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 38 | import java.util.List; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.drivers.huawei.DriverUtil.DEV_INFO_FAILURE; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 42 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE; |
| 43 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_FILTER; |
| 44 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_GET; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 45 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_CLOSE_IFM; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 46 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_FILTER; |
| 47 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_GET; |
| 48 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFM; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 49 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_IFS; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 50 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_MSG; |
| 51 | import static org.onosproject.drivers.huawei.DriverUtil.RPC_SYS; |
| 52 | import static org.onosproject.net.Device.Type.ROUTER; |
| 53 | import static org.slf4j.LoggerFactory.getLogger; |
| 54 | |
| 55 | /** |
| 56 | * Representation of device information and ports via NETCONF for huawei |
| 57 | * routers. |
| 58 | */ |
| 59 | public class HuaweiDeviceDescription extends AbstractHandlerBehaviour |
Andrea Campanella | 527f8d0 | 2017-06-27 15:22:23 +0200 | [diff] [blame^] | 60 | implements PortStatisticsDiscovery, DeviceDescriptionDiscovery { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 61 | |
| 62 | private final Logger log = getLogger(getClass()); |
| 63 | |
| 64 | /** |
| 65 | * Constructs huawei device description. |
| 66 | */ |
| 67 | public HuaweiDeviceDescription() { |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Discovers device details, for huawei device by getting the system |
| 72 | * information. |
| 73 | * |
| 74 | * @return device description |
| 75 | */ |
| 76 | @Override |
| 77 | public DeviceDescription discoverDeviceDetails() { |
| 78 | NetconfSession session = getNetconfSession(); |
| 79 | String sysInfo; |
| 80 | try { |
| 81 | sysInfo = session.get(getVersionReq()); |
| 82 | } catch (IOException e) { |
| 83 | throw new IllegalArgumentException( |
| 84 | new NetconfException(DEV_INFO_FAILURE)); |
| 85 | } |
| 86 | |
| 87 | String[] details = parseSysInfoXml(sysInfo); |
| 88 | DeviceService devSvc = checkNotNull(handler().get(DeviceService.class)); |
| 89 | DeviceId devId = handler().data().deviceId(); |
| 90 | Device dev = devSvc.getDevice(devId); |
| 91 | return new DefaultDeviceDescription(dev.id().uri(), ROUTER, |
| 92 | details[0], details[1], |
| 93 | details[2], details[3], |
| 94 | dev.chassisId()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Discovers interface details, for huawei device. |
| 99 | * |
| 100 | * @return port list |
| 101 | */ |
| 102 | @Override |
| 103 | public List<PortDescription> discoverPortDetails() { |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 104 | return ImmutableList.copyOf(parseInterfaceXml(getInterfaces())); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the NETCONF session of the device. |
| 109 | * |
| 110 | * @return session |
| 111 | */ |
| 112 | private NetconfSession getNetconfSession() { |
| 113 | NetconfController controller = checkNotNull( |
| 114 | handler().get(NetconfController.class)); |
| 115 | return controller.getDevicesMap().get(handler().data().deviceId()) |
| 116 | .getSession(); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Returns the rpc request message for fetching system details in huawei |
| 121 | * device. |
| 122 | * |
| 123 | * @return rpc request message |
| 124 | */ |
| 125 | private String getVersionReq() { |
| 126 | StringBuilder rpc = new StringBuilder(RPC_MSG); |
| 127 | rpc.append(RPC_GET); |
| 128 | rpc.append(RPC_FILTER); |
| 129 | rpc.append(RPC_SYS); |
| 130 | rpc.append(RPC_CLOSE_FILTER); |
| 131 | rpc.append(RPC_CLOSE_GET); |
| 132 | rpc.append(RPC_CLOSE); |
| 133 | return rpc.toString(); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Parses system info received from huawei device. |
| 138 | * |
| 139 | * @param sysInfo system info |
| 140 | * @return parsed values |
| 141 | */ |
| 142 | private String[] parseSysInfoXml(String sysInfo) { |
| 143 | HuaweiXmlParser parser = new HuaweiXmlParser(sysInfo); |
| 144 | parser.parseSysInfo(); |
| 145 | return parser.getInfo(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Returns the rpc request message for fetching interface details in |
| 150 | * huawei device. |
| 151 | * |
| 152 | * @return rpc request message |
| 153 | */ |
| 154 | private String getInterfacesReq() { |
| 155 | StringBuilder rpc = new StringBuilder(RPC_MSG); |
| 156 | rpc.append(RPC_GET); |
| 157 | rpc.append(RPC_FILTER); |
| 158 | rpc.append(RPC_IFM); |
| 159 | rpc.append(RPC_IFS); |
| 160 | rpc.append(RPC_CLOSE_IFM); |
| 161 | rpc.append(RPC_CLOSE_FILTER); |
| 162 | rpc.append(RPC_CLOSE_GET); |
| 163 | rpc.append(RPC_CLOSE); |
| 164 | return rpc.toString(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Parses interfaces received from huawei device. |
| 169 | * |
| 170 | * @param interfaces interfaces |
| 171 | * @return port list |
| 172 | */ |
| 173 | private List<PortDescription> parseInterfaceXml(String interfaces) { |
| 174 | HuaweiXmlParser parser = new HuaweiXmlParser(interfaces); |
| 175 | parser.parseInterfaces(); |
| 176 | return parser.getPorts(); |
| 177 | } |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 178 | |
| 179 | @Override |
| 180 | public Collection<PortStatistics> discoverPortStatistics() { |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 181 | String interfaces = getInterfaces(); |
| 182 | if (StringUtils.isNotBlank(interfaces)) { |
| 183 | Collection<PortStatistics> portStats = getPortStatistics(interfaces); |
| 184 | return ImmutableList.copyOf(portStats); |
| 185 | } |
| 186 | return null; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | private String getInterfaces() { |
| 190 | NetconfSession session = getNetconfSession(); |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 191 | String interfaces = null; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 192 | try { |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 193 | interfaces = session.get(getInterfacesReq()); |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 194 | } catch (IOException e) { |
Vidyashree Rama | cf117ce | 2017-04-14 08:54:39 +0530 | [diff] [blame] | 195 | log.info("Failed to retrive interface {} ", e.getMessage()); |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 196 | } |
Vidyashree Rama | d89a153 | 2017-03-30 15:13:52 +0530 | [diff] [blame] | 197 | return interfaces; |
Gaurav Agrawal | 9912f29 | 2017-03-30 01:22:40 +0530 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | private Collection<PortStatistics> getPortStatistics(String ifs) { |
| 201 | HuaweiXmlParser parser = new HuaweiXmlParser(ifs); |
| 202 | return parser.parsePortsStatistics(handler().data().deviceId()); |
| 203 | } |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 204 | } |