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