2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.bgpmanager;
10 import com.google.common.base.Preconditions;
11 import java.util.List;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.netvirt.fibmanager.api.RouteOrigin;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class FibDSWriter {
27 private static final Logger LOG = LoggerFactory.getLogger(FibDSWriter.class);
28 private final DataBroker dataBroker;
30 public FibDSWriter(final DataBroker dataBroker) {
31 this.dataBroker = dataBroker;
34 public synchronized void addFibEntryToDS(String rd, String macAddress, String prefix, List<String> nextHopList,
35 VrfEntry.EncapType encapType, int label, long l3vni,
36 String gatewayMacAddress, RouteOrigin origin) {
37 if (rd == null || rd.isEmpty()) {
38 LOG.error("Prefix {} not associated with vpn", prefix);
42 Preconditions.checkNotNull(nextHopList, "NextHopList can't be null");
44 for (String nextHop : nextHopList) {
45 if (nextHop == null || nextHop.isEmpty()) {
46 LOG.error("nextHop list contains null element");
49 LOG.debug("Created vrfEntry for {} nexthop {} label {}", prefix, nextHop, label);
53 // Looking for existing prefix in MDSAL database
54 InstanceIdentifier<VrfEntry> vrfEntryId =
55 InstanceIdentifier.builder(FibEntries.class)
56 .child(VrfTables.class, new VrfTablesKey(rd))
57 .child(VrfEntry.class, new VrfEntryKey(prefix)).build();
59 VrfEntryBuilder vrfEntryBuilder = new VrfEntryBuilder().setDestPrefix(prefix)
60 .setNextHopAddressList(nextHopList).setLabel((long)label).setOrigin(origin.getValue());
61 buildVpnEncapSpecificInfo(vrfEntryBuilder, encapType, (long)label, l3vni, macAddress, gatewayMacAddress);
62 BgpUtil.write(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfEntryId, vrfEntryBuilder.build());
65 private static void buildVpnEncapSpecificInfo(VrfEntryBuilder builder, VrfEntry.EncapType encapType, long label,
66 long l3vni, String macAddress, String gatewayMac) {
67 if (encapType.equals(VrfEntry.EncapType.Mplsgre)) {
68 builder.setLabel(label);
70 builder.setL3vni(l3vni).setGatewayMacAddress(gatewayMac);
72 builder.setEncapType(encapType);
75 public synchronized void removeFibEntryFromDS(String rd, String prefix) {
77 if (rd == null || rd.isEmpty()) {
78 LOG.error("Prefix {} not associated with vpn", prefix);
81 LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {}", prefix, rd);
83 InstanceIdentifierBuilder<VrfEntry> idBuilder =
84 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd)).child(
85 VrfEntry.class, new VrfEntryKey(prefix));
86 InstanceIdentifier<VrfEntry> vrfEntryId = idBuilder.build();
87 BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfEntryId);
91 public synchronized void removeVrfFromDS(String rd) {
92 LOG.debug("Removing vrf table for rd {}", rd);
94 InstanceIdentifierBuilder<VrfTables> idBuilder =
95 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
96 InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
98 BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfTableId);