blob: 613f6d4b124ba3b19f41e243b7cd81bafb05b6c8 [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Michele Santuari21c14012016-11-14 13:31:33 +01003 *
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
17package org.onosproject.drivers.juniper;
18
Michele Santuari21c14012016-11-14 13:31:33 +010019import com.google.common.annotations.Beta;
Michele Santuarif5945372017-01-19 16:39:21 +010020import com.google.common.collect.ImmutableSet;
Michele Santuari21c14012016-11-14 13:31:33 +010021import com.google.common.collect.Iterables;
Michele Santuari21c14012016-11-14 13:31:33 +010022import org.onosproject.net.Device;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Port;
25import org.onosproject.net.behaviour.LinkDiscovery;
26import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.driver.AbstractHandlerBehaviour;
28import org.onosproject.net.link.LinkDescription;
29import org.onosproject.netconf.NetconfController;
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070030import org.onosproject.netconf.NetconfException;
Michele Santuari21c14012016-11-14 13:31:33 +010031import org.onosproject.netconf.NetconfSession;
32import org.slf4j.Logger;
33
Michele Santuari21c14012016-11-14 13:31:33 +010034import java.util.HashSet;
35import java.util.Optional;
36import java.util.Set;
37
38import static com.google.common.base.Preconditions.checkNotNull;
39import static org.onosproject.drivers.juniper.JuniperUtils.LinkAbstraction;
40import static org.onosproject.drivers.juniper.JuniperUtils.parseJuniperLldp;
41import static org.onosproject.drivers.juniper.JuniperUtils.requestBuilder;
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070042import static org.onosproject.drivers.utilities.XmlConfigParser.loadXmlString;
Michele Santuari21c14012016-11-14 13:31:33 +010043import static org.onosproject.net.AnnotationKeys.PORT_NAME;
44import static org.onosproject.drivers.juniper.JuniperUtils.REQ_LLDP_NBR_INFO;
Michele Santuari21c14012016-11-14 13:31:33 +010045import 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
53public 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 HIGUCHI234eaf32017-09-06 13:45:05 -070069 } catch (NetconfException e) {
Michele Santuarif5945372017-01-19 16:39:21 +010070 log.warn("Failed to retrieve ports for device {}", localDeviceId);
71 return ImmutableSet.of();
Michele Santuari21c14012016-11-14 13:31:33 +010072 }
73 log.debug("Reply from device {} : {}", localDeviceId, reply);
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -070074 Set<LinkAbstraction> linkAbstractions = parseJuniperLldp(loadXmlString(reply));
Michele Santuari21c14012016-11-14 13:31:33 +010075 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 Santuaria85bd3b2017-05-05 12:57:40 +0200112 if (port.number().toLong()
Michele Santuari21c14012016-11-14 13:31:33 +0100113 == linkAbs.remotePortIndex) {
114 return true;
115 }
116 return false;
117 }).findAny();
118 if (!remotePort.isPresent()) {
Michele Santuaria85bd3b2017-05-05 12:57:40 +0200119 log.warn("Port number {} does not exist in device {}",
Michele Santuari21c14012016-11-14 13:31:33 +0100120 linkAbs.remotePortIndex, remoteDevice.id());
121 continue;
122 }
123
124 JuniperUtils.createBiDirLinkDescription(localDeviceId,
125 localPort.get(),
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -0700126 remoteDevice.id(),
127 remotePort.get(),
128 descriptions);
Michele Santuari21c14012016-11-14 13:31:33 +0100129
130 }
131 return descriptions;
132 }
133}