962ba396af965c755e478d800d1649eb5a5d23b6
[netvirt.git] / vpnservice / dhcpservice / dhcpservice-impl / src / main / java / org / opendaylight / netvirt / dhcpservice / DhcpL2GwUtil.java
1 /*
2  * Copyright (c) 2016 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.dhcpservice;
9
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;
28
29 @Singleton
30 public class DhcpL2GwUtil {
31
32     private static final Predicate<List<?>> EMPTY_LIST = (list) -> list == null || list.isEmpty();
33
34     private static final Predicate<Optional<Node>> CONTAINS_GLOBAL_AUGMENTATION = (optionalNode) -> {
35         return optionalNode.isPresent() && optionalNode.get().getAugmentation(HwvtepGlobalAugmentation.class) != null;
36     };
37
38     private static final Predicate<Optional<Node>> CONTAINS_SWITCH_AUGMENTATION = (optionalNode) -> {
39         return optionalNode.isPresent() && optionalNode.get().getAugmentation(PhysicalSwitchAugmentation.class) != null;
40     };
41
42     private final DataBroker dataBroker;
43
44     @Inject
45     public DhcpL2GwUtil(DataBroker dataBroker) {
46         this.dataBroker = dataBroker;
47     }
48
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;
56                 break;
57             }
58         }
59         return targetDevice != null ? targetDevice.getTunnelIp() : getTunnelIp(nodeIid);
60     }
61
62
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)) {
67             return null;
68         }
69         List<Switches> switchIids = nodeOptional.get().getAugmentation(HwvtepGlobalAugmentation.class).getSwitches();
70         if (EMPTY_LIST.test(switchIids)) {
71             return null;
72         }
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)) {
76             return null;
77         }
78         List<TunnelIps> tunnelIps = nodeOptional.get().getAugmentation(PhysicalSwitchAugmentation.class).getTunnelIps();
79         if (EMPTY_LIST.test(tunnelIps)) {
80             return null;
81         }
82         return tunnelIps.get(0).getKey().getTunnelIpsKey();
83     }
84
85 }