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