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