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