Fix Checkstyle violations related to exception handling in 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 LOG = 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         listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
57             getWildCardPath(), ElanNodeListener.this, AsyncDataBroker.DataChangeScope.SUBTREE);
58     }
59
60     private InstanceIdentifier<Node> getWildCardPath() {
61         return InstanceIdentifier.create(Nodes.class).child(Node.class);
62     }
63
64     @Override
65     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
66     }
67
68     @Override
69     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
70     }
71
72     @Override
73     protected void add(InstanceIdentifier<Node> identifier, Node add) {
74         NodeId nodeId = add.getId();
75         String[] node =  nodeId.getValue().split(":");
76         if (node.length < 2) {
77             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
78             return;
79         }
80         BigInteger dpId = new BigInteger(node[1]);
81         createTableMissEntry(dpId);
82     }
83
84     public void createTableMissEntry(BigInteger dpnId) {
85         setupTableMissSmacFlow(dpnId);
86         setupTableMissDmacFlow(dpnId);
87     }
88
89     private void setupTableMissSmacFlow(BigInteger dpId) {
90         List<InstructionInfo> mkInstructions = new ArrayList<>();
91         List<ActionInfo> actionsInfos = new ArrayList<>();
92         actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
93         mkInstructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
94         mkInstructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_DMAC_TABLE }));
95
96         List<MatchInfo> mkMatches = new ArrayList<>();
97         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_SMAC_TABLE,
98                 getTableMissFlowRef(NwConstants.ELAN_SMAC_TABLE), 0, "ELAN sMac Table Miss Flow", 0, 0,
99                 ElanConstants.COOKIE_ELAN_KNOWN_SMAC, mkMatches, mkInstructions);
100         mdsalManager.installFlow(flowEntity);
101     }
102
103     private void setupTableMissDmacFlow(BigInteger dpId) {
104         List<MatchInfo> mkMatches = new ArrayList<>();
105
106         List<InstructionInfo> mkInstructions = new ArrayList<>();
107         mkInstructions.add(
108                 new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.ELAN_UNKNOWN_DMAC_TABLE }));
109
110         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.ELAN_DMAC_TABLE,
111                 getTableMissFlowRef(NwConstants.ELAN_DMAC_TABLE), 0, "ELAN dMac Table Miss Flow", 0, 0,
112                 ElanConstants.COOKIE_ELAN_KNOWN_DMAC, mkMatches, mkInstructions);
113         mdsalManager.installFlow(flowEntity);
114     }
115
116     private String getTableMissFlowRef(long tableId) {
117         return new StringBuffer().append(tableId).toString();
118     }
119
120     @Override
121     public void close() throws Exception {
122         if (listenerRegistration != null) {
123             listenerRegistration.close();
124         }
125     }
126 }