Java tutorial
/* * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.netvirt.bgpmanager; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker; import org.opendaylight.netvirt.fibmanager.api.FibHelper; import org.opendaylight.netvirt.fibmanager.api.RouteOrigin; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentrybase.RoutePaths; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FibDSWriter { private static final Logger LOG = LoggerFactory.getLogger(FibDSWriter.class); private final DataBroker dataBroker; private final SingleTransactionDataBroker singleTxDB; public FibDSWriter(final DataBroker dataBroker) { this.dataBroker = dataBroker; this.singleTxDB = new SingleTransactionDataBroker(dataBroker); } public synchronized void addFibEntryToDS(String rd, String macAddress, String prefix, List<String> nextHopList, VrfEntry.EncapType encapType, int label, long l3vni, String gatewayMacAddress, RouteOrigin origin) { if (rd == null || rd.isEmpty()) { LOG.error("Prefix {} not associated with vpn", prefix); return; } Preconditions.checkNotNull(nextHopList, "NextHopList can't be null"); for (String nextHop : nextHopList) { if (nextHop == null || nextHop.isEmpty()) { LOG.error("nextHop list contains null element"); return; } LOG.debug("Created vrfEntry for {} nexthop {} label {}", prefix, nextHop, label); } // Looking for existing prefix in MDSAL database InstanceIdentifier<VrfEntry> vrfEntryId = InstanceIdentifier.builder(FibEntries.class) .child(VrfTables.class, new VrfTablesKey(rd)).child(VrfEntry.class, new VrfEntryKey(prefix)) .build(); VrfEntryBuilder vrfEntryBuilder = new VrfEntryBuilder().setDestPrefix(prefix).setOrigin(origin.getValue()); buildVpnEncapSpecificInfo(vrfEntryBuilder, encapType, (long) label, l3vni, macAddress, gatewayMacAddress, nextHopList); BgpUtil.update(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfEntryId, vrfEntryBuilder.build()); } private static void buildVpnEncapSpecificInfo(VrfEntryBuilder builder, VrfEntry.EncapType encapType, long label, long l3vni, String macAddress, String gatewayMac, List<String> nextHopList) { if (!encapType.equals(VrfEntry.EncapType.Mplsgre)) { builder.setL3vni(l3vni); } builder.setEncapType(encapType); builder.setGatewayMacAddress(gatewayMac); Long lbl = encapType.equals(VrfEntry.EncapType.Mplsgre) ? label : null; List<RoutePaths> routePaths = nextHopList.stream().filter(nextHop -> nextHop != null && !nextHop.isEmpty()) .map(nextHop -> { return FibHelper.buildRoutePath(nextHop, lbl); }).collect(Collectors.toList()); builder.setRoutePaths(routePaths); } public synchronized void removeFibEntryFromDS(String rd, String prefix) { if (rd == null || rd.isEmpty()) { LOG.error("Prefix {} not associated with vpn", prefix); return; } LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {}", prefix, rd); InstanceIdentifierBuilder<VrfEntry> idBuilder = InstanceIdentifier.builder(FibEntries.class) .child(VrfTables.class, new VrfTablesKey(rd)).child(VrfEntry.class, new VrfEntryKey(prefix)); InstanceIdentifier<VrfEntry> vrfEntryId = idBuilder.build(); BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfEntryId); } public synchronized void removeOrUpdateFibEntryFromDS(String rd, String prefix, String nextHop) { if (rd == null || rd.isEmpty()) { LOG.error("Prefix {} not associated with vpn", prefix); return; } LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {} and nextHop {}", prefix, rd, nextHop); try { InstanceIdentifier<VrfEntry> vrfEntryId = InstanceIdentifier.builder(FibEntries.class) .child(VrfTables.class, new VrfTablesKey(rd)).child(VrfEntry.class, new VrfEntryKey(prefix)) .build(); Optional<VrfEntry> existingVrfEntry = singleTxDB.syncReadOptional(LogicalDatastoreType.CONFIGURATION, vrfEntryId); List<RoutePaths> routePaths = existingVrfEntry.transform(VrfEntry::getRoutePaths) .or(Collections.EMPTY_LIST); if (routePaths.size() == 1) { if (routePaths.get(0).getNexthopAddress().equals(nextHop)) { BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfEntryId); } } else { routePaths.stream().map(routePath -> routePath.getNexthopAddress()) .filter(nextHopAddress -> nextHopAddress.equals(nextHop)).findFirst().ifPresent(nh -> { InstanceIdentifier<RoutePaths> routePathId = FibHelper.buildRoutePathId(rd, prefix, nextHop); BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, routePathId); }); } } catch (ReadFailedException e) { LOG.error("Error while reading vrfEntry for rd {}, prefix {}", rd, prefix); return; } } public synchronized void removeVrfFromDS(String rd) { LOG.debug("Removing vrf table for rd {}", rd); InstanceIdentifierBuilder<VrfTables> idBuilder = InstanceIdentifier.builder(FibEntries.class) .child(VrfTables.class, new VrfTablesKey(rd)); InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build(); BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfTableId); } }