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.vpnservice.fibmanager;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import java.math.BigInteger;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Future;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.vpnmanager.api.IVpnManager;
29 import org.opendaylight.vpnservice.AbstractDataChangeListener;
30 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
31 import org.opendaylight.vpnservice.mdsalutil.ActionType;
32 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
33 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
34 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
35 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
36 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
37 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
38 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
39 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
40 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
41 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
42 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.GetEgressPointerInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.GetEgressPointerOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.L3nexthopService;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.RemoveLocalNextHopInputBuilder;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
56 import org.opendaylight.yangtools.yang.binding.RpcService;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class FibManager extends AbstractDataChangeListener<VrfEntry> implements AutoCloseable{
62 private static final Logger LOG = LoggerFactory.getLogger(FibManager.class);
63 private static final String FLOWID_PREFIX = "L3.";
64 private ListenerRegistration<DataChangeListener> listenerRegistration;
65 private final DataBroker broker;
66 private final L3nexthopService l3nexthopService;
67 private IMdsalApiManager mdsalManager;
68 private IVpnManager vpnmanager;
70 private static final short L3_FIB_TABLE = 21;
71 private static final short L3_LFIB_TABLE = 20;
72 private static final BigInteger COOKIE_VM_LFIB_TABLE = new BigInteger("8000002", 16);
73 private static final BigInteger COOKIE_VM_FIB_TABLE = new BigInteger("8000003", 16);
74 private static final int DEFAULT_FIB_FLOW_PRIORITY = 10;
77 private static final FutureCallback<Void> DEFAULT_CALLBACK =
78 new FutureCallback<Void>() {
79 public void onSuccess(Void result) {
80 LOG.debug("Success in Datastore write operation");
83 public void onFailure(Throwable error) {
84 LOG.error("Error in Datastore write operation", error);
88 public FibManager(final DataBroker db, final RpcService nextHopService) {
89 super(VrfEntry.class);
91 l3nexthopService = (L3nexthopService)nextHopService;
96 public void close() throws Exception {
97 if (listenerRegistration != null) {
99 listenerRegistration.close();
100 } catch (final Exception e) {
101 LOG.error("Error when cleaning up DataChangeListener.", e);
103 listenerRegistration = null;
105 LOG.info("Fib Manager Closed");
109 public void setMdsalManager(IMdsalApiManager mdsalManager) {
110 this.mdsalManager = mdsalManager;
113 public void setVpnmanager(IVpnManager vpnmanager) {
114 this.vpnmanager = vpnmanager;
117 private void registerListener(final DataBroker db) {
119 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
120 getWildCardPath(), FibManager.this, DataChangeScope.SUBTREE);
121 } catch (final Exception e) {
122 LOG.error("FibManager DataChange listener registration fail!", e);
123 throw new IllegalStateException("FibManager registration Listener failed.", e);
127 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
128 InstanceIdentifier<T> path) {
130 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
132 Optional<T> result = Optional.absent();
134 result = tx.read(datastoreType, path).get();
135 } catch (Exception e) {
136 throw new RuntimeException(e);
142 private InstanceIdentifier<VrfEntry> getWildCardPath() {
143 return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class).child(VrfEntry.class);
146 private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
147 InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
148 WriteTransaction tx = broker.newWriteOnlyTransaction();
149 tx.put(datastoreType, path, data, true);
150 Futures.addCallback(tx.submit(), callback);
154 protected void add(final InstanceIdentifier<VrfEntry> identifier,
155 final VrfEntry vrfEntry) {
156 LOG.trace("key: " + identifier + ", value=" + vrfEntry );
157 createFibEntries(identifier, vrfEntry);
161 protected void remove(InstanceIdentifier<VrfEntry> identifier, VrfEntry vrfEntry) {
162 LOG.trace("key: " + identifier + ", value=" + vrfEntry);
163 deleteFibEntries(identifier, vrfEntry);
167 protected void update(InstanceIdentifier<VrfEntry> identifier, VrfEntry original, VrfEntry update) {
168 LOG.trace("key: " + identifier + ", original=" + original + ", update=" + update );
171 private void createFibEntries(final InstanceIdentifier<VrfEntry> identifier,
172 final VrfEntry vrfEntry) {
173 final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
174 Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
175 Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
177 Long vpnId = getVpnId(vrfTableKey.getRouteDistinguisher());
178 Preconditions.checkNotNull(vpnId, "Vpn Instance not available!");
179 Collection<BigInteger> dpns = vpnmanager.getDpnsForVpn(vpnId);
180 for (BigInteger dpId : dpns) {
181 addRouteInternal(dpId, vpnId, vrfTableKey, vrfEntry);
185 private void addRouteInternal(final BigInteger dpId, final long vpnId, final VrfTablesKey vrfTableKey,
186 final VrfEntry vrfEntry) {
187 String rd = vrfTableKey.getRouteDistinguisher();
188 LOG.debug("adding route " + vrfEntry.getDestPrefix() + " " + rd);
190 GetEgressPointerOutput adjacency = resolveAdjacency(dpId, vpnId, vrfEntry);
192 boolean isLocalRoute = false;
193 if(adjacency != null) {
194 groupId = adjacency.getEgressPointer();
195 isLocalRoute = adjacency.isLocalDestination();
198 LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
199 vrfEntry.getNextHopAddress(), rd);
200 LOG.warn("Failed to add Route: {} in vpn: {}",
201 vrfEntry.getDestPrefix(), rd);
205 makeConnectedRoute(dpId, vpnId, vrfEntry, rd, groupId, NwConstants.ADD_FLOW);
208 makeLFibTableEntry(dpId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.ADD_FLOW);
212 "Successfully added fib entry for " + vrfEntry.getDestPrefix() + " vpnId " + vpnId);
215 private void deleteFibEntries(final InstanceIdentifier<VrfEntry> identifier,
216 final VrfEntry vrfEntry) {
217 final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
218 Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
219 Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
221 Long vpnId = getVpnId(vrfTableKey.getRouteDistinguisher());
222 Preconditions.checkNotNull(vpnId, "Vpn Instance not available!");
223 Collection<BigInteger> dpns = vpnmanager.getDpnsForVpn(vpnId);
224 for (BigInteger dpId : dpns) {
225 deleteRoute(dpId, vpnId, vrfTableKey, vrfEntry);
229 public void deleteRoute(final BigInteger dpId, final long vpnId, final VrfTablesKey vrfTableKey,
230 final VrfEntry vrfEntry) {
231 LOG.debug("deleting route "+ vrfEntry.getDestPrefix() + " "+vpnId);
232 String rd = vrfTableKey.getRouteDistinguisher();
233 GetEgressPointerOutput adjacency = resolveAdjacency(dpId, vpnId, vrfEntry);
235 boolean isLocalRoute = false;
236 if(adjacency != null) {
237 groupId = adjacency.getEgressPointer();
238 isLocalRoute = adjacency.isLocalDestination();
241 LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
242 vrfEntry.getNextHopAddress(), rd);
243 LOG.warn("Failed to add Route: {} in vpn: {}",
244 vrfEntry.getDestPrefix(), rd);
248 makeConnectedRoute(dpId, vpnId, vrfEntry, rd, groupId, NwConstants.DEL_FLOW);
251 makeLFibTableEntry(dpId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.DEL_FLOW);
252 deleteLocalAdjacency(dpId, vpnId, vrfEntry);
255 LOG.debug("Successfully delete fib entry for "+ vrfEntry.getDestPrefix() + " vpnId "+vpnId);
258 private long getIpAddress(byte[] rawIpAddress) {
259 return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
260 + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
263 private void makeConnectedRoute(BigInteger dpId, long vpnId, VrfEntry vrfEntry, String rd,
264 long groupId, int addOrRemove) {
265 LOG.trace("makeConnectedRoute: vrfEntry {}",vrfEntry);
266 String values[] = vrfEntry.getDestPrefix().split("/");
267 String ipAddress = values[0];
268 int prefixLength = (values.length == 1) ? 32 : Integer.parseInt(values[1]);
269 LOG.debug("Adding route to DPN. ip {} masklen {}", ipAddress, prefixLength);
270 InetAddress destPrefix = null;
272 destPrefix = InetAddress.getByName(ipAddress);
273 } catch (UnknownHostException e) {
274 LOG.error("UnknowHostException in addRoute. Failed to add Route for ipPrefix {}", vrfEntry.getDestPrefix());
278 List<MatchInfo> matches = new ArrayList<MatchInfo>();
280 matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
281 BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
283 matches.add(new MatchInfo(MatchFieldType.eth_type,
284 new long[] { 0x0800L }));
286 if(prefixLength != 0) {
287 matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
288 getIpAddress(destPrefix.getAddress()), prefixLength }));
291 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
292 List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
294 if(addOrRemove == NwConstants.ADD_FLOW) {
295 actionsInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
296 actionsInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
297 actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId)}));
298 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
301 String flowRef = getFlowRef(dpId, L3_FIB_TABLE, rd, destPrefix);
303 FlowEntity flowEntity;
305 int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
306 flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_FIB_TABLE, flowRef,
307 priority, flowRef, 0, 0,
308 COOKIE_VM_FIB_TABLE, matches, instructions);
310 if (addOrRemove == NwConstants.ADD_FLOW) {
311 mdsalManager.installFlow(flowEntity);
313 mdsalManager.removeFlow(flowEntity);
317 private void makeLFibTableEntry(BigInteger dpId, long label, long groupId,
318 String nextHop, int addOrRemove) {
319 List<MatchInfo> matches = new ArrayList<MatchInfo>();
320 matches.add(new MatchInfo(MatchFieldType.eth_type,
321 new long[] { 0x8847L }));
322 matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(label)}));
324 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
325 List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
326 actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
327 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
329 // Install the flow entry in L3_LFIB_TABLE
330 String flowRef = getFlowRef(dpId, L3_LFIB_TABLE, label, nextHop);
332 FlowEntity flowEntity;
333 flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_LFIB_TABLE, flowRef,
334 DEFAULT_FIB_FLOW_PRIORITY, flowRef, 0, 0,
335 COOKIE_VM_LFIB_TABLE, matches, instructions);
337 if (addOrRemove == NwConstants.ADD_FLOW) {
338 mdsalManager.installFlow(flowEntity);
340 mdsalManager.removeFlow(flowEntity);
342 LOG.debug("LFIB Entry for dpID {} : label : {} group {} modified successfully {}",dpId, label, groupId );
345 private void deleteLocalAdjacency(final BigInteger dpId, final long vpnId, final VrfEntry vrfEntry) {
346 LOG.trace("deleteLocalAdjacency called with dpid {}, vpnId{}, VrfEntry {}",dpId, vpnId, vrfEntry);;
348 Future<RpcResult<Void>> result =
349 l3nexthopService.removeLocalNextHop(new RemoveLocalNextHopInputBuilder().setDpnId(dpId)
350 .setIpPrefix(vrfEntry.getDestPrefix())
351 .setNexthopIp(vrfEntry.getNextHopAddress())
354 RpcResult<Void> rpcResult = result.get();
355 if (rpcResult.isSuccessful()) {
356 LOG.debug("Local Next hop for {} on dpn {} successfully deleted", vrfEntry.getDestPrefix(), dpId);
358 LOG.error("Local Next hop for {} on dpn {} not deleted", vrfEntry.getDestPrefix(), dpId);
360 } catch (NullPointerException | InterruptedException | ExecutionException e) {
365 public void populateFibOnNewDpn(BigInteger dpnId, long vpnId, String rd) {
366 LOG.trace("New dpn {} for vpn {} : populateFibOnNewDpn", dpnId, rd);
367 InstanceIdentifier<VrfTables> id = buildVrfId(rd);
368 Optional<VrfTables> vrfTable = read(LogicalDatastoreType.CONFIGURATION, id);
369 if(vrfTable.isPresent()) {
370 for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
371 addRouteInternal(dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
376 public void cleanUpDpnForVpn(BigInteger dpnId, long vpnId, String rd) {
377 LOG.trace("Remove dpn {} for vpn {} : cleanUpDpnForVpn", dpnId, rd);
378 InstanceIdentifier<VrfTables> id = buildVrfId(rd);
379 Optional<VrfTables> vrfTable = read(LogicalDatastoreType.CONFIGURATION, id);
380 if(vrfTable.isPresent()) {
381 for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
382 deleteRoute(dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
387 public static InstanceIdentifier<VrfTables> buildVrfId(String rd) {
388 InstanceIdentifierBuilder<VrfTables> idBuilder =
389 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
390 InstanceIdentifier<VrfTables> id = idBuilder.build();
394 private String getFlowRef(BigInteger dpnId, short tableId, long label, String nextHop) {
395 return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
396 .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
397 .append(label).append(NwConstants.FLOWID_SEPARATOR)
398 .append(nextHop).toString();
401 private String getFlowRef(BigInteger dpnId, short tableId, String rd, InetAddress destPrefix) {
402 return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
403 .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
404 .append(rd).append(NwConstants.FLOWID_SEPARATOR)
405 .append(destPrefix.getHostAddress()).toString();
408 protected GetEgressPointerOutput resolveAdjacency(final BigInteger dpId, final long vpnId,
409 final VrfEntry vrfEntry) {
410 GetEgressPointerOutput adjacency = null;
411 LOG.trace("resolveAdjacency called with dpid {}, vpnId{}, VrfEntry {}",dpId, vpnId, vrfEntry);;
413 Future<RpcResult<GetEgressPointerOutput>> result =
414 l3nexthopService.getEgressPointer(new GetEgressPointerInputBuilder().setDpnId(dpId)
415 .setIpPrefix(vrfEntry.getDestPrefix())
416 .setNexthopIp(vrfEntry.getNextHopAddress())
419 RpcResult<GetEgressPointerOutput> rpcResult = result.get();
420 if (rpcResult.isSuccessful()) {
421 adjacency = rpcResult.getResult();
423 LOG.error("Next hop information not available");
425 } catch (NullPointerException | InterruptedException | ExecutionException e) {
431 protected Long getVpnId(String rd) {
433 InstanceIdentifier<VpnInstances> id = InstanceIdentifier.create(VpnInstances.class);
434 Optional<VpnInstances> vpnInstances = read(LogicalDatastoreType.OPERATIONAL, id);
435 if(vpnInstances.isPresent()) {
436 List<VpnInstance> vpns = vpnInstances.get().getVpnInstance();
437 for(VpnInstance vpn : vpns) {
438 if(vpn.getIpv4Family().getRouteDistinguisher().equals(rd)) {
439 VpnInstance1 vpnInstanceId = vpn.getAugmentation(VpnInstance1.class);
440 if (vpnInstanceId != null) {
441 vpnId = vpnInstanceId.getVpnId();
450 public void processNodeAdd(BigInteger dpnId) {
451 LOG.debug("Received notification to install TableMiss entries for dpn {} ", dpnId);
452 makeTableMissFlow(dpnId, NwConstants.ADD_FLOW);
455 private void makeTableMissFlow(BigInteger dpnId, int addOrRemove) {
456 final BigInteger COOKIE_TABLE_MISS = new BigInteger("1030000", 16);
457 // Instruction to punt to controller
458 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
459 List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
460 actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
461 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
462 List<MatchInfo> matches = new ArrayList<MatchInfo>();
463 FlowEntity flowEntityLfib = MDSALUtil.buildFlowEntity(dpnId, L3_LFIB_TABLE,
464 getFlowRef(dpnId, L3_LFIB_TABLE, NwConstants.TABLE_MISS_FLOW),
465 NwConstants.TABLE_MISS_PRIORITY, "Table Miss", 0, 0, COOKIE_TABLE_MISS, matches, instructions);
467 FlowEntity flowEntityFib = MDSALUtil.buildFlowEntity(dpnId,L3_FIB_TABLE, getFlowRef(dpnId, L3_FIB_TABLE, NwConstants.TABLE_MISS_FLOW),
468 NwConstants.TABLE_MISS_PRIORITY, "FIB Table Miss Flow", 0, 0, COOKIE_VM_FIB_TABLE,
469 matches, instructions);
471 if (addOrRemove == NwConstants.ADD_FLOW) {
472 LOG.debug("Invoking MDSAL to install Table Miss Entries");
473 mdsalManager.installFlow(flowEntityLfib);
474 mdsalManager.installFlow(flowEntityFib);
476 mdsalManager.removeFlow(flowEntityLfib);
477 mdsalManager.removeFlow(flowEntityFib);
482 private String getFlowRef(BigInteger dpnId, short tableId, int tableMiss) {
483 return new StringBuffer().append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
484 .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tableMiss)
485 .append(FLOWID_PREFIX).toString();