import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
+import org.opendaylight.vpnservice.mdsalutil.NwConstants;
import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.OperStatus;
import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.AdjacencyList;
import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
- private ListenerRegistration<DataChangeListener> listenerRegistration;
+ private ListenerRegistration<DataChangeListener> listenerRegistration, interfaceListenerRegistration;
private final DataBroker broker;
private final IBgpManager bgpManager;
private IFibManager fibManager;
private IMdsalApiManager mdsalManager;
private IInterfaceManager interfaceManager;
private IdManagerService idManager;
- private Map<Long, Collection<Long>> vpnToDpnsDb;
- private Map<Long, Collection<String>> dpnToInterfaceDb;
+ private Map<Long, Collection<BigInteger>> vpnToDpnsDb;
+ private Map<BigInteger, Collection<String>> dpnToInterfaceDb;
+ private InterfaceListener interfaceListener;
private static final FutureCallback<Void> DEFAULT_CALLBACK =
new FutureCallback<Void>() {
this.bgpManager = bgpManager;
vpnToDpnsDb = new ConcurrentHashMap<>();
dpnToInterfaceDb = new ConcurrentHashMap<>();
+ interfaceListener = new InterfaceListener();
registerListener(db);
}
if (listenerRegistration != null) {
try {
listenerRegistration.close();
+ interfaceListenerRegistration.close();
} catch (final Exception e) {
LOG.error("Error when cleaning up DataChangeListener.", e);
}
listenerRegistration = null;
+ interfaceListenerRegistration = null;
}
LOG.info("VPN Interface Manager Closed");
}
try {
listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
getWildCardPath(), VpnInterfaceManager.this, DataChangeScope.SUBTREE);
+ interfaceListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
+ getInterfaceListenerPath(), interfaceListener, DataChangeScope.SUBTREE);
} catch (final Exception e) {
LOG.error("VPN Service DataChange listener registration fail!", e);
throw new IllegalStateException("VPN Service registration Listener failed.", e);
}
}
+ private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> getInterfaceListenerPath() {
+ return InstanceIdentifier.create(InterfacesState.class)
+ .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class);
+ }
+
@Override
protected void add(final InstanceIdentifier<VpnInterface> identifier,
final VpnInterface vpnInterface) {
//Get the rd of the vpn instance
String rd = getRouteDistinguisher(intf.getVpnInstanceName());
- long dpnId = interfaceManager.getDpnForInterface(intfName);
+ BigInteger dpnId = interfaceManager.getDpnForInterface(intfName);
String nextHopIp = interfaceManager.getEndpointIpForDpn(dpnId);
LOG.trace("NextHops are {}", nextHops);
for (Adjacency nextHop : nextHops) {
- String key = nextHop.getIpAddress();
+ String key = rd + VpnConstants.SEPARATOR + nextHop.getIpAddress();
long label = getUniqueId(key);
value.add(new AdjacencyBuilder(nextHop).setLabel(label).build());
}
InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
syncWrite(LogicalDatastoreType.OPERATIONAL, interfaceId, opInterface, DEFAULT_CALLBACK);
for (Adjacency nextHop : nextHops) {
- String key = nextHop.getIpAddress();
+ String key = rd + VpnConstants.SEPARATOR + nextHop.getIpAddress();
long label = getUniqueId(key);
updatePrefixToBGP(rd, nextHop, nextHopIp, label);
}
return rd;
}
- private synchronized void updateMappingDbs(long vpnId, long dpnId, String intfName, String rd) {
- Collection<Long> dpnIds = vpnToDpnsDb.get(vpnId);
+ private synchronized void updateMappingDbs(long vpnId, BigInteger dpnId, String intfName, String rd) {
+ Collection<BigInteger> dpnIds = vpnToDpnsDb.get(vpnId);
if(dpnIds == null) {
dpnIds = new HashSet<>();
}
dpnToInterfaceDb.put(dpnId, intfNames);
}
- private synchronized void remoteFromMappingDbs(long vpnId, long dpnId, String inftName, String rd) {
+ private synchronized void remoteFromMappingDbs(long vpnId, BigInteger dpnId, String inftName, String rd) {
Collection<String> intfNames = dpnToInterfaceDb.get(dpnId);
if(intfNames == null) {
return;
dpnToInterfaceDb.put(dpnId, intfNames);
//TODO: Delay 'DPN' removal so that other services can cleanup the entries for this dpn
if(intfNames.isEmpty()) {
- Collection<Long> dpnIds = vpnToDpnsDb.get(vpnId);
+ Collection<BigInteger> dpnIds = vpnToDpnsDb.get(vpnId);
if(dpnIds == null) {
return;
}
LOG.trace("Bind service on interface {} for VPN: {}", intf, vpnName);
long vpnId = getVpnId(vpnName);
- long dpId = interfaceManager.getDpnForInterface(intf.getName());
- if(dpId == 0L) {
+ BigInteger dpId = interfaceManager.getDpnForInterface(intf.getName());
+ if(dpId.equals(BigInteger.ZERO)) {
LOG.warn("DPN for interface {} not found. Bind service on this interface aborted.", intf.getName());
return;
} else {
mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { gotoTableId }));
List<MatchInfo> matches = new ArrayList<MatchInfo>();
- matches.add(new MatchInfo(MatchFieldType.in_port, new long[] {
- dpId, portNo }));
+ matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
+ dpId, BigInteger.valueOf(portNo) }));
FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, VpnConstants.LPORT_INGRESS_TABLE, flowRef,
priority, flowName, 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
mdsalManager.installFlow(flowEntity);
}
- private String getVpnInterfaceFlowRef(long dpId, short tableId,
+ private String getVpnInterfaceFlowRef(BigInteger dpId, short tableId,
long vpnId, long portNo) {
return new StringBuilder().append(dpId).append(tableId).append(vpnId).append(portNo).toString();
}
}
}
}
-// InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
-// delete(LogicalDatastoreType.OPERATIONAL, interfaceId);
}
private <T extends DataObject> void delete(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
LOG.trace("Unbind service on interface {} for VPN: {}", intf, vpnName);
long vpnId = getVpnId(vpnName);
- long dpId = interfaceManager.getDpnForInterface(intf.getName());
- if(dpId == 0L) {
+ BigInteger dpId = interfaceManager.getDpnForInterface(intf);
+ if(dpId.equals(BigInteger.ZERO)) {
LOG.warn("DPN for interface {} not found. Unbind service on this interface aborted.", intf.getName());
return;
} else {
String rd = getRouteDistinguisher(vpnName);
remoteFromMappingDbs(vpnId, dpId, intf.getName(), rd);
+ LOG.debug("removed vpn mapping for interface {} from VPN RD {}", intf.getName(), rd);
}
- long portNo = interfaceManager.getPortForInterface(intf.getName());
+ long portNo = interfaceManager.getPortForInterface(intf);
String flowRef = getVpnInterfaceFlowRef(dpId, VpnConstants.LPORT_INGRESS_TABLE, vpnId, portNo);
String flowName = intf.getName();
int priority = VpnConstants.DEFAULT_FLOW_PRIORITY;
List<MatchInfo> matches = new ArrayList<MatchInfo>();
- matches.add(new MatchInfo(MatchFieldType.in_port, new long[] {
- dpId, portNo }));
+ matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
+ dpId, BigInteger.valueOf(portNo) }));
FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, VpnConstants.LPORT_INGRESS_TABLE, flowRef,
priority, flowName, 0, 0, null, matches, null);
+ LOG.debug("Remove ingress flow for port {} in dpn {}", portNo, dpId.intValue());
mdsalManager.removeFlow(flowEntity);
}
private void removePrefixFromBGP(String rd, Adjacency nextHop) {
- //public void deletePrefix(String rd, String prefix) throws Exception;
try {
bgpManager.deletePrefix(rd, nextHop.getIpAddress());
} catch(Exception e) {
@Override
protected void update(InstanceIdentifier<VpnInterface> identifier,
VpnInterface original, VpnInterface update) {
- // TODO Auto-generated method stub
+ LOG.trace("Update VPN Interface {} , original {}, update {}",
+ identifier, original, update);
+ String vpnName = original.getVpnInstanceName();
+
+ boolean vpnNameChanged = false;
+ String rd = getRouteDistinguisher(vpnName);
+ String newRd = rd;
+ if(!vpnName.equals(update.getVpnInstanceName())) {
+ //VPN for this interface got changed.
+ //Remove the interface from old VPN and add it to new VPN
+ String newVpnName = update.getVpnInstanceName();
+ newRd = getRouteDistinguisher(newVpnName);
+ if(newRd.equals("")) {
+ LOG.warn("VPN Instance {} not found. Update operation aborted", newVpnName);
+ return;
+ }
+ vpnNameChanged = true;
+ LOG.debug("New VPN Name for the interface {} is {}", newVpnName, original.getName());
+ }
+ BigInteger dpnId = interfaceManager.getDpnForInterface(original.getName());
+ String nextHopIp = interfaceManager.getEndpointIpForDpn(dpnId);
+ //List<Adjacency> oldAdjs = original.getAugmentation(Adjacencies.class).getAdjacency();
+ List<Adjacency> newAdjs = update.getAugmentation(Adjacencies.class).getAdjacency();
+ if(vpnNameChanged && newAdjs != null && !newAdjs.isEmpty()) {
+ long label = VpnConstants.INVALID_ID;
+ InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
+ Optional<Adjacencies> adjacencies = read(LogicalDatastoreType.OPERATIONAL, path);
+ if (adjacencies.isPresent()) {
+ List<Adjacency> nextHops = adjacencies.get().getAdjacency();
+ for(Adjacency nextHop : nextHops) {
+ label = nextHop.getLabel();
+ if(label == VpnConstants.INVALID_ID) {
+ //Generate label using ID Manager
+ String key = newRd + VpnConstants.SEPARATOR + nextHop.getIpAddress();
+ label = getUniqueId(key);
+ }
+ removePrefixFromBGP(rd, nextHop);
+ //updatePrefixToBGP(newRd, nextHop, nextHopIp, label);
+ }
+ updateNextHops(identifier, update);
+ asyncUpdate(LogicalDatastoreType.OPERATIONAL, identifier, update, DEFAULT_CALLBACK);
+ }
+ } else {
+ LOG.debug("No Update information is available for VPN Interface to proceed");
+ }
+ }
+
+ protected <T extends DataObject> void asyncUpdate(LogicalDatastoreType datastoreType,
+ InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
+ WriteTransaction tx = broker.newWriteOnlyTransaction();
+ tx.merge(datastoreType, path, data, true);
+ Futures.addCallback(tx.submit(), callback);
}
private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
tx.submit();
}
- synchronized Collection<Long> getDpnsForVpn(long vpnId) {
- Collection<Long> dpnIds = vpnToDpnsDb.get(vpnId);
+ synchronized Collection<BigInteger> getDpnsForVpn(long vpnId) {
+ Collection<BigInteger> dpnIds = vpnToDpnsDb.get(vpnId);
if(dpnIds != null) {
return ImmutableList.copyOf(dpnIds);
} else {
return Collections.emptyList();
}
}
+
+ VpnInterface getVpnInterface(String interfaceName) {
+ Optional<VpnInterfaces> optVpnInterfaces = read(LogicalDatastoreType.CONFIGURATION, VpnUtil.getVpnInterfacesIdentifier());
+ if(optVpnInterfaces.isPresent()) {
+ List<VpnInterface> interfaces = optVpnInterfaces.get().getVpnInterface();
+ for(VpnInterface intf : interfaces) {
+ if(intf.getName().equals(interfaceName)) {
+ return intf;
+ }
+ }
+ }
+ return null;
+ }
+
+ private Interface getInterface(String interfaceName) {
+ Optional<Interface> optInterface = read(LogicalDatastoreType.CONFIGURATION, VpnUtil.getInterfaceIdentifier(interfaceName));
+ if(optInterface.isPresent()) {
+ return optInterface.get();
+ }
+ return null;
+ }
+
+ private String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
+ return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
+ }
+
+
+ protected void makeTunnelIngressFlow(BigInteger dpnId, String ifName, int addOrRemoveFlow) {
+ long portNo = 0;
+ String flowName = ifName;
+ String flowRef = getTunnelInterfaceFlowRef(dpnId, VpnConstants.LPORT_INGRESS_TABLE, ifName);
+ List<MatchInfo> matches = new ArrayList<MatchInfo>();
+ List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
+ if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
+ portNo = interfaceManager.getPortForInterface(ifName);
+ matches.add(new MatchInfo(MatchFieldType.in_port, new BigInteger[] {
+ dpnId, BigInteger.valueOf(portNo) }));
+ mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] {VpnConstants.LFIB_TABLE}));
+ }
+
+ BigInteger COOKIE_VM_INGRESS_TABLE = new BigInteger("8000001", 16);
+ FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, VpnConstants.LPORT_INGRESS_TABLE, flowRef,
+ VpnConstants.DEFAULT_FLOW_PRIORITY, flowName, 0, 0, COOKIE_VM_INGRESS_TABLE, matches, mkInstructions);
+
+ if (NwConstants.ADD_FLOW == addOrRemoveFlow) {
+ mdsalManager.installFlow(flowEntity);
+ } else {
+ mdsalManager.removeFlow(flowEntity);
+ }
+ }
+
+ private class InterfaceListener extends AbstractDataChangeListener<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> {
+
+ public InterfaceListener() {
+ super(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class);
+ }
+
+ @Override
+ protected void remove(InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> identifier,
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface del) {
+ LOG.trace("Operational Interface remove event - {}", del);
+ }
+
+ @Override
+ protected void update(InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> identifier,
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface original,
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface update) {
+ LOG.trace("Operation Interface update event - Old: {}, New: {}", original, update);
+ String interfaceName = update.getName();
+ Interface intf = getInterface(interfaceName);
+ if (intf != null && intf.getType().equals(L3tunnel.class)) {
+ BigInteger dpnId = interfaceManager.getDpnForInterface(interfaceName);
+ if(update.getOperStatus().equals(OperStatus.Up)) {
+ //Create ingress to LFIB
+ LOG.debug("Installing Ingress for tunnel interface {}", interfaceName);
+ makeTunnelIngressFlow(dpnId, interfaceName, NwConstants.ADD_FLOW);
+ } else if(update.getOperStatus().equals(OperStatus.Down)) {
+ LOG.debug("Removing Ingress flow for tunnel interface {}", interfaceName);
+ makeTunnelIngressFlow(dpnId, interfaceName, NwConstants.DEL_FLOW);
+ }
+ } else {
+ VpnInterface vpnInterface = getVpnInterface(interfaceName);
+ if(vpnInterface != null) {
+ if(update.getOperStatus().equals(OperStatus.Up)) {
+ LOG.debug("Installing VPN related rules for interface {}", interfaceName);
+ addInterface(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
+ } else if(update.getOperStatus().equals(OperStatus.Down)) {
+ LOG.debug("Removing VPN related rules for interface {}", interfaceName);
+ VpnInterfaceManager.this.remove(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
+ }
+ } else {
+ LOG.debug("No VPN Interface associated with interface {} to handle Update Operation", interfaceName);
+ }
+ }
+ }
+
+ @Override
+ protected void add(InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> identifier,
+ org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface add) {
+ LOG.trace("Operational Interface add event - {}", add);
+ String interfaceName = add.getName();
+ Interface intf = getInterface(interfaceName);
+ if (intf != null && intf.getType().equals(L3tunnel.class)) {
+ BigInteger dpnId = interfaceManager.getDpnForInterface(interfaceName);
+ if(add.getOperStatus().equals(OperStatus.Up)) {
+ //Create ingress to LFIB
+ LOG.debug("Installing Ingress for tunnel interface {}", interfaceName);
+ makeTunnelIngressFlow(dpnId, interfaceName, NwConstants.ADD_FLOW);
+ }
+ } else {
+ VpnInterface vpnInterface = getVpnInterface(interfaceName);
+ if(vpnInterface != null) {
+ if(add.getOperStatus().equals(OperStatus.Up)) {
+ LOG.debug("Installing VPN related rules for interface {}", interfaceName);
+ addInterface(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface);
+ }
+ } else {
+ LOG.debug("No VPN Interface associated with interface {} to handle add Operation", interfaceName);
+ }
+ }
+ }
+ }
}