Imported vpnservice as a subtree
[netvirt.git] / vpnservice / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / internal / ElanForwardingEntriesHandler.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.vpnservice.elan.internal;
9
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.vpnservice.elan.utils.ElanUtils;
16 import org.opendaylight.vpnservice.interfacemgr.globals.InterfaceInfo;
17 import org.opendaylight.vpnservice.itm.api.IITMProvider;
18 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
19 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
20 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInterfaces;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.interfaces.ElanInterface;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntry;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntryBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.forwarding.entries.MacEntryKey;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34 public class ElanForwardingEntriesHandler extends AbstractDataChangeListener<ElanInterface> implements AutoCloseable {
35
36     private static final Logger logger = LoggerFactory.getLogger(ElanForwardingEntriesHandler.class);
37     private DataBroker broker;
38     private ListenerRegistration<DataChangeListener> listenerRegistration;
39
40     public ElanForwardingEntriesHandler(DataBroker db){
41         super(ElanInterface.class);
42         this.broker = db;
43     }
44
45     private InstanceIdentifier<?> getWildCardPath() {
46         return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
47     }
48
49     public void updateElanInterfaceForwardingTablesList(String elanInstanceName, String interfaceName, String existingInterfaceName, MacEntry mac) {
50         if(existingInterfaceName == interfaceName) {
51             logger.error(String.format("Static MAC address %s has already been added for the same ElanInstance %s on the same Logical Interface Port %s."
52                     + " No operation will be done.", mac.getMacAddress().toString(), elanInstanceName, interfaceName));
53         } else {
54             logger.warn(String.format("Static MAC address %s had already been added for ElanInstance %s on Logical Interface Port %s. "
55                     + "This would be considered as MAC movement scenario and old static mac will be removed and new static MAC will be added"
56                     + "for ElanInstance %s on Logical Interface Port %s", mac.getMacAddress().toString(), elanInstanceName, interfaceName, elanInstanceName, interfaceName));
57             //Update the  ElanInterface Forwarding Container & ElanForwarding Container
58             deleteElanInterfaceForwardingTablesList(existingInterfaceName, mac);
59             createElanInterfaceForwardingTablesList(interfaceName, mac);
60             updateElanForwardingTablesList(elanInstanceName, interfaceName, mac);
61         }
62
63     }
64
65     public void addElanInterfaceForwardingTableList(ElanInstance elanInstance, String interfaceName, PhysAddress physAddress) {
66         MacEntry macEntry = new MacEntryBuilder().setIsStaticAddress(true).setMacAddress(physAddress).setInterface(interfaceName).setKey(new MacEntryKey(physAddress)).build();
67         createElanForwardingTablesList(elanInstance.getElanInstanceName(), macEntry);
68         createElanInterfaceForwardingTablesList(interfaceName, macEntry);
69     }
70
71     public void deleteElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac) {
72         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
73         MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
74         if(existingInterfaceMacEntry != null) {
75             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId);
76         }
77     }
78
79     public void createElanInterfaceForwardingTablesList(String interfaceName, MacEntry mac) {
80         InstanceIdentifier<MacEntry> existingMacEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, mac.getMacAddress());
81         MacEntry existingInterfaceMacEntry = ElanUtils.getInterfaceMacEntriesOperationalDataPathFromId(existingMacEntryId);
82         if(existingInterfaceMacEntry == null) {
83             MacEntry macEntry = new MacEntryBuilder().setMacAddress(mac.getMacAddress()).setInterface(interfaceName).setIsStaticAddress(true).setKey(new MacEntryKey(mac.getMacAddress())).build();
84             MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, existingMacEntryId, macEntry);
85
86         }
87     }
88
89     public void updateElanForwardingTablesList(String elanName, String interfaceName, MacEntry mac) {
90         InstanceIdentifier<MacEntry> macEntryId =  ElanUtils.getMacEntryOperationalDataPath(elanName, mac.getMacAddress());
91         MacEntry existingMacEntry = ElanUtils.getMacEntryFromElanMacId(macEntryId);
92         if(existingMacEntry != null) {
93             ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
94             MacEntry newMacEntry = new MacEntryBuilder().setInterface(interfaceName).setIsStaticAddress(true).setMacAddress(mac.getMacAddress()).setKey(new MacEntryKey(mac.getMacAddress())).build();
95             MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, macEntryId, newMacEntry);
96         }
97     }
98
99     private void createElanForwardingTablesList(String elanName, MacEntry macEntry) {
100         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanName, macEntry.getMacAddress());
101         Optional<MacEntry> existingMacEntry = ElanUtils.read(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
102         if(!existingMacEntry.isPresent()) {
103             MDSALUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, macEntryId, macEntry);
104         }
105     }
106
107     public void deleteElanInterfaceForwardingEntries(ElanInstance elanInfo, InterfaceInfo interfaceInfo, MacEntry macEntry) {
108         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getMacEntryOperationalDataPath(elanInfo.getElanInstanceName(), macEntry.getMacAddress());
109         ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
110         deleteElanInterfaceForwardingTablesList(interfaceInfo.getInterfaceName(), macEntry);
111         ElanUtils.deleteMacFlows(elanInfo, interfaceInfo, macEntry);
112     }
113
114     public void deleteElanInterfaceMacForwardingEntries(String interfaceName, PhysAddress physAddress) {
115         InstanceIdentifier<MacEntry> macEntryId = ElanUtils.getInterfaceMacEntriesIdentifierOperationalDataPath(interfaceName, physAddress);
116         ElanUtils.delete(broker, LogicalDatastoreType.OPERATIONAL, macEntryId);
117     }
118
119     @Override
120     protected void remove(InstanceIdentifier<ElanInterface> identifier, ElanInterface del) {
121
122     }
123
124     @Override
125     protected void update(InstanceIdentifier<ElanInterface> identifier, ElanInterface original, ElanInterface update) {
126
127     }
128
129     @Override
130     protected void add(InstanceIdentifier<ElanInterface> identifier, ElanInterface add) {
131
132     }
133
134     @Override
135     public void close() throws Exception {
136         if (listenerRegistration != null) {
137             try {
138                 listenerRegistration.close();
139             } catch (final Exception e) {
140                 logger.error("Error when cleaning up Elan DataChangeListener.", e);
141             }
142             listenerRegistration = null;
143         }
144     }
145 }