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 com.google.common.base.Optional;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
17 import java.util.List;
20 import java.util.concurrent.Future;
21 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 public class ElanServiceProvider implements IElanService {
70 private static final Logger LOG = LoggerFactory.getLogger(ElanServiceProvider.class);
72 private final IdManagerService idManager;
73 private final IInterfaceManager interfaceManager;
74 private final ElanInstanceManager elanInstanceManager;
75 private final ElanBridgeManager bridgeMgr;
76 private final DataBroker broker;
77 private final ElanStatusMonitor elanStatusMonitor;
78 private static ElanUtils elanUtils;
80 private boolean generateIntBridgeMac = true;
82 public ElanServiceProvider(IdManagerService idManager, IInterfaceManager interfaceManager,
83 ElanInstanceManager elanInstanceManager, ElanBridgeManager bridgeMgr,
84 DataBroker dataBroker,
85 ElanInterfaceManager elanInterfaceManager,
86 ElanStatusMonitor elanStatusMonitor, ElanUtils elanUtils, EntityOwnershipService entityOwnershipService) {
87 this.idManager = idManager;
88 this.interfaceManager = interfaceManager;
89 this.elanInstanceManager = elanInstanceManager;
90 this.bridgeMgr = bridgeMgr;
91 this.broker = dataBroker;
92 this.elanStatusMonitor = elanStatusMonitor;
93 this.elanUtils = elanUtils;
94 elanInterfaceManager.setElanUtils(elanUtils);
96 EntityOwnerUtils.registerEntityCandidateForOwnerShip(entityOwnershipService,
97 HwvtepSouthboundConstants.ELAN_ENTITY_TYPE, HwvtepSouthboundConstants.ELAN_ENTITY_TYPE,
99 } catch (CandidateAlreadyRegisteredException e) {
100 LOG.error("failed to register the entity");
105 LOG.info("Starting ElnaServiceProvider");
106 elanStatusMonitor.reportStatus("STARTING");
110 elanStatusMonitor.reportStatus("OPERATIONAL");
111 } catch (Exception e) {
112 LOG.error("Error initializing services", e);
113 elanStatusMonitor.reportStatus("ERROR");
117 private void createIdPool() {
118 CreateIdPoolInput createPool = new CreateIdPoolInputBuilder().setPoolName(ElanConstants.ELAN_ID_POOL_NAME)
119 .setLow(ElanConstants.ELAN_ID_LOW_VALUE).setHigh(ElanConstants.ELAN_ID_HIGH_VALUE).build();
121 Future<RpcResult<Void>> result = idManager.createIdPool(createPool);
122 if (result != null && result.get().isSuccessful()) {
123 LOG.debug("ELAN Id Pool is created successfully");
125 } catch (Exception e) {
126 LOG.error("Failed to create ELAN Id pool {}", e);
131 public boolean createElanInstance(String elanInstanceName, long macTimeout, String description) {
132 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
133 boolean isSuccess = true;
134 if (existingElanInstance != null) {
135 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
136 LOG.debug("Elan Instance is already present in the Operational DS {}", existingElanInstance);
139 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
140 .setDescription(description).setMacTimeout(macTimeout)
141 .setKey(new ElanInstanceKey(elanInstanceName)).build();
142 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
143 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
144 LOG.debug("Updating the Elan Instance {} with MAC TIME-OUT %l and Description %s ",
145 updateElanInstance, macTimeout, description);
148 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
149 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
151 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
152 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
153 LOG.debug("Creating the new Elan Instance {}", elanInstance);
159 public boolean createEtreeInstance(String elanInstanceName, long macTimeout, String description) {
160 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
161 boolean isSuccess = true;
162 if (existingElanInstance != null) {
163 if (compareWithExistingElanInstance(existingElanInstance, macTimeout, description)) {
164 LOG.warn("Etree Instance is already present in the Operational DS {}", existingElanInstance);
167 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
168 ElanInstance updateElanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
169 .setDescription(description).setMacTimeout(macTimeout)
170 .setKey(new ElanInstanceKey(elanInstanceName))
171 .addAugmentation(EtreeInstance.class, etreeInstance).build();
172 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
173 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), updateElanInstance);
174 LOG.debug("Updating the Etree Instance {} with MAC TIME-OUT %l and Description %s ",
175 updateElanInstance, macTimeout, description);
178 EtreeInstance etreeInstance = new EtreeInstanceBuilder().build();
179 ElanInstance elanInstance = new ElanInstanceBuilder().setElanInstanceName(elanInstanceName)
180 .setMacTimeout(macTimeout).setDescription(description).setKey(new ElanInstanceKey(elanInstanceName))
181 .addAugmentation(EtreeInstance.class, etreeInstance).build();
182 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
183 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName), elanInstance);
184 LOG.debug("Creating the new Etree Instance {}", elanInstance);
190 public EtreeInterface getEtreeInterfaceByElanInterfaceName(String elanInterface) {
191 return ElanUtils.getEtreeInterfaceByElanInterfaceName(broker, elanInterface);
194 public static boolean compareWithExistingElanInstance(ElanInstance existingElanInstance, long macTimeOut,
195 String description) {
196 boolean isEqual = false;
197 if (existingElanInstance.getMacTimeout() == macTimeOut
198 && existingElanInstance.getDescription().equals(description)) {
205 public void updateElanInstance(String elanInstanceName, long newMacTimout, String newDescription) {
206 createElanInstance(elanInstanceName, newMacTimout, newDescription);
210 public boolean deleteEtreeInstance(String etreeInstanceName) {
211 return deleteElanInstance(etreeInstanceName);
215 public boolean deleteElanInstance(String elanInstanceName) {
216 boolean isSuccess = false;
217 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
218 if (existingElanInstance == null) {
219 LOG.debug("Elan Instance is not present {}", existingElanInstance);
222 LOG.debug("Deletion of the existing Elan Instance {}", existingElanInstance);
223 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
224 ElanUtils.getElanInstanceConfigurationDataPath(elanInstanceName));
230 public void addEtreeInterface(String etreeInstanceName, String interfaceName, EtreeInterfaceType interfaceType,
231 List<String> staticMacAddresses, String description) {
232 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(etreeInstanceName);
233 if (existingElanInstance != null && existingElanInstance.getAugmentation(EtreeInstance.class) != null) {
234 EtreeInterface etreeInterface = new EtreeInterfaceBuilder().setEtreeInterfaceType(interfaceType).build();
235 ElanInterface elanInterface;
236 if (staticMacAddresses == null) {
237 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
238 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
239 .addAugmentation(EtreeInterface.class, etreeInterface).build();
241 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(etreeInstanceName)
242 .setDescription(description).setName(interfaceName)
243 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
244 .setKey(new ElanInterfaceKey(interfaceName))
245 .addAugmentation(EtreeInterface.class, etreeInterface).build();
247 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
248 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
249 LOG.debug("Creating the new Etree Interface {}", elanInterface);
254 public void addElanInterface(String elanInstanceName, String interfaceName, List<String> staticMacAddresses,
255 String description) {
256 ElanInstance existingElanInstance = elanInstanceManager.getElanInstanceByName(elanInstanceName);
257 if (existingElanInstance != null) {
258 ElanInterface elanInterface;
259 if (staticMacAddresses == null) {
260 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
261 .setDescription(description).setName(interfaceName).setKey(new ElanInterfaceKey(interfaceName))
264 elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
265 .setDescription(description).setName(interfaceName)
266 .setStaticMacEntries(getPhysAddress(staticMacAddresses))
267 .setKey(new ElanInterfaceKey(interfaceName)).build();
269 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
270 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
271 LOG.debug("Creating the new ELan Interface {}", elanInterface);
276 public void updateElanInterface(String elanInstanceName, String interfaceName,
277 List<String> updatedStaticMacAddresses, String newDescription) {
278 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
279 if (existingElanInterface == null) {
282 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
283 List<PhysAddress> updatedMacAddresses = getPhysAddress(updatedStaticMacAddresses);
284 List<PhysAddress> updatedPhysAddress = getUpdatedPhyAddress(existingMacAddress, updatedMacAddresses);
285 if (updatedPhysAddress.size() > 0) {
286 LOG.debug("updating the ElanInterface with new Mac Entries {}", updatedStaticMacAddresses);
287 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
288 .setName(interfaceName).setDescription(newDescription).setStaticMacEntries(updatedPhysAddress)
289 .setKey(new ElanInterfaceKey(interfaceName)).build();
290 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
291 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
296 public void deleteEtreeInterface(String elanInstanceName, String interfaceName) {
297 deleteElanInterface(elanInstanceName, interfaceName);
298 LOG.debug("deleting the Etree Interface {}", interfaceName);
302 public void deleteElanInterface(String elanInstanceName, String interfaceName) {
303 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
304 if (existingElanInterface != null) {
305 ElanUtils.delete(broker, LogicalDatastoreType.CONFIGURATION,
306 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName));
307 LOG.debug("deleting the Elan Interface {}", existingElanInterface);
312 public void addStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress) {
313 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
314 PhysAddress updateStaticMacAddress = new PhysAddress(macAddress);
315 if (existingElanInterface != null) {
316 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
317 if (existingMacAddress.contains(updateStaticMacAddress)) {
320 existingMacAddress.add(updateStaticMacAddress);
321 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
322 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
323 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
325 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
326 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
331 public void deleteStaticMacAddress(String elanInstanceName, String interfaceName, String macAddress)
332 throws MacNotFoundException {
333 ElanInterface existingElanInterface = ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
334 PhysAddress physAddress = new PhysAddress(macAddress);
335 if (existingElanInterface == null) {
338 List<PhysAddress> existingMacAddress = existingElanInterface.getStaticMacEntries();
339 if (existingMacAddress.contains(physAddress)) {
340 existingMacAddress.remove(physAddress);
341 ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName)
342 .setName(interfaceName).setStaticMacEntries(existingMacAddress)
343 .setDescription(existingElanInterface.getDescription()).setKey(new ElanInterfaceKey(interfaceName))
345 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
346 ElanUtils.getElanInterfaceConfigurationDataPathId(interfaceName), elanInterface);
348 throw new MacNotFoundException("deleteStaticMacAddress did not find MAC: " + macAddress);
353 public Collection<MacEntry> getElanMacTable(String elanInstanceName) {
354 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
355 List<MacEntry> macAddress = new ArrayList<>();
356 if (elanInfo == null) {
359 List<String> elanInterfaces = elanInfo.getElanInterfaces();
360 if (elanInterfaces != null && elanInterfaces.size() > 0) {
361 for (String elanInterface : elanInterfaces) {
362 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
363 if (elanInterfaceMac != null && elanInterfaceMac.getMacEntry() != null
364 && elanInterfaceMac.getMacEntry().size() > 0) {
365 macAddress.addAll(elanInterfaceMac.getMacEntry());
373 public void flushMACTable(String elanInstanceName) {
374 Elan elanInfo = ElanUtils.getElanByName(broker, elanInstanceName);
375 if (elanInfo == null) {
378 List<String> elanInterfaces = elanInfo.getElanInterfaces();
379 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
382 for (String elanInterface : elanInterfaces) {
383 ElanInterfaceMac elanInterfaceMac = elanUtils.getElanInterfaceMacByInterfaceName(elanInterface);
384 if (elanInterfaceMac.getMacEntry() != null && elanInterfaceMac.getMacEntry().size() > 0) {
385 List<MacEntry> macEntries = elanInterfaceMac.getMacEntry();
386 for (MacEntry macEntry : macEntries) {
388 deleteStaticMacAddress(elanInstanceName, elanInterface, macEntry.getMacAddress().getValue());
389 } catch (MacNotFoundException e) {
390 LOG.error("Mac Not Found Exception {}", e);
398 public static List<PhysAddress> getPhysAddress(List<String> macAddress) {
399 List<PhysAddress> physAddresses = new ArrayList<>();
400 for (String mac : macAddress) {
401 physAddresses.add(new PhysAddress(mac));
403 return physAddresses;
406 public List<PhysAddress> getUpdatedPhyAddress(List<PhysAddress> originalAddresses,
407 List<PhysAddress> updatePhyAddresses) {
408 if (updatePhyAddresses != null && !updatePhyAddresses.isEmpty()) {
409 List<PhysAddress> existingClonedPhyAddress = new ArrayList<>();
410 if (originalAddresses != null && !originalAddresses.isEmpty()) {
411 existingClonedPhyAddress.addAll(0, originalAddresses);
412 originalAddresses.removeAll(updatePhyAddresses);
413 updatePhyAddresses.removeAll(existingClonedPhyAddress);
416 return updatePhyAddresses;
420 public ElanInstance getElanInstance(String elanName) {
421 return ElanUtils.getElanInstanceByName(broker, elanName);
425 public List<ElanInstance> getElanInstances() {
426 List<ElanInstance> elanList = new ArrayList<>();
427 InstanceIdentifier<ElanInstances> elanInstancesIdentifier = InstanceIdentifier.builder(ElanInstances.class)
429 Optional<ElanInstances> elansOptional = elanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
430 elanInstancesIdentifier);
431 if (elansOptional.isPresent()) {
432 elanList.addAll(elansOptional.get().getElanInstance());
438 public List<String> getElanInterfaces(String elanInstanceName) {
439 List<String> elanInterfaces = new ArrayList<>();
440 InstanceIdentifier<ElanInterfaces> elanInterfacesIdentifier = InstanceIdentifier.builder(ElanInterfaces.class)
442 Optional<ElanInterfaces> elanInterfacesOptional = elanUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
443 elanInterfacesIdentifier);
444 if (!elanInterfacesOptional.isPresent()) {
445 return elanInterfaces;
447 List<ElanInterface> elanInterfaceList = elanInterfacesOptional.get().getElanInterface();
448 for (ElanInterface elanInterface : elanInterfaceList) {
449 if (elanInterface.getElanInstanceName().equals(elanInstanceName)) {
450 elanInterfaces.add(elanInterface.getName());
453 return elanInterfaces;
456 public boolean getGenerateIntBridgeMac() {
457 return generateIntBridgeMac;
460 public void setGenerateIntBridgeMac(boolean generateIntBridgeMac) {
461 this.generateIntBridgeMac = generateIntBridgeMac;
465 public void createExternalElanNetworks(Node node) {
466 handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
467 createExternalElanNetwork(elanInstance, interfaceName);
473 public void createExternalElanNetwork(ElanInstance elanInstance) {
474 handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
475 createExternalElanNetwork(elanInstance1, interfaceName);
480 private void createExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
481 if (interfaceName == null) {
482 LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
486 String elanInterfaceName = createIetfInterfaces(elanInstance, interfaceName);
487 addElanInterface(elanInstance.getElanInstanceName(), elanInterfaceName, null, null);
491 public void deleteExternalElanNetworks(Node node) {
492 handleExternalElanNetworks(node, (elanInstance, interfaceName) -> {
493 deleteExternalElanNetwork(elanInstance, interfaceName);
499 public void deleteExternalElanNetwork(ElanInstance elanInstance) {
500 handleExternalElanNetwork(elanInstance, (elanInstance1, interfaceName) -> {
501 deleteExternalElanNetwork(elanInstance1, interfaceName);
506 private void deleteExternalElanNetwork(ElanInstance elanInstance, String interfaceName) {
507 if (interfaceName == null) {
508 LOG.trace("No physial interface is attached to {}", elanInstance.getPhysicalNetworkName());
512 String elanInstanceName = elanInstance.getElanInstanceName();
513 for (String elanInterface : getExternalElanInterfaces(elanInstanceName)) {
514 if (elanInterface.startsWith(interfaceName)) {
515 deleteIetfInterface(elanInterface);
516 deleteElanInterface(elanInstanceName, elanInterface);
522 public void updateExternalElanNetworks(Node origNode, Node updatedNode) {
523 if (!bridgeMgr.isIntegrationBridge(updatedNode)) {
527 List<ElanInstance> elanInstances = getElanInstances();
528 if (elanInstances == null || elanInstances.isEmpty()) {
529 LOG.trace("No ELAN instances found");
533 Optional<Map<String, String>> origProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(origNode,
534 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
535 Optional<Map<String, String>> updatedProviderMapOpt = bridgeMgr.getOpenvswitchOtherConfigMap(updatedNode,
536 ElanBridgeManager.PROVIDER_MAPPINGS_KEY);
537 Map<String, String> origProviderMappping = origProviderMapOpt.or(Collections.emptyMap());
538 Map<String, String> updatedProviderMappping = updatedProviderMapOpt.or(Collections.emptyMap());
540 for (ElanInstance elanInstance : elanInstances) {
541 String physicalNetworkName = elanInstance.getPhysicalNetworkName();
542 if (physicalNetworkName != null) {
543 String origPortName = origProviderMappping.get(physicalNetworkName);
544 String updatedPortName = updatedProviderMappping.get(physicalNetworkName);
545 if (origPortName != null && !origPortName.equals(updatedPortName)) {
546 deleteExternalElanNetwork(elanInstance, getExtInterfaceName(origNode, physicalNetworkName));
548 if (updatedPortName != null && !updatedPortName.equals(origPortName)) {
549 createExternalElanNetwork(elanInstance, getExtInterfaceName(updatedNode, updatedPortName));
556 public String getExternalElanInterface(String elanInstanceName, BigInteger dpnId) {
557 DpnInterfaces dpnInterfaces = elanUtils.getElanInterfaceInfoByElanDpn(elanInstanceName, dpnId);
558 if (dpnInterfaces == null || dpnInterfaces.getInterfaces() == null) {
559 LOG.trace("Elan {} does not have interfaces in DPN {}", elanInstanceName, dpnId);
563 for (String dpnInterface : dpnInterfaces.getInterfaces()) {
564 if (elanUtils.isExternal(dpnInterface)) {
569 LOG.trace("Elan {} does not have any external interace attached to DPN {}", elanInstanceName, dpnId);
574 public Collection<String> getExternalElanInterfaces(String elanInstanceName) {
575 List<String> elanInterfaces = getElanInterfaces(elanInstanceName);
576 if (elanInterfaces == null || elanInterfaces.isEmpty()) {
577 LOG.trace("No ELAN interfaces defined for {}", elanInstanceName);
578 return Collections.emptySet();
581 Set<String> externalElanInterfaces = new HashSet<>();
582 for (String elanInterface : elanInterfaces) {
583 if (elanUtils.isExternal(elanInterface)) {
584 externalElanInterfaces.add(elanInterface);
588 return externalElanInterfaces;
592 public boolean isExternalInterface(String interfaceName) {
593 return elanUtils.isExternal(interfaceName);
597 public ElanInterface getElanInterfaceByElanInterfaceName(String interfaceName) {
598 return ElanUtils.getElanInterfaceByElanInterfaceName(broker, interfaceName);
602 * Create ietf-interfaces based on the ELAN segment type.<br>
603 * For segment type flat - create transparent interface pointing to the
604 * patch-port attached to the physnet port.<br>
605 * For segment type vlan - create trunk interface pointing to the patch-port
606 * attached to the physnet port + trunk-member interface pointing to the
609 * @param elanInstance
612 * parent interface name
613 * @return the name of the interface to be added to the ELAN instance i.e.
614 * trunk-member name for vlan network and transparent for flat
615 * network or null otherwise
617 private String createIetfInterfaces(ElanInstance elanInstance, String parentRef) {
618 String interfaceName = null;
621 if (ElanUtils.isFlat(elanInstance)) {
622 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + "flat";
623 interfaceManager.createVLANInterface(interfaceName, parentRef, null, null, null,
624 IfL2vlan.L2vlanMode.Transparent, true);
625 } else if (ElanUtils.isVlan(elanInstance)) {
626 Long segmentationId = elanInstance.getSegmentationId();
627 interfaceName = parentRef + IfmConstants.OF_URI_SEPARATOR + segmentationId;
628 String trunkName = parentRef + IfmConstants.OF_URI_SEPARATOR + "trunk";
629 // trunk interface may have been created by other vlan network
630 Interface trunkInterface = ElanUtils.getInterfaceFromConfigDS(trunkName, broker);
631 if (trunkInterface == null) {
632 interfaceManager.createVLANInterface(trunkName, parentRef, null, null, null,
633 IfL2vlan.L2vlanMode.Trunk, true);
635 interfaceManager.createVLANInterface(interfaceName, trunkName, null, segmentationId.intValue(), null,
636 IfL2vlan.L2vlanMode.TrunkMember, true);
638 } catch (InterfaceAlreadyExistsException e) {
639 LOG.trace("Interface {} was already created", interfaceName);
642 return interfaceName;
645 private void deleteIetfInterface(String interfaceName) {
646 InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
647 InstanceIdentifier<Interface> interfaceInstanceIdentifier = InstanceIdentifier.builder(Interfaces.class)
648 .child(Interface.class, interfaceKey).build();
649 MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, interfaceInstanceIdentifier);
650 LOG.debug("Deleting IETF interface {}", interfaceName);
653 private String getExtInterfaceName(Node node, String physicalNetworkName) {
654 if (physicalNetworkName == null) {
658 String providerMappingValue = bridgeMgr.getProviderMappingValue(node, physicalNetworkName);
659 if (providerMappingValue == null) {
660 LOG.trace("No provider mapping found for physicalNetworkName {} node {}", physicalNetworkName,
661 node.getNodeId().getValue());
665 return bridgeMgr.southboundUtils.getDataPathId(node) + IfmConstants.OF_URI_SEPARATOR
666 + bridgeMgr.getIntBridgePortNameFor(node, providerMappingValue);
669 private void handleExternalElanNetworks(Node node, BiFunction<ElanInstance, String, Void> function) {
670 if (!bridgeMgr.isIntegrationBridge(node)) {
674 List<ElanInstance> elanInstances = getElanInstances();
675 if (elanInstances == null || elanInstances.isEmpty()) {
676 LOG.trace("No ELAN instances found");
680 for (ElanInstance elanInstance : elanInstances) {
681 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
682 if (interfaceName != null) {
683 function.apply(elanInstance, interfaceName);
688 private void handleExternalElanNetwork(ElanInstance elanInstance, BiFunction<ElanInstance, String, Void> function) {
689 String elanInstanceName = elanInstance.getElanInstanceName();
690 if (elanInstance.getPhysicalNetworkName() == null) {
691 LOG.trace("No physical network attached to {}", elanInstanceName);
695 List<Node> nodes = bridgeMgr.southboundUtils.getOvsdbNodes();
696 if (nodes == null || nodes.isEmpty()) {
697 LOG.trace("No OVS nodes found while creating external network for ELAN {}",
698 elanInstance.getElanInstanceName());
702 for (Node node : nodes) {
703 if (bridgeMgr.isIntegrationBridge(node)) {
704 String interfaceName = getExtInterfaceName(node, elanInstance.getPhysicalNetworkName());
705 function.apply(elanInstance, interfaceName);