Shashikanth VH
Committed by ShashikanthVH-Huawei

Bgp link listener deleted.

Change-Id: Ie853639739dd474bea2aebc013bb378c6dc8163a
......@@ -58,20 +58,6 @@ public interface BgpController {
void removeListener(BgpNodeListener listener);
/**
* Register a listener for BGP message events.
*
* @param listener the listener to notify
*/
void addLinkListener(BgpLinkListener listener);
/**
* Unregister a listener.
*
* @param listener the listener to unregister
*/
void removeLinkListener(BgpLinkListener listener);
/**
* Send a message to a particular bgp peer.
*
* @param bgpId the id of the peer to send message.
......@@ -128,11 +114,4 @@ public interface BgpController {
* @return node listener
*/
Set<BgpNodeListener> listener();
/**
* Return BGP link listener.
*
* @return link listener
*/
Set<BgpLinkListener> linkListener();
}
......
......@@ -46,20 +46,17 @@ public final class BgpDpid {
this.stringBuilder = new StringBuilder("bgpls://");
if (linkNlri.getRouteDistinguisher() != null) {
this.stringBuilder.append(Long.toString(linkNlri.getRouteDistinguisher().getRouteDistinguisher()))
.append(':');
this.stringBuilder.append(linkNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':');
}
try {
this.stringBuilder.append(linkNlri.getProtocolId().toString())
.append(':')
.append(Long.toString(linkNlri.getIdentifier()))
.append('/');
this.stringBuilder.append(linkNlri.getProtocolId()).append(':').append(linkNlri.getIdentifier())
.append('/');
if (nodeDescriptorType == NODE_DESCRIPTOR_LOCAL) {
add(linkNlri.localNodeDescriptors().toString());
add(linkNlri.localNodeDescriptors());
} else if (nodeDescriptorType == NODE_DESCRIPTOR_REMOTE) {
add(linkNlri.remoteNodeDescriptors().toString());
add(linkNlri.remoteNodeDescriptors());
}
} catch (BgpParseException e) {
log.info("Exception BgpId string: " + e.toString());
......@@ -76,18 +73,15 @@ public final class BgpDpid {
this.stringBuilder = new StringBuilder("bgpls://");
if (nodeNlri.getRouteDistinguisher() != null) {
this.stringBuilder.append(Long.toString(nodeNlri.getRouteDistinguisher().getRouteDistinguisher()))
.append(':');
this.stringBuilder.append(nodeNlri.getRouteDistinguisher().getRouteDistinguisher()).append(':');
}
try {
this.stringBuilder.append(nodeNlri.getProtocolId().toString())
.append(':')
.append(Long.toString(nodeNlri.getIdentifier()))
.append('/');
this.stringBuilder.append(nodeNlri.getProtocolId()).append(':').append(nodeNlri.getIdentifier())
.append('/');
add(nodeNlri.getLocalNodeDescriptors().toString());
add(nodeNlri.getLocalNodeDescriptors());
} catch (BgpParseException e) {
log.info("Exception node string: " + e.toString());
......
......@@ -30,7 +30,6 @@ import org.onosproject.bgp.controller.BgpCfg;
import org.onosproject.bgp.controller.BgpController;
import org.onosproject.bgp.controller.BgpId;
import org.onosproject.bgp.controller.BgpPeer;
import org.onosproject.bgp.controller.BgpLinkListener;
import org.onosproject.bgp.controller.BgpNodeListener;
import org.onosproject.bgp.controller.BgpPeerManager;
import org.onosproject.bgpio.exceptions.BgpParseException;
......@@ -49,7 +48,6 @@ public class BgpControllerImpl implements BgpController {
protected BgpPeerManagerImpl peerManager = new BgpPeerManagerImpl();
protected Set<BgpNodeListener> bgpNodeListener = new CopyOnWriteArraySet<>();
protected Set<BgpLinkListener> bgpLinkListener = new CopyOnWriteArraySet<>();
final Controller ctrl = new Controller(this);
......@@ -95,21 +93,6 @@ public class BgpControllerImpl implements BgpController {
}
@Override
public void addLinkListener(BgpLinkListener listener) {
this.bgpLinkListener.add(listener);
}
@Override
public void removeLinkListener(BgpLinkListener listener) {
this.bgpLinkListener.remove(listener);
}
@Override
public Set<BgpLinkListener> linkListener() {
return bgpLinkListener;
}
@Override
public void writeMsg(BgpId bgpId, BgpMessage msg) {
this.getPeer(bgpId).sendMessage(msg);
}
......
......@@ -13,14 +13,9 @@
package org.onosproject.provider.bgp.topology.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.onosproject.bgp.controller.BgpDpid.uri;
import static org.onosproject.net.DeviceId.deviceId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.onlab.packet.ChassisId;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
......@@ -29,27 +24,16 @@ import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onosproject.bgp.controller.BgpController;
import org.onosproject.bgp.controller.BgpDpid;
import org.onosproject.bgp.controller.BgpLinkListener;
import org.onosproject.bgp.controller.BgpNodeListener;
import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.Device;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Link;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.device.DefaultDeviceDescription;
import org.onosproject.net.device.DefaultPortDescription;
import org.onosproject.net.device.DeviceDescription;
import org.onosproject.net.device.DeviceProvider;
import org.onosproject.net.device.DeviceProviderRegistry;
import org.onosproject.net.device.DeviceProviderService;
import org.onosproject.net.device.PortDescription;
import org.onosproject.net.link.DefaultLinkDescription;
import org.onosproject.net.link.LinkDescription;
import org.onosproject.net.link.LinkProvider;
import org.onosproject.net.link.LinkProviderRegistry;
import org.onosproject.net.link.LinkProviderService;
import org.onosproject.net.provider.AbstractProvider;
import org.onosproject.net.provider.ProviderId;
import org.slf4j.Logger;
......@@ -59,7 +43,7 @@ import org.slf4j.LoggerFactory;
* Provider which uses an BGP controller to detect network infrastructure topology.
*/
@Component(immediate = true)
public class BgpTopologyProvider extends AbstractProvider implements LinkProvider, DeviceProvider {
public class BgpTopologyProvider extends AbstractProvider implements DeviceProvider {
public BgpTopologyProvider() {
super(new ProviderId("bgp", "org.onosproject.provider.bgp"));
......@@ -68,86 +52,31 @@ public class BgpTopologyProvider extends AbstractProvider implements LinkProvide
private static final Logger log = LoggerFactory.getLogger(BgpTopologyProvider.class);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected LinkProviderRegistry linkProviderRegistry;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DeviceProviderRegistry deviceProviderRegistry;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected BgpController controller;
private DeviceProviderService deviceProviderService;
private LinkProviderService linkProviderService;
private HashMap<String, List<PortDescription>> portMap = new HashMap<>();
private InternalBgpProvider listener = new InternalBgpProvider();
private static final String UNKNOWN = "unknown";
@Activate
public void activate() {
linkProviderService = linkProviderRegistry.register(this);
deviceProviderService = deviceProviderRegistry.register(this);
controller.addListener(listener);
controller.addLinkListener(listener);
}
@Deactivate
public void deactivate() {
linkProviderRegistry.unregister(this);
linkProviderService = null;
controller.removeListener(listener);
controller.removeLinkListener(listener);
}
private List<PortDescription> buildPortDescriptions(String linkUri) {
List<PortDescription> portList;
if (portMap.containsKey(linkUri)) {
portList = portMap.get(linkUri);
} else {
portList = new ArrayList<>();
}
// TODO: port description
portList.add(new DefaultPortDescription(null, true));
portMap.put(linkUri, portList);
return portList;
}
/**
* Build a link description from a bgp link.
*
* @param bgpLink bgp link
* @return linkDescription link description
*/
private LinkDescription buildLinkDescription(BgpLinkLsNlriVer4 bgpLink) {
LinkDescription ld = null;
checkNotNull(bgpLink);
BgpDpid localNodeUri = new BgpDpid(bgpLink, BgpDpid.NODE_DESCRIPTOR_LOCAL);
DeviceId srcDeviceID = deviceId(uri(localNodeUri.toString()));
BgpDpid remoteNodeUri = new BgpDpid(bgpLink, BgpDpid.NODE_DESCRIPTOR_REMOTE);
DeviceId dstDeviceID = deviceId(uri(remoteNodeUri.toString()));
deviceProviderService.updatePorts(srcDeviceID, buildPortDescriptions(localNodeUri.toString()));
deviceProviderService.updatePorts(dstDeviceID, buildPortDescriptions(remoteNodeUri.toString()));
ConnectPoint src = new ConnectPoint(srcDeviceID, null);
ConnectPoint dst = new ConnectPoint(dstDeviceID, null);
ld = new DefaultLinkDescription(src, dst, Link.Type.INDIRECT);
return ld;
}
/*
* Implements device and link update.
*/
private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener {
private class InternalBgpProvider implements BgpNodeListener {
@Override
public void addNode(BgpNodeLSNlriVer4 nodeNlri) {
......@@ -177,40 +106,6 @@ public class BgpTopologyProvider extends AbstractProvider implements LinkProvide
BgpDpid nodeUri = new BgpDpid(nodeNlri);
deviceProviderService.deviceDisconnected(deviceId(uri(nodeUri.toString())));
}
@Override
public void addLink(BgpLinkLsNlriVer4 linkNlri) {
log.debug("Add link {}", linkNlri.toString());
if (linkProviderService == null) {
return;
}
LinkDescription ld = buildLinkDescription(linkNlri);
if (ld == null) {
log.error("Invalid link info.");
return;
}
linkProviderService.linkDetected(ld);
}
@Override
public void deleteLink(BgpLinkLsNlriVer4 linkNlri) {
log.debug("Delete link {}", linkNlri.toString());
if (linkProviderService == null) {
return;
}
LinkDescription ld = buildLinkDescription(linkNlri);
if (ld == null) {
log.error("Invalid link info.");
return;
}
linkProviderService.linkVanished(ld);
}
}
@Override
......
......@@ -31,22 +31,17 @@ import org.onosproject.bgp.controller.BgpCfg;
import org.onosproject.bgp.controller.BgpController;
import org.onosproject.bgp.controller.BgpId;
import org.onosproject.bgp.controller.BgpPeer;
import org.onosproject.bgp.controller.BgpLinkListener;
import org.onosproject.bgp.controller.BgpNodeListener;
import org.onosproject.bgp.controller.BgpPeerManager;
import org.onosproject.bgpio.exceptions.BgpParseException;
import org.onosproject.bgpio.protocol.BgpMessage;
import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
import org.onosproject.bgpio.types.AutonomousSystemTlv;
import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
import org.onosproject.bgpio.types.BgpValueType;
import org.onosproject.bgpio.types.RouteDistinguisher;
import org.onosproject.bgpio.util.Constants;
import org.onosproject.net.ConnectPoint;
import org.onosproject.net.DeviceId;
import org.onosproject.net.MastershipRole;
import org.onosproject.net.device.DeviceDescription;
......@@ -55,10 +50,6 @@ import org.onosproject.net.device.DeviceProviderRegistry;
import org.onosproject.net.device.DeviceProviderService;
import org.onosproject.net.device.PortDescription;
import org.onosproject.net.device.PortStatistics;
import org.onosproject.net.link.LinkDescription;
import org.onosproject.net.link.LinkProvider;
import org.onosproject.net.link.LinkProviderRegistry;
import org.onosproject.net.link.LinkProviderService;
import org.onosproject.net.provider.ProviderId;
public class BgpTopologyProviderTest {
......@@ -74,17 +65,14 @@ public class BgpTopologyProviderTest {
+ "%20subtlvs=[autonomoussystemtlv%7btype=512,%20length=4,%20asnum=100%7d]%7d");
private final BgpTopologyProvider provider = new BgpTopologyProvider();
private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
private final TestController controller = new TestController();
@Before
public void startUp() {
provider.deviceProviderRegistry = nodeRegistry;
provider.linkProviderRegistry = linkRegistry;
provider.controller = controller;
provider.activate();
assertNotNull("provider should be registered", nodeRegistry.provider);
assertNotNull("provider should be registered", linkRegistry.provider);
assertNotNull("listener should be registered", controller.nodeListener);
}
......@@ -94,7 +82,6 @@ public class BgpTopologyProviderTest {
assertNull("listener should be removed", controller.nodeListener);
provider.controller = null;
provider.deviceProviderRegistry = null;
provider.linkProviderRegistry = null;
}
/* Class implement device test registry */
......@@ -165,69 +152,9 @@ public class BgpTopologyProviderTest {
}
}
/* class implement link test registery */
private class TestLinkRegistry implements LinkProviderRegistry {
LinkProvider provider;
Set<DeviceId> connected = new HashSet<>();
@Override
public LinkProviderService register(LinkProvider provider) {
this.provider = provider;
return new TestProviderService();
}
@Override
public void unregister(LinkProvider provider) {
}
@Override
public Set<ProviderId> getProviders() {
return null;
}
private class TestProviderService implements LinkProviderService {
@Override
public LinkProvider provider() {
// TODO Auto-generated method stub
return null;
}
@Override
public void linkDetected(LinkDescription linkDescription) {
if ((linkDescription.src().deviceId().equals(DID3))
&& (linkDescription.dst().deviceId().equals(DID3))) {
connected.add(linkDescription.src().deviceId());
}
}
@Override
public void linkVanished(LinkDescription linkDescription) {
if ((linkDescription.src().deviceId().equals(DID3))
&& (linkDescription.dst().deviceId().equals(DID3))) {
connected.remove(linkDescription.src().deviceId());
}
}
@Override
public void linksVanished(ConnectPoint connectPoint) {
// TODO Auto-generated method stub
}
@Override
public void linksVanished(DeviceId deviceId) {
connected.remove(deviceId);
}
}
}
/* class implement test controller */
private class TestController implements BgpController {
protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
protected Set<BgpLinkListener> linkListener = new CopyOnWriteArraySet<>();
@Override
public void addListener(BgpNodeListener nodeListener) {
......@@ -240,16 +167,6 @@ public class BgpTopologyProviderTest {
}
@Override
public void addLinkListener(BgpLinkListener linkListener) {
this.linkListener.add(linkListener);
}
@Override
public void removeLinkListener(BgpLinkListener linkListener) {
this.linkListener = null;
}
@Override
public Iterable<BgpPeer> getPeers() {
// TODO Auto-generated method stub
return null;
......@@ -309,12 +226,6 @@ public class BgpTopologyProviderTest {
// TODO Auto-generated method stub
return null;
}
@Override
public Set<BgpLinkListener> linkListener() {
// TODO Auto-generated method stub
return null;
}
}
/* Validate node is added to the device validating URI, RIB should get updated properly */
......@@ -387,37 +298,4 @@ public class BgpTopologyProviderTest {
assertTrue("Failed to add device", (nodeRegistry.connected.size() == 0));
}
}
/* Validate link is added to the device validating URI, RIB should get updated properly */
@Test
public void bgpTopologyProviderTestAddLink1() {
NodeDescriptors localNodeDescriptors;
NodeDescriptors remoteNodeDescriptors;
LinkedList<BgpValueType> subTlvs;
LinkedList<BgpValueType> linkDescriptor = new LinkedList<>();
BgpValueType tlvLocalRemoteId;
short deslength = AutonomousSystemTlv.LENGTH;
short desType = AutonomousSystemTlv.TYPE;
BgpValueType tlv = new AutonomousSystemTlv(100);
subTlvs = new LinkedList<>();
subTlvs.add(tlv);
localNodeDescriptors = new NodeDescriptors(subTlvs, deslength, desType);
remoteNodeDescriptors = new NodeDescriptors(subTlvs, deslength, desType);
tlvLocalRemoteId = new LinkLocalRemoteIdentifiersTlv(1, 2);
linkDescriptor.add(tlvLocalRemoteId);
BgpLinkLSIdentifier linkLSIdentifier = new BgpLinkLSIdentifier(localNodeDescriptors, remoteNodeDescriptors,
linkDescriptor);
BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkLSIdentifier, null, false);
for (BgpLinkListener l : controller.linkListener) {
l.addLink(linkNlri);
assertTrue(linkRegistry.connected.size() == 1);
l.deleteLink(linkNlri);
assertTrue(linkRegistry.connected.size() == 0);
}
}
}
......