2 * Copyright (c) 2017 HPE 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.elan.utils;
10 import com.google.common.base.Optional;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
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.controller.md.sal.common.api.data.TransactionCommitFailedException;
19 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
20 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
21 import org.opendaylight.netvirt.elanmanager.api.IElanService;
22 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
23 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.BridgeRefInfo;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge.ref.info.BridgeRefEntry;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.meta.rev160406.bridge.ref.info.BridgeRefEntryKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.TunnelTypeVxlan;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.TransportZones;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZone;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.TransportZoneKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.Subnets;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.SubnetsKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.Vteps;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.rev160406.transport.zones.transport.zone.subnets.VtepsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class TransportZoneNotificationUtil {
49 private static final Logger LOG = LoggerFactory.getLogger(TransportZoneNotificationUtil.class);
50 private static final String TUNNEL_PORT = "tunnel_port";
51 private static final String LOCAL_IP = "local_ip";
52 private static final String ALL_SUBNETS_GW = "0.0.0.0";
53 private static final String ALL_SUBNETS = "0.0.0.0/0";
54 private final DataBroker dataBroker;
55 private final MdsalUtils mdsalUtils;
56 private final SouthboundUtils southBoundUtils;
57 private final IElanService elanService;
58 private final ElanConfig elanConfig;
60 public TransportZoneNotificationUtil(final DataBroker dbx, final IInterfaceManager interfaceManager,
61 final IElanService elanService, final ElanConfig elanConfig) {
62 this.dataBroker = dbx;
63 this.mdsalUtils = new MdsalUtils(dbx);
64 this.elanService = elanService;
65 this.elanConfig = elanConfig;
66 southBoundUtils = new SouthboundUtils(mdsalUtils);
69 public boolean shouldCreateVtep(List<VpnInterfaces> vpnInterfaces) {
70 if (vpnInterfaces == null || vpnInterfaces.isEmpty()) {
74 for (VpnInterfaces vpnInterface : vpnInterfaces) {
75 String interfaceName = vpnInterface.getInterfaceName();
77 ElanInterface elanInt = elanService.getElanInterfaceByElanInterfaceName(interfaceName);
78 if (elanInt == null) {
82 if (ElanUtils.isVxlanNetwork(dataBroker, elanInt.getElanInstanceName())) {
85 LOG.debug("Non-VXLAN elanInstance: " + elanInt.getElanInstanceName());
92 private TransportZone createZone(String subnetIp, String zoneName) {
93 List<Subnets> subnets = new ArrayList<>();
94 subnets.add(buildSubnets(subnetIp));
95 TransportZoneBuilder tzb = new TransportZoneBuilder().setKey(new TransportZoneKey(zoneName))
96 .setTunnelType(TunnelTypeVxlan.class).setZoneName(zoneName).setSubnets(subnets);
100 private void updateTransportZone(TransportZone zone, BigInteger dpnId) throws TransactionCommitFailedException {
101 InstanceIdentifier<TransportZone> path = InstanceIdentifier.builder(TransportZones.class)
102 .child(TransportZone.class, new TransportZoneKey(zone.getZoneName())).build();
104 SingleTransactionDataBroker.syncUpdate(dataBroker, LogicalDatastoreType.CONFIGURATION, path, zone);
105 LOG.info("Transport zone {} updated due to dpn {} handling.", zone.getZoneName(), dpnId);
108 @SuppressWarnings("checkstyle:IllegalCatch")
109 public void updateTransportZone(String zoneName, BigInteger dpnId) {
110 InstanceIdentifier<TransportZone> inst = InstanceIdentifier.create(TransportZones.class)
111 .child(TransportZone.class, new TransportZoneKey(zoneName));
113 // FIXME: Read this through a cache
114 TransportZone zone = mdsalUtils.read(LogicalDatastoreType.CONFIGURATION, inst);
117 zone = createZone(ALL_SUBNETS, zoneName);
121 if (addVtep(zone, ALL_SUBNETS, dpnId)) {
122 updateTransportZone(zone, dpnId);
124 } catch (Exception e) {
125 LOG.error("Failed to add tunnels for dpn {} in zone {}", dpnId, zoneName, e);
130 * Tries to add a vtep for a transport zone.
132 * @return Whether a vtep was added or not.
134 private boolean addVtep(TransportZone zone, String subnetIp, BigInteger dpnId) throws Exception {
135 Subnets subnets = getOrAddSubnet(zone.getSubnets(), subnetIp);
136 for (Vteps existingVtep : subnets.getVteps()) {
137 if (existingVtep.getDpnId().equals(dpnId)) {
142 Optional<IpAddress> nodeIp = getNodeIP(dpnId);
144 if (nodeIp.isPresent()) {
145 VtepsBuilder vtepsBuilder =
146 new VtepsBuilder().setDpnId(dpnId).setIpAddress(nodeIp.get()).setPortname(TUNNEL_PORT)
147 .setOptionOfTunnel(elanConfig.isUseOfTunnels());
148 subnets.getVteps().add(vtepsBuilder.build());
156 // search for relevant subnets for the given subnetIP, add one if it is
158 private Subnets getOrAddSubnet(List<Subnets> subnets, String subnetIp) {
159 IpPrefix subnetPrefix = new IpPrefix(subnetIp.toCharArray());
161 if (subnets != null) {
162 for (Subnets subnet : subnets) {
163 if (subnet.getPrefix().equals(subnetPrefix)) {
169 Subnets retSubnet = buildSubnets(subnetIp);
170 subnets.add(retSubnet);
175 private Subnets buildSubnets(String subnetIp) {
176 SubnetsBuilder subnetsBuilder = new SubnetsBuilder().setDeviceVteps(new ArrayList<>())
177 .setGatewayIp(new IpAddress(ALL_SUBNETS_GW.toCharArray()))
178 .setKey(new SubnetsKey(new IpPrefix(subnetIp.toCharArray()))).setVlanId(0)
179 .setVteps(new ArrayList<Vteps>());
180 return subnetsBuilder.build();
183 private Optional<IpAddress> getNodeIP(BigInteger dpId) throws Exception {
184 Optional<Node> node = getPortsNode(dpId);
186 if (node.isPresent()) {
187 String localIp = southBoundUtils.getOpenvswitchOtherConfig(node.get(), LOCAL_IP);
188 if (localIp == null) {
189 LOG.error("missing local_ip key in ovsdb:openvswitch-other-configs in operational"
190 + " network-topology for node: " + node.get().getNodeId().getValue());
192 return Optional.of(new IpAddress(localIp.toCharArray()));
196 return Optional.absent();
199 @SuppressWarnings("unchecked")
200 private Optional<Node> getPortsNode(BigInteger dpnId) throws Exception {
201 InstanceIdentifier<BridgeRefEntry> bridgeRefInfoPath = InstanceIdentifier.create(BridgeRefInfo.class)
202 .child(BridgeRefEntry.class, new BridgeRefEntryKey(dpnId));
204 // FIXME: Read this through a cache
205 BridgeRefEntry bridgeRefEntry = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, bridgeRefInfoPath);
206 if (bridgeRefEntry == null) {
207 LOG.error("no bridge ref entry found for dpnId: " + dpnId);
208 return Optional.absent();
211 InstanceIdentifier<Node> nodeId =
212 ((InstanceIdentifier<OvsdbBridgeAugmentation>) bridgeRefEntry.getBridgeReference().getValue())
213 .firstIdentifierOf(Node.class);
215 // FIXME: Read this through a cache
216 Node node = mdsalUtils.read(LogicalDatastoreType.OPERATIONAL, nodeId);
219 LOG.error("missing node for dpnId: " + dpnId);
220 return Optional.absent();
223 return Optional.of(node);