4cea2b858818042f144a49ad6cfa7b6c5615c5e7
[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.MatchInfoBase;
26 import org.opendaylight.genius.mdsalutil.NwConstants;
27 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
28 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
29 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
30 import org.opendaylight.netvirt.elan.utils.ElanConstants;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.config.rev150710.ElanConfig;
35 import org.opendaylight.yangtools.concepts.ListenerRegistration;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class ElanNodeListener extends AbstractDataChangeListener<Node> implements AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(ElanNodeListener.class);
43     private static final String LEARN_MATCH_REG4_VALUE = "1";
44
45     private final DataBroker broker;
46     private final IMdsalApiManager mdsalManager;
47     private final int tempSmacLearnTimeout;
48
49     private ListenerRegistration<DataChangeListener> listenerRegistration;
50
51     public ElanNodeListener(DataBroker dataBroker, IMdsalApiManager mdsalManager, ElanConfig elanConfig) {
52         super(Node.class);
53         this.broker = dataBroker;
54         this.mdsalManager = mdsalManager;
55         this.tempSmacLearnTimeout = elanConfig.getTempSmacLearnTimeout();
56     }
57
58     public void init() {
59         registerListener(broker);
60     }
61
62     private void registerListener(final DataBroker db) {
63         listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
64             getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
65     }
66
67     private InstanceIdentifier<Node> getWildCardPath() {
68         return InstanceIdentifier.create(Nodes.class).child(Node.class);
69     }
70
71     @Override
72     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
73     }
74
75     @Override
76     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
77     }
78
79     @Override
80     protected void add(InstanceIdentifier<Node> identifier, Node add) {
81         NodeId nodeId = add.getId();
82         String[] node =  nodeId.getValue().split(":");
83         if (node.length < 2) {
84             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
85             return;
86         }
87         BigInteger dpId = new BigInteger(node[1]);
88         createTableMissEntry(dpId);
89     }
90
91     public void createTableMissEntry(BigInteger dpnId) {
92         setupTableMissSmacFlow(dpnId);
93         setupTableMissDmacFlow(dpnId);
94     }
95
96     private void setupTableMissSmacFlow(BigInteger dpId) {
97         List<ActionInfo> actionsInfos = new ArrayList<>();
98         actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
99
100         String[][] learnActionMatches = new String[2][];
101         learnActionMatches[0] = new String[] { NwConstants.LearnFlowModsType.MATCH_FROM_FIELD.name(),
102                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getHexType(),
103                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getHexType(),
104                 NwConstants.NxmOfFieldType.NXM_OF_ETH_SRC.getFlowModHeaderLen() };
105         learnActionMatches[1] = new String[] {
106                 NwConstants.LearnFlowModsType.COPY_FROM_VALUE.name(), LEARN_MATCH_REG4_VALUE,
107                 NwConstants.NxmOfFieldType.NXM_NX_REG4.getHexType(), "8" };
108
109         String[] header = new String[] {
110             String.valueOf(0),
111             String.valueOf(tempSmacLearnTimeout),
112             BigInteger.ZERO.toString(),
113             ElanConstants.COOKIE_ELAN_LEARNED_SMAC.toString(),
114             "0",
115             Short.toString(NwConstants.ELAN_SMAC_LEARNED_TABLE),
116             String.valueOf(0),
117             String.valueOf(0)
118         };
119         actionsInfos.add(new ActionInfo(ActionType.learn, header, learnActionMatches));
120
121         List<InstructionInfo> mkInstructions = new ArrayList<>();
122         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
123         mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
124
125         List<MatchInfo> mkMatches = new ArrayList<>();
126         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
127                 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
128                 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
129         mdsalManager.installFlow(flowEntity);
130
131         addSmacBaseTableFlow(dpId);
132         addSmacLearnedTableFlow(dpId);
133     }
134
135     private void addSmacBaseTableFlow(BigInteger dpId) {
136         //T48 - resubmit to T49 & T50
137         List<ActionInfo> actionsInfo = new ArrayList<>();
138         actionsInfo.add(new ActionInfo(ActionType.nx_resubmit,
139                 new String[] {Short.toString(NwConstants.ELAN_SMAC_LEARNED_TABLE) }));
140         actionsInfo.add(new ActionInfo(ActionType.nx_resubmit,
141                 new String[] {Short.toString(NwConstants.ELAN_SMAC_TABLE) }));
142         List<InstructionInfo> mkInstruct = new ArrayList<>();
143         mkInstruct.add(new InstructionInfo(InstructionType.apply_actions, actionsInfo));
144         List<MatchInfo> mkMatch = new ArrayList<>();
145         FlowEntity doubleResubmitTable = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_BASE_TABLE,
146                 getTableMissFlowRef(NwConstants.ELAN_BASE_TABLE),
147                 0, "Elan sMac resubmit table", 0, 0,
148                 ElanConstants.COOKIE_ELAN_BASE_SMAC, mkMatch, mkInstruct);
149         mdsalManager.installFlow(doubleResubmitTable);
150     }
151
152     private void addSmacLearnedTableFlow(BigInteger dpId) {
153         //T50 - match on Reg4 and goto T51
154         List<MatchInfoBase> mkMatches = new ArrayList<>();
155         mkMatches.add(new NxMatchInfo(NxMatchFieldType.nxm_reg_4, new long[] {
156                 Long.valueOf(LEARN_MATCH_REG4_VALUE).longValue() }));
157         List<InstructionInfo> mkInstructions = new ArrayList<>();
158         mkInstructions.add(new InstructionInfo(InstructionType.goto_table,
159                 new long[] { NwConstants.ELAN_DMAC_TABLE }));
160         String flowRef = new StringBuffer().append(NwConstants.ELAN_SMAC_TABLE).append(NwConstants.FLOWID_SEPARATOR)
161                 .append(LEARN_MATCH_REG4_VALUE).toString();
162         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE, flowRef,
163                 10, "ELAN sMac Table Reg4 Flow", 0, 0,
164                 ElanConstants.COOKIE_ELAN_KNOWN_SMAC.add(new BigInteger(LEARN_MATCH_REG4_VALUE)),
165                 mkMatches, mkInstructions);
166         mdsalManager.installFlow(flowEntity);
167     }
168
169     private void setupTableMissDmacFlow(BigInteger dpId) {
170         List<MatchInfo> mkMatches = new ArrayList<>();
171
172         List<InstructionInfo> mkInstructions = new ArrayList<>();
173         mkInstructions.add(
174                 new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
175
176         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
177                 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
178                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
179         mdsalManager.installFlow(flowEntity);
180     }
181
182     private String getTableMissFlowRef(long tableId) {
183         return new StringBuffer().append(tableId).toString();
184     }
185
186     @Override
187     public void close() throws Exception {
188         if (listenerRegistration != null) {
189             listenerRegistration.close();
190         }
191     }
192 }