1c338135794f1786aed9c917f8b9f3be45dcb5ae
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / QosNodeListener.java
1 /*
2  * Copyright (c) 2017 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.qosservice;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.genius.mdsalutil.ActionInfo;
20 import org.opendaylight.genius.mdsalutil.FlowEntity;
21 import org.opendaylight.genius.mdsalutil.InstructionInfo;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
26 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
27 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
28 import org.opendaylight.genius.srm.RecoverableListener;
29 import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
30 import org.opendaylight.netvirt.qosservice.recovery.QosServiceRecoveryHandler;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
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.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @Singleton
40 public class QosNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, QosNodeListener>
41         implements RecoverableListener {
42     private static final Logger LOG = LoggerFactory.getLogger(QosNodeListener.class);
43
44     private final DataBroker dataBroker;
45     private final IMdsalApiManager mdsalUtils;
46
47     @Inject
48     public QosNodeListener(final DataBroker dataBroker, final IMdsalApiManager mdsalUtils,
49                            final ServiceRecoveryRegistry serviceRecoveryRegistry,
50                            final QosServiceRecoveryHandler qosServiceRecoveryHandler) {
51         super(FlowCapableNode.class, QosNodeListener.class);
52         this.dataBroker = dataBroker;
53         this.mdsalUtils = mdsalUtils;
54         serviceRecoveryRegistry.addRecoverableListener(qosServiceRecoveryHandler.buildServiceRegistryKey(),
55                 this);
56         LOG.trace("{} created",  getClass().getSimpleName());
57     }
58
59     @Override
60     @PostConstruct
61     public void init() {
62         registerListener();
63         LOG.trace("{} init and registerListener done", getClass().getSimpleName());
64     }
65
66     @Override
67     public void registerListener() {
68         registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
69     }
70
71     @Override
72     protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
73         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
74     }
75
76     @Override
77     protected void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
78         //do nothing
79     }
80
81     @Override
82     protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModificationBefore,
83                           FlowCapableNode dataObjectModificationAfter) {
84         //do nothing
85     }
86
87     @Override
88     protected void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode dataObjectModification) {
89         NodeKey nodeKey = key.firstKeyOf(Node.class);
90         BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(nodeKey.getId());
91         createTableMissEntry(dpId);
92     }
93
94     @Override
95     protected QosNodeListener getDataTreeChangeListener() {
96         return QosNodeListener.this;
97     }
98
99     public void createTableMissEntry(BigInteger dpnId) {
100         List<MatchInfo> matches = new ArrayList<>();
101         List<InstructionInfo> instructions = new ArrayList<>();
102         List<ActionInfo> actionsInfos = new ArrayList<>();
103         actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
104         instructions.add(new InstructionApplyActions(actionsInfos));
105         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpnId, NwConstants.QOS_DSCP_TABLE, "QoSTableMissFlow",
106                 0, "QoS Table Miss Flow", 0, 0,
107                 NwConstants.COOKIE_QOS_TABLE, matches, instructions);
108         mdsalUtils.installFlow(flowEntity);
109     }
110 }
111
112