2 * Copyright (c) 2016, 2017 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
9 package org.opendaylight.netvirt.elan.internal;
11 import static java.util.Collections.emptyList;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Objects;
21 import java.util.Optional;
23 import java.util.concurrent.Future;
24 import java.util.function.BiFunction;
25 import javax.inject.Inject;
26 import javax.inject.Singleton;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.genius.infra.Datastore;
30 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
31 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
32 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
33 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
34 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
35 import org.opendaylight.genius.mdsalutil.MDSALUtil;
36 import org.opendaylight.genius.mdsalutil.NwConstants;
37 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
38 import org.opendaylight.genius.utils.ServiceIndex;
39 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
40 import org.opendaylight.infrautils.inject.AbstractLifecycle;
41 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
42 import org.opendaylight.mdsal.binding.api.DataBroker;
43 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
44 import org.opendaylight.mdsal.eos.binding.api.Entity;
45 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
46 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
47 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
48 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderInput;
49 import org.opendaylight.netvirt.elan.arp.responder.ArpResponderUtil;
50 import org.opendaylight.netvirt.elan.cache.ElanInstanceCache;
51 import org.opendaylight.netvirt.elan.cache.ElanInterfaceCache;
52 import org.opendaylight.netvirt.elan.utils.ElanConstants;
53 import org.opendaylight.netvirt.elan.utils.ElanUtils;
54 import org.opendaylight.netvirt.elanmanager.api.ElanHelper;
55 import org.opendaylight.netvirt.elanmanager.api.IElanService;
56 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
57 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
58 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
59 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntries;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.elan._interface.StaticMacEntriesBuilder;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
86 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
87 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
88 import org.opendaylight.yangtools.yang.common.RpcResult;
89 import org.opendaylight.yangtools.yang.common.Uint32;
90 import org.opendaylight.yangtools.yang.common.Uint64;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
95 public class ElanServiceProvider extends AbstractLifecycle implements IElanService {
97 private static final Logger LOG = LoggerFactory.getLogger(ElanServiceProvider.class);
99 private final IdManagerService idManager;
100 private final IInterfaceManager interfaceManager;
101 private final ElanBridgeManager bridgeMgr;
102 private final DataBroker broker;
103 private final ManagedNewTransactionRunner txRunner;
104 private final ElanUtils elanUtils;
105 private final SouthboundUtils southboundUtils;
106 private final IMdsalApiManager mdsalManager;
107 private final ElanInstanceCache elanInstanceCache;
108 private final ElanInterfaceCache elanInterfaceCache;
109 private boolean isL2BeforeL3;
111 private final EntityOwnershipCandidateRegistration candidateRegistration;
114 public ElanServiceProvider(IdManagerService idManager, IInterfaceManager interfaceManager,
115 ElanBridgeManager bridgeMgr,
116 DataBroker dataBroker,
118 EntityOwnershipService entityOwnershipService,
119 SouthboundUtils southboundUtils, ElanInstanceCache elanInstanceCache,
120 ElanInterfaceCache elanInterfaceCache, IMdsalApiManager mdsalManager) {
121 this.idManager = idManager;
122 this.interfaceManager = interfaceManager;
123 this.bridgeMgr = bridgeMgr;
124 this.broker = dataBroker;
125 this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
126 this.elanUtils = elanUtils;
127 this.southboundUtils = southboundUtils;
128 this.elanInstanceCache = elanInstanceCache;
129 this.elanInterfaceCache = elanInterfaceCache;
130 this.mdsalManager = mdsalManager;
132 candidateRegistration = registerCandidate(entityOwnershipService);
136 private static EntityOwnershipCandidateRegistration registerCandidate(
137 EntityOwnershipService entityOwnershipService) {
139 return entityOwnershipService.registerCandidate(
140 new Entity(HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE));
141 } catch (CandidateAlreadyRegisteredException e) {
142 LOG.error("failed to register the entity");
148 @SuppressWarnings("checkstyle:IllegalCatch")
149 protected void start() throws Exception {
150 LOG.info("Starting ElanServiceProvider");
156 protected void stop() {
157 if (candidateRegistration != null) {
158 candidateRegistration.close();
161 LOG.info("ElanServiceProvider stopped");
165 // Confusing with isOpenstackVniSemanticsEnforced but this is an interface method so can't change it.
166 @SuppressFBWarnings("NM_CONFUSING")
167 public Boolean isOpenStackVniSemanticsEnforced() {
168 return elanUtils.isOpenstackVniSemanticsEnforced();
171 private void createIdPool() throws Exception {
172 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
173 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
174 Future<RpcResult<CreateIdPoolOutput>> result = idManager.createIdPool(createPool);
175 if (result != null && result.get().isSuccessful()) {
176 LOG.debug("ELAN Id Pool is created successfully");
181 public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
182 Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
183 boolean isSuccess = true;
184 if (existingElanInstance.isPresent()) {
185 if (compareWithExistingElanInstance(existingElanInstance.get(), macTimeout, description)) {
186 LOG.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
189 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
190 .setDescription(description).setMacTimeout(macTimeout)
191 .withKey(new ElanInstanceKey(elanInstanceName)).build();
192 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
193 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
194 LOG.debug("Updating the Elan Instance {} with MAC TIME-OUT {} and Description {}",
195 updateElanInstance, macTimeout, description);
198 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
199 .setMacTimeout(macTimeout).setDescription(description)
200 .withKey(new ElanInstanceKey(elanInstanceName)).build();
201 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
202 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
203 LOG.debug("Creating the new Elan Instance {}", elanInstance);
209 public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
210 Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
211 boolean isSuccess = true;
212 if (existingElanInstance.isPresent()) {
213 if (compareWithExistingElanInstance(existingElanInstance.get(), macTimeout, description)) {
214 LOG.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
217 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
218 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
219 .setDescription(description).setMacTimeout(macTimeout)
220 .withKey(new ElanInstanceKey(elanInstanceName))
221 .addAugmentation(EtreeInstance.class, etreeInstance).build();
222 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
223 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
224 LOG.debug("Updating the Etree Instance {} with MAC TIME-OUT {} and Description {} ",
225 updateElanInstance, macTimeout, description);
228 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
229 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
230 .setMacTimeout(macTimeout).setDescription(description)
231 .withKey(new ElanInstanceKey(elanInstanceName))
232 .addAugmentation(EtreeInstance.class, etreeInstance).build();
233 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
234 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
235 LOG.debug("Creating the new Etree Instance {}", elanInstance);
242 public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
243 return elanInterfaceCache.getEtreeInterface(elanInterface).orElse(null);
246 public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
247 String description) {
248 boolean isEqual = false;
249 if (existingElanInstance.getMacTimeout().longValue() == macTimeOut
250 && Objects.equals(existingElanInstance.getDescription(), description)) {
257 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
258 createElanInstance(elanInstanceName, newMacTimout, newDescription);
262 public boolean deleteEtreeInstance(String etreeInstanceName) {
263 return deleteElanInstance(etreeInstanceName);
267 public boolean deleteElanInstance(String elanInstanceName) {
268 Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
269 if (!existingElanInstance.isPresent()) {
270 LOG.debug("Elan Instance is not present for {}", elanInstanceName);
273 LOG.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
274 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
275 ElanHelper.getElanInstanceConfigurationDataPath(elanInstanceName));
280 public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
281 List<String> staticMacAddresses, String description) {
282 Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(etreeInstanceName);
283 if (existingElanInstance.isPresent()
284 && existingElanInstance.get().augmentation(EtreeInstance.class) != null) {
285 EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
286 ElanInterface elanInterface;
287 if (staticMacAddresses == null) {
288 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
289 .setDescription(description).setName(interfaceName).withKey(new ElanInterfaceKey(interfaceName))
290 .addAugmentation(EtreeInterface.class, etreeInterface).build();
292 List<StaticMacEntries> staticMacEntries = ElanUtils.getStaticMacEntries(staticMacAddresses);
293 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
294 .setDescription(description).setName(interfaceName)
295 .setStaticMacEntries(staticMacEntries)
296 .withKey(new ElanInterfaceKey(interfaceName))
297 .addAugmentation(EtreeInterface.class, etreeInterface).build();
299 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
300 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
301 LOG.debug("Creating the new Etree Interface {}", elanInterface);
306 public void addElanInterface(String elanInstanceName, String interfaceName,
307 @Nullable List<String> staticMacAddresses, @Nullable String description) {
308 Optional<ElanInstance> existingElanInstance = elanInstanceCache.get(elanInstanceName);
309 if (existingElanInstance.isPresent()) {
310 ElanInterfaceBuilder elanInterfaceBuilder = new ElanInterfaceBuilder()
311 .setElanInstanceName(elanInstanceName)
312 .setDescription(description).setName(interfaceName)
313 .withKey(new ElanInterfaceKey(interfaceName));
314 if (staticMacAddresses != null) {
315 List<StaticMacEntries> staticMacEntries = ElanUtils.getStaticMacEntries(staticMacAddresses);
316 elanInterfaceBuilder.setStaticMacEntries(staticMacEntries);
318 ElanInterface elanInterface = elanInterfaceBuilder.build();
319 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
320 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
321 LOG.debug("Created the new ELan Interface {}", elanInterface);
326 public void updateElanInterface(String elanInstanceName, String interfaceName,
327 List<String> updatedStaticMacAddresses, String newDescription) {
328 Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
329 if (!existingElanInterface.isPresent()) {
333 List<StaticMacEntries> updatedStaticMacEntries = ElanUtils.getStaticMacEntries(updatedStaticMacAddresses);
334 LOG.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
335 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
336 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedStaticMacEntries)
337 .withKey(new ElanInterfaceKey(interfaceName)).build();
338 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
339 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
343 public void deleteEtreeInterface(String interfaceName) {
344 deleteElanInterface(interfaceName);
345 LOG.debug("deleting the Etree Interface {}", interfaceName);
349 public void deleteElanInterface(String interfaceName) {
350 Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
351 if (existingElanInterface.isPresent()) {
352 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
353 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
354 LOG.debug("deleting the Elan Interface {}", existingElanInterface);
359 public void addStaticMacAddress(String interfaceName, String macAddress) {
360 Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
361 if (existingElanInterface.isPresent()) {
362 StaticMacEntriesBuilder staticMacEntriesBuilder = new StaticMacEntriesBuilder();
363 StaticMacEntries staticMacEntry = staticMacEntriesBuilder.setMacAddress(
364 new PhysAddress(macAddress)).build();
365 InstanceIdentifier<StaticMacEntries> staticMacEntriesIdentifier =
366 ElanUtils.getStaticMacEntriesCfgDataPathIdentifier(interfaceName, macAddress);
367 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, staticMacEntriesIdentifier, staticMacEntry);
372 public void deleteStaticMacAddress(String interfaceName, String macAddress) {
373 Optional<ElanInterface> existingElanInterface = elanInterfaceCache.get(interfaceName);
374 if (existingElanInterface.isPresent()) {
375 InstanceIdentifier<StaticMacEntries> staticMacEntriesIdentifier =
376 ElanUtils.getStaticMacEntriesCfgDataPathIdentifier(interfaceName,
378 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, staticMacEntriesIdentifier);
383 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
384 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
385 List<MacEntry> macAddress = new ArrayList<>();
386 if (elanInfo == null) {
389 List<String> elanInterfaces = elanInfo.getElanInterfaces();
390 if (elanInterfaces != null && elanInterfaces.size() > 0) {
391 for (String elanInterface : elanInterfaces) {
392 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
393 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
394 && elanInterfaceMac.getMacEntry().size() > 0) {
395 macAddress.addAll(elanInterfaceMac.getMacEntry());
403 public void flushMACTable(String elanInstanceName) {
404 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
405 if (elanInfo == null) {
408 List<String> elanInterfaces = elanInfo.getElanInterfaces();
409 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
412 for (String elanInterface : elanInterfaces) {
413 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
414 if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
415 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
416 for (MacEntry macEntry : macEntries) {
417 deleteStaticMacAddress(elanInterface, macEntry.getMacAddress().getValue());
426 public ElanInstance getElanInstance(String elanName) {
427 return elanInstanceCache.get(elanName).orElse(null);
431 public List<ElanInstance> getElanInstances() {
432 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
434 return ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanInstancesIdentifier).map(
435 ElanInstances::getElanInstance).orElse(emptyList());
440 public List<String> getElanInterfaces(String elanInstanceName) {
441 List<String> elanInterfaces = new ArrayList<>();
442 InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
444 Optional<ElanInterfaces> elanInterfacesOptional = ElanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
445 elanInterfacesIdentifier);
446 if (!elanInterfacesOptional.isPresent()) {
447 return elanInterfaces;
449 List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().nonnullElanInterface();
450 for (ElanInterface elanInterface : elanInterfaceList) {
451 if (Objects.equals(elanInterface.getElanInstanceName(), elanInstanceName)) {
452 elanInterfaces.add(elanInterface.getName());
455 return elanInterfaces;
459 public void createExternalElanNetworks(Node node) {
460 handleExternalElanNetworks(node, true, (elanInstance, interfaceName) -> {
461 createExternalElanNetwork(elanInstance, interfaceName);
467 public void createExternalElanNetwork(ElanInstance elanInstance) {
468 handleExternalElanNetwork(elanInstance, false, (elanInstance1, interfaceName) -> {
469 createExternalElanNetwork(elanInstance1, interfaceName);
474 protected void createExternalElanNetwork(ElanInstance elanInstance, Uint64 dpId) {
475 String providerIntfName = bridgeMgr.getProviderInterfaceName(dpId, elanInstance.getPhysicalNetworkName());
476 String intfName = providerIntfName + IfmConstants.OF_URI_SEPARATOR + elanInstance.getSegmentationId();
477 Interface memberIntf = interfaceManager.getInterfaceInfoFromConfigDataStore(intfName);
478 if (memberIntf == null) {
479 LOG.debug("creating vlan prv intf in elan {}, dpn {}", elanInstance.getElanInstanceName(),
481 createExternalElanNetwork(elanInstance, providerIntfName);
485 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
486 if (interfaceName == null) {
487 LOG.trace("No physical interface is attached to {}", elanInstance.getPhysicalNetworkName());
491 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
492 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
496 public void updateExternalElanNetwork(ElanInstance elanInstance) {
497 handleExternalElanNetwork(elanInstance, true, (elanInstance1, interfaceName) -> {
498 createExternalElanNetwork(elanInstance1, interfaceName);
504 public void deleteExternalElanNetworks(Node node) {
505 handleExternalElanNetworks(node, false, (elanInstance, interfaceName) -> {
506 deleteExternalElanNetwork(elanInstance, interfaceName);
512 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
513 handleExternalElanNetwork(elanInstance, false, (elanInstance1, interfaceName) -> {
514 deleteExternalElanNetwork(elanInstance1, interfaceName);
519 protected void deleteExternalElanNetwork(ElanInstance elanInstance, Uint64 dpnId) {
520 String providerIntfName = bridgeMgr.getProviderInterfaceName(dpnId, elanInstance.getPhysicalNetworkName());
521 String intfName = providerIntfName + IfmConstants.OF_URI_SEPARATOR + elanInstance.getSegmentationId();
522 Interface memberIntf = interfaceManager.getInterfaceInfoFromConfigDataStore(intfName);
523 if (memberIntf != null) {
524 deleteElanInterface(intfName);
525 deleteIetfInterface(intfName);
526 LOG.debug("delete vlan prv intf {} in elan {}, dpID {}", intfName,
527 elanInstance.getElanInstanceName(), dpnId);
529 LOG.debug("vlan prv intf {} not found in interfacemgr config DS", intfName);
533 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
534 if (interfaceName == null) {
535 LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
539 String elanInstanceName = elanInstance.getElanInstanceName();
540 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
541 if (elanInterface.startsWith(interfaceName)) {
542 if (ElanUtils.isVlan(elanInstance)) {
543 deleteIetfInterface(elanInterface);
545 String trunkInterfaceName = getTrunkInterfaceName(interfaceName);
546 if (shouldDeleteTrunk(trunkInterfaceName, elanInterface)) {
547 deleteIetfInterface(trunkInterfaceName);
549 deleteElanInterface(elanInterface);
554 private boolean shouldDeleteTrunk(String trunkInterfaceName, String elanInterfaceName) {
555 List<Interface> childInterfaces = interfaceManager.getChildInterfaces(trunkInterfaceName);
556 if (childInterfaces == null || childInterfaces.isEmpty()
557 || childInterfaces.size() == 1 && elanInterfaceName.equals(childInterfaces.get(0).getName())) {
558 LOG.debug("No more VLAN member interfaces left for trunk {}", trunkInterfaceName);
562 LOG.debug("Trunk interface {} has {} VLAN member interfaces left", trunkInterfaceName, childInterfaces.size());
567 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
568 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
572 List<ElanInstance> elanInstances = getElanInstances();
573 if (elanInstances == null || elanInstances.isEmpty()) {
574 LOG.trace("No ELAN instances found");
578 LOG.trace("updateExternalElanNetworks, orig bridge {} . updated bridge {}", origNode, updatedNode);
580 Map<String, String> origProviderMappping = getMapFromOtherConfig(origNode,
581 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
582 Map<String, String> updatedProviderMappping = getMapFromOtherConfig(updatedNode,
583 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
585 boolean hasDatapathIdOnOrigNode = bridgeMgr.hasDatapathID(origNode);
586 boolean hasDatapathIdOnUpdatedNode = bridgeMgr.hasDatapathID(updatedNode);
587 Uint64 origDpnID = bridgeMgr.getDatapathId(origNode);
589 for (ElanInstance elanInstance : elanInstances) {
590 String physicalNetworkName = elanInstance.getPhysicalNetworkName();
591 boolean createExternalElanNw = true;
592 if (physicalNetworkName != null) {
593 String origPortName = origProviderMappping.get(physicalNetworkName);
594 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
596 * for internal vlan network, vlan provider interface creation should be
597 * triggered only if there is existing vlan provider intf indicating presence
598 * of VM ports on the DPN
600 if (hasDatapathIdOnOrigNode && !elanInstance.isExternal()
601 && ElanUtils.isVlan(elanInstance)) {
602 String externalIntf = getExternalElanInterface(elanInstance.getElanInstanceName(),
604 if (externalIntf == null) {
605 createExternalElanNw = false;
608 if (hasPortNameRemoved(origPortName, updatedPortName)) {
609 deleteExternalElanNetwork(elanInstance,
610 bridgeMgr.getProviderInterfaceName(origNode, physicalNetworkName));
613 if (createExternalElanNw && (hasPortNameUpdated(origPortName, updatedPortName)
614 || hasDatapathIdAdded(hasDatapathIdOnOrigNode, hasDatapathIdOnUpdatedNode))) {
615 createExternalElanNetwork(elanInstance,
616 bridgeMgr.getProviderInterfaceName(updatedNode, physicalNetworkName));
622 private static boolean hasDatapathIdAdded(boolean hasDatapathIdOnOrigNode, boolean hasDatapathIdOnUpdatedNode) {
623 return !hasDatapathIdOnOrigNode && hasDatapathIdOnUpdatedNode;
626 private static boolean hasPortNameUpdated(String origPortName, String updatedPortName) {
627 return updatedPortName != null && !updatedPortName.equals(origPortName);
630 private static boolean hasPortNameRemoved(String origPortName, String updatedPortName) {
631 return origPortName != null && !origPortName.equals(updatedPortName);
634 private Map<String, String> getMapFromOtherConfig(Node node, String otherConfigColumn) {
635 return bridgeMgr.getOpenvswitchOtherConfigMap(node, otherConfigColumn);
639 public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
640 List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
641 if (elanInterfaces.isEmpty()) {
642 LOG.trace("No ELAN interfaces defined for {}", elanInstanceName);
643 return Collections.emptySet();
646 Set<String> externalElanInterfaces = new HashSet<>();
647 for (String elanInterface : elanInterfaces) {
648 if (interfaceManager.isExternalInterface(elanInterface)) {
649 externalElanInterfaces.add(elanInterface);
653 return externalElanInterfaces;
657 public String getExternalElanInterface(String elanInstanceName, Uint64 dpnId) {
658 return elanUtils.getExternalElanInterface(elanInstanceName, dpnId);
662 public boolean isExternalInterface(String interfaceName) {
663 return interfaceManager.isExternalInterface(interfaceName);
668 public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
669 return elanInterfaceCache.get(interfaceName).orElse(null);
673 public void handleKnownL3DmacAddress(String macAddress, String elanInstanceName, int addOrRemove) {
674 if (addOrRemove == NwConstants.ADD_FLOW) {
675 addKnownL3DmacAddress(macAddress, elanInstanceName);
677 removeKnownL3DmacAddress(macAddress, elanInstanceName);
682 public void addKnownL3DmacAddress(String macAddress, String elanInstanceName) {
684 LOG.trace("ELAN service is after L3VPN in the Netvirt pipeline skip known L3DMAC flows installation");
687 ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
688 if (elanInstance == null) {
689 LOG.warn("Null elan instance {}", elanInstanceName);
693 List<Uint64> dpnsIdsForElanInstance = elanUtils.getParticipatingDpnsInElanInstance(elanInstanceName);
694 if (dpnsIdsForElanInstance.isEmpty()) {
695 LOG.warn("No DPNs for elan instance {}", elanInstance);
699 elanUtils.addDmacRedirectToDispatcherFlows(elanInstance.getElanTag(), elanInstanceName, macAddress,
700 dpnsIdsForElanInstance);
704 public void removeKnownL3DmacAddress(String macAddress, String elanInstanceName) {
706 LOG.trace("ELAN service is after L3VPN in the Netvirt pipeline skip known L3DMAC flows installation");
709 ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
710 if (elanInstance == null) {
711 LOG.warn("Null elan instance {}", elanInstanceName);
715 List<Uint64> dpnsIdsForElanInstance = elanUtils.getParticipatingDpnsInElanInstance(elanInstanceName);
716 if (dpnsIdsForElanInstance.isEmpty()) {
717 LOG.warn("No DPNs for elan instance {}", elanInstance);
721 elanUtils.removeDmacRedirectToDispatcherFlows(elanInstance.getElanTag(),
722 macAddress, dpnsIdsForElanInstance);
726 * Create ietf-interfaces based on the ELAN segment type.<br>
727 * For segment type flat - create transparent interface pointing to the
728 * patch-port attached to the physnet port.<br>
729 * For segment type vlan - create trunk interface pointing to the patch-port
730 * attached to the physnet port + trunk-member interface pointing to the
733 * @param elanInstance
736 * parent interface name
737 * @return the name of the interface to be added to the ELAN instance i.e.
738 * trunk-member name for vlan network and transparent for flat
739 * network or null otherwise
741 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
742 String interfaceName = null;
745 String trunkName = getTrunkInterfaceName(parentRef);
746 // trunk interface may have been created by other vlan network
747 Interface trunkInterface = interfaceManager.getInterfaceInfoFromConfigDataStore(trunkName);
748 if (trunkInterface == null) {
749 interfaceManager.createVLANInterface(trunkName, parentRef, null, null,
750 IfL2vlan.L2vlanMode.Trunk, true);
752 if (ElanUtils.isFlat(elanInstance)) {
753 interfaceName = trunkName;
754 } else if (ElanUtils.isVlan(elanInstance)) {
755 Long segmentationId = elanInstance.getSegmentationId().toJava();
756 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
757 interfaceManager.createVLANInterface(interfaceName, trunkName, segmentationId.intValue(), null,
758 IfL2vlan.L2vlanMode.TrunkMember, true);
760 } catch (InterfaceAlreadyExistsException e) {
761 LOG.trace("Interface {} was already created", interfaceName);
764 return interfaceName;
767 private void deleteIetfInterface(String interfaceName) {
768 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
769 InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
770 .child(Interface.class, interfaceKey).build();
771 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
772 LOG.debug("Deleting IETF interface {}", interfaceName);
775 private void handleExternalElanNetworks(Node node, boolean skipIntVlanNw,
776 BiFunction<ElanInstance, String, Void> function) {
777 if (!bridgeMgr.isIntegrationBridge(node)) {
781 List<ElanInstance> elanInstances = getElanInstances();
782 if (elanInstances == null || elanInstances.isEmpty()) {
783 LOG.trace("No ELAN instances found");
787 for (ElanInstance elanInstance : elanInstances) {
788 if (skipIntVlanNw && !elanInstance.isExternal() && ElanUtils.isVlan(elanInstance)) {
791 String interfaceName = bridgeMgr.getProviderInterfaceName(node, elanInstance.getPhysicalNetworkName());
792 if (interfaceName != null) {
793 function.apply(elanInstance, interfaceName);
798 private void handleExternalElanNetwork(ElanInstance elanInstance, boolean update,
799 BiFunction<ElanInstance, String, Void> function) {
800 String elanInstanceName = elanInstance.getElanInstanceName();
801 boolean isFlatOrVlanNetwork = (ElanUtils.isFlat(elanInstance) || ElanUtils.isVlan(elanInstance));
802 if (!isFlatOrVlanNetwork) {
803 LOG.error("Network is not of type FLAT/VLAN."
804 + "Ignoring Elan-interface creation for given ProviderInterface {}",
805 elanInstance.getPhysicalNetworkName());
808 if (elanInstance.getPhysicalNetworkName() == null) {
809 LOG.trace("No physical network attached to {}", elanInstanceName);
813 List<Node> nodes = southboundUtils.getOvsdbNodes();
814 if (nodes == null || nodes.isEmpty()) {
815 LOG.trace("No OVS nodes found while creating external network for ELAN {}",
816 elanInstance.getElanInstanceName());
820 for (Node node : nodes) {
821 if (bridgeMgr.isIntegrationBridge(node)) {
822 if (update && !elanInstance.isExternal()) {
823 DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName,
824 bridgeMgr.getDatapathId(node));
825 if (dpnInterfaces == null || dpnInterfaces.getInterfaces().isEmpty()) {
829 String interfaceName = bridgeMgr.getProviderInterfaceName(node, elanInstance.getPhysicalNetworkName());
830 function.apply(elanInstance, interfaceName);
835 private static String getTrunkInterfaceName(String parentRef) {
836 return parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
839 private void setIsL2BeforeL3() {
840 short elanServiceRealIndex = ServiceIndex.getIndex(NwConstants.ELAN_SERVICE_NAME,
841 NwConstants.ELAN_SERVICE_INDEX);
842 short l3vpnServiceRealIndex = ServiceIndex.getIndex(NwConstants.L3VPN_SERVICE_NAME,
843 NwConstants.L3VPN_SERVICE_INDEX);
844 if (elanServiceRealIndex < l3vpnServiceRealIndex) {
845 LOG.info("ELAN service is set before L3VPN service in the Netvirt pipeline");
848 LOG.info("ELAN service is set after L3VPN service in the Netvirt pipeline");
849 isL2BeforeL3 = false;
854 public void addArpResponderFlow(ArpResponderInput arpResponderInput) {
855 String ingressInterfaceName = arpResponderInput.getInterfaceName();
856 String macAddress = arpResponderInput.getSha();
857 String ipAddress = arpResponderInput.getSpa();
858 int lportTag = arpResponderInput.getLportTag();
859 Uint64 dpnId = Uint64.valueOf(arpResponderInput.getDpId());
861 LOG.info("Installing the ARP responder flow on DPN {} for Interface {} with MAC {} & IP {}", dpnId,
862 ingressInterfaceName, macAddress, ipAddress);
863 Optional<ElanInterface> elanIface = elanInterfaceCache.get(ingressInterfaceName);
864 ElanInstance elanInstance = elanIface.isPresent()
865 ? elanInstanceCache.get(elanIface.get().getElanInstanceName()).orElse(null) : null;
866 if (elanInstance == null) {
867 LOG.debug("addArpResponderFlow: elanInstance is null, Failed to install arp responder flow for dpnId {}"
868 + "for Interface {} with MAC {} & IP {}", dpnId, ingressInterfaceName, macAddress, ipAddress);
871 String flowId = ArpResponderUtil.getFlowId(lportTag, ipAddress);
873 MDSALUtil.buildFlowNew(NwConstants.ARP_RESPONDER_TABLE, flowId, NwConstants.DEFAULT_ARP_FLOW_PRIORITY,
875 ArpResponderUtil.generateCookie(lportTag, ipAddress),
876 ArpResponderUtil.getMatchCriteria(lportTag, elanInstance, ipAddress),
877 arpResponderInput.getInstructions());
878 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
879 tx -> mdsalManager.addFlow(tx, dpnId, flowEntity)), LOG, "Error adding flow {}", flowEntity);
880 LOG.info("Installed the ARP Responder flow for Interface {}", ingressInterfaceName);
884 public void addExternalTunnelArpResponderFlow(ArpResponderInput arpResponderInput, String elanInstanceName) {
885 Uint64 dpnId = Uint64.valueOf(arpResponderInput.getDpId());
886 String ipAddress = arpResponderInput.getSpa();
887 String macAddress = arpResponderInput.getSha();
889 LOG.trace("Installing the ExternalTunnel ARP responder flow on DPN {} for ElanInstance {} with MAC {} & IP {}",
890 dpnId, elanInstanceName, macAddress, ipAddress);
892 ElanInstance elanInstance = elanInstanceCache.get(elanInstanceName).orElse(null);
893 if (elanInstance == null) {
894 LOG.warn("Null elan instance {}", elanInstanceName);
898 int lportTag = arpResponderInput.getLportTag();
899 String flowId = ArpResponderUtil.getFlowId(lportTag, ipAddress);
901 MDSALUtil.buildFlowNew(NwConstants.ARP_RESPONDER_TABLE, flowId, NwConstants.DEFAULT_ARP_FLOW_PRIORITY,
903 ArpResponderUtil.generateCookie(lportTag, ipAddress),
904 ArpResponderUtil.getMatchCriteria(lportTag, elanInstance, ipAddress),
905 arpResponderInput.getInstructions());
906 LoggingFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
907 tx -> mdsalManager.addFlow(tx, dpnId, flowEntity)), LOG, "Error adding flow {}", flowEntity);
908 LOG.trace("Installed the ExternalTunnel ARP Responder flow for ElanInstance {}", elanInstanceName);
912 public void removeArpResponderFlow(ArpResponderInput arpResponderInput) {
913 elanUtils.removeArpResponderFlow(Uint64.valueOf(arpResponderInput.getDpId()),
914 arpResponderInput.getInterfaceName(),
915 arpResponderInput.getSpa(), arpResponderInput.getLportTag());
919 * Uses the IdManager to retrieve a brand new ElanTag.
923 * @return the integer
926 public Uint32 retrieveNewElanTag(String idKey) {
927 return ElanUtils.retrieveNewElanTag(idManager, idKey);
931 public InstanceIdentifier<DpnInterfaces> getElanDpnInterfaceOperationalDataPath(
932 String elanInstanceName, Uint64 dpnId) {
933 return ElanUtils.getElanDpnInterfaceOperationalDataPath(elanInstanceName, dpnId);
937 public DpnInterfaces getElanInterfaceInfoByElanDpn(String elanInstanceName, Uint64 dpId) {
938 return elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpId);