Switch to JDT annotations for Nullable and NonNull
[netvirt.git] / coe / impl / src / main / java / org / opendaylight / netvirt / coe / listeners / InventoryNodeListener.java
1 /*
2  * Copyright (c) 2017, 2018 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.coe.listeners;
9
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.apache.aries.blueprint.annotation.service.Reference;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.infra.Datastore;
20 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
22 import org.opendaylight.genius.mdsalutil.ActionInfo;
23 import org.opendaylight.genius.mdsalutil.FlowEntity;
24 import org.opendaylight.genius.mdsalutil.InstructionInfo;
25 import org.opendaylight.genius.mdsalutil.MDSALUtil;
26 import org.opendaylight.genius.mdsalutil.MatchInfo;
27 import org.opendaylight.genius.mdsalutil.NwConstants;
28 import org.opendaylight.genius.mdsalutil.actions.ActionNxResubmit;
29 import org.opendaylight.genius.mdsalutil.instructions.InstructionApplyActions;
30 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
31 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
32 import org.opendaylight.serviceutils.tools.mdsal.listener.AbstractSyncDataTreeChangeListener;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 @Singleton
41 public class InventoryNodeListener extends AbstractSyncDataTreeChangeListener<Node> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(InventoryNodeListener.class);
44     private final IMdsalApiManager mdsalApiManager;
45     private final ManagedNewTransactionRunner txRunner;
46
47     @Inject
48     public InventoryNodeListener(@Reference final DataBroker dataBroker,
49                                  @Reference final IMdsalApiManager mdsalApiManager) {
50         super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class).child(Node.class));
51         this.mdsalApiManager = mdsalApiManager;
52         this.txRunner = new ManagedNewTransactionRunnerImpl(dataBroker);
53
54     }
55
56     @Override
57     public void remove(@NonNull InstanceIdentifier<Node> instanceIdentifier, @NonNull Node node) {
58         // Do nothing
59     }
60
61     @Override
62     public void update(@NonNull InstanceIdentifier<Node> instanceIdentifier, @NonNull Node originalNode,
63                        @NonNull final Node updatedNode) {
64         // Nothing to do
65     }
66
67     @Override
68     public void add(@NonNull InstanceIdentifier<Node> instanceIdentifier, @NonNull Node node) {
69         NodeId nodeId = node.getId();
70         String[] nodeIdVal =  nodeId.getValue().split(":");
71         if (nodeIdVal.length < 2) {
72             LOG.warn("Unexpected nodeId {}", nodeId.getValue());
73             return;
74         }
75         BigInteger dpId = new BigInteger(nodeIdVal[1]);
76         setupTableMissForCoeKubeProxyTable(dpId);
77     }
78
79     private void setupTableMissForCoeKubeProxyTable(BigInteger dpId) {
80         List<MatchInfo> matches = new ArrayList<>();
81         List<InstructionInfo> instructions = new ArrayList<>();
82         List<ActionInfo> actionsInfos = new ArrayList<>();
83         actionsInfos.add(new ActionNxResubmit(NwConstants.LPORT_DISPATCHER_TABLE));
84         instructions.add(new InstructionApplyActions(actionsInfos));
85         FlowEntity flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.COE_KUBE_PROXY_TABLE,
86                 "COEKubeProxyTableMissFlow",0,
87                 "COEKubeProxy Table Miss Flow", 0, 0,
88                 NwConstants.COOKIE_COE_KUBE_PROXY_TABLE, matches, instructions);
89         ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(Datastore.CONFIGURATION,
90             tx -> mdsalApiManager.addFlow(tx, flowEntity)), LOG, "Error adding flow {}", flowEntity);
91     }
92 }