Merge "Supporting DHCP as a Service"
[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 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.netvirt.elan.utils.ElanConstants;
15 import org.opendaylight.genius.mdsalutil.AbstractDataChangeListener;
16 import org.opendaylight.genius.mdsalutil.*;
17 import org.opendaylight.genius.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     private static volatile ElanNodeListener elanNodeListener = null;
34     private ElanServiceProvider elanServiceProvider = null;
35     private ListenerRegistration<DataChangeListener> listenerRegistration;
36     public static ElanNodeListener getElanNodeListener(ElanServiceProvider elanServiceProvider) {
37         if (elanNodeListener == null)
38             synchronized (ElanNodeListener.class) {
39                 if (elanNodeListener == null)
40                 {
41                     ElanNodeListener elanNodeListener = new ElanNodeListener(elanServiceProvider);
42                     return elanNodeListener;
43
44                 }
45             }
46         return elanNodeListener;
47     }
48
49     public ElanNodeListener(ElanServiceProvider elanServiceProvider) {
50         super(Node.class);
51         this.elanServiceProvider= elanServiceProvider;
52         registerListener(this.elanServiceProvider.getBroker());
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[] { ElanConstants.ELAN_DMAC_TABLE }));
104
105         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_SMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_SMAC_TABLE),
106             0, "ELAN sMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_SMAC,
107             mkMatches, mkInstructions);
108         this.elanServiceProvider.getMdsalManager().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[] { ElanConstants.ELAN_UNKNOWN_DMAC_TABLE }));
116
117         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, ElanConstants.ELAN_DMAC_TABLE, getTableMissFlowRef(ElanConstants.ELAN_DMAC_TABLE),
118             0, "ELAN dMac Table Miss Flow", 0, 0, ElanConstants.COOKIE_ELAN_KNOWN_DMAC,
119             mkMatches, mkInstructions);
120         this.elanServiceProvider.getMdsalManager().installFlow(flowEntity);
121     }
122
123     private String getTableMissFlowRef(long tableId) {
124         return new StringBuffer().append(tableId).toString();
125     }
126
127
128 }