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; |
donghyeok.ho | 82749aa | 2018-07-16 19:57:03 +0900 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 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; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 28 | import org.onosproject.net.link.LinkDescription; |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 29 | import org.onosproject.netconf.NetconfException; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 30 | import org.onosproject.netconf.NetconfSession; |
| 31 | import org.slf4j.Logger; |
| 32 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 33 | import java.util.HashSet; |
| 34 | import java.util.Optional; |
| 35 | import java.util.Set; |
| 36 | |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 37 | import static org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction; |
| 38 | import static org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp; |
| 39 | import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder; |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 40 | import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 41 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
| 42 | import static org.onosproject.drivers.juniper.JuniperUtils.REQ_LLDP_NBR_INFO; |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 43 | import static org.slf4j.LoggerFactory.getLogger; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Retrieve Links discovered by the device LLDP. |
| 48 | * Tested with MX240 junos 14.2 |
| 49 | */ |
| 50 | @Beta |
Kieran McPeake | 9e64ab5 | 2019-06-07 10:22:29 +0100 | [diff] [blame^] | 51 | public class LinkDiscoveryJuniperImpl extends JuniperAbstractHandlerBehaviour |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 52 | implements LinkDiscovery { |
| 53 | |
| 54 | private final Logger log = getLogger(getClass()); |
| 55 | |
| 56 | @Override |
| 57 | public Set<LinkDescription> getLinks() { |
| 58 | DeviceId localDeviceId = this.handler().data().deviceId(); |
Kieran McPeake | 9e64ab5 | 2019-06-07 10:22:29 +0100 | [diff] [blame^] | 59 | NetconfSession session = lookupNetconfSession(localDeviceId); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 60 | |
| 61 | String reply; |
| 62 | try { |
| 63 | reply = session.get(requestBuilder(REQ_LLDP_NBR_INFO)); |
Yuta HIGUCHI | 234eaf3 | 2017-09-06 13:45:05 -0700 | [diff] [blame] | 64 | } catch (NetconfException e) { |
Kieran McPeake | e1b418f | 2019-05-23 13:42:13 +0100 | [diff] [blame] | 65 | log.warn("Failed to retrieve lldp-neighbors-information for device {}", localDeviceId); |
Michele Santuari | f594537 | 2017-01-19 16:39:21 +0100 | [diff] [blame] | 66 | return ImmutableSet.of(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 67 | } |
| 68 | log.debug("Reply from device {} : {}", localDeviceId, reply); |
Yuta HIGUCHI | 0184a7b | 2017-03-31 13:13:58 -0700 | [diff] [blame] | 69 | Set<LinkAbstraction> linkAbstractions = parseJuniperLldp(loadXmlString(reply)); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 70 | log.debug("Set of LinkAbstraction discovered {}", linkAbstractions); |
| 71 | |
| 72 | DeviceService deviceService = this.handler().get(DeviceService.class); |
| 73 | Set<LinkDescription> descriptions = new HashSet<>(); |
| 74 | |
| 75 | //for each lldp neighbor create two LinkDescription |
| 76 | for (LinkAbstraction linkAbs : linkAbstractions) { |
| 77 | |
| 78 | //find source port by local port name |
| 79 | Optional<Port> localPort = deviceService.getPorts(localDeviceId).stream() |
Kieran McPeake | 201fe1f | 2019-06-03 12:47:22 +0100 | [diff] [blame] | 80 | .filter(port -> linkAbs.localPortName.equals( |
| 81 | port.annotations().value(PORT_NAME))).findAny(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 82 | if (!localPort.isPresent()) { |
| 83 | log.warn("Port name {} does not exist in device {}", |
| 84 | linkAbs.localPortName, localDeviceId); |
| 85 | continue; |
| 86 | } |
| 87 | //find destination device by remote chassis id |
| 88 | com.google.common.base.Optional<Device> dev = Iterables.tryFind( |
| 89 | deviceService.getAvailableDevices(), |
| 90 | input -> input.chassisId().equals(linkAbs.remoteChassisId)); |
| 91 | |
| 92 | if (!dev.isPresent()) { |
Kieran McPeake | e1b418f | 2019-05-23 13:42:13 +0100 | [diff] [blame] | 93 | log.warn("Device with chassis ID {} does not exist. Referenced by {}/{}", |
| 94 | linkAbs.remoteChassisId, localDeviceId, linkAbs); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | Device remoteDevice = dev.get(); |
| 98 | |
| 99 | //find destination port by interface index |
| 100 | Optional<Port> remotePort = deviceService.getPorts(remoteDevice.id()) |
| 101 | .stream().filter(port -> { |
donghyeok.ho | e6d7187 | 2018-07-25 18:57:44 +0900 | [diff] [blame] | 102 | if (port.number().toLong() == linkAbs.remotePortIndex) { |
| 103 | return true; |
| 104 | } |
| 105 | if (port.annotations().value(AnnotationKeys.PORT_MAC) != null |
| 106 | && linkAbs.remotePortId != null |
| 107 | && port.annotations().value(AnnotationKeys.PORT_MAC).equals(linkAbs.remotePortId)) { |
| 108 | return true; |
| 109 | } |
| 110 | if (port.annotations().value(AnnotationKeys.PORT_NAME) != null |
| 111 | && linkAbs.remotePortId != null |
| 112 | && port.annotations().value(AnnotationKeys.PORT_NAME).equals(linkAbs.remotePortId)) { |
| 113 | return true; |
| 114 | } |
| 115 | if (port.annotations().value(AnnotationKeys.PORT_NAME) != null |
| 116 | && linkAbs.remotePortDescription != null |
| 117 | && port.annotations().value(AnnotationKeys.PORT_NAME) |
| 118 | .equals(linkAbs.remotePortDescription)) { |
| 119 | return true; |
| 120 | } |
| 121 | return false; |
| 122 | }).findAny(); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 123 | if (!remotePort.isPresent()) { |
Kieran McPeake | e1b418f | 2019-05-23 13:42:13 +0100 | [diff] [blame] | 124 | log.warn("Port does not exist in remote device {}. Referenced by {}/{}", |
| 125 | remoteDevice.id(), localDeviceId, linkAbs); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 126 | continue; |
| 127 | } |
| 128 | |
DongRyeol Cha | 81935a6 | 2018-11-30 16:51:21 +0900 | [diff] [blame] | 129 | if (!localPort.get().isEnabled() || !remotePort.get().isEnabled()) { |
| 130 | log.debug("Ports are disabled. Cannot create a link between {}/{} and {}/{}", |
| 131 | localDeviceId, localPort.get(), remoteDevice.id(), remotePort.get()); |
| 132 | continue; |
| 133 | } |
| 134 | |
DongRyeol Cha | ce65cc0 | 2018-07-23 15:02:28 +0900 | [diff] [blame] | 135 | JuniperUtils.createOneWayLinkDescription(localDeviceId, |
| 136 | localPort.get(), |
| 137 | remoteDevice.id(), |
| 138 | remotePort.get(), |
| 139 | descriptions); |
Michele Santuari | 21c1401 | 2016-11-14 13:31:33 +0100 | [diff] [blame] | 140 | } |
| 141 | return descriptions; |
| 142 | } |
| 143 | } |