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
9 package org.opendaylight.netvirt.elan.internal;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.HashSet;
16 import java.util.List;
19 import java.util.concurrent.Future;
20 import java.util.function.BiFunction;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.genius.interfacemanager.exceptions.InterfaceAlreadyExistsException;
27 import org.opendaylight.genius.interfacemanager.globals.IfmConstants;
28 import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
29 import org.opendaylight.genius.mdsalutil.MDSALUtil;
30 import org.opendaylight.genius.utils.clustering.EntityOwnerUtils;
31 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
32 import org.opendaylight.netvirt.elan.statusanddiag.ElanStatusMonitor;
33 import org.opendaylight.netvirt.elan.utils.ElanConstants;
34 import org.opendaylight.netvirt.elan.utils.ElanUtils;
35 import org.opendaylight.netvirt.elanmanager.api.IElanService;
36 import org.opendaylight.netvirt.elanmanager.exceptions.MacNotFoundException;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.rev160406.IfL2vlan;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstance;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInstanceBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterface.EtreeInterfaceType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.etree.rev160614.EtreeInterfaceBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan._interface.forwarding.entries.ElanInterfaceMac;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterfaceKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.state.Elan;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.forwarding.entries.MacEntry;
62 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.common.RpcResult;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import com.google.common.base.Optional;
70 public class ElanServiceProvider implements IElanService {
72 private static final Logger LOG = LoggerFactory.getLogger(ElanServiceProvider.class);
74 private final IdManagerService idManager;
75 private final IInterfaceManager interfaceManager;
76 private final ElanInstanceManager elanInstanceManager;
77 private final ElanBridgeManager bridgeMgr;
78 private final DataBroker broker;
79 private final ElanStatusMonitor elanStatusMonitor;
80 private static ElanUtils elanUtils;
82 private boolean generateIntBridgeMac = true;
84 public ElanServiceProvider(IdManagerService idManager, IInterfaceManager interfaceManager,
85 ElanInstanceManager elanInstanceManager, ElanBridgeManager bridgeMgr,
86 DataBroker dataBroker,
87 ElanInterfaceManager elanInterfaceManager,
88 ElanStatusMonitor elanStatusMonitor, ElanUtils elanUtils, EntityOwnershipService entityOwnershipService) {
89 this.idManager = idManager;
90 this.interfaceManager = interfaceManager;
91 this.elanInstanceManager = elanInstanceManager;
92 this.bridgeMgr = bridgeMgr;
93 this.broker = dataBroker;
94 this.elanStatusMonitor = elanStatusMonitor;
95 this.elanUtils = elanUtils;
96 elanInterfaceManager.setElanUtils(elanUtils);
98 EntityOwnerUtils.registerEntityCandidateForOwnerShip(entityOwnershipService,
99 HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
101 } catch (CandidateAlreadyRegisteredException e) {
102 LOG.error("failed to register the entity");
107 LOG.info("Starting ElnaServiceProvider");
108 elanStatusMonitor.reportStatus("STARTING");
112 elanStatusMonitor.reportStatus("OPERATIONAL");
113 } catch (Exception e) {
114 LOG.error("Error initializing services", e);
115 elanStatusMonitor.reportStatus("ERROR");
119 private void createIdPool() {
120 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
121 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
123 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
124 if (result != null && result.get().isSuccessful()) {
125 LOG.debug("ELAN Id Pool is created successfully");
127 } catch (Exception e) {
128 LOG.error("Failed to create ELAN Id pool {}", e);
133 public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
134 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
135 boolean isSuccess = true;
136 if (existingElanInstance != null) {
137 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
138 LOG.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
141 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
142 .setDescription(description).setMacTimeout(macTimeout)
143 .setKey(new ElanInstanceKey(elanInstanceName)).build();
144 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
145 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
146 LOG.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
147 updateElanInstance, macTimeout, description);
150 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
151 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
153 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
154 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
155 LOG.debug("Creating the new Elan Instance {}", elanInstance);
161 public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
162 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
163 boolean isSuccess = true;
164 if (existingElanInstance != null) {
165 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
166 LOG.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
169 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
170 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
171 .setDescription(description).setMacTimeout(macTimeout)
172 .setKey(new ElanInstanceKey(elanInstanceName))
173 .addAugmentation(EtreeInstance.class, etreeInstance).build();
174 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
175 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
176 LOG.debug("Updating the Etree Instance {} with MAC TIME-OUT %l and Description %s ",
177 updateElanInstance, macTimeout, description);
180 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
181 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
182 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
183 .addAugmentation(EtreeInstance.class, etreeInstance).build();
184 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
185 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
186 LOG.debug("Creating the new Etree Instance {}", elanInstance);
192 public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
193 return ElanUtils.getEtreeInterfaceByElanInterfaceName(broker, elanInterface);
196 public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
197 String description) {
198 boolean isEqual = false;
199 if (existingElanInstance.getMacTimeout() == macTimeOut
200 && existingElanInstance.getDescription().equals(description)) {
207 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
208 createElanInstance(elanInstanceName, newMacTimout, newDescription);
212 public boolean deleteEtreeInstance(String etreeInstanceName) {
213 return deleteElanInstance(etreeInstanceName);
217 public boolean deleteElanInstance(String elanInstanceName) {
218 boolean isSuccess = false;
219 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
220 if (existingElanInstance == null) {
221 LOG.debug("Elan Instance is not present {}", existingElanInstance);
224 LOG.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
225 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
226 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
232 public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
233 List<String> staticMacAddresses, String description) {
234 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(etreeInstanceName);
235 if (existingElanInstance != null && existingElanInstance.getAugmentation(EtreeInstance.class) != null) {
236 EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
237 ElanInterface elanInterface;
238 if (staticMacAddresses == null) {
239 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
240 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
241 .addAugmentation(EtreeInterface.class, etreeInterface).build();
243 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
244 .setDescription(description).setName(interfaceName)
245 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
246 .setKey(new ElanInterfaceKey(interfaceName))
247 .addAugmentation(EtreeInterface.class, etreeInterface).build();
249 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
250 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
251 LOG.debug("Creating the new Etree Interface {}", elanInterface);
256 public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
257 String description) {
258 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
259 if (existingElanInstance != null) {
260 ElanInterface elanInterface;
261 if (staticMacAddresses == null) {
262 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
263 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
266 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
267 .setDescription(description).setName(interfaceName)
268 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
269 .setKey(new ElanInterfaceKey(interfaceName)).build();
271 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
272 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
273 LOG.debug("Creating the new ELan Interface {}", elanInterface);
278 public void updateElanInterface(String elanInstanceName, String interfaceName,
279 List<String> updatedStaticMacAddresses, String newDescription) {
280 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
281 if (existingElanInterface == null) {
284 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
285 List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
286 List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
287 if (updatedPhysAddress.size() > 0) {
288 LOG.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
289 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
290 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
291 .setKey(new ElanInterfaceKey(interfaceName)).build();
292 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
293 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
298 public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
299 deleteElanInterface(elanInstanceName, interfaceName);
300 LOG.debug("deleting the Etree Interface {}", interfaceName);
304 public void deleteElanInterface(String elanInstanceName, String interfaceName) {
305 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
306 if (existingElanInterface != null) {
307 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
308 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
309 LOG.debug("deleting the Elan Interface {}", existingElanInterface);
314 public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
315 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
316 PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
317 if (existingElanInterface != null) {
318 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
319 if (existingMacAddress.contains(updateStaticMacAddress)) {
322 existingMacAddress.add(updateStaticMacAddress);
323 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
324 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
325 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
327 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
328 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
333 public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
334 throws MacNotFoundException {
335 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
336 PhysAddress physAddress = new PhysAddress(macAddress);
337 if (existingElanInterface == null) {
340 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
341 if (existingMacAddress.contains(physAddress)) {
342 existingMacAddress.remove(physAddress);
343 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
344 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
345 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
347 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
348 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
350 throw new MacNotFoundException("deleteStaticMacAddress did not find MAC: " + macAddress);
355 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
356 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
357 List<MacEntry> macAddress = new ArrayList<>();
358 if (elanInfo == null) {
361 List<String> elanInterfaces = elanInfo.getElanInterfaces();
362 if (elanInterfaces != null && elanInterfaces.size() > 0) {
363 for (String elanInterface : elanInterfaces) {
364 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
365 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
366 && elanInterfaceMac.getMacEntry().size() > 0) {
367 macAddress.addAll(elanInterfaceMac.getMacEntry());
375 public void flushMACTable(String elanInstanceName) {
376 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
377 if (elanInfo == null) {
380 List<String> elanInterfaces = elanInfo.getElanInterfaces();
381 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
384 for (String elanInterface : elanInterfaces) {
385 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
386 if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
387 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
388 for (MacEntry macEntry : macEntries) {
390 deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
391 } catch (MacNotFoundException e) {
392 LOG.error("Mac Not Found Exception {}", e);
400 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
401 List<PhysAddress> physAddresses = new ArrayList<>();
402 for (String mac : macAddress) {
403 physAddresses.add(new PhysAddress(mac));
405 return physAddresses;
408 public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
409 List<PhysAddress> updatePhyAddresses) {
410 if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
411 List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
412 if (originalAddresses != null && !originalAddresses.isEmpty()) {
413 existingClonedPhyAddress.addAll(0, originalAddresses);
414 originalAddresses.removeAll(updatePhyAddresses);
415 updatePhyAddresses.removeAll(existingClonedPhyAddress);
418 return updatePhyAddresses;
422 public ElanInstance getElanInstance(String elanName) {
423 return ElanUtils.getElanInstanceByName(broker, elanName);
427 public List<ElanInstance> getElanInstances() {
428 List<ElanInstance> elanList = new ArrayList<>();
429 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
431 Optional<ElanInstances> elansOptional = elanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
432 elanInstancesIdentifier);
433 if (elansOptional.isPresent()) {
434 elanList.addAll(elansOptional.get().getElanInstance());
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().getElanInterface();
450 for (ElanInterface elanInterface : elanInterfaceList) {
451 if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
452 elanInterfaces.add(elanInterface.getName());
455 return elanInterfaces;
458 public boolean getGenerateIntBridgeMac() {
459 return generateIntBridgeMac;
462 public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
463 this.generateIntBridgeMac = generateIntBridgeMac;
467 public void createExternalElanNetworks(Node node) {
468 handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
469 createExternalElanNetwork(elanInstance, interfaceName);
475 public void createExternalElanNetwork(ElanInstance elanInstance) {
476 handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
477 createExternalElanNetwork(elanInstance1, interfaceName);
482 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
483 if (interfaceName == null) {
484 LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
488 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
489 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
493 public void deleteExternalElanNetworks(Node node) {
494 handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
495 deleteExternalElanNetwork(elanInstance, interfaceName);
501 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
502 handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
503 deleteExternalElanNetwork(elanInstance1, interfaceName);
508 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
509 if (interfaceName == null) {
510 LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
514 String elanInstanceName = elanInstance.getElanInstanceName();
515 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
516 if (elanInterface.startsWith(interfaceName)) {
517 deleteIetfInterface(elanInterface);
518 deleteElanInterface(elanInstanceName, elanInterface);
524 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
525 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
529 List<ElanInstance> elanInstances = getElanInstances();
530 if (elanInstances == null || elanInstances.isEmpty()) {
531 LOG.trace("No ELAN instances found");
535 Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
536 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
537 Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
538 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
539 Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
540 Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
542 for (ElanInstance elanInstance : elanInstances) {
543 String physicalNetworkName = elanInstance.getPhysicalNetworkName();
544 if (physicalNetworkName != null) {
545 String origPortName = origProviderMappping.get(physicalNetworkName);
546 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
547 if (origPortName != null && !origPortName.equals(updatedPortName)) {
548 deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
550 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
551 createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
558 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
559 DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
560 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
561 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
565 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
566 if (elanUtils.isExternal(dpnInterface)) {
571 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
576 public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
577 List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
578 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
579 LOG.trace("No ELAN interfaces defined for {}", elanInstanceName);
580 return Collections.emptySet();
583 Set<String> externalElanInterfaces = new HashSet<>();
584 for (String elanInterface : elanInterfaces) {
585 if (elanUtils.isExternal(elanInterface)) {
586 externalElanInterfaces.add(elanInterface);
590 return externalElanInterfaces;
594 public boolean isExternalInterface(String interfaceName) {
595 return elanUtils.isExternal(interfaceName);
599 public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
600 return ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
604 * Create ietf-interfaces based on the ELAN segment type.<br>
605 * For segment type flat - create transparent interface pointing to the
606 * patch-port attached to the physnet port.<br>
607 * For segment type vlan - create trunk interface pointing to the patch-port
608 * attached to the physnet port + trunk-member interface pointing to the
611 * @param elanInstance
614 * parent interface name
615 * @return the name of the interface to be added to the ELAN instance i.e.
616 * trunk-member name for vlan network and transparent for flat
617 * network or null otherwise
619 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
620 String interfaceName = null;
623 if (ElanUtils.isFlat(elanInstance)) {
624 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
625 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
626 IfL2vlan.L2vlanMode.Transparent, true);
627 } else if (ElanUtils.isVlan(elanInstance)) {
628 Long segmentationId = elanInstance.getSegmentationId();
629 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
630 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
631 // trunk interface may have been created by other vlan network
632 Interface trunkInterface = ElanUtils.getInterfaceFromConfigDS(trunkName, broker);
633 if (trunkInterface == null) {
634 interfaceManager.createVLANInterface(trunkName, parentRef, null, null, null,
635 IfL2vlan.L2vlanMode.Trunk, true);
637 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
638 IfL2vlan.L2vlanMode.TrunkMember, true);
640 } catch (InterfaceAlreadyExistsException e) {
641 LOG.trace("Interface {} was already created", interfaceName);
644 return interfaceName;
647 private void deleteIetfInterface(String interfaceName) {
648 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
649 InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
650 .child(Interface.class, interfaceKey).build();
651 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
652 LOG.debug("Deleting IETF interface {}", interfaceName);
655 private String getExtInterfaceName(Node node, String physicalNetworkName) {
656 if (physicalNetworkName == null) {
660 String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
661 if (providerMappingValue == null) {
662 LOG.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
663 node.getNodeId().getValue());
667 return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
668 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
671 private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
672 if (!bridgeMgr.isIntegrationBridge(node)) {
676 List<ElanInstance> elanInstances = getElanInstances();
677 if (elanInstances == null || elanInstances.isEmpty()) {
678 LOG.trace("No ELAN instances found");
682 for (ElanInstance elanInstance : elanInstances) {
683 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
684 if (interfaceName != null) {
685 function.apply(elanInstance, interfaceName);
690 private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
691 String elanInstanceName = elanInstance.getElanInstanceName();
692 if (elanInstance.getPhysicalNetworkName() == null) {
693 LOG.trace("No physical network attached to {}", elanInstanceName);
697 List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
698 if (nodes == null || nodes.isEmpty()) {
699 LOG.trace("No OVS nodes found while creating external network for ELAN {}",
700 elanInstance.getElanInstanceName());
704 for (Node node : nodes) {
705 if (bridgeMgr.isIntegrationBridge(node)) {
706 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
707 function.apply(elanInstance, interfaceName);