50d1615c08aea6b75f6ef8512f04bacb8055b8ee
[netvirt.git] / policyservice / impl / src / main / java / org / opendaylight / netvirt / policyservice / listeners / PolicyNodeListener.java
1 /*
2  * Copyright (c) 2017 Hewlett Packard Enterprise, Co. 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
9 package org.opendaylight.netvirt.policyservice.listeners;
10
11 import java.math.BigInteger;
12 import java.util.Collections;
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.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.netvirt.policyservice.util.PolicyServiceFlowUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Singleton
31 public class PolicyNodeListener extends AsyncDataTreeChangeListenerBase<FlowCapableNode, PolicyNodeListener> {
32     private static final Logger LOG = LoggerFactory.getLogger(PolicyNodeListener.class);
33
34     private final DataBroker dataBroker;
35     private final PolicyServiceFlowUtil policyFlowUtil;
36
37     @Inject
38     public PolicyNodeListener(final DataBroker dataBroker, final PolicyServiceFlowUtil policyFlowUtil) {
39         this.dataBroker = dataBroker;
40         this.policyFlowUtil = policyFlowUtil;
41     }
42
43     @Override
44     @PostConstruct
45     public void init() {
46         LOG.info("init");
47         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
48     }
49
50     @Override
51     protected PolicyNodeListener getDataTreeChangeListener() {
52         return this;
53     }
54
55     @Override
56     protected InstanceIdentifier<FlowCapableNode> getWildCardPath() {
57         return InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
58     }
59
60     @Override
61     protected void remove(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode flowCapableNode) {
62
63     }
64
65     @Override
66     protected void update(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode origFlowCapableNode,
67             FlowCapableNode updatedFlowCapableNode) {
68
69     }
70
71     @Override
72     protected void add(InstanceIdentifier<FlowCapableNode> key, FlowCapableNode flowCapableNode) {
73         BigInteger dpId = MDSALUtil.getDpnIdFromNodeName(key.firstKeyOf(Node.class).getId());
74         WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
75         createTableMissFlow(dpId, NwConstants.EGRESS_POLICY_CLASSIFIER_TABLE,
76                 NwConstants.EGRESS_POLICY_CLASSIFIER_COOKIE, tx);
77         createTableMissFlow(dpId, NwConstants.EGRESS_POLICY_ROUTING_TABLE, NwConstants.EGRESS_POLICY_ROUTING_COOKIE,
78                 tx);
79         tx.submit();
80     }
81
82     private void createTableMissFlow(BigInteger dpId, short tableId, BigInteger cookie, WriteTransaction tx) {
83         policyFlowUtil.updateFlowToTx(dpId, tableId, tableId + "_Miss", NwConstants.TABLE_MISS_PRIORITY, cookie,
84                 Collections.emptyList(), policyFlowUtil.getTableMissInstructions(), NwConstants.ADD_FLOW, tx);
85     }
86
87 }