X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mdsalutil%2Fmdsalutil-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fvpnservice%2Fmdsalutil%2FMDSALUtil.java;fp=mdsalutil%2Fmdsalutil-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fvpnservice%2Fmdsalutil%2FMDSALUtil.java;h=bcaf558cd81906a2b40af70cdc9d611ec5d9b34e;hb=2bfbbe0cf9942ff975dc82fc298c603fd9cef6a6;hp=0000000000000000000000000000000000000000;hpb=272200b362c916ee5af9fa07a5dcd34c1b4a10b6;p=vpnservice.git diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/mdsalutil/MDSALUtil.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/mdsalutil/MDSALUtil.java new file mode 100644 index 00000000..bcaf558c --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/vpnservice/mdsalutil/MDSALUtil.java @@ -0,0 +1,368 @@ +/* + * Copyright (c) 2013 Ericsson AB. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + */ + +package org.opendaylight.vpnservice.mdsalutil; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; + +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie; +import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.statistics.rev131111.GetGroupStatisticsInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.BucketBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; +import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.TransmitPacketInputBuilder; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder; + +import com.google.common.base.Joiner; +import com.google.common.primitives.Bytes; +import com.google.common.primitives.Ints; + +public class MDSALUtil { + + public static final String NODE_PREFIX = "openflow"; + public static final String SEPARATOR = ":"; // TODO name separtor correctly + + private static final Buckets EMPTY_Buckets = new BucketsBuilder().build(); + private static final Instructions EMPTY_Instructions = new InstructionsBuilder().setInstruction( + new ArrayList()).build(); + private static final Match EMPTY_Matches = new MatchBuilder().build(); + + // public static Map syncStatusMap = new + // ConcurrentHashMap(); + public static Map> lportMap = new ConcurrentHashMap>(); + public static Map> lportDownMap = new ConcurrentHashMap>(); + // public static Map ofRefMap = new + // ConcurrentHashMap(); + public static Map> ofRefMapDpn = new ConcurrentHashMap>(); + public static Map> ofRefGroupMapDpn = new ConcurrentHashMap>(); + public static Map> ofRefFlowMapDpn = new ConcurrentHashMap>(); + + public static FlowEntity buildFlowEntity(long dpnId, short tableId, String flowId, int priority, String flowName, + int idleTimeOut, int hardTimeOut, BigInteger cookie, List listMatchInfo, + List listInstructionInfo) { + + FlowEntity flowEntity = new FlowEntity(dpnId); + + flowEntity.setTableId(tableId); + flowEntity.setFlowId(flowId); + flowEntity.setPriority(priority); + flowEntity.setFlowName(flowName); + flowEntity.setIdleTimeOut(idleTimeOut); + flowEntity.setHardTimeOut(hardTimeOut); + flowEntity.setCookie(cookie); + flowEntity.setMatchInfoList(listMatchInfo); + flowEntity.setInstructionInfoList(listInstructionInfo); + + return flowEntity; + } + + // TODO FIX ME + /* + public static Flow buildResyncFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut, + int hardTimeOut, BigInteger cookie, List listMatchInfo, + List listInstructionInfo, boolean isStrict, boolean isResync) { + FlowKey key = new FlowKey(new FlowId(flowId)); + return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key) + .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo)) + .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut) + .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict) + .setCookie(new FlowCookie(cookie)).setResyncFlag(isResync).build(); + } + */ + // TODO: CHECK IF THIS IS USED + public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut, + int hardTimeOut, BigInteger cookie, List listMatchInfo, List listInstructionInfo) { + return MDSALUtil.buildFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie, + listMatchInfo, listInstructionInfo, true); + } + + public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut, + int hardTimeOut, BigInteger cookie, List listMatchInfo, + List listInstructionInfo, boolean isStrict) { + FlowKey key = new FlowKey(new FlowId(flowId)); + return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key) + .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo)) + .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut) + .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict) + .setCookie(new FlowCookie(cookie)).build(); + } + + // TODO FIX ME + /* + public static Flow buildResyncFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut, + int hardTimeOut, BigInteger cookie, List listMatchInfo, + List listInstructionInfo, boolean isStrict, boolean isResync, boolean isSendFlowRem) { + FlowKey key = new FlowKey(new FlowId(flowId)); + return new FlowBuilder().setMatch(buildMatches(listMatchInfo)).setKey(key) + .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo)) + .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut) + .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict) + .setCookie(new FlowCookie(cookie)) + .setFlags(new FlowModFlags(false, false, false, false, isSendFlowRem)).setResyncFlag(isResync).build(); + + } + */ +/* + public static Flow buildFlow(short tableId, String flowId, int priority, String flowName, int idleTimeOut, + int hardTimeOut, BigInteger cookie, List listMatchInfo, + List listInstructionInfo, boolean isStrict, boolean isSendFlowRem) { + return buildResyncFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie, listMatchInfo, + listInstructionInfo, isStrict, false, isSendFlowRem); + + } +*/ + + + public static GroupEntity buildGroupEntity(long dpnId, long groupId, String groupName, GroupTypes groupType, + List listBucketInfo) { + + GroupEntity groupEntity = new GroupEntity(dpnId); + + groupEntity.setGroupId(groupId); + groupEntity.setGroupName(groupName); + groupEntity.setGroupType(groupType); + groupEntity.setBucketInfoList(listBucketInfo); + + return groupEntity; + } + + // FIXME -- AS ReSync is not required for ODL VPN Service + /* + public static Group buildGroup(long groupId, String groupName, GroupTypes groupType, List listBucketInfo) { + return buildGroup(groupId, groupName, groupType, listBucketInfo, false); + } + + public static Group buildGroup(long groupId, String groupName, GroupTypes groupType, + List listBucketInfo, boolean isResync) { + return new GroupBuilder().setBuckets(buildBuckets(listBucketInfo)).setKey(new GroupKey(new GroupId(groupId))) + .setBarrier(false).setGroupId(new GroupId(Long.valueOf(groupId))).setGroupType(groupType) + .setGroupName(groupName).setResyncFlag(isResync).build(); + } +*/ + public static GetFlowStatisticsFromFlowTableInput buildGetFlowStatisticsFromFlowTableInput(short tableId, + List listMatchInfo, long dpnId) { + return new GetFlowStatisticsFromFlowTableInputBuilder() + .setTableId(Short.valueOf(tableId)) + .setMatch(buildMatches(listMatchInfo)) + .setNode( + new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())).build(); + + } + + public static GetGroupStatisticsInput buildGetGroupStatistics(long groupId, long dpnId) { + return new GetGroupStatisticsInputBuilder() + .setGroupId(new GroupId(Long.valueOf(groupId))) + .setNode( + new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())).build(); + } + + public static TransmitPacketInput getPacketOutDefault(List actionInfos, byte[] payload, long dpnId) { + return new TransmitPacketInputBuilder() + .setAction(buildActions(actionInfos)) + .setPayload(payload) + .setNode( + new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())) + .setIngress(getDefaultNodeConnRef(dpnId)).setEgress(getDefaultNodeConnRef(dpnId)).build(); + } + + public static TransmitPacketInput getPacketOut(List actionInfos, byte[] payload, long dpnId, + NodeConnectorRef ingress) { + return new TransmitPacketInputBuilder() + .setAction(buildActions(actionInfos)) + .setPayload(payload) + .setNode( + new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())) + .setIngress(ingress).setEgress(ingress).build(); + } + + private static List buildActions(List actions) { + List actionsList = new ArrayList(); + for (ActionInfo actionInfo : actions) { + actionsList.add(actionInfo.buildAction()); + } + return actionsList; + } + + public static String longToIp(long ip, long mask) { + StringBuilder sb = new StringBuilder(15); + Joiner joiner = Joiner.on('.'); + + joiner.appendTo(sb, Bytes.asList(Ints.toByteArray((int) ip))); + + sb.append("/" + mask); + + return sb.toString(); + } + + protected static Buckets buildBuckets(List listBucketInfo) { + long i = 0; + if (listBucketInfo != null) { + BucketsBuilder bucketsBuilder = new BucketsBuilder(); + List bucketList = new ArrayList(); + + for (BucketInfo bucketInfo : listBucketInfo) { + BucketBuilder bucketBuilder = new BucketBuilder(); + List actionsList = new ArrayList(); + + bucketInfo.buildAndAddActions(actionsList); + bucketBuilder.setAction(actionsList); + bucketBuilder.setWeight(bucketInfo.getWeight()); + bucketBuilder.setBucketId(new BucketId(i++)); + bucketBuilder.setWeight(bucketInfo.getWeight()).setWatchPort(bucketInfo.getWatchPort()) + .setWatchGroup(bucketInfo.getWatchGroup()); + bucketList.add(bucketBuilder.build()); + } + + bucketsBuilder.setBucket(bucketList); + return bucketsBuilder.build(); + } + + return EMPTY_Buckets; + } + + protected static Instructions buildInstructions(List listInstructionInfo) { + if (listInstructionInfo != null) { + List instructions = new ArrayList(); + int instructionKey = 0; + + for (InstructionInfo instructionInfo : listInstructionInfo) { + instructions.add(instructionInfo.buildInstruction(instructionKey)); + instructionKey++; + } + + return new InstructionsBuilder().setInstruction(instructions).build(); + } + + return EMPTY_Instructions; + } + + protected static Match buildMatches(List listMatchInfo) { + if (listMatchInfo != null) { + MatchBuilder matchBuilder = new MatchBuilder(); + Map, Object> mapMatchBuilder = new HashMap, Object>(); + + for (MatchInfo matchInfo : listMatchInfo) { + matchInfo.createInnerMatchBuilder(mapMatchBuilder); + } + + for (MatchInfo matchInfo : listMatchInfo) { + matchInfo.setMatch(matchBuilder, mapMatchBuilder); + } + + return matchBuilder.build(); + } + + return EMPTY_Matches; + } + + // TODO: Check the port const + public static NodeConnectorRef getDefaultNodeConnRef(long nDpId) { + return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd"); + } + + public static NodeConnectorRef getNodeConnRef(long nDpId, String port) { + return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, port); + } + + public static NodeConnectorRef getNodeConnRef(String sNodeId, String port) { + String sNodeConnectorKey; + StringBuilder sbTmp; + NodeId nodeId; + NodeKey nodeKey; + NodeConnectorId nodeConnectorId; + NodeConnectorKey nodeConnectorKey; + InstanceIdentifierBuilder nodesInstanceIdentifierBuilder; + InstanceIdentifierBuilder nodeInstanceIdentifierBuilder; + InstanceIdentifierBuilder nodeConnectorInstanceIdentifierBuilder; + InstanceIdentifier nodeConnectorInstanceIdentifier; + NodeConnectorRef nodeConnectorRef; + + sbTmp = new StringBuilder(); + + sbTmp.append(sNodeId); + sbTmp.append(SEPARATOR); + sbTmp.append(port); + + sNodeConnectorKey = sbTmp.toString(); + nodeConnectorId = new NodeConnectorId(sNodeConnectorKey); + nodeConnectorKey = new NodeConnectorKey(nodeConnectorId); + + nodeId = new NodeId(sNodeId); + nodeKey = new NodeKey(nodeId); + + nodesInstanceIdentifierBuilder = InstanceIdentifier. builder(Nodes.class); + nodeInstanceIdentifierBuilder = nodesInstanceIdentifierBuilder. child(Node.class, nodeKey); + nodeConnectorInstanceIdentifierBuilder = nodeInstanceIdentifierBuilder. child( + NodeConnector.class, nodeConnectorKey); + nodeConnectorInstanceIdentifier = nodeConnectorInstanceIdentifierBuilder.toInstance(); + nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier); + return nodeConnectorRef; + } + + public static long getDpnIdFromNodeName(NodeId nodeId) { + return getDpnIdFromNodeName(nodeId.getValue()); + } + + public static long getDpnIdFromNodeName(String sMdsalNodeName) { + String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1); + return Long.parseLong(sDpId); + } + + public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) { + return getOfPortNumberFromPortName(nodeConnectorId.getValue()); + } + + public static long getOfPortNumberFromPortName(String sMdsalPortName) { + String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1); + return Long.parseLong(sPortNumber); + } +}