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.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
13 import com.google.common.util.concurrent.FutureCallback;
15 import org.opendaylight.vpnmanager.api.IVpnManager;
16 import org.opendaylight.vpnservice.AbstractDataChangeListener;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
20 import org.opendaylight.vpnservice.mdsalutil.ActionType;
21 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
22 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
23 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
24 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
25 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
26 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
27 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
28 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
29 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.GetEgressPointerInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.GetEgressPointerOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.L3nexthopService;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
43 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
44 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
45 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
47 import org.opendaylight.yangtools.yang.binding.RpcService;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import com.google.common.base.Optional;
53 import java.math.BigInteger;
54 import java.net.InetAddress;
55 import java.net.UnknownHostException;
56 import java.util.ArrayList;
57 import java.util.Collection;
58 import java.util.List;
60 import java.util.concurrent.ExecutionException;
61 import java.util.concurrent.Future;
63 public class FibManager extends AbstractDataChangeListener<VrfEntry> implements AutoCloseable{
64 private static final Logger LOG = LoggerFactory.getLogger(FibManager.class);
65 private static final String FLOWID_PREFIX = "L3.";
66 private ListenerRegistration<DataChangeListener> listenerRegistration;
67 private final DataBroker broker;
68 private final L3nexthopService l3nexthopService;
69 private IMdsalApiManager mdsalManager;
70 private IVpnManager vpnmanager;
72 private static final short L3_FIB_TABLE = 20;
73 private static final short L3_LFIB_TABLE = 21;
74 private static final BigInteger COOKIE_VM_LFIB_TABLE = new BigInteger("8000002", 16);
75 private static final BigInteger COOKIE_VM_FIB_TABLE = new BigInteger("8000003", 16);
76 private static final int DEFAULT_FIB_FLOW_PRIORITY = 10;
79 private static final FutureCallback<Void> DEFAULT_CALLBACK =
80 new FutureCallback<Void>() {
81 public void onSuccess(Void result) {
82 LOG.debug("Success in Datastore write operation");
85 public void onFailure(Throwable error) {
86 LOG.error("Error in Datastore write operation", error);
90 public FibManager(final DataBroker db, final RpcService nextHopService) {
91 super(VrfEntry.class);
93 l3nexthopService = (L3nexthopService)nextHopService;
98 public void close() throws Exception {
99 if (listenerRegistration != null) {
101 listenerRegistration.close();
102 } catch (final Exception e) {
103 LOG.error("Error when cleaning up DataChangeListener.", e);
105 listenerRegistration = null;
107 LOG.info("Fib Manager Closed");
111 public void setMdsalManager(IMdsalApiManager mdsalManager) {
112 this.mdsalManager = mdsalManager;
115 public void setVpnmanager(IVpnManager vpnmanager) {
116 this.vpnmanager = vpnmanager;
119 private void registerListener(final DataBroker db) {
121 listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
122 getWildCardPath(), FibManager.this, DataChangeScope.SUBTREE);
123 } catch (final Exception e) {
124 LOG.error("FibManager DataChange listener registration fail!", e);
125 throw new IllegalStateException("FibManager registration Listener failed.", e);
129 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
130 InstanceIdentifier<T> path) {
132 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
134 Optional<T> result = Optional.absent();
136 result = tx.read(datastoreType, path).get();
137 } catch (Exception e) {
138 throw new RuntimeException(e);
144 private InstanceIdentifier<VrfEntry> getWildCardPath() {
145 return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class).child(VrfEntry.class);
148 private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
149 InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
150 WriteTransaction tx = broker.newWriteOnlyTransaction();
151 tx.put(datastoreType, path, data, true);
152 Futures.addCallback(tx.submit(), callback);
156 protected void add(final InstanceIdentifier<VrfEntry> identifier,
157 final VrfEntry vrfEntry) {
158 LOG.trace("key: " + identifier + ", value=" + vrfEntry );
159 createFibEntries(identifier, vrfEntry);
163 protected void remove(InstanceIdentifier<VrfEntry> identifier, VrfEntry vrfEntry) {
164 LOG.trace("key: " + identifier + ", value=" + vrfEntry);
165 deleteFibEntries(identifier, vrfEntry);
169 protected void update(InstanceIdentifier<VrfEntry> identifier, VrfEntry original, VrfEntry update) {
170 LOG.trace("key: " + identifier + ", original=" + original + ", update=" + update );
173 private void createFibEntries(final InstanceIdentifier<VrfEntry> identifier,
174 final VrfEntry vrfEntry) {
175 final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
176 Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
177 Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
179 Long vpnId = getVpnId(vrfTableKey.getRouteDistinguisher());
180 Preconditions.checkNotNull(vpnId, "Vpn Instance not available!");
181 Collection<Long> dpns = vpnmanager.getDpnsForVpn(vpnId);
182 for (Long dpId : dpns) {
183 addRouteInternal(dpId, vpnId, vrfTableKey, vrfEntry);
187 private void addRouteInternal(final long dpId, final long vpnId, final VrfTablesKey vrfTableKey,
188 final VrfEntry vrfEntry) {
189 String rd = vrfTableKey.getRouteDistinguisher();
190 LOG.debug("adding route " + vrfEntry.getDestPrefix() + " " + rd);
192 GetEgressPointerOutput adjacency = resolveAdjacency(dpId, vpnId, vrfEntry);
194 boolean isLocalRoute = false;
195 if(adjacency != null) {
196 groupId = adjacency.getEgressPointer();
197 isLocalRoute = adjacency.isLocalDestination();
200 LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
201 vrfEntry.getNextHopAddress(), rd);
202 LOG.warn("Failed to add Route: {} in vpn: {}",
203 vrfEntry.getDestPrefix(), rd);
207 makeConnectedRoute(dpId, vpnId, vrfEntry, rd, groupId, NwConstants.ADD_FLOW);
210 makeLFibTableEntry(dpId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.ADD_FLOW);
214 "Successfully added fib entry for " + vrfEntry.getDestPrefix() + " vpnId " + vpnId);
217 private void deleteFibEntries(final InstanceIdentifier<VrfEntry> identifier,
218 final VrfEntry vrfEntry) {
219 final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
220 Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
221 Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
223 Long vpnId = getVpnId(vrfTableKey.getRouteDistinguisher());
224 Preconditions.checkNotNull(vpnId, "Vpn Instance not available!");
225 Collection<Long> dpns = vpnmanager.getDpnsForVpn(vpnId);
226 for (Long dpId : dpns) {
227 deleteRoute(dpId, vpnId, vrfTableKey, vrfEntry);
231 public void deleteRoute(final long dpId, final long vpnId, final VrfTablesKey vrfTableKey,
232 final VrfEntry vrfEntry) {
233 LOG.debug("deleting route "+ vrfEntry.getDestPrefix() + " "+vpnId);
234 String rd = vrfTableKey.getRouteDistinguisher();
235 GetEgressPointerOutput adjacency = resolveAdjacency(dpId, vpnId, vrfEntry);
237 boolean isLocalRoute = false;
238 if(adjacency != null) {
239 groupId = adjacency.getEgressPointer();
240 isLocalRoute = adjacency.isLocalDestination();
243 LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
244 vrfEntry.getNextHopAddress(), rd);
245 LOG.warn("Failed to add Route: {} in vpn: {}",
246 vrfEntry.getDestPrefix(), rd);
250 makeConnectedRoute(dpId, vpnId, vrfEntry, rd, groupId, NwConstants.DEL_FLOW);
253 makeLFibTableEntry(dpId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.DEL_FLOW);
256 LOG.debug("Successfully delete fib entry for "+ vrfEntry.getDestPrefix() + " vpnId "+vpnId);
259 private long getIpAddress(byte[] rawIpAddress) {
260 return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
261 + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
264 private void makeConnectedRoute(long dpId, long vpnId, VrfEntry vrfEntry, String rd,
265 long groupId, int addOrRemove) {
266 String values[] = vrfEntry.getDestPrefix().split("/");
267 LOG.debug("Adding route to DPN. ip {} masklen {}", values[0], values[1]);
268 String ipAddress = values[0];
269 int prefixLength = Integer.parseInt(values[1]);
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.group, new String[] { String.valueOf(groupId)}));
296 actionsInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { Long.toString(vrfEntry.getLabel())}));
297 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
300 String flowRef = getFlowRef(dpId, L3_FIB_TABLE, rd, destPrefix);
302 FlowEntity flowEntity;
304 int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
305 flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_FIB_TABLE, flowRef,
306 priority, flowRef, 0, 0,
307 COOKIE_VM_FIB_TABLE, matches, instructions);
309 if (addOrRemove == NwConstants.ADD_FLOW) {
310 mdsalManager.installFlow(flowEntity);
312 mdsalManager.removeFlow(flowEntity);
316 private void makeLFibTableEntry(long dpId, long label, long groupId,
317 String nextHop, int addOrRemove) {
318 List<MatchInfo> matches = new ArrayList<MatchInfo>();
319 matches.add(new MatchInfo(MatchFieldType.eth_type,
320 new long[] { 0x8847L }));
321 matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(label)}));
323 List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
324 List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
325 actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
326 instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
328 // Install the flow entry in L3_LFIB_TABLE
329 String flowRef = getFlowRef(dpId, L3_LFIB_TABLE, label, nextHop);
331 FlowEntity flowEntity;
332 flowEntity = MDSALUtil.buildFlowEntity(dpId, L3_LFIB_TABLE, flowRef,
333 DEFAULT_FIB_FLOW_PRIORITY, flowRef, 0, 0,
334 COOKIE_VM_LFIB_TABLE, matches, instructions);
336 if (addOrRemove == NwConstants.ADD_FLOW) {
337 mdsalManager.installFlow(flowEntity);
339 mdsalManager.removeFlow(flowEntity);
341 LOG.debug("LFIB Entry for dpID {} : label : {} grpup {} modified successfully {}",dpId, label, groupId );
344 private String getFlowRef(long dpnId, short tableId, long label, String nextHop) {
345 return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
346 .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
347 .append(label).append(NwConstants.FLOWID_SEPARATOR)
348 .append(nextHop).toString();
351 private String getFlowRef(long dpnId, short tableId, String rd, InetAddress destPrefix) {
352 return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
353 .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
354 .append(rd).append(NwConstants.FLOWID_SEPARATOR)
355 .append(destPrefix.getHostAddress()).toString();
358 private GetEgressPointerOutput resolveAdjacency(final long dpId, final long vpnId,
359 final VrfEntry vrfEntry) {
360 GetEgressPointerOutput adjacency = null;
361 LOG.trace("resolveAdjacency called with dpid {}, vpnId{}, VrfEntry {}",dpId, vpnId, vrfEntry);;
363 Future<RpcResult<GetEgressPointerOutput>> result =
364 l3nexthopService.getEgressPointer(new GetEgressPointerInputBuilder().setDpnId(dpId)
365 .setIpPrefix(vrfEntry.getDestPrefix())
366 .setNexthopIp(vrfEntry.getNextHopAddress())
369 RpcResult<GetEgressPointerOutput> rpcResult = result.get();
370 if (rpcResult.isSuccessful()) {
371 adjacency = rpcResult.getResult();
373 LOG.error("Next hop information not available");
375 } catch (NullPointerException | InterruptedException | ExecutionException e) {
381 private Long getVpnId(String rd) {
383 InstanceIdentifier<VpnInstances> id = InstanceIdentifier.create(VpnInstances.class);
384 Optional<VpnInstances> vpnInstances = read(LogicalDatastoreType.OPERATIONAL, id);
385 if(vpnInstances.isPresent()) {
386 List<VpnInstance> vpns = vpnInstances.get().getVpnInstance();
387 for(VpnInstance vpn : vpns) {
388 if(vpn.getIpv4Family().getRouteDistinguisher().equals(rd)) {
389 VpnInstance1 vpnInstanceId = vpn.getAugmentation(VpnInstance1.class);
390 if (vpnInstanceId != null) {
391 vpnId = vpnInstanceId.getVpnId();