2 * Copyright (c) 2016 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.dhcpservice;
10 import com.google.common.base.Optional;
11 import java.util.List;
12 import java.util.concurrent.ConcurrentMap;
13 import java.util.function.Predicate;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.mdsalutil.MDSALUtil;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
20 import org.opendaylight.netvirt.neutronvpn.api.l2gw.utils.L2GatewayCacheUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 public class DhcpL2GwUtil {
32 private static final Predicate<List<?>> EMPTY_LIST = (list) -> list == null || list.isEmpty();
34 private static final Predicate<Optional<Node>> CONTAINS_GLOBAL_AUGMENTATION = (optionalNode) -> {
35 return optionalNode.isPresent() && optionalNode.get().getAugmentation(HwvtepGlobalAugmentation.class) != null;
38 private static final Predicate<Optional<Node>> CONTAINS_SWITCH_AUGMENTATION = (optionalNode) -> {
39 return optionalNode.isPresent() && optionalNode.get().getAugmentation(PhysicalSwitchAugmentation.class) != null;
42 private final DataBroker dataBroker;
45 public DhcpL2GwUtil(DataBroker dataBroker) {
46 this.dataBroker = dataBroker;
49 public IpAddress getHwvtepNodeTunnelIp(InstanceIdentifier<Node> nodeIid) {
50 ConcurrentMap<String, L2GatewayDevice> devices = L2GatewayCacheUtils.getCache();
51 String nodeId = nodeIid.firstKeyOf(Node.class).getNodeId().getValue();
52 L2GatewayDevice targetDevice = null;
53 for (L2GatewayDevice device : devices.values()) {
54 if (nodeId.equals(device.getHwvtepNodeId())) {
55 targetDevice = device;
59 return targetDevice != null ? targetDevice.getTunnelIp() : getTunnelIp(nodeIid);
63 private IpAddress getTunnelIp(InstanceIdentifier<Node> nodeIid) {
64 Optional<Node> nodeOptional =
65 MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, nodeIid);
66 if (!CONTAINS_GLOBAL_AUGMENTATION.test(nodeOptional)) {
69 List<Switches> switchIids = nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class).getSwitches();
70 if (EMPTY_LIST.test(switchIids)) {
73 InstanceIdentifier<Node> psIid = (InstanceIdentifier<Node>) switchIids.get(0).getSwitchRef().getValue();
74 nodeOptional = MDSALUtil.read(dataBroker, LogicalDatastoreType.OPERATIONAL, psIid);
75 if (!CONTAINS_SWITCH_AUGMENTATION.test(nodeOptional)) {
78 List<TunnelIps> tunnelIps = nodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class).getTunnelIps();
79 if (EMPTY_LIST.test(tunnelIps)) {
82 return tunnelIps.get(0).getKey().getTunnelIpsKey();