+ protected void add(final InstanceIdentifier<VrfEntry> identifier,
+ final VrfEntry vrfEntry) {
+ LOG.trace("key: " + identifier + ", value=" + vrfEntry );
+ createFibEntries(identifier, vrfEntry);
+ }
+
+ @Override
+ protected void remove(InstanceIdentifier<VrfEntry> identifier, VrfEntry vrfEntry) {
+ LOG.trace("key: " + identifier + ", value=" + vrfEntry);
+ deleteFibEntries(identifier, vrfEntry);
+ }
+
+ @Override
+ protected void update(InstanceIdentifier<VrfEntry> identifier, VrfEntry original, VrfEntry update) {
+ LOG.trace("key: " + identifier + ", original=" + original + ", update=" + update );
+ createFibEntries(identifier, update);
+ }
+
+ private void createFibEntries(final InstanceIdentifier<VrfEntry> identifier,
+ final VrfEntry vrfEntry) {
+ final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
+ Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
+ Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
+
+ VpnInstanceOpDataEntry vpnInstance = getVpnInstance(vrfTableKey.getRouteDistinguisher());
+ Preconditions.checkNotNull(vpnInstance, "Vpn Instance not available!");
+ Preconditions.checkNotNull(vpnInstance.getVpnId(), "Vpn Instance with rd " + vpnInstance.getVrfId() + "has null vpnId!");
+
+ Collection<VpnToDpnList> vpnToDpnList = vpnInstance.getVpnToDpnList();
+ BigInteger localDpnId = createLocalFibEntry(vpnInstance.getVpnId(),
+ vrfTableKey.getRouteDistinguisher(), vrfEntry);
+ if (vpnToDpnList != null) {
+ for (VpnToDpnList curDpn : vpnToDpnList) {
+ if (!curDpn.getDpnId().equals(localDpnId)) {
+ createRemoteFibEntry(localDpnId, curDpn.getDpnId(), vpnInstance.getVpnId(),
+ vrfTableKey, vrfEntry);
+ }
+ }
+ }
+ }
+
+ public BigInteger createLocalFibEntry(Long vpnId, String rd, VrfEntry vrfEntry) {
+ BigInteger localDpnId = BigInteger.ZERO;
+ Prefixes localNextHopInfo = getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
+ String localNextHopIP = vrfEntry.getDestPrefix();
+
+ if(localNextHopInfo == null) {
+ //Is this fib route an extra route? If yes, get the nexthop which would be an adjacency in the vpn
+ Extraroute extra_route = getVpnToExtraroute(rd, vrfEntry.getDestPrefix());
+ if (extra_route != null) {
+ localNextHopInfo = getPrefixToInterface(vpnId, extra_route.getNexthopIp() + "/32");
+ localNextHopIP = extra_route.getNexthopIp() + "/32";
+ }
+ }
+
+ if(localNextHopInfo != null) {
+ localDpnId = localNextHopInfo.getDpnId();
+ long groupId = nextHopManager.createLocalNextHop(vpnId, localDpnId, localNextHopInfo.getVpnInterfaceName(), localNextHopIP);
+ List<ActionInfo> actionInfos = new ArrayList<ActionInfo>();
+
+ actionInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId)}));
+
+ makeConnectedRoute(localDpnId, vpnId, vrfEntry, rd, actionInfos, NwConstants.ADD_FLOW);
+ makeLFibTableEntry(localDpnId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.ADD_FLOW);
+
+ LOG.debug("Installing tunnel table entry on dpn {} for interface {} with label {}",
+ localDpnId, localNextHopInfo.getVpnInterfaceName(), vrfEntry.getLabel());
+ makeTunnelTableEntry(localDpnId, vrfEntry.getLabel(), groupId);
+
+ }
+ return localDpnId;
+ }
+
+ private void makeTunnelTableEntry(BigInteger dpId, long label, long groupId/*String egressInterfaceName*/) {
+ List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+ actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
+
+
+ createTerminatingServiceActions(dpId, (int)label, actionsInfos);
+
+ LOG.debug("Terminating service Entry for dpID {} : label : {} egress : {} installed successfully {}",
+ dpId, label, groupId);
+ }
+
+ public void createTerminatingServiceActions( BigInteger destDpId, int label, List<ActionInfo> actionsInfos) {
+ List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
+
+ LOG.info("create terminatingServiceAction on DpnId = {} and serviceId = {} and actions = {}", destDpId , label,actionsInfos);
+
+ // Matching metadata
+ // FIXME vxlan vni bit set is not working properly with OVS.need to revisit
+ mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {BigInteger.valueOf(label)}));
+
+ List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
+ mkInstructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+
+ FlowEntity terminatingServiceTableFlowEntity = MDSALUtil.buildFlowEntity(destDpId, NwConstants.INTERNAL_TUNNEL_TABLE,
+ getFlowRef(destDpId, NwConstants.INTERNAL_TUNNEL_TABLE,label), 5, String.format("%s:%d","TST Flow Entry ",label),
+ 0, 0, COOKIE_TUNNEL.add(BigInteger.valueOf(label)),mkMatches, mkInstructions);
+
+ mdsalManager.installFlow(terminatingServiceTableFlowEntity);
+ }
+
+ private void removeTunnelTableEntry(BigInteger dpId, long label) {
+ FlowEntity flowEntity;
+ LOG.info("remove terminatingServiceActions called with DpnId = {} and label = {}", dpId , label);
+ List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
+ // Matching metadata
+ mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {BigInteger.valueOf(label)}));
+ flowEntity = MDSALUtil.buildFlowEntity(dpId,
+ NwConstants.INTERNAL_TUNNEL_TABLE,
+ getFlowRef(dpId, NwConstants.INTERNAL_TUNNEL_TABLE, (int)label),
+ 5, String.format("%s:%d","TST Flow Entry ",label), 0, 0,
+ COOKIE_TUNNEL.add(BigInteger.valueOf(label)), mkMatches, null);
+ mdsalManager.removeFlow(flowEntity);
+ LOG.debug("Terminating service Entry for dpID {} : label : {} removed successfully {}",dpId, label);
+ }
+
+ public BigInteger deleteLocalFibEntry(Long vpnId, String rd, VrfEntry vrfEntry) {
+ BigInteger localDpnId = BigInteger.ZERO;
+ boolean isExtraRoute = false;
+ VpnNexthop localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, vrfEntry.getDestPrefix());
+ String localNextHopIP = vrfEntry.getDestPrefix();
+
+ if(localNextHopInfo == null) {
+ //Is this fib route an extra route? If yes, get the nexthop which would be an adjacency in the vpn
+ Extraroute extra_route = getVpnToExtraroute(rd, vrfEntry.getDestPrefix());
+ if (extra_route != null) {
+ localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, extra_route.getNexthopIp() + "/32");
+ localNextHopIP = extra_route.getNexthopIp() + "/32";
+ isExtraRoute = true;
+ }
+ }
+
+
+ if(localNextHopInfo != null) {
+ localDpnId = localNextHopInfo.getDpnId();
+ Prefixes prefix = getPrefixToInterface(vpnId, isExtraRoute ? localNextHopIP : vrfEntry.getDestPrefix());
+ makeConnectedRoute(localDpnId, vpnId, vrfEntry, rd, null /* invalid */,
+ NwConstants.DEL_FLOW);
+ makeLFibTableEntry(localDpnId, vrfEntry.getLabel(), 0 /* invalid */,
+ vrfEntry.getNextHopAddress(), NwConstants.DEL_FLOW);
+ removeTunnelTableEntry(localDpnId, vrfEntry.getLabel());
+ deleteLocalAdjacency(localDpnId, vpnId, localNextHopIP);
+ }
+ return localDpnId;
+ }
+
+ private InstanceIdentifier<Prefixes> getPrefixToInterfaceIdentifier(Long vpnId, String ipPrefix) {
+ return InstanceIdentifier.builder(PrefixToInterface.class)
+ .child(VpnIds.class, new VpnIdsKey(vpnId)).child(Prefixes.class, new PrefixesKey(ipPrefix)).build();
+ }
+
+ private Prefixes getPrefixToInterface(Long vpnId, String ipPrefix) {
+ Optional<Prefixes> localNextHopInfoData =
+ FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, getPrefixToInterfaceIdentifier(vpnId, ipPrefix));
+ return localNextHopInfoData.isPresent() ? localNextHopInfoData.get() : null;
+ }
+
+ private InstanceIdentifier<Extraroute> getVpnToExtrarouteIdentifier(String vrfId, String ipPrefix) {
+ return InstanceIdentifier.builder(VpnToExtraroute.class)
+ .child(Vpn.class, new VpnKey(vrfId)).child(Extraroute.class,
+ new ExtrarouteKey(ipPrefix)).build();
+ }
+
+ private Extraroute getVpnToExtraroute(String rd, String ipPrefix) {
+ Optional<Extraroute> extraRouteInfo =
+ FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, getVpnToExtrarouteIdentifier(rd, ipPrefix));
+ return extraRouteInfo.isPresent() ? extraRouteInfo.get() : null;
+
+ }
+
+ private Class<? extends TunnelTypeBase> getTunnelType(String ifName) {
+ try {
+ Future<RpcResult<GetTunnelTypeOutput>> result = interfaceManager.getTunnelType(
+ new GetTunnelTypeInputBuilder().setIntfName(ifName).build());
+ RpcResult<GetTunnelTypeOutput> rpcResult = result.get();
+ if(!rpcResult.isSuccessful()) {
+ LOG.warn("RPC Call to getTunnelInterfaceId returned with Errors {}", rpcResult.getErrors());
+ } else {
+ return rpcResult.getResult().getTunnelType();
+ }
+
+ } catch (InterruptedException | ExecutionException e) {
+ LOG.warn("Exception when getting tunnel interface Id for tunnel type {}", e);
+ }
+
+ return null;
+
+ }
+ private void createRemoteFibEntry(final BigInteger localDpnId, final BigInteger remoteDpnId,
+ final long vpnId, final VrfTablesKey vrfTableKey,
+ final VrfEntry vrfEntry) {
+ String rd = vrfTableKey.getRouteDistinguisher();
+ LOG.debug("adding route " + vrfEntry.getDestPrefix() + " " + rd);
+ /********************************************/
+ String tunnelInterface = resolveAdjacency(localDpnId, remoteDpnId, vpnId, vrfEntry, rd);
+ if(tunnelInterface == null) {
+ LOG.error("Could not get interface for nexthop: {} in vpn {}",
+ vrfEntry.getNextHopAddress(), rd);
+ LOG.warn("Failed to add Route: {} in vpn: {}",
+ vrfEntry.getDestPrefix(), rd);
+ return;
+ }
+ List<ActionInfo> actionInfos = new ArrayList<>();
+ Class<? extends TunnelTypeBase> tunnel_type = getTunnelType(tunnelInterface);
+ if (tunnel_type.equals(TunnelTypeMplsOverGre.class)) {
+ LOG.debug("Push label action for prefix {}", vrfEntry.getDestPrefix());
+ actionInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
+ actionInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
+ } else {
+ int label = vrfEntry.getLabel().intValue();
+ BigInteger tunnelId;
+ // FIXME vxlan vni bit set is not working properly with OVS.need to revisit
+ if(tunnel_type.equals(TunnelTypeVxlan.class)) {
+ tunnelId = BigInteger.valueOf(label);
+ } else {
+ tunnelId = BigInteger.valueOf(label);
+ }
+ LOG.debug("adding set tunnel id action for label {}", label);
+ actionInfos.add(new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[]{
+ tunnelId}));
+ }
+ actionInfos.addAll(nextHopManager.getEgressActionsForInterface(tunnelInterface));
+/*
+ List<ActionInfo> actionInfos = resolveAdjacency(localDpnId, remoteDpnId, vpnId, vrfEntry);
+ if(actionInfos == null) {
+ LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
+ vrfEntry.getNextHopAddress(), rd);
+ LOG.warn("Failed to add Route: {} in vpn: {}",
+ vrfEntry.getDestPrefix(), rd);
+ return;
+ }
+ BigInteger dpnId = nextHopManager.getDpnForPrefix(vpnId, vrfEntry.getDestPrefix());
+ if(dpnId == null) {
+ //This route may be extra route... try to query with nexthop Ip
+ LOG.debug("Checking for extra route to install remote fib entry {}", vrfEntry.getDestPrefix());
+ dpnId = nextHopManager.getDpnForPrefix(vpnId, vrfEntry.getNextHopAddress() + "/32");
+ }
+ if(dpnId == null) {
+ LOG.debug("Push label action for prefix {}", vrfEntry.getDestPrefix());
+ actionInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
+ actionInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
+ } else {
+ int label = vrfEntry.getLabel().intValue();
+ LOG.debug("adding set tunnel id action for label {}", label);
+ actionInfos.add(new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {
+ MetaDataUtil.getTunnelIdWithValidVniBitAndVniSet(label),
+ MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID }));
+ }
+**/
+ makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, actionInfos, NwConstants.ADD_FLOW);
+ LOG.debug(
+ "Successfully added fib entry for " + vrfEntry.getDestPrefix() + " vpnId " + vpnId);
+ }
+
+ private void delIntfFromDpnToVpnList(long vpnId, BigInteger dpnId, String intfName, String rd) {
+ InstanceIdentifier<VpnToDpnList> id = FibUtil.getVpnToDpnListIdentifier(rd, dpnId);
+ Optional<VpnToDpnList> dpnInVpn = FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
+ if (dpnInVpn.isPresent()) {
+ List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+ .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
+ org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces
+ currVpnInterface = new org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesBuilder().setInterfaceName(intfName).build();
+
+ if (vpnInterfaces.remove(currVpnInterface)) {
+ if (vpnInterfaces.isEmpty()) {
+ LOG.trace("Last vpn interface {} on dpn {} for vpn {}. Clean up fib in dpn", intfName, dpnId, rd);
+ FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id);
+ cleanUpDpnForVpn(dpnId, vpnId, rd);
+ } else {
+ LOG.trace("Delete vpn interface {} from dpn {} to vpn {} list.", intfName, dpnId, rd);
+ FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id.child(
+ org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
+ .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
+ new org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesKey(intfName)));
+ }
+ }
+ }
+ }
+
+ private void cleanUpOpDataForFib(Long vpnId, String rd, final VrfEntry vrfEntry) {
+ /* Get interface info from prefix to interface mapping;
+ Use the interface info to get the corresponding vpn interface op DS entry,
+ remove the adjacency corresponding to this fib entry.
+ If adjacency removed is the last adjacency, clean up the following:
+ - vpn interface from dpntovpn list, dpn if last vpn interface on dpn
+ - prefix to interface entry
+ - vpn interface op DS
+ */
+ Prefixes prefixInfo = getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
+ Extraroute extraRoute = null;
+ if (prefixInfo == null) {
+ extraRoute = getVpnToExtraroute(rd, vrfEntry.getDestPrefix());
+ if(extraRoute != null) {
+ prefixInfo = getPrefixToInterface(vpnId, extraRoute.getNexthopIp() + "/32");
+ //clean up the vpn to extra route entry in DS
+ FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, FibUtil.getVpnToExtrarouteIdentifier(rd, vrfEntry.getDestPrefix()));
+ }
+ }
+ if (prefixInfo == null) {
+ LOG.debug("Cleanup VPN Data Failed as unable to find prefix Info for " + vrfEntry.getDestPrefix());
+ return; //Don't have any info for this prefix (shouldn't happen); need to return
+ }
+ String ifName = prefixInfo.getVpnInterfaceName();
+ Optional<Adjacencies> optAdjacencies = FibUtil.read(broker, LogicalDatastoreType.OPERATIONAL, FibUtil.getAdjListPath(ifName));
+ int numAdj = 0;
+ if (optAdjacencies.isPresent()) {
+ numAdj = optAdjacencies.get().getAdjacency().size();
+ }
+ LOG.trace("cleanUpOpDataForFib: remove adjacency for prefix: {} {}", vpnId, vrfEntry.getDestPrefix());
+ //remove adjacency corr to prefix
+ if (numAdj > 1) {
+ FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
+ FibUtil.getAdjacencyIdentifier(ifName, vrfEntry.getDestPrefix()));
+ }
+
+ if ((numAdj - 1) == 0) { //there are no adjacencies left for this vpn interface, clean up
+ //clean up the vpn interface from DpnToVpn list
+ LOG.trace("Clean up vpn interface {} from dpn {} to vpn {} list.", ifName, prefixInfo.getDpnId(), rd);
+ FibUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
+ FibUtil.getVpnInterfaceIdentifier(ifName));
+ }
+ }
+
+ private void deleteFibEntries(final InstanceIdentifier<VrfEntry> identifier,
+ final VrfEntry vrfEntry) {
+ final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
+ Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
+ Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
+
+ VpnInstanceOpDataEntry vpnInstance = getVpnInstance(vrfTableKey.getRouteDistinguisher());
+ Preconditions.checkNotNull(vpnInstance, "Vpn Instance not available!");
+ Collection<VpnToDpnList> vpnToDpnList = vpnInstance.getVpnToDpnList();
+ BigInteger localDpnId = deleteLocalFibEntry(vpnInstance.getVpnId(),
+ vrfTableKey.getRouteDistinguisher(), vrfEntry);
+ if (vpnToDpnList != null) {
+ for (VpnToDpnList curDpn : vpnToDpnList) {
+ if (!curDpn.getDpnId().equals(localDpnId)) {
+ deleteRemoteRoute(localDpnId, curDpn.getDpnId(), vpnInstance.getVpnId(), vrfTableKey, vrfEntry);
+ }
+ }
+ }
+ //The flow/group entry has been deleted from config DS; need to clean up associated operational
+ //DS entries in VPN Op DS, VpnInstanceOpData and PrefixToInterface to complete deletion
+ cleanUpOpDataForFib(vpnInstance.getVpnId(), vrfTableKey.getRouteDistinguisher(), vrfEntry);