+ public static java.util.Optional<Long> getLabelForNextHop(final VrfEntry vrfEntry, String nextHopIp) {
+ List<RoutePaths> routePaths = vrfEntry.getRoutePaths();
+ if (routePaths == null || routePaths.isEmpty()) {
+ return java.util.Optional.empty();
+ }
+ return routePaths.stream()
+ .filter(routePath -> routePath.getNexthopAddress().equals(nextHopIp))
+ .findFirst()
+ .map(routePath -> java.util.Optional.of(routePath.getLabel()))
+ .orElse(java.util.Optional.empty());
+ }
+
+ public static InstanceIdentifier<Interface> buildStateInterfaceId(String interfaceName) {
+ InstanceIdentifier.InstanceIdentifierBuilder<Interface> idBuilder =
+ InstanceIdentifier.builder(InterfacesState.class)
+ .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
+ .interfaces.state.Interface.class, new org.opendaylight.yang.gen.v1.urn.ietf
+ .params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state
+ .InterfaceKey(interfaceName));
+ InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
+ .interfaces.state.Interface> id = idBuilder.build();
+ return id;
+ }
+
+ public static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces
+ .state.Interface getInterfaceStateFromOperDS(DataBroker dataBroker, String interfaceName) {
+ InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508
+ .interfaces.state.Interface> ifStateId = buildStateInterfaceId(interfaceName);
+ Optional<Interface> ifStateOptional = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, ifStateId);
+ if (ifStateOptional.isPresent()) {
+ return ifStateOptional.get();
+ }
+
+ return null;
+ }
+
+ public static String getCreateLocalNextHopJobKey(Long vpnId, BigInteger dpnId, String prefix) {
+ return "FIB-" + vpnId.toString() + "-" + dpnId.toString() + "-" + prefix;
+ }
+
+ public static String getJobKeyForRdPrefix(String rd, String prefix) {
+ return "FIB-" + rd + "-" + prefix;
+ }
+
+ public static String getJobKeyForVpnIdDpnId(Long vpnId, BigInteger dpnId) {
+ return "FIB-" + vpnId.toString() + "-" + dpnId.toString() ;
+ }
+
+ public static void updateUsedRdAndVpnToExtraRoute(WriteTransaction writeOperTxn, DataBroker broker,
+ String tunnelIpRemoved, String primaryRd, String prefix) {
+ Optional<VpnInstanceOpDataEntry> optVpnInstance = getVpnInstanceOpData(broker, primaryRd);
+ if (!optVpnInstance.isPresent()) {
+ return;
+ }
+ VpnInstanceOpDataEntry vpnInstance = optVpnInstance.get();
+ String vpnName = vpnInstance.getVpnInstanceName();
+ long vpnId = vpnInstance.getVpnId();
+ List<String> usedRds = VpnExtraRouteHelper.getUsedRds(broker, vpnId, prefix);
+ // To identify the rd to be removed, iterate through the allocated rds for the prefix and check
+ // which rd is allocated for the particular OVS.
+ for (String usedRd : usedRds) {
+ Optional<Routes> vpnExtraRoutes = VpnExtraRouteHelper
+ .getVpnExtraroutes(broker, vpnName, usedRd, prefix);
+ if (vpnExtraRoutes.isPresent()) {
+ // Since all the nexthops under one OVS will be present under one rd, only 1 nexthop is read
+ // to identify the OVS
+ String nextHopRemoved = vpnExtraRoutes.get().getNexthopIpList().get(0);
+ Prefixes prefixToInterface = getPrefixToInterface(broker, vpnId, getIpPrefix(nextHopRemoved));
+ if (prefixToInterface != null && tunnelIpRemoved
+ .equals(getEndpointIpAddressForDPN(broker, prefixToInterface.getDpnId()))) {
+ writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL,
+ getAdjacencyIdentifier(prefixToInterface.getVpnInterfaceName(), prefix));
+ writeOperTxn.delete(LogicalDatastoreType.OPERATIONAL,
+ VpnExtraRouteHelper.getVpnToExtrarouteVrfIdIdentifier(vpnName, usedRd, prefix));
+ writeOperTxn.delete(LogicalDatastoreType.CONFIGURATION,
+ VpnExtraRouteHelper.getUsedRdsIdentifier(vpnId, prefix, nextHopRemoved));
+ break;
+ }
+ }
+ }
+ }
+
+ private static String getEndpointIpAddressForDPN(DataBroker broker, BigInteger dpnId) {
+ //TODO: Move it to a common place for vpn and fib
+ String nextHopIp = null;
+ InstanceIdentifier<DPNTEPsInfo> tunnelInfoId =
+ InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class, new DPNTEPsInfoKey(dpnId)).build();
+ Optional<DPNTEPsInfo> tunnelInfo = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, tunnelInfoId);
+ if (tunnelInfo.isPresent()) {
+ List<TunnelEndPoints> nexthopIpList = tunnelInfo.get().getTunnelEndPoints();
+ if (nexthopIpList != null && !nexthopIpList.isEmpty()) {
+ nextHopIp = String.valueOf(nexthopIpList.get(0).getIpAddress().getValue());
+ }
+ }
+ return nextHopIp;
+ }
+
+ public static String getIpPrefix(String prefix) {
+ String[] prefixValues = prefix.split(FibConstants.PREFIX_SEPARATOR);
+ if (prefixValues.length == 1) {
+ prefix = prefix + NwConstants.IPV4PREFIX;
+ }
+ return prefix;
+ }
+
+ public static boolean isTunnelInterface(AdjacencyResult adjacencyResult) {
+ return Tunnel.class.equals(adjacencyResult.getInterfaceType());
+ }
+
+ public static InstanceIdentifier<VrfEntry> getNextHopIdentifier(String rd, String prefix) {
+ return InstanceIdentifier.builder(FibEntries.class)
+ .child(VrfTables.class,new VrfTablesKey(rd)).child(VrfEntry.class,new VrfEntryKey(prefix)).build();
+ }
+
+ public static List<String> getNextHopAddresses(DataBroker broker, String rd, String prefix) {
+ InstanceIdentifier<VrfEntry> vrfEntryId = getNextHopIdentifier(rd, prefix);
+ Optional<VrfEntry> vrfEntry = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, vrfEntryId);
+ if (vrfEntry.isPresent()) {
+ return FibHelper.getNextHopListFromRoutePaths(vrfEntry.get());
+ } else {
+ return Collections.emptyList();
+ }
+ }
+
+ public static Subnetmap getSubnetMap(DataBroker broker, Uuid subnetId) {
+ InstanceIdentifier<Subnetmap> subnetmapId = InstanceIdentifier.builder(Subnetmaps.class)
+ .child(Subnetmap.class, new SubnetmapKey(subnetId)).build();
+ return MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, subnetmapId).orNull();
+ }
+
+ public static String getGreLbGroupKey(List<String> availableDcGws) {
+ Preconditions.checkNotNull(availableDcGws, "AvailableDcGws is null");
+ return "gre-" + availableDcGws.stream().sorted().collect(joining(":"));
+ }
+
+ public static void updateLbGroupInfo(BigInteger dpnId, String destinationIp, String groupIdKey,
+ String groupId, WriteTransaction tx) {
+ InstanceIdentifier<DpnLbNexthops> id = getDpnLbNexthopsIdentifier(dpnId, destinationIp);
+ DpnLbNexthops dpnToLbNextHop = buildDpnLbNextHops(dpnId, destinationIp, groupIdKey);
+ tx.merge(LogicalDatastoreType.OPERATIONAL, id, dpnToLbNextHop);
+ InstanceIdentifier<Nexthops> nextHopsId = getNextHopsIdentifier(groupIdKey);
+ Nexthops nextHopsToGroupId = buildNextHops(dpnId, groupIdKey, groupId);
+ tx.merge(LogicalDatastoreType.OPERATIONAL, nextHopsId, nextHopsToGroupId);
+ }
+
+ public static void removeDpnIdToNextHopInfo(String destinationIp, BigInteger dpnId, WriteTransaction tx) {
+ InstanceIdentifier<DpnLbNexthops> id = getDpnLbNexthopsIdentifier(dpnId, destinationIp);
+ tx.delete(LogicalDatastoreType.OPERATIONAL, id);
+ }
+
+ public static void removeOrUpdateNextHopInfo(BigInteger dpnId, String nextHopKey, String groupId,
+ Nexthops nexthops, WriteTransaction tx) {
+ InstanceIdentifier<Nexthops> nextHopsId = getNextHopsIdentifier(nextHopKey);
+ List<String> targetDeviceIds = nexthops.getTargetDeviceId();
+ targetDeviceIds.remove(dpnId.toString());
+ if (targetDeviceIds.size() == 0) {
+ tx.delete(LogicalDatastoreType.OPERATIONAL, nextHopsId);