2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.frm.util;
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;
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;
52 throw new IllegalStateException("This class should not be instantiated.");
55 public static NodeId getNodeIdFromNodeIdentifier(final InstanceIdentifier<FlowCapableNode> nodeIdent) {
56 return nodeIdent.firstKeyOf(Node.class).getId();
59 public static String getFlowId(final FlowRef flowRef) {
60 return flowRef.getValue().firstKeyOf(Flow.class).getId().getValue();
63 public static Uint8 getTableId(final FlowTableRef flowTableRef) {
64 return flowTableRef.getValue().firstKeyOf(Table.class).getId();
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);
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();
84 for (Action action : actions) {
85 if (action.getAction().implementedInterface()
86 .equals(ActionType.GROUP_ACTION.getActionType())) {
87 return ((GroupActionCase) action.getAction()).getGroupAction()
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;
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);
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);
116 return result.getResult().getResult();
118 } catch (InterruptedException | ExecutionException | TimeoutException e) {
119 LOG.error("Error while retrieving active bundle present for node {}", dpId , e);
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);