Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.juniper; |
| 18 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Iterables; |
| 22 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.Port; |
| 26 | import org.onosproject.net.behaviour.LinkDiscovery; |
| 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 29 | import org.onosproject.net.link.LinkDescription; |
| 30 | import org.onosproject.netconf.NetconfController; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfSession; |
| 32 | import org.slf4j.Logger; |
| 33 | |
| 34 | import java.io.ByteArrayInputStream; |
| 35 | import java.io.IOException; |
| 36 | import java.util.HashSet; |
| 37 | import java.util.Optional; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
| 41 | import static org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction; |
| 42 | import static org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp; |
| 43 | import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder; |
| 44 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 45 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_LLDP_NBR_INFO; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * Retrieve Links discovered by the device LLDP. |
| 51 | * Tested with MX240 junos 14.2 |
| 52 | */ |
| 53 | @Beta |
| 54 | public class LinkDiscoveryJuniperImpl extends AbstractHandlerBehaviour |
| 55 | implements LinkDiscovery { |
| 56 | |
| 57 | private final Logger log = getLogger(getClass()); |
| 58 | |
| 59 | @Override |
| 60 | public Set<LinkDescription> getLinks() { |
| 61 | DeviceId localDeviceId = this.handler().data().deviceId(); |
| 62 | NetconfController controller = |
| 63 | checkNotNull(handler().get(NetconfController.class)); |
| 64 | NetconfSession session = |
| 65 | controller.getDevicesMap().get(localDeviceId).getSession(); |
| 66 | |
| 67 | String reply; |
| 68 | try { |
| 69 | reply = session.get(requestBuilder(REQ_LLDP_NBR_INFO)); |
| 70 | } catch (IOException e) { |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 71 | log.warn("Failed to retrieve ports for device {}", localDeviceId); |
| 72 | return ImmutableSet.of(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 73 | } |
| 74 | log.debug("Reply from device {} : {}", localDeviceId, reply); |
| 75 | Set<LinkAbstraction> linkAbstractions = parseJuniperLldp( |
| 76 | XmlConfigParser.loadXml(new ByteArrayInputStream(reply.getBytes()))); |
| 77 | log.debug("Set of LinkAbstraction discovered {}", linkAbstractions); |
| 78 | |
| 79 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 80 | Set<LinkDescription> descriptions = new HashSet<>(); |
| 81 | |
| 82 | //for each lldp neighbor create two LinkDescription |
| 83 | for (LinkAbstraction linkAbs : linkAbstractions) { |
| 84 | |
| 85 | //find source port by local port name |
| 86 | Optional<Port> localPort = deviceService.getPorts(localDeviceId).stream() |
| 87 | .filter(port -> { |
| 88 | if (linkAbs.localPortName.equals( |
| 89 | port.annotations().value(PORT_NAME))) { |
| 90 | return true; |
| 91 | } |
| 92 | return false; |
| 93 | }).findAny(); |
| 94 | if (!localPort.isPresent()) { |
| 95 | log.warn("Port name {} does not exist in device {}", |
| 96 | linkAbs.localPortName, localDeviceId); |
| 97 | continue; |
| 98 | } |
| 99 | //find destination device by remote chassis id |
| 100 | com.google.common.base.Optional<Device> dev = Iterables.tryFind( |
| 101 | deviceService.getAvailableDevices(), |
| 102 | input -> input.chassisId().equals(linkAbs.remoteChassisId)); |
| 103 | |
| 104 | if (!dev.isPresent()) { |
| 105 | log.warn("Device with chassis ID {} does not exist", |
| 106 | linkAbs.remoteChassisId); |
| 107 | continue; |
| 108 | } |
| 109 | Device remoteDevice = dev.get(); |
| 110 | |
| 111 | //find destination port by interface index |
| 112 | Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id()) |
| 113 | .stream().filter(port -> { |
| 114 | if (port.annotations().value("index") != null && |
| 115 | Integer.parseInt(port.annotations().value("index")) |
| 116 | == linkAbs.remotePortIndex) { |
| 117 | return true; |
| 118 | } |
| 119 | return false; |
| 120 | }).findAny(); |
| 121 | if (!remotePort.isPresent()) { |
| 122 | log.warn("Port with index {} does not exist in device {}", |
| 123 | linkAbs.remotePortIndex, remoteDevice.id()); |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | JuniperUtils.createBiDirLinkDescription(localDeviceId, |
| 128 | localPort.get(), |
| 129 | remoteDevice.id(), |
| 130 | remotePort.get(), |
| 131 | descriptions); |
| 132 | |
| 133 | } |
| 134 | return descriptions; |
| 135 | } |
| 136 | } |