blob: 1c9f208c76995485c8a4d2794010e4a1ac921ecb [file] [log] [blame]
Michele Santuari21c14012016-11-14 13:31:33 +01001/*
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
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;
Michele Santuari21c14012016-11-14 13:31:33 +010030import org.onosproject.netconf.NetconfSession;
31import org.slf4j.Logger;
32
Michele Santuari21c14012016-11-14 13:31:33 +010033import java.io.IOException;
34import 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));
69 } catch (IOException 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 -> {
112 if (port.annotations().value("index") != null &&
113 Integer.parseInt(port.annotations().value("index"))
114 == linkAbs.remotePortIndex) {
115 return true;
116 }
117 return false;
118 }).findAny();
119 if (!remotePort.isPresent()) {
120 log.warn("Port with index {} does not exist in device {}",
121 linkAbs.remotePortIndex, remoteDevice.id());
122 continue;
123 }
124
125 JuniperUtils.createBiDirLinkDescription(localDeviceId,
126 localPort.get(),
Yuta HIGUCHI0184a7b2017-03-31 13:13:58 -0700127 remoteDevice.id(),
128 remotePort.get(),
129 descriptions);
Michele Santuari21c14012016-11-14 13:31:33 +0100130
131 }
132 return descriptions;
133 }
134}