22c18830552a4123dd8f4d7b5418b30a6fb0f376
[netvirt.git] / vpnservice / bgpmanager / bgpmanager-impl / src / main / java / org / opendaylight / netvirt / bgpmanager / FibDSWriter.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.bgpmanager;
9
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;
25
26 public class FibDSWriter {
27     private static final Logger LOG = LoggerFactory.getLogger(FibDSWriter.class);
28     private final DataBroker dataBroker;
29
30     public FibDSWriter(final DataBroker dataBroker) {
31         this.dataBroker = dataBroker;
32     }
33
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);
39             return;
40         }
41
42         Preconditions.checkNotNull(nextHopList, "NextHopList can't be null");
43
44         for (String nextHop : nextHopList) {
45             if (nextHop == null || nextHop.isEmpty()) {
46                 LOG.error("nextHop list contains null element");
47                 return;
48             }
49             LOG.debug("Created vrfEntry for {} nexthop {} label {}", prefix, nextHop, label);
50
51         }
52
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();
58
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());
63     }
64
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);
69         } else {
70             builder.setL3vni(l3vni).setGatewayMacAddress(gatewayMac);
71         }
72         builder.setEncapType(encapType);
73     }
74
75     public synchronized void removeFibEntryFromDS(String rd, String prefix) {
76
77         if (rd == null || rd.isEmpty()) {
78             LOG.error("Prefix {} not associated with vpn", prefix);
79             return;
80         }
81         LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {}", prefix, rd);
82
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);
88
89     }
90
91     public synchronized void removeVrfFromDS(String rd) {
92         LOG.debug("Removing vrf table for  rd {}", rd);
93
94         InstanceIdentifierBuilder<VrfTables> idBuilder =
95                 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
96         InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
97
98         BgpUtil.delete(dataBroker, LogicalDatastoreType.CONFIGURATION, vrfTableId);
99
100     }
101 }