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.nexthopmgr;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.Future;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.FutureCallback;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
27 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
31 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstance1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.CreateIdPoolInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.GetUniqueIdInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.*;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.l3nexthop.*;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.l3nexthop.tunnelnexthops.*;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.l3nexthop.vpnnexthops.*;
43 import org.opendaylight.vpnservice.interfacemgr.interfaces.IInterfaceManager;
44 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
45 import org.opendaylight.vpnservice.mdsalutil.ActionType;
46 import org.opendaylight.vpnservice.mdsalutil.BucketInfo;
47 import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
48 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
49 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
50 import org.opendaylight.idmanager.IdManager;
52 import java.util.concurrent.ExecutionException;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class NexthopManager implements L3nexthopService, AutoCloseable {
58 private static final Logger LOG = LoggerFactory.getLogger(NexthopManager.class);
59 private final DataBroker broker;
60 private IMdsalApiManager mdsalManager;
61 private IInterfaceManager interfaceManager;
62 private IdManager idManager;
64 private static final FutureCallback<Void> DEFAULT_CALLBACK =
65 new FutureCallback<Void>() {
66 public void onSuccess(Void result) {
67 LOG.info("Success in Datastore write operation");
69 public void onFailure(Throwable error) {
70 LOG.error("Error in Datastore write operation", error);
75 * Provides nexthop functions
76 * Creates group ID pool
78 * @param db - dataBroker reference
80 public NexthopManager(final DataBroker db) {
85 public void close() throws Exception {
86 LOG.info("NextHop Manager Closed");
89 public void setInterfaceManager(IInterfaceManager ifManager) {
90 this.interfaceManager = ifManager;
93 public void setMdsalManager(IMdsalApiManager mdsalManager) {
94 this.mdsalManager = mdsalManager;
97 public void setIdManager(IdManager idManager) {
98 this.idManager = idManager;
101 protected void createNexthopPointerPool() {
102 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
103 .setPoolName("nextHopPointerPool")
105 .setPoolSize(new BigInteger("65535"))
107 //TODO: Error handling
108 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
110 // LOG.info("Result2: {}",result.get());
111 // } catch (InterruptedException | ExecutionException e) {
112 // // TODO Auto-generated catch block
113 // LOG.error("Error in result.get");
119 private long getVpnId(String vpnName) {
120 InstanceIdentifierBuilder<VpnInstance> idBuilder = InstanceIdentifier.builder(VpnInstances.class)
121 .child(VpnInstance.class, new VpnInstanceKey(vpnName));
123 InstanceIdentifier<VpnInstance> id = idBuilder.build();
124 InstanceIdentifier<VpnInstance1> idx = id.augmentation(VpnInstance1.class);
125 Optional<VpnInstance1> vpn = read(LogicalDatastoreType.OPERATIONAL, idx);
127 if (vpn.isPresent()) {
128 return vpn.get().getVpnId();
134 private long getDpnId(String ifName) {
135 String[] fields = ifName.split(":");
136 long dpn = Integer.parseInt(fields[1]);
140 private int createNextHopPointer(String nexthopKey) {
141 GetUniqueIdInput getIdInput = new GetUniqueIdInputBuilder()
142 .setPoolName("nextHopPointerPool").setIdKey(nexthopKey)
144 //TODO: Proper error handling once IdManager code is complete
146 Future<RpcResult<GetUniqueIdOutput>> result = idManager.getUniqueId(getIdInput);
147 RpcResult<GetUniqueIdOutput> rpcResult = result.get();
148 return rpcResult.getResult().getIdValue().intValue();
149 } catch (NullPointerException | InterruptedException | ExecutionException e) {
155 public void createLocalNextHop(String ifName, String vpnName, String ipAddress, String macAddress) {
156 String nhKey = new String("nexthop." + vpnName + ipAddress);
157 int groupId = createNextHopPointer(nhKey);
159 long vpnId = getVpnId(vpnName);
160 long dpnId = interfaceManager.getDpnForInterface(ifName);
161 VpnNexthop nexthop = getVpnNexthop(vpnId, ipAddress);
162 if (nexthop == null) {
163 List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
164 List<ActionInfo> listActionInfo = interfaceManager.getInterfaceEgressActions(ifName);
165 BucketInfo bucket = new BucketInfo(listActionInfo);
167 if (macAddress != null) {
168 listActionInfo.add(new ActionInfo(ActionType.set_field_eth_dest, new String[]{macAddress}));
170 //FIXME: Log message here.
172 listBucketInfo.add(bucket);
173 GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
174 dpnId, groupId, ipAddress, GroupTypes.GroupIndirect, listBucketInfo);
177 mdsalManager.installGroup(groupEntity);
180 addVpnNexthopToDS(vpnId, ipAddress, groupId);
186 public void createRemoteNextHop(String ifName, String ofPortId, String ipAddress) {
187 String nhKey = new String("nexthop." + ifName + ipAddress);
188 int groupId = createNextHopPointer(nhKey);
190 long dpnId = getDpnId(ofPortId);
191 TunnelNexthop nexthop = getTunnelNexthop(dpnId, ipAddress);
192 if (nexthop == null) {
194 List<BucketInfo> listBucketInfo = new ArrayList<BucketInfo>();
195 List<ActionInfo> listActionInfo = interfaceManager.getInterfaceEgressActions(ifName);
196 BucketInfo bucket = new BucketInfo(listActionInfo);
198 listBucketInfo.add(bucket);
199 GroupEntity groupEntity = MDSALUtil.buildGroupEntity(
200 dpnId, groupId, ipAddress, GroupTypes.GroupIndirect, listBucketInfo);
201 mdsalManager.installGroup(groupEntity);
204 addTunnelNexthopToDS(dpnId, ipAddress, groupId);
210 private void addVpnNexthopToDS(long vpnId, String ipPrefix, long egressPointer) {
212 InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
213 .child(VpnNexthops.class, new VpnNexthopsKey(vpnId));
215 // check if vpn node is there or to be created
216 InstanceIdentifier<VpnNexthops> id = idBuilder.build();
217 Optional<VpnNexthops> nexthops = read(LogicalDatastoreType.OPERATIONAL, id);
218 if (!nexthops.isPresent()) {
220 VpnNexthops node = new VpnNexthopsBuilder().setKey(new VpnNexthopsKey(vpnId)).setVpnId(vpnId).build();
221 asyncWrite(LogicalDatastoreType.OPERATIONAL, id, node, DEFAULT_CALLBACK);
224 // Add nexthop to vpn node
225 VpnNexthop nh = new VpnNexthopBuilder().
226 setKey(new VpnNexthopKey(ipPrefix)).
227 setIpAddress(ipPrefix).
228 setEgressPointer(egressPointer).build();
230 InstanceIdentifier<VpnNexthop> id1 = idBuilder
231 .child(VpnNexthop.class, new VpnNexthopKey(ipPrefix)).build();
233 asyncWrite(LogicalDatastoreType.OPERATIONAL, id1, nh, DEFAULT_CALLBACK);
237 private void addTunnelNexthopToDS(long dpnId, String ipPrefix, long egressPointer) {
238 InstanceIdentifierBuilder<TunnelNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
239 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId));
241 // check if dpn node is there or to be created
242 InstanceIdentifier<TunnelNexthops> id = idBuilder.build();
243 Optional<TunnelNexthops> nexthops = read(LogicalDatastoreType.OPERATIONAL, id);
244 if (!nexthops.isPresent()) {
246 TunnelNexthops node = new TunnelNexthopsBuilder()
247 .setKey(new TunnelNexthopsKey(dpnId))
250 asyncWrite(LogicalDatastoreType.OPERATIONAL, id, node, DEFAULT_CALLBACK);
253 // Add nexthop to dpn node
254 TunnelNexthop nh = new TunnelNexthopBuilder().
255 setKey(new TunnelNexthopKey(ipPrefix)).
256 setIpAddress(ipPrefix).
257 setEgressPointer(egressPointer).build();
259 InstanceIdentifier<TunnelNexthop> id1 = idBuilder
260 .child(TunnelNexthop.class, new TunnelNexthopKey(ipPrefix)).build();
262 asyncWrite(LogicalDatastoreType.OPERATIONAL, id1, nh, DEFAULT_CALLBACK);
266 private VpnNexthop getVpnNexthop(long vpnId, String ipAddress) {
268 // check if vpn node is there
269 InstanceIdentifierBuilder<VpnNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
270 .child(VpnNexthops.class, new VpnNexthopsKey(vpnId));
271 InstanceIdentifier<VpnNexthops> id = idBuilder.build();
272 Optional<VpnNexthops> vpnNexthops = read(LogicalDatastoreType.OPERATIONAL, id);
273 if (vpnNexthops.isPresent()) {
275 // get nexthops list for vpn
276 List<VpnNexthop> nexthops = vpnNexthops.get().getVpnNexthop();
277 for (VpnNexthop nexthop : nexthops) {
278 if (nexthop.getIpAddress().equals(ipAddress)) {
284 //return null if not found
288 private TunnelNexthop getTunnelNexthop(long dpnId, String ipAddress) {
290 InstanceIdentifierBuilder<TunnelNexthops> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
291 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId));
293 // check if vpn node is there
294 InstanceIdentifier<TunnelNexthops> id = idBuilder.build();
295 Optional<TunnelNexthops> dpnNexthops = read(LogicalDatastoreType.OPERATIONAL, id);
296 if (dpnNexthops.isPresent()) {
297 List<TunnelNexthop> nexthops = dpnNexthops.get().getTunnelNexthop();
298 for (TunnelNexthop nexthop : nexthops) {
299 if (nexthop.getIpAddress().equals(ipAddress)) {
307 public long getNextHopPointer(long dpnId, long vpnId, String prefixIp, String nextHopIp) {
308 String endpointIp = interfaceManager.getEndpointIpForDpn(dpnId);
309 if (nextHopIp.equals(endpointIp)) {
310 VpnNexthop vpnNextHop = getVpnNexthop(vpnId, prefixIp);
311 return vpnNextHop.getEgressPointer();
313 TunnelNexthop tunnelNextHop = getTunnelNexthop(dpnId, nextHopIp);
314 return tunnelNextHop.getEgressPointer();
318 private void removeTunnelNexthopFromDS(long dpnId, String ipPrefix) {
320 InstanceIdentifierBuilder<TunnelNexthop> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
321 .child(TunnelNexthops.class, new TunnelNexthopsKey(dpnId))
322 .child(TunnelNexthop.class, new TunnelNexthopKey(ipPrefix));
323 InstanceIdentifier<TunnelNexthop> id = idBuilder.build();
325 delete(LogicalDatastoreType.OPERATIONAL, id);
328 private void removeVpnNexthopFromDS(long vpnId, String ipPrefix) {
330 InstanceIdentifierBuilder<VpnNexthop> idBuilder = InstanceIdentifier.builder(L3nexthop.class)
331 .child(VpnNexthops.class, new VpnNexthopsKey(vpnId))
332 .child(VpnNexthop.class, new VpnNexthopKey(ipPrefix));
333 InstanceIdentifier<VpnNexthop> id = idBuilder.build();
335 delete(LogicalDatastoreType.OPERATIONAL, id);
339 public void removeLocalNextHop(String vpnName, String ipAddress) {
341 long vpnId = getVpnId(vpnName);
343 VpnNexthop nh = getVpnNexthop(vpnId, ipAddress);
345 // how to inform and remove dependent FIB entries??
346 // we need to do it before the group is removed
351 removeVpnNexthopFromDS(vpnId, ipAddress);
358 public void removeRemoteNextHop(long dpnId, String ipAddress) {
360 TunnelNexthop nh = getTunnelNexthop(dpnId, ipAddress);
362 // how to inform and remove dependent FIB entries??
363 // we need to do it before the group is removed
367 removeTunnelNexthopFromDS(dpnId, ipAddress);
375 public Future<RpcResult<GetEgressPointerOutput>> getEgressPointer(
376 GetEgressPointerInput input) {
378 GetEgressPointerOutputBuilder output = new GetEgressPointerOutputBuilder();
380 String endpointIp = interfaceManager.getEndpointIpForDpn(input.getDpnId());
381 if (input.getNexthopIp().equals(endpointIp)) {
382 VpnNexthop vpnNextHop = getVpnNexthop(input.getVpnId(), input.getIpPrefix());
383 output.setEgressPointer(vpnNextHop.getEgressPointer());
384 output.setLocalDestination(true);
386 TunnelNexthop tunnelNextHop = getTunnelNexthop(input.getDpnId(), input.getNexthopIp());
387 output.setEgressPointer(tunnelNextHop.getEgressPointer());
388 output.setLocalDestination(false);
391 RpcResultBuilder<GetEgressPointerOutput> rpcResultBuilder = RpcResultBuilder.success();
392 rpcResultBuilder.withResult(output.build());
394 return Futures.immediateFuture(rpcResultBuilder.build());
398 private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
399 InstanceIdentifier<T> path) {
401 ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
403 Optional<T> result = Optional.absent();
405 result = tx.read(datastoreType, path).get();
406 } catch (Exception e) {
407 throw new RuntimeException(e);
413 private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
414 InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
415 WriteTransaction tx = broker.newWriteOnlyTransaction();
416 tx.put(datastoreType, path, data, true);
417 Futures.addCallback(tx.submit(), callback);
421 private <T extends DataObject> void delete(LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
422 WriteTransaction tx = broker.newWriteOnlyTransaction();
423 tx.delete(datastoreType, path);
424 Futures.addCallback(tx.submit(), DEFAULT_CALLBACK);