Ray Milkey
Committed by Gerrit Code Review

ONOS-3460 - Link provider that enforces strict configuration

This provider uses the "links" configuration of the network
config and only allows discovery of links that are in
the config.

Refactoring will be done in a subsequent patch set - the DiscoveryContext and
LinkDiscovery classes are duplicates, they need to be refactored so the
LLDP provider and the Network Config provider can share them.

Change-Id: I4de12fc1c4ffa05e3cac7767b8a31f48ba379f6c
......@@ -35,7 +35,7 @@ import static org.onosproject.net.DeviceId.deviceId;
description = "Lists all infrastructure links")
public class LinksListCommand extends AbstractShellCommand {
private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s";
private static final String FMT = "src=%s/%s, dst=%s/%s, type=%s, state=%s%s, expected=%s";
private static final String COMPACT = "%s/%s-%s/%s";
@Argument(index = 0, name = "uri", description = "Device ID",
......@@ -93,7 +93,8 @@ public class LinksListCommand extends AbstractShellCommand {
return String.format(FMT, link.src().deviceId(), link.src().port(),
link.dst().deviceId(), link.dst().port(),
link.type(), link.state(),
annotations(link.annotations()));
annotations(link.annotations()),
link.isExpected());
}
/**
......
......@@ -119,7 +119,8 @@ public class DefaultLink extends AbstractProjectableModel implements Link {
final DefaultLink other = (DefaultLink) obj;
return Objects.equals(this.src, other.src) &&
Objects.equals(this.dst, other.dst) &&
Objects.equals(this.type, other.type);
Objects.equals(this.type, other.type) &&
Objects.equals(this.isExpected, other.isExpected);
}
return false;
}
......
......@@ -31,6 +31,10 @@ public class DefaultLinkDescription extends AbstractDescription
private final ConnectPoint src;
private final ConnectPoint dst;
private final Link.Type type;
private final boolean isExpected;
public static final boolean EXPECTED = true;
public static final boolean NOT_EXPECTED = false;
/**
* Creates a link description using the supplied information.
......@@ -38,14 +42,32 @@ public class DefaultLinkDescription extends AbstractDescription
* @param src link source
* @param dst link destination
* @param type link type
* @param isExpected is the link expected to be part of this configuration
* @param annotations optional key/value annotations
*/
public DefaultLinkDescription(ConnectPoint src, ConnectPoint dst,
Link.Type type, SparseAnnotations... annotations) {
Link.Type type,
boolean isExpected,
SparseAnnotations... annotations) {
super(annotations);
this.src = src;
this.dst = dst;
this.type = type;
this.isExpected = isExpected;
}
/**
* Creates a link description using the supplied information.
*
* @param src link source
* @param dst link destination
* @param type link type
* @param annotations optional key/value annotations
*/
public DefaultLinkDescription(ConnectPoint src, ConnectPoint dst,
Link.Type type,
SparseAnnotations... annotations) {
this(src, dst, type, EXPECTED, annotations);
}
@Override
......@@ -64,18 +86,24 @@ public class DefaultLinkDescription extends AbstractDescription
}
@Override
public boolean isExpected() {
return isExpected;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("src", src())
.add("dst", dst())
.add("type", type())
.add("isExpected", isExpected())
.add("annotations", annotations())
.toString();
}
@Override
public int hashCode() {
return Objects.hashCode(super.hashCode(), src, dst, type);
return Objects.hashCode(super.hashCode(), src, dst, type, isExpected);
}
@Override
......@@ -87,7 +115,8 @@ public class DefaultLinkDescription extends AbstractDescription
DefaultLinkDescription that = (DefaultLinkDescription) object;
return Objects.equal(this.src, that.src)
&& Objects.equal(this.dst, that.dst)
&& Objects.equal(this.type, that.type);
&& Objects.equal(this.type, that.type)
&& Objects.equal(this.isExpected, that.isExpected);
}
return false;
}
......
......@@ -45,5 +45,12 @@ public interface LinkDescription extends Description {
*/
Link.Type type();
/**
* Returns true if the link is expected, false otherwise.
*
* @return expected flag
*/
boolean isExpected();
// Add further link attributes
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.link.impl;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.config.Config;
public class CoreConfig extends Config<ApplicationId> {
private static final String LINK_DISCOVERY_MODE = "linkDiscoveryMode";
protected static final String DEFAULT_LINK_DISCOVERY_MODE = "PERMISSIVE";
/**
* Returns the link discovery mode.
*
* @return link discovery mode
*/
public ECLinkStore.LinkDiscoveryMode linkDiscoveryMode() {
return ECLinkStore.LinkDiscoveryMode
.valueOf(get(LINK_DISCOVERY_MODE, DEFAULT_LINK_DISCOVERY_MODE));
}
}
......@@ -15,21 +15,6 @@
*/
package org.onosproject.store.link.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.DefaultAnnotations.merge;
import static org.onosproject.net.DefaultAnnotations.union;
import static org.onosproject.net.Link.State.ACTIVE;
import static org.onosproject.net.Link.State.INACTIVE;
import static org.onosproject.net.Link.Type.DIRECT;
import static org.onosproject.net.Link.Type.INDIRECT;
import static org.onosproject.net.LinkKey.linkKey;
import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
......@@ -48,6 +33,8 @@ import org.onlab.util.KryoNamespace;
import org.onlab.util.SharedExecutors;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.NodeId;
import org.onosproject.core.ApplicationId;
import org.onosproject.core.CoreService;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.AnnotationsUtil;
......@@ -56,8 +43,12 @@ import org.onosproject.net.DefaultAnnotations;
import org.onosproject.net.DefaultLink;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.LinkKey;
import org.onosproject.net.Link.Type;
import org.onosproject.net.LinkKey;
import org.onosproject.net.config.ConfigFactory;
import org.onosproject.net.config.NetworkConfigEvent;
import org.onosproject.net.config.NetworkConfigListener;
import org.onosproject.net.config.NetworkConfigRegistry;
import org.onosproject.net.device.DeviceClockService;
import org.onosproject.net.link.DefaultLinkDescription;
import org.onosproject.net.link.LinkDescription;
......@@ -82,6 +73,22 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.Futures;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.net.DefaultAnnotations.merge;
import static org.onosproject.net.DefaultAnnotations.union;
import static org.onosproject.net.Link.State.ACTIVE;
import static org.onosproject.net.Link.State.INACTIVE;
import static org.onosproject.net.Link.Type.DIRECT;
import static org.onosproject.net.Link.Type.INDIRECT;
import static org.onosproject.net.LinkKey.linkKey;
import static org.onosproject.net.config.basics.SubjectFactories.APP_SUBJECT_FACTORY;
import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
import static org.onosproject.net.link.LinkEvent.Type.LINK_UPDATED;
import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.PUT;
import static org.onosproject.store.service.EventuallyConsistentMapEvent.Type.REMOVE;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Manages the inventory of links using a {@code EventuallyConsistentMap}.
*/
......@@ -91,11 +98,29 @@ public class ECLinkStore
extends AbstractStore<LinkEvent, LinkStoreDelegate>
implements LinkStore {
/**
* Modes for dealing with newly discovered links.
*/
protected enum LinkDiscoveryMode {
/**
* Permissive mode - all newly discovered links are valid.
*/
PERMISSIVE,
/**
* Strict mode - all newly discovered links must be defined in
* the network config.
*/
STRICT
}
private final Logger log = getLogger(getClass());
private final Map<LinkKey, Link> links = Maps.newConcurrentMap();
private EventuallyConsistentMap<Provided<LinkKey>, LinkDescription> linkDescriptions;
private ApplicationId appId;
private static final MessageSubject LINK_INJECT_MESSAGE = new MessageSubject("inject-link-request");
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
......@@ -113,9 +138,20 @@ public class ECLinkStore
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ClusterService clusterService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected NetworkConfigRegistry netCfgService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected CoreService coreService;
private EventuallyConsistentMapListener<Provided<LinkKey>, LinkDescription> linkTracker =
new InternalLinkTracker();
// Listener for config changes
private final InternalConfigListener cfgListener = new InternalConfigListener();
protected LinkDiscoveryMode linkDiscoveryMode = LinkDiscoveryMode.STRICT;
protected static final KryoSerializer SERIALIZER = new KryoSerializer() {
@Override
protected void setupKryoPool() {
......@@ -129,6 +165,12 @@ public class ECLinkStore
@Activate
public void activate() {
appId = coreService.registerApplication("org.onosproject.core");
netCfgService.registerConfigFactory(factory);
netCfgService.addListener(cfgListener);
cfgListener.reconfigure(netCfgService.getConfig(appId, CoreConfig.class));
KryoNamespace.Builder serializer = KryoNamespace.newBuilder()
.register(KryoNamespaces.API)
.register(MastershipBasedTimestamp.class)
......@@ -162,6 +204,8 @@ public class ECLinkStore
linkDescriptions.destroy();
links.clear();
clusterCommunicator.removeSubscriber(LINK_INJECT_MESSAGE);
netCfgService.removeListener(cfgListener);
netCfgService.unregisterConfigFactory(factory);
log.info("Stopped");
}
......@@ -255,6 +299,7 @@ public class ECLinkStore
current.src(),
current.dst(),
current.type() == DIRECT ? DIRECT : updated.type(),
current.isExpected(),
union(current.annotations(), updated.annotations()));
}
return updated;
......@@ -268,6 +313,7 @@ public class ECLinkStore
eventType.set(LINK_ADDED);
return newLink;
} else if (existingLink.state() != newLink.state() ||
existingLink.isExpected() != newLink.isExpected() ||
(existingLink.type() == INDIRECT && newLink.type() == DIRECT) ||
!AnnotationsUtil.isEqual(existingLink.annotations(), newLink.annotations())) {
eventType.set(LINK_UPDATED);
......@@ -316,14 +362,28 @@ public class ECLinkStore
linkDescriptions.get(key).annotations()));
});
boolean isDurable = Objects.equals(annotations.get().value(AnnotationKeys.DURABLE), "true");
Link.State initialLinkState;
boolean isExpected;
if (linkDiscoveryMode == LinkDiscoveryMode.PERMISSIVE) {
initialLinkState = ACTIVE;
isExpected =
Objects.equals(annotations.get().value(AnnotationKeys.DURABLE), "true");
} else {
initialLinkState = base.isExpected() ? ACTIVE : INACTIVE;
isExpected = base.isExpected();
}
return DefaultLink.builder()
.providerId(baseProviderId)
.src(src)
.dst(dst)
.type(type)
.state(ACTIVE)
.isExpected(isDurable)
.state(initialLinkState)
.isExpected(isExpected)
.annotations(annotations.get())
.build();
}
......@@ -350,7 +410,7 @@ public class ECLinkStore
return null;
}
if (link.isDurable()) {
if (linkDiscoveryMode == LinkDiscoveryMode.PERMISSIVE && link.isExpected()) {
// FIXME: this will not sync link state!!!
return link.state() == INACTIVE ? null :
updateLink(linkKey(link.src(), link.dst()), link,
......@@ -421,4 +481,47 @@ public class ECLinkStore
}
}
}
private class InternalConfigListener implements NetworkConfigListener {
void reconfigure(CoreConfig coreConfig) {
if (coreConfig == null) {
linkDiscoveryMode = LinkDiscoveryMode.PERMISSIVE;
} else {
linkDiscoveryMode = coreConfig.linkDiscoveryMode();
}
if (linkDiscoveryMode == LinkDiscoveryMode.STRICT) {
// Remove any previous links to force them to go through the strict
// discovery process
linkDescriptions.clear();
links.clear();
}
log.debug("config set link discovery mode to {}",
linkDiscoveryMode.name());
}
@Override
public void event(NetworkConfigEvent event) {
if ((event.type() == NetworkConfigEvent.Type.CONFIG_ADDED ||
event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) &&
event.configClass().equals(CoreConfig.class)) {
CoreConfig cfg = netCfgService.getConfig(appId, CoreConfig.class);
reconfigure(cfg);
log.info("Reconfigured");
}
}
}
// Configuration properties factory
private final ConfigFactory factory =
new ConfigFactory<ApplicationId, CoreConfig>(APP_SUBJECT_FACTORY,
CoreConfig.class,
"core") {
@Override
public CoreConfig createConfig() {
return new CoreConfig();
}
};
}
......
......@@ -15,12 +15,20 @@
*/
package org.onosproject.store.link.impl;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.RandomUtils;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Deactivate;
......@@ -32,7 +40,6 @@ import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.cluster.NodeId;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.AnnotationKeys;
import org.onosproject.net.AnnotationsUtil;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DefaultAnnotations;
......@@ -60,20 +67,12 @@ import org.onosproject.store.serializers.KryoSerializer;
import org.onosproject.store.serializers.custom.DistributedStoreSerializers;
import org.slf4j.Logger;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Function;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Predicates.notNull;
......@@ -433,7 +432,9 @@ public class GossipLinkStore
new DefaultLinkDescription(
linkDescription.value().src(),
linkDescription.value().dst(),
newType, merged),
newType,
existingLinkDescription.value().isExpected(),
merged),
linkDescription.timestamp());
}
return descs.put(providerId, newLinkDescription);
......@@ -608,14 +609,17 @@ public class GossipLinkStore
annotations = merge(annotations, e.getValue().value().annotations());
}
boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
//boolean isDurable = Objects.equals(annotations.value(AnnotationKeys.DURABLE), "true");
// TEMP
Link.State initialLinkState = base.value().isExpected() ? ACTIVE : INACTIVE;
return DefaultLink.builder()
.providerId(baseProviderId)
.src(src)
.dst(dst)
.type(type)
.state(ACTIVE)
.isExpected(isDurable)
.state(initialLinkState)
.isExpected(base.value().isExpected())
.annotations(annotations)
.build();
}
......
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2014 Open Networking Laboratory
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>onos-providers</artifactId>
<groupId>org.onosproject</groupId>
<version>1.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>onos-netcfg-links-provider</artifactId>
<packaging>bundle</packaging>
<description>
Links provider that uses network config service to predefine devices and links.
</description>
<url>http://onosproject.org</url>
<properties>
<onos.app.name>org.onosproject.netcfglinksprovider</onos.app.name>
<onos.app.origin>ON.Lab</onos.app.origin>
</properties>
<dependencies>
<dependency>
<groupId>org.onosproject</groupId>
<artifactId>onlab-osgi</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.provider.netcfglinks;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.LinkKey;
import org.onosproject.net.link.LinkProviderService;
import org.onosproject.net.packet.PacketService;
/**
* Shared context for use by link discovery.
*/
interface DiscoveryContext {
/**
* Returns the shared mastership service reference.
*
* @return mastership service
*/
MastershipService mastershipService();
/**
* Returns the shared link provider service reference.
*
* @return link provider service
*/
LinkProviderService providerService();
/**
* Returns the shared packet service reference.
*
* @return packet service
*/
PacketService packetService();
/**
* Returns the probe rate in millis.
*
* @return probe rate
*/
long probeRate();
/**
* Indicates whether to emit BDDP.
*
* @return true to emit BDDP
*/
boolean useBddp();
/**
* Touches the link identified by the given key to indicate that it's active.
*
* @param key link key
*/
void touchLink(LinkKey key);
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.provider.netcfglinks;
import java.nio.ByteBuffer;
import java.util.Set;
import org.jboss.netty.util.Timeout;
import org.jboss.netty.util.TimerTask;
import org.onlab.packet.Ethernet;
import org.onlab.packet.ONOSLLDP;
import org.onlab.util.Timer;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link.Type;
import org.onosproject.net.LinkKey;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.link.DefaultLinkDescription;
import org.onosproject.net.link.LinkDescription;
import org.onosproject.net.packet.DefaultOutboundPacket;
import org.onosproject.net.packet.OutboundPacket;
import org.onosproject.net.packet.PacketContext;
import org.slf4j.Logger;
import com.google.common.collect.Sets;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.onosproject.net.PortNumber.portNumber;
import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Run discovery process from a physical switch. Ports are initially labeled as
* slow ports. When an LLDP is successfully received, label the remote port as
* fast. Every probeRate milliseconds, loop over all fast ports and send an
* LLDP, send an LLDP for a single slow port. Based on FlowVisor topology
* discovery implementation.
*/
class LinkDiscovery implements TimerTask {
private final Logger log = getLogger(getClass());
private static final String SRC_MAC = "DE:AD:BE:EF:BA:11";
private final Device device;
private final DiscoveryContext context;
private final ONOSLLDP lldpPacket;
private final Ethernet ethPacket;
private final Ethernet bddpEth;
private Timeout timeout;
private volatile boolean isStopped;
// Set of ports to be probed
private final Set<Long> ports = Sets.newConcurrentHashSet();
/**
* Instantiates discovery manager for the given physical switch. Creates a
* generic LLDP packet that will be customized for the port it is sent out on.
* Starts the the timer for the discovery process.
*
* @param device the physical switch
* @param context discovery context
*/
LinkDiscovery(Device device, DiscoveryContext context) {
this.device = device;
this.context = context;
lldpPacket = new ONOSLLDP();
lldpPacket.setChassisId(device.chassisId());
lldpPacket.setDevice(device.id().toString());
ethPacket = new Ethernet();
ethPacket.setEtherType(Ethernet.TYPE_LLDP);
ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
ethPacket.setPayload(this.lldpPacket);
ethPacket.setPad(true);
bddpEth = new Ethernet();
bddpEth.setPayload(lldpPacket);
bddpEth.setEtherType(Ethernet.TYPE_BSN);
bddpEth.setDestinationMACAddress(ONOSLLDP.BDDP_MULTICAST);
bddpEth.setPad(true);
isStopped = true;
start();
log.debug("Started discovery manager for switch {}", device.id());
}
synchronized void stop() {
if (!isStopped) {
isStopped = true;
timeout.cancel();
} else {
log.warn("LinkDiscovery stopped multiple times?");
}
}
synchronized void start() {
if (isStopped) {
isStopped = false;
timeout = Timer.getTimer().newTimeout(this, 0, MILLISECONDS);
} else {
log.warn("LinkDiscovery started multiple times?");
}
}
synchronized boolean isStopped() {
return isStopped || timeout.isCancelled();
}
/**
* Add physical port to discovery process.
* Send out initial LLDP and label it as slow port.
*
* @param port the port
*/
void addPort(Port port) {
boolean newPort = ports.add(port.number().toLong());
boolean isMaster = context.mastershipService().isLocalMaster(device.id());
if (newPort && isMaster) {
log.debug("Sending initial probe to port {}@{}", port.number().toLong(), device.id());
sendProbes(port.number().toLong());
}
}
/**
* removed physical port from discovery process.
* @param port the port number
*/
void removePort(PortNumber port) {
ports.remove(port.toLong());
}
/**
* Handles an incoming LLDP packet. Creates link in topology and adds the
* link for staleness tracking.
*
* @param packetContext packet context
* @return true if handled
*/
boolean handleLldp(PacketContext packetContext) {
Ethernet eth = packetContext.inPacket().parsed();
if (eth == null) {
return false;
}
ONOSLLDP onoslldp = ONOSLLDP.parseONOSLLDP(eth);
if (onoslldp != null) {
PortNumber srcPort = portNumber(onoslldp.getPort());
PortNumber dstPort = packetContext.inPacket().receivedFrom().port();
DeviceId srcDeviceId = DeviceId.deviceId(onoslldp.getDeviceString());
DeviceId dstDeviceId = packetContext.inPacket().receivedFrom().deviceId();
ConnectPoint src = new ConnectPoint(srcDeviceId, srcPort);
ConnectPoint dst = new ConnectPoint(dstDeviceId, dstPort);
LinkDescription ld = eth.getEtherType() == Ethernet.TYPE_LLDP ?
new DefaultLinkDescription(src, dst, Type.DIRECT, DefaultLinkDescription.EXPECTED) :
new DefaultLinkDescription(src, dst, Type.INDIRECT, DefaultLinkDescription.EXPECTED);
try {
context.providerService().linkDetected(ld);
context.touchLink(LinkKey.linkKey(src, dst));
} catch (IllegalStateException e) {
return true;
}
return true;
}
return false;
}
/**
* Execute this method every t milliseconds. Loops over all ports
* labeled as fast and sends out an LLDP. Send out an LLDP on a single slow
* port.
*
* @param t timeout
*/
@Override
public void run(Timeout t) {
if (isStopped()) {
return;
}
if (context.mastershipService().isLocalMaster(device.id())) {
log.trace("Sending probes from {}", device.id());
ports.forEach(this::sendProbes);
}
if (!isStopped()) {
timeout = Timer.getTimer().newTimeout(this, context.probeRate(), MILLISECONDS);
}
}
/**
* Creates packet_out LLDP for specified output port.
*
* @param port the port
* @return Packet_out message with LLDP data
*/
private OutboundPacket createOutBoundLldp(Long port) {
if (port == null) {
return null;
}
lldpPacket.setPortId(port.intValue());
ethPacket.setSourceMACAddress(SRC_MAC);
return new DefaultOutboundPacket(device.id(),
builder().setOutput(portNumber(port)).build(),
ByteBuffer.wrap(ethPacket.serialize()));
}
/**
* Creates packet_out BDDP for specified output port.
*
* @param port the port
* @return Packet_out message with LLDP data
*/
private OutboundPacket createOutBoundBddp(Long port) {
if (port == null) {
return null;
}
lldpPacket.setPortId(port.intValue());
bddpEth.setSourceMACAddress(SRC_MAC);
return new DefaultOutboundPacket(device.id(),
builder().setOutput(portNumber(port)).build(),
ByteBuffer.wrap(bddpEth.serialize()));
}
private void sendProbes(Long portNumber) {
log.trace("Sending probes out to {}@{}", portNumber, device.id());
OutboundPacket pkt = createOutBoundLldp(portNumber);
context.packetService().emit(pkt);
if (context.useBddp()) {
OutboundPacket bpkt = createOutBoundBddp(portNumber);
context.packetService().emit(bpkt);
}
}
boolean containsPort(long portNumber) {
return ports.contains(portNumber);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Provider to pre-discover links and devices based on a specified network
* config.
*/
package org.onosproject.provider.netcfglinks;
......@@ -43,6 +43,7 @@
<module>bgp</module>
<module>snmp</module>
<module>rest</module>
<module>netcfglinks</module>
</modules>
<dependencies>
......
......@@ -71,7 +71,7 @@ public class ONOSLLDP extends LLDP {
private final byte[] ttlValue = new byte[] {0, 0x78};
// Only needs to be accessed from LinkProbeFactory.
protected ONOSLLDP(byte ... subtype) {
public ONOSLLDP(byte ... subtype) {
super();
for (byte st : subtype) {
opttlvs.put(st, new LLDPOrganizationalTLV());
......
......@@ -278,6 +278,7 @@ public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
ObjectNode payload = objectNode()
.put("id", compactLinkString(link))
.put("type", link.type().toString().toLowerCase())
.put("expected", link.isExpected())
.put("online", link.state() == Link.State.ACTIVE)
.put("linkWidth", 1.2)
.put("src", link.src().deviceId().toString())
......
......@@ -526,6 +526,12 @@ html[data-platform='iPad'] #topo-p-detail {
opacity: .5;
stroke-dasharray: 8 4;
}
/* FIXME: Review for not-permitted links */
#ov-topo svg .link.not-permitted {
stroke: rgb(255,0,0);
stroke-width: 5.0;
stroke-dasharray: 8 4;
}
#ov-topo svg .link.secondary {
stroke-width: 3px;
......
......@@ -323,7 +323,8 @@
.domain([1, 12])
.range([widthRatio, 12 * widthRatio])
.clamp(true),
allLinkTypes = 'direct indirect optical tunnel';
allLinkTypes = 'direct indirect optical tunnel',
allLinkSubTypes = 'inactive not-permitted';
function restyleLinkElement(ldata, immediate) {
// this fn's job is to look at raw links and decide what svg classes
......@@ -333,6 +334,7 @@
type = ldata.type(),
lw = ldata.linkWidth(),
online = ldata.online(),
modeCls = ldata.expected() ? 'inactive' : 'not-permitted',
delay = immediate ? 0 : 1000;
// FIXME: understand why el is sometimes undefined on addLink events...
......@@ -343,7 +345,8 @@
// a more efficient way to fix it.
if (el && !el.empty()) {
el.classed('link', true);
el.classed('inactive', !online);
el.classed(allLinkSubTypes, false);
el.classed(modeCls, !online);
el.classed(allLinkTypes, false);
if (type) {
el.classed(type, true);
......
......@@ -198,6 +198,11 @@
t = lnk.fromTarget;
return (s && s.type) || (t && t.type) || defaultLinkType;
},
expected: function () {
var s = lnk.fromSource,
t = lnk.fromTarget;
return (s && s.expected) && (t && t.expected);
},
online: function () {
var s = lnk.fromSource,
t = lnk.fromTarget,
......
......@@ -301,8 +301,12 @@
return linkTypePres[d.type()] || d.type();
}
function linkExpected(d) {
return d.expected();
}
var coreOrder = [
'Type', '-',
'Type', 'Expected', '-',
'A_type', 'A_id', 'A_label', 'A_port', '-',
'B_type', 'B_id', 'B_label', 'B_port', '-'
],
......@@ -332,6 +336,7 @@
propOrder: order,
props: {
Type: linkType(data),
Expected: linkExpected(data),
A_type: data.source.class,
A_id: data.source.id,
......