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