Merge "OPNFLWPLUG-1076 Migrate lldp-speaker, forwardingrules-sync and arbitratorrecon...
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / util / FrmUtil.java
1 /*
2  * Copyright (c) 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
9 package org.opendaylight.openflowplugin.applications.frm.util;
10
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.openflowplugin.applications.frm.ActionType;
18 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCase;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowTableRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.rev170124.BundleId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.GetActiveBundleInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.GetActiveBundleOutput;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42 import org.opendaylight.yangtools.yang.common.Uint8;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public final class FrmUtil {
47     private static final Logger LOG = LoggerFactory.getLogger(FrmUtil.class);
48     private static final String SEPARATOR = ":";
49     private static final long RPC_RESULT_TIMEOUT = 2500;
50
51     private FrmUtil() {
52         throw new IllegalStateException("This class should not be instantiated.");
53     }
54
55     public static NodeId getNodeIdFromNodeIdentifier(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
56         return nodeIdent.firstKeyOf(Node.class).getId();
57     }
58
59     public static String getFlowId(final FlowRef flowRef) {
60         return flowRef.getValue().firstKeyOf(Flow.class).getId().getValue();
61     }
62
63     public static Uint8 getTableId(final FlowTableRef flowTableRef) {
64         return flowTableRef.getValue().firstKeyOf(Table.class).getId();
65     }
66
67     public static BigInteger getDpnIdFromNodeName(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
68         String nodeId = nodeIdent.firstKeyOf(Node.class).getId().getValue();
69         String dpId = nodeId.substring(nodeId.lastIndexOf(SEPARATOR) + 1);
70         return new BigInteger(dpId);
71     }
72
73     public static Uint32 isFlowDependentOnGroup(final Flow flow) {
74         LOG.debug("Check if flow {} is dependent on group", flow);
75         if (flow.getInstructions() != null) {
76             List<Instruction> instructions = flow.getInstructions().getInstruction();
77             for (Instruction instruction : instructions) {
78                 List<Action> actions = Collections.emptyList();
79                 if (instruction.getInstruction().implementedInterface()
80                         .equals(ActionType.APPLY_ACTION.getActionType())) {
81                     actions = ((ApplyActionsCase) instruction.getInstruction())
82                             .getApplyActions().getAction();
83                 }
84                 for (Action action : actions) {
85                     if (action.getAction().implementedInterface()
86                             .equals(ActionType.GROUP_ACTION.getActionType())) {
87                         return ((GroupActionCase) action.getAction()).getGroupAction()
88                                 .getGroupId();
89                     }
90                 }
91             }
92         }
93         return null;
94     }
95
96     public static InstanceIdentifier<Group> buildGroupInstanceIdentifier(
97             final InstanceIdentifier<FlowCapableNode> nodeIdent, final Uint32 groupId) {
98         NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
99         InstanceIdentifier<Group> groupInstanceId = InstanceIdentifier.builder(Nodes.class)
100                 .child(Node.class, new NodeKey(nodeId)).augmentation(FlowCapableNode.class)
101                 .child(Group.class, new GroupKey(new GroupId(groupId))).build();
102         return groupInstanceId;
103     }
104
105     public static BundleId getActiveBundle(final InstanceIdentifier<FlowCapableNode> nodeIdent,
106             final ForwardingRulesManager provider) {
107         BigInteger dpId = getDpnIdFromNodeName(nodeIdent);
108         final NodeRef nodeRef = new NodeRef(nodeIdent.firstIdentifierOf(Node.class));
109         GetActiveBundleInputBuilder input = new GetActiveBundleInputBuilder().setNodeId(dpId).setNode(nodeRef);
110         try {
111             RpcResult<GetActiveBundleOutput> result = provider.getArbitratorReconciliationManager()
112                     .getActiveBundle(input.build()).get(RPC_RESULT_TIMEOUT, TimeUnit.MILLISECONDS);
113             if (!result.isSuccessful()) {
114                 LOG.trace("Error while retrieving active bundle present for node {}", dpId);
115             } else {
116                 return result.getResult().getResult();
117             }
118         } catch (InterruptedException | ExecutionException | TimeoutException e) {
119             LOG.error("Error while retrieving active bundle present for node {}", dpId , e);
120         }
121         return null;
122     }
123
124     public static boolean isGroupExistsOnDevice(final InstanceIdentifier<FlowCapableNode> nodeIdent,
125             final Uint32 groupId, final ForwardingRulesManager provider) {
126         NodeId nodeId = getNodeIdFromNodeIdentifier(nodeIdent);
127         return provider.getDevicesGroupRegistry().isGroupPresent(nodeId, groupId);
128     }
129 }