ELAN FT Support for BE
[vpnservice.git] / elanmanager / elanmanager-impl / src / main / java / org / opendaylight / vpnservice / elan / internal / ElanNodeListener.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 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
11 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
12 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
15 import org.opendaylight.vpnservice.elan.utils.ElanConstants;
16 import org.opendaylight.vpnservice.mdsalutil.*;
17 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 public class ElanNodeListener extends AbstractDataChangeListener<Node> {
31
32     private static final Logger logger = LoggerFactory.getLogger(ElanNodeListener.class);
33
34     private IMdsalApiManager mdsalManager;
35     private ListenerRegistration<DataChangeListener> listenerRegistration;
36     private final DataBroker broker;
37
38     public ElanNodeListener(final DataBroker db, IMdsalApiManager mdsalManager) {
39         super(Node.class);
40         broker = db;
41         this.mdsalManager = mdsalManager;
42         registerListener(db);
43     }
44
45     private void registerListener(final DataBroker db) {
46         try {
47             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
48                     getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
49         } catch (final Exception e) {
50             logger.error("IfmNodeConnectorListener: DataChange listener registration fail!", e);
51             throw new IllegalStateException("IfmNodeConnectorListener: registration Listener failed.", e);
52         }
53     }
54
55     private InstanceIdentifier<Node> getWildCardPath() {
56         return InstanceIdentifier.create(Nodes.class).child(Node.class);
57     }
58
59
60     @Override
61     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
62
63     }
64
65     @Override
66     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
67
68     }
69
70     @Override
71     protected void add(InstanceIdentifier<Node> identifier, Node add) {
72         NodeId nodeId = add.getId();
73         String[] node =  nodeId.getValue().split(":");
74         BigInteger dpId = new BigInteger(node[1]);
75         createTableMissEntry(dpId);
76     }
77
78     public void createTableMissEntry(BigInteger dpnId) {
79         setupTableMissSmacFlow(dpnId);
80         setupTableMissDmacFlow(dpnId);
81     }
82
83     private void setupTableMissSmacFlow(BigInteger dpId) {
84         List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
85         List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
86         List <ActionInfo> actionsInfos = new ArrayList <ActionInfo> ();
87         actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
88         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
89         mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_DMAC_TABLE }));
90
91         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_SMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_SMAC_TABLE),
92                 0, "ELAN sMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC,
93                 mkMatches, mkInstructions);
94         mdsalManager.installFlow(flowEntity);
95     }
96
97     private void setupTableMissDmacFlow(BigInteger dpId) {
98         List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
99
100         List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
101         mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { ElanConstants.ELAN_UNKNOWN_DMAC_TABLE }));
102
103         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_DMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_DMAC_TABLE),
104                 0, "ELAN dMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC,
105                 mkMatches, mkInstructions);
106         mdsalManager.installFlow(flowEntity);
107     }
108
109     private String getTableMissFlowRef(long tableId) {
110         return new StringBuffer().append(tableId).toString();
111     }
112 }