Convert L2GatewayCacheUtils to non-static singleton service
[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.function.Predicate;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.mdsalutil.MDSALUtil;
18 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayCache;
19 import org.opendaylight.netvirt.neutronvpn.api.l2gw.L2GatewayDevice;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.Switches;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.physical._switch.attributes.TunnelIps;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27
28 @Singleton
29 public class DhcpL2GwUtil {
30
31     private static final Predicate<List<?>> EMPTY_LIST = (list) -> list == null || list.isEmpty();
32
33     private static final Predicate<Optional<Node>> CONTAINS_GLOBAL_AUGMENTATION = (optionalNode) -> {
34         return optionalNode.isPresent() && optionalNode.get().getAugmentation(HwvtepGlobalAugmentation.class) != null;
35     };
36
37     private static final Predicate<Optional<Node>> CONTAINS_SWITCH_AUGMENTATION = (optionalNode) -> {
38         return optionalNode.isPresent() && optionalNode.get().getAugmentation(PhysicalSwitchAugmentation.class) != null;
39     };
40
41     private final DataBroker dataBroker;
42     private final L2GatewayCache l2GatewayCache;
43
44     @Inject
45     public DhcpL2GwUtil(DataBroker dataBroker, L2GatewayCache l2GatewayCache) {
46         this.dataBroker = dataBroker;
47         this.l2GatewayCache = l2GatewayCache;
48     }
49
50     public IpAddress getHwvtepNodeTunnelIp(InstanceIdentifier<Node> nodeIid) {
51         String nodeId = nodeIid.firstKeyOf(Node.class).getNodeId().getValue();
52         L2GatewayDevice targetDevice = null;
53         for (L2GatewayDevice device : l2GatewayCache.getAll()) {
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 }