Using Table Ids from NwConstants.java
[netvirt.git] / vpnservice / cloud-servicechain / cloud-servicechain-impl / src / main / java / org / opendaylight / netvirt / cloudservicechain / listeners / ElanDpnInterfacesListener.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.netvirt.cloudservicechain.listeners;
9
10 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev170511.elan.to.pseudo.port.data.list.ElanToPseudoPortData;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
15 import org.opendaylight.yangtools.concepts.ListenerRegistration;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanDpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.ElanDpnInterfacesList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.dpn.interfaces.elan.dpn.interfaces.list.DpnInterfaces;
22 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
23 import org.opendaylight.netvirt.cloudservicechain.utils.ElanServiceChainUtils;
24 import org.opendaylight.netvirt.cloudservicechain.CloudServiceChainConstants;
25 import org.opendaylight.genius.mdsalutil.NwConstants;
26 import java.math.BigInteger;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Optional;
31
32 public class ElanDpnInterfacesListener extends AbstractDataChangeListener<DpnInterfaces> implements AutoCloseable {
33
34     private ListenerRegistration<DataChangeListener> listenerRegistration;
35     private static final Logger logger = LoggerFactory.getLogger(ElanDpnInterfacesListener.class);
36     private final DataBroker broker;
37     private IMdsalApiManager mdsalManager;
38
39     public ElanDpnInterfacesListener(final DataBroker db, final IMdsalApiManager mdsalMgr) {
40         super(DpnInterfaces.class);
41         this.broker = db;
42         this.mdsalManager = mdsalMgr;
43         registerListener(db);
44     }
45
46     private void registerListener(final DataBroker db) {
47         try {
48             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
49                     getWildCardPath(),
50                     ElanDpnInterfacesListener.this,
51                     AsyncDataBroker.DataChangeScope.SUBTREE);
52         } catch (final Exception e) {
53             logger.error("ElanDpnInterfacesListener: DataChange listener registration fail!", e);
54             throw new IllegalStateException("ElanDpnInterfacesListener: registration Listener failed.", e);
55         }
56     }
57
58     public InstanceIdentifier<DpnInterfaces> getWildCardPath() {
59         return InstanceIdentifier.builder(ElanDpnInterfaces.class).child(ElanDpnInterfacesList.class)
60                 .child(DpnInterfaces.class).build();
61     }
62
63     @Override
64     protected void add(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
65         final String elanName = getElanName(identifier);
66         BigInteger addDpnId = dpnInterfaces.getDpId();
67         Optional<ElanToPseudoPortData> elanLPortListOpc = ElanServiceChainUtils.getElanToLportTagList(broker, elanName);
68         if (elanLPortListOpc.isPresent()) {
69             int scfTag = elanLPortListOpc.get().getScfTag();
70             Long elanLportTag = elanLPortListOpc.get().getElanLportTag();
71             if ( elanLportTag != null ) {
72                 short tableId = NwConstants.SCF_DOWN_SUB_FILTER_TCP_BASED_TABLE;
73                 handleUpdate(addDpnId, elanName, tableId, elanLportTag.intValue(), scfTag, NwConstants.ADD_FLOW);
74             } else {
75                 logger.debug("Could not find lportTag for elan={}", elanName);
76             }
77         }
78
79     }
80
81     @Override
82     protected void remove(InstanceIdentifier<DpnInterfaces> identifier, final DpnInterfaces dpnInterfaces) {
83         final String elanName = getElanName(identifier);
84         BigInteger removeDpnId = dpnInterfaces.getDpId();
85         Optional<ElanToPseudoPortData> elanLPortListOpc = ElanServiceChainUtils.getElanToLportTagList(broker, elanName);
86         if (elanLPortListOpc.isPresent()) {
87             Integer scfTag = elanLPortListOpc.get().getScfTag();
88             Long elanLportTag = elanLPortListOpc.get().getElanLportTag();
89             if ( scfTag != null && elanLportTag != null ) {
90                 handleUpdate(removeDpnId, elanName,  (short) 0 /* tableId, ignored in removals */,
91                              elanLportTag.intValue(), 0 /* scfTag, ignored in removals */, NwConstants.DEL_FLOW);
92             } else {
93                 logger.debug("One of scfTag or lPortTag is null for elan={}:  scfTag={}  lportTag={}",
94                              elanName, scfTag, elanLportTag);
95             }
96         }
97     }
98
99     @Override
100     protected void update(InstanceIdentifier<DpnInterfaces> identifier, DpnInterfaces original,
101             final DpnInterfaces dpnInterfaces) {
102
103     }
104
105     @Override
106     public void close() {
107         if (listenerRegistration != null) {
108             try {
109                 listenerRegistration.close();
110             } catch (final Exception e) {
111                 logger.error("Error when cleaning up DataChangeListener.", e);
112             }
113             listenerRegistration = null;
114         }
115         logger.info("ElanDpnInterfaces listener Closed");
116     }
117
118     private String getElanName(InstanceIdentifier<DpnInterfaces> identifier) {
119         return identifier.firstKeyOf(ElanDpnInterfacesList.class).getElanInstanceName();
120     }
121
122     private void handleUpdate(BigInteger dpnId, String elanName,  short tableId, int elanLportTag, int scfTag, int addOrRemove) {
123         Optional<ElanInstance> elanInstance = ElanServiceChainUtils.getElanInstanceByName(broker, elanName);
124         if ( !elanInstance.isPresent() ) {
125             logger.debug("Could not find an Elan Instance with name={}", elanName);
126             return;
127         }
128
129         Long vni = elanInstance.get().getSegmentationId();
130         int elanTag = elanInstance.get().getElanTag().intValue();
131
132         ElanServiceChainUtils.programLPortDispatcherToScf(mdsalManager, dpnId, elanTag, elanLportTag, tableId, scfTag,
133                                                           addOrRemove);
134         ElanServiceChainUtils.programLPortDispatcherFromScf(mdsalManager, dpnId, elanLportTag, elanTag, addOrRemove);
135         ElanServiceChainUtils.programExternalTunnelTable(mdsalManager, dpnId, elanLportTag, vni, elanTag, addOrRemove);
136     }
137 }