X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mdsalutil%2Fmdsalutil-api%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgenius%2Fmdsalutil%2FMDSALUtil.java;h=ebbf828a0d86bb9319279fa5e8523677196b6e04;hb=3a5d1e58dadf17654064234bdeb08597b7a48fe5;hp=c1d8c4d4ed8d95f254db6c4147181c7c6463f422;hpb=892f6fa23efd0fc186e5aee8cc3c3c6bdf6b0285;p=genius.git diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/MDSALUtil.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/MDSALUtil.java index c1d8c4d4e..ebbf828a0 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/MDSALUtil.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/MDSALUtil.java @@ -8,19 +8,21 @@ package org.opendaylight.genius.mdsalutil; +import com.google.common.base.Optional; +import com.google.common.net.InetAddresses; import java.math.BigInteger; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; - import org.opendaylight.controller.liblldp.HexEncode; import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker; +import org.opendaylight.genius.mdsalutil.actions.ActionDrop; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder; @@ -91,11 +93,11 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdenti import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; - +@SuppressWarnings("checkstyle:AbbreviationAsWordInName") public class MDSALUtil { + private static final Logger LOG = LoggerFactory.getLogger(MDSALUtil.class); + public enum MdsalOp { CREATION_OP, UPDATE_OP, REMOVAL_OP } public static final String NODE_PREFIX = "openflow"; @@ -103,34 +105,37 @@ public class MDSALUtil { public static final long WATCH_PORT = 0xffffffffL; public static final long WATCH_GROUP = 0xffffffffL; public static final String SEPARATOR = ":"; - private static final Buckets EMPTY_Buckets = new BucketsBuilder().build(); - private static final Instructions EMPTY_Instructions = new InstructionsBuilder().setInstruction( + 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(); - private static final Logger logger = LoggerFactory.getLogger(MDSALUtil.class); - - public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, String flowName, - int idleTimeOut, int hardTimeOut, BigInteger cookie, List listMatchInfoBase, - 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(listMatchInfoBase); - flowEntity.setInstructionInfoList(listInstructionInfo); - - return flowEntity; + private static final Match EMPTY_MATCHES = new MatchBuilder().build(); + + public static FlowEntity buildFlowEntity(BigInteger dpnId, short tableId, String flowId, int priority, + String flowName, int idleTimeOut, int hardTimeOut, BigInteger cookie, + List listMatchInfoBase, List listInstructionInfo) { + + FlowEntityBuilder builder = new FlowEntityBuilder() + .setDpnId(dpnId) + .setTableId(tableId) + .setFlowId(flowId) + .setPriority(priority) + .setFlowName(flowName) + .setIdleTimeOut(idleTimeOut) + .setHardTimeOut(hardTimeOut) + .setCookie(cookie); + if (listMatchInfoBase != null) { + builder.addAllMatchInfoList(listMatchInfoBase); + } + if (listInstructionInfo != null) { + builder.addAllInstructionInfoList(listInstructionInfo); + } + return builder.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 listMatchInfoBase, List listInstructionInfo) { + int hardTimeOut, BigInteger cookie, List listMatchInfoBase, + List listInstructionInfo) { return MDSALUtil.buildFlow(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie, listMatchInfoBase, listInstructionInfo, true); } @@ -140,9 +145,9 @@ public class MDSALUtil { List listInstructionInfo, boolean isStrict) { FlowKey key = new FlowKey(new FlowId(flowId)); return new FlowBuilder().setMatch(buildMatches(listMatchInfoBase)).setKey(key) - .setPriority(Integer.valueOf(priority)).setInstructions(buildInstructions(listInstructionInfo)) + .setPriority(priority).setInstructions(buildInstructions(listInstructionInfo)) .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut) - .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict) + .setFlowName(flowName).setTableId(tableId).setStrict(isStrict) .setCookie(new FlowCookie(cookie)).build(); } @@ -151,7 +156,8 @@ public class MDSALUtil { } public static Flow buildFlowNew(short tableId, String flowId, int priority, String flowName, int idleTimeOut, - int hardTimeOut, BigInteger cookie, List listMatchInfoBase, List listInstructionInfo) { + int hardTimeOut, BigInteger cookie, List listMatchInfoBase, + List listInstructionInfo) { return MDSALUtil.buildFlowNew(tableId, flowId, priority, flowName, idleTimeOut, hardTimeOut, cookie, listMatchInfoBase, listInstructionInfo, true); } @@ -161,9 +167,10 @@ public class MDSALUtil { List listInstructionInfo, boolean isStrict) { FlowKey key = new FlowKey(new FlowId(flowId)); return new FlowBuilder().setMatch(buildMatches(listMatchInfoBase)).setKey(key) - .setPriority(Integer.valueOf(priority)).setInstructions(new InstructionsBuilder().setInstruction(listInstructionInfo).build()) + .setPriority(priority) + .setInstructions(new InstructionsBuilder().setInstruction(listInstructionInfo).build()) .setBarrier(false).setInstallHw(true).setHardTimeout(hardTimeOut).setIdleTimeout(idleTimeOut) - .setFlowName(flowName).setTableId(Short.valueOf(tableId)).setStrict(isStrict) + .setFlowName(flowName).setTableId(tableId).setStrict(isStrict) .setCookie(new FlowCookie(cookie)).build(); } @@ -182,11 +189,12 @@ public class MDSALUtil { public static Group buildGroup(long groupId, String groupName, GroupTypes groupType, Buckets buckets) { GroupId groupIdentifier = new GroupId(groupId); - return new GroupBuilder().setGroupId(groupIdentifier).setKey(new GroupKey(groupIdentifier)).setGroupName(groupName) - .setGroupType(groupType).setBuckets(buckets).build(); + return new GroupBuilder().setGroupId(groupIdentifier).setKey(new GroupKey(groupIdentifier)) + .setGroupName(groupName).setGroupType(groupType).setBuckets(buckets).build(); } - public static TransmitPacketInput getPacketOutDefault(List actionInfos, byte[] payload, BigInteger dpnId) { + public static TransmitPacketInput getPacketOutDefault(List actionInfos, byte[] payload, + BigInteger dpnId) { return new TransmitPacketInputBuilder() .setAction(buildActions(actionInfos)) .setPayload(payload) @@ -218,6 +226,23 @@ public class MDSALUtil { .setIngress(ingress).setEgress(ingress).build(); } + public static TransmitPacketInput getPacketOut(List actionInfos, byte[] payload, BigInteger dpnId, + NodeConnectorRef nodeConnRef) { + // TODO Auto-generated method stub + return null; + } + + public static TransmitPacketInput getPacketOut(List actions, byte[] payload, BigInteger dpnId) { + NodeConnectorRef ncRef = getDefaultNodeConnRef(dpnId); + return new TransmitPacketInputBuilder() + .setAction(actions) + .setPayload(payload) + .setNode( + new NodeRef(InstanceIdentifier.builder(Nodes.class) + .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())) + .setIngress(ncRef).setEgress(ncRef).build(); + } + public static Action retrieveSetTunnelIdAction(BigInteger tunnelId, int actionKey) { return new ActionBuilder().setAction( new SetFieldCaseBuilder().setSetField(new SetFieldBuilder().setTunnel(new TunnelBuilder() @@ -234,18 +259,25 @@ public class MDSALUtil { } public static String longToIp(long ip, long mask) { - return ((ip & 0xFF000000) >> 3 * 8) + "." + - ((ip & 0x00FF0000) >> 2 * 8) + "." + - ((ip & 0x0000FF00) >> 8) + "." + - (ip & 0x000000FF) + - (mask == 0 ? "" : "/" + mask); + return ((ip & 0xFF000000) >> 3 * 8) + "." + + ((ip & 0x00FF0000) >> 2 * 8) + "." + + ((ip & 0x0000FF00) >> 8) + "." + + (ip & 0x000000FF) + + (mask == 0 ? "" : "/" + mask); } + public static BigInteger getBigIntIpFromIpAddress(IpAddress ipAddr) { + String ipString = ipAddr.getIpv4Address().getValue(); + int ipInt = InetAddresses.coerceToInteger(InetAddresses.forString(ipString)); + return BigInteger.valueOf(ipInt & 0xffffffffL); + } - public static Bucket buildBucket(List actionsList, int weight, int bucketId, long watchPort, long watchGroup) { - return new BucketBuilder().setAction(actionsList).setWeight(weight) - .setWatchGroup(watchGroup).setWatchPort(watchPort).setBucketId(new BucketId(Long.valueOf(bucketId))).setKey(new BucketKey(new BucketId(Long.valueOf(bucketId)))).build(); + public static Bucket buildBucket(List actionsList, int weight, int bucketId, long watchPort, + long watchGroup) { + return new BucketBuilder().setAction(actionsList).setWeight(weight).setWatchGroup(watchGroup) + .setWatchPort(watchPort).setBucketId(new BucketId(Long.valueOf(bucketId))) + .setKey(new BucketKey(new BucketId(Long.valueOf(bucketId)))).build(); } public static Buckets buildBucketLists(List bucketList) { @@ -253,7 +285,7 @@ public class MDSALUtil { } protected static Buckets buildBuckets(List listBucketInfo) { - long i = 0; + long index = 0; if (listBucketInfo != null) { BucketsBuilder bucketsBuilder = new BucketsBuilder(); List bucketList = new ArrayList<>(); @@ -262,7 +294,7 @@ public class MDSALUtil { BucketBuilder bucketBuilder = new BucketBuilder(); bucketBuilder.setAction(bucketInfo.buildActions()); bucketBuilder.setWeight(bucketInfo.getWeight()); - bucketBuilder.setBucketId(new BucketId(i++)); + bucketBuilder.setBucketId(new BucketId(index++)); bucketBuilder.setWeight(bucketInfo.getWeight()).setWatchPort(bucketInfo.getWatchPort()) .setWatchGroup(bucketInfo.getWatchGroup()); bucketList.add(bucketBuilder.build()); @@ -272,7 +304,7 @@ public class MDSALUtil { return bucketsBuilder.build(); } - return EMPTY_Buckets; + return EMPTY_BUCKETS; } public static Instructions buildInstructions(List listInstructionInfo) { @@ -288,7 +320,7 @@ public class MDSALUtil { return new InstructionsBuilder().setInstruction(instructions).build(); } - return EMPTY_Instructions; + return EMPTY_INSTRUCTIONS; } public static Match buildMatches(List listMatchInfoBase) { @@ -296,61 +328,46 @@ public class MDSALUtil { MatchBuilder matchBuilder = new MatchBuilder(); Map, Object> mapMatchBuilder = new HashMap<>(); - for (MatchInfoBase MatchInfoBase : listMatchInfoBase) { - MatchInfoBase.createInnerMatchBuilder(mapMatchBuilder); + for (MatchInfoBase matchInfoBase : listMatchInfoBase) { + matchInfoBase.createInnerMatchBuilder(mapMatchBuilder); } - for (MatchInfoBase MatchInfoBase : listMatchInfoBase) { - MatchInfoBase.setMatch(matchBuilder, mapMatchBuilder); + for (MatchInfoBase matchInfoBase : listMatchInfoBase) { + matchInfoBase.setMatch(matchBuilder, mapMatchBuilder); } return matchBuilder.build(); } - return EMPTY_Matches; + return EMPTY_MATCHES; } // TODO: Check the port const - public static NodeConnectorRef getDefaultNodeConnRef(BigInteger nDpId) { - return getNodeConnRef(NODE_PREFIX + SEPARATOR + nDpId, "0xfffffffd"); - } - - public static NodeConnectorRef getNodeConnRef(BigInteger 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); + public static NodeConnectorRef getDefaultNodeConnRef(BigInteger dpId) { + return getNodeConnRef(NODE_PREFIX + SEPARATOR + dpId, "0xfffffffd"); + } + + public static NodeConnectorRef getNodeConnRef(BigInteger dpId, String port) { + return getNodeConnRef(NODE_PREFIX + SEPARATOR + dpId, port); + } + + public static NodeConnectorRef getNodeConnRef(String nodeId, String port) { + StringBuilder sb = new StringBuilder(); + sb.append(nodeId); + sb.append(SEPARATOR); + sb.append(port); + String nodeConnectorKeyAsString = sb.toString(); + NodeConnectorId nodeConnectorId = new NodeConnectorId(nodeConnectorKeyAsString); + NodeConnectorKey nodeConnectorKey = new NodeConnectorKey(nodeConnectorId); + + NodeKey nodeKey = new NodeKey(new NodeId(nodeId)); + InstanceIdentifierBuilder nodeInstanceIdentifierBuilder + = InstanceIdentifier.builder(Nodes.class).child(Node.class, nodeKey); + InstanceIdentifierBuilder nodeConnectorInstanceIdentifierBuilder + = nodeInstanceIdentifierBuilder.child(NodeConnector.class, nodeConnectorKey); + InstanceIdentifier nodeConnectorInstanceIdentifier + = nodeConnectorInstanceIdentifierBuilder.toInstance(); + NodeConnectorRef nodeConnectorRef = new NodeConnectorRef(nodeConnectorInstanceIdentifier); return nodeConnectorRef; } @@ -358,34 +375,40 @@ public class MDSALUtil { return getDpnIdFromNodeName(nodeId.getValue()); } - public static BigInteger getDpnIdFromNodeName(String sMdsalNodeName) { - String sDpId = sMdsalNodeName.substring(sMdsalNodeName.lastIndexOf(":") + 1); - return new BigInteger(sDpId); + public static BigInteger getDpnIdFromNodeName(String mdsalNodeName) { + String dpId = mdsalNodeName.substring(mdsalNodeName.lastIndexOf(":") + 1); + return new BigInteger(dpId); } public static long getOfPortNumberFromPortName(NodeConnectorId nodeConnectorId) { return getOfPortNumberFromPortName(nodeConnectorId.getValue()); } + public static long getOfPortNumberFromPortName(String mdsalPortName) { + String portNumber = mdsalPortName.substring(mdsalPortName.lastIndexOf(":") + 1); + return Long.parseLong(portNumber); + } + public static long getDpnIdFromPortName(NodeConnectorId nodeConnectorId) { + if (nodeConnectorId == null || nodeConnectorId.getValue() == null) { + return -1; + } try { String ofPortName = nodeConnectorId.getValue(); - return Long.parseLong(ofPortName.substring(ofPortName.indexOf(":")+1, + return Long.parseLong(ofPortName.substring(ofPortName.indexOf(":") + 1, ofPortName.lastIndexOf(":"))); - } catch (Exception e) { - logger.error("NodeConnectorId not of expected format openflow:dpnid:portnum"); + } catch (NumberFormatException | IndexOutOfBoundsException e) { + LOG.error("NodeConnectorId not of expected format openflow:dpnid:portnum"); return -1; } } - public static long getOfPortNumberFromPortName(String sMdsalPortName) { - String sPortNumber = sMdsalPortName.substring(sMdsalPortName.lastIndexOf(":") + 1); - return Long.parseLong(sPortNumber); - } - - public static TransmitPacketInput getPacketOut(List actionInfos, byte[] payload, BigInteger dpnId, - NodeConnectorRef nodeConnRef) { - // TODO Auto-generated method stub + public static BigInteger getDpnId(String datapathId) { + if (datapathId != null) { + String dpIdStr = datapathId.replace(":", ""); + BigInteger dpnId = new BigInteger(dpIdStr, 16); + return dpnId; + } return null; } @@ -393,7 +416,7 @@ public class MDSALUtil { Action popVlanAction = new ActionBuilder().setAction( new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build()) .setKey(new ActionKey(actionKey)).build(); - List listAction = new ArrayList<> (); + List listAction = new ArrayList<>(); listAction.add(popVlanAction); return buildApplyActionsInstruction(listAction, instructionKey); } @@ -409,9 +432,10 @@ public class MDSALUtil { nxRegLoadBuilder.setDst(dst); nxRegLoadBuilder.setValue(new BigInteger(Long.toString(value))); ActionBuilder ab = new ActionBuilder(); - ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(nxRegLoadBuilder.build()).build()); + ab.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder() + .setNxRegLoad(nxRegLoadBuilder.build()).build()); ab.setKey(new ActionKey(actionKey)); - List listAction = new ArrayList<> (); + List listAction = new ArrayList<>(); listAction.add(ab.build()); return buildApplyActionsInstruction(listAction, instructionKey); } @@ -420,16 +444,6 @@ public class MDSALUtil { return buildApplyActionsInstruction(actions, 0); } - public static Instruction buildWriteActionsInstruction(List actions) { - WriteActions writeActions = new WriteActionsBuilder().setAction(actions).build(); - WriteActionsCase writeActionsCase = new WriteActionsCaseBuilder().setWriteActions(writeActions).build(); - InstructionBuilder instructionBuilder = new InstructionBuilder(); - - instructionBuilder.setInstruction(writeActionsCase); - instructionBuilder.setKey(new InstructionKey(0)); - return instructionBuilder.build(); - } - public static Instruction buildApplyActionsInstruction(List listAction, int instructionKey) { ApplyActions applyActions = new ApplyActionsBuilder().setAction(listAction).build(); ApplyActionsCase applyActionsCase = new ApplyActionsCaseBuilder().setApplyActions(applyActions).build(); @@ -440,14 +454,24 @@ public class MDSALUtil { return instructionBuilder.build(); } + public static Instruction buildWriteActionsInstruction(List actions) { + WriteActions writeActions = new WriteActionsBuilder().setAction(actions).build(); + WriteActionsCase writeActionsCase = new WriteActionsCaseBuilder().setWriteActions(writeActions).build(); + InstructionBuilder instructionBuilder = new InstructionBuilder(); + + instructionBuilder.setInstruction(writeActionsCase); + instructionBuilder.setKey(new InstructionKey(0)); + return instructionBuilder.build(); + } + public static List buildInstructionsDrop() { return buildInstructionsDrop(0); } public static List buildInstructionsDrop(int instructionKey) { List mkInstructions = new ArrayList<>(); - List actionsInfos = new ArrayList <> (); - actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[]{}).buildAction()); + List actionsInfos = new ArrayList<>(); + actionsInfos.add(new ActionDrop().buildAction()); mkInstructions.add(getWriteActionsInstruction(actionsInfos, instructionKey)); return mkInstructions; } @@ -486,72 +510,85 @@ public class MDSALUtil { .setKey(new InstructionKey(instructionKey)).build(); } + /** + * Deprecated read. + * @deprecated Use {@link SingleTransactionDataBroker#syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional( + * DataBroker, LogicalDatastoreType, InstanceIdentifier)} + */ + @Deprecated public static Optional read(LogicalDatastoreType datastoreType, InstanceIdentifier path, DataBroker broker) { - - ReadOnlyTransaction tx = broker.newReadOnlyTransaction(); - - Optional result = Optional.absent(); - try { - result = tx.read(datastoreType, path).get(); - } catch (Exception e) { - logger.error("An error occured while reading data from the path {} with the exception {}", path, e); - } - return result; - } - - public static Optional read(DataBroker broker, - LogicalDatastoreType datastoreType, InstanceIdentifier path) { - - ReadOnlyTransaction tx = broker.newReadOnlyTransaction(); - - Optional result = Optional.absent(); + return SingleTransactionDataBroker + .syncReadOptionalAndTreatReadFailedExceptionAsAbsentOptional(broker, datastoreType, path); + } + + /** + * Deprecated read. + * @deprecated Use {@link SingleTransactionDataBroker#syncReadOptional( + * DataBroker, LogicalDatastoreType, InstanceIdentifier)} + */ + @Deprecated + public static Optional read(DataBroker broker, LogicalDatastoreType datastoreType, + InstanceIdentifier path) { try { - result = tx.read(datastoreType, path).get(); - } catch (Exception e) { + return SingleTransactionDataBroker.syncReadOptional(broker, datastoreType, path); + } catch (ReadFailedException e) { throw new RuntimeException(e); } - return result; } + /** + * Deprecated write. + * + * @deprecated Use + * {@link SingleTransactionDataBroker#syncWrite( + * DataBroker, LogicalDatastoreType, InstanceIdentifier, DataObject)} + */ + @Deprecated public static void syncWrite(DataBroker broker, LogicalDatastoreType datastoreType, InstanceIdentifier path, T data) { - WriteTransaction tx = broker.newWriteOnlyTransaction(); - tx.put(datastoreType, path, data, true); - CheckedFuture futures = tx.submit(); try { - futures.get(); - } catch (InterruptedException | ExecutionException e) { - logger.error("Error writing to datastore (path, data) : ({}, {})", path, data); - throw new RuntimeException(e.getMessage()); + SingleTransactionDataBroker.syncWrite(broker, datastoreType, path, data); + } catch (TransactionCommitFailedException e) { + LOG.error("Error writing to datastore (path, data) : ({}, {})", path, data); + throw new RuntimeException(e); } } + /** + * Deprecated update. + * + * @deprecated Use + * {@link SingleTransactionDataBroker#syncUpdate( + * DataBroker, LogicalDatastoreType, InstanceIdentifier, DataObject)} + */ + @Deprecated public static void syncUpdate(DataBroker broker, LogicalDatastoreType datastoreType, InstanceIdentifier path, T data) { - WriteTransaction tx = broker.newWriteOnlyTransaction(); - tx.merge(datastoreType, path, data, true); - CheckedFuture futures = tx.submit(); try { - futures.get(); - } catch (InterruptedException | ExecutionException e) { - logger.error("Error writing to datastore (path, data) : ({}, {})", path, data); - throw new RuntimeException(e.getMessage()); + SingleTransactionDataBroker.syncUpdate(broker, datastoreType, path, data); + } catch (TransactionCommitFailedException e) { + LOG.error("Error writing to datastore (path, data) : ({}, {})", path, data); + throw new RuntimeException(e); } } + /** + * Deprecated delete. + * + * @deprecated Use + * {@link SingleTransactionDataBroker#syncDelete(DataBroker, LogicalDatastoreType, InstanceIdentifier)} + */ + @Deprecated public static void syncDelete(DataBroker broker, - LogicalDatastoreType datastoreType, InstanceIdentifier obj) { - WriteTransaction tx = broker.newWriteOnlyTransaction(); - tx.delete(datastoreType, obj); - CheckedFuture futures = tx.submit(); + LogicalDatastoreType datastoreType, InstanceIdentifier path) { try { - futures.get(); - } catch (InterruptedException | ExecutionException e) { - logger.error("Error deleting from datastore (path) : ({})", obj); - throw new RuntimeException(e.getMessage()); + SingleTransactionDataBroker.syncDelete(broker, datastoreType, path); + } catch (TransactionCommitFailedException e) { + LOG.error("Error deleting from datastore (path) : ({})", path, e); + throw new RuntimeException(e); } } @@ -559,7 +596,7 @@ public class MDSALUtil { InstanceIdentifier nodeConnectorId) { Optional optNc = MDSALDataStoreUtils.read(broker, LogicalDatastoreType.OPERATIONAL, nodeConnectorId); - if(optNc.isPresent()) { + if (optNc.isPresent()) { NodeConnector nc = optNc.get(); FlowCapableNodeConnector fcnc = nc.getAugmentation(FlowCapableNodeConnector.class); MacAddress macAddress = fcnc.getHardwareAddress(); @@ -581,49 +618,25 @@ public class MDSALUtil { .child(Node.class, new NodeKey(nodeId)) .child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId)).build(); - - Optional nodeConnectorOptional = read( - dataBroker, - LogicalDatastoreType.OPERATIONAL, ncIdentifier); - if (!nodeConnectorOptional.isPresent()) { - return null; - } - NodeConnector nc = nodeConnectorOptional.get(); - FlowCapableNodeConnector fc = nc - .getAugmentation(FlowCapableNodeConnector.class); - return fc.getName(); + return read(dataBroker, LogicalDatastoreType.OPERATIONAL, ncIdentifier).transform( + nc -> nc.getAugmentation(FlowCapableNodeConnector.class).getName()).orNull(); } public static NodeConnectorId getNodeConnectorId(DataBroker dataBroker, NodeConnectorRef ref) { - Optional nc = (Optional) read( - dataBroker, - LogicalDatastoreType.OPERATIONAL, ref.getValue()); - if(nc.isPresent()){ - return nc.get().getId(); - } - return null; - } - - public static TransmitPacketInput getPacketOut(List actions, byte[] payload, BigInteger dpnId) { - NodeConnectorRef ncRef = getDefaultNodeConnRef(dpnId); - return new TransmitPacketInputBuilder() - .setAction(actions) - .setPayload(payload) - .setNode( - new NodeRef(InstanceIdentifier.builder(Nodes.class) - .child(Node.class, new NodeKey(new NodeId("openflow:" + dpnId))).toInstance())) - .setIngress(ncRef).setEgress(ncRef).build(); + return ((Optional) read(dataBroker, LogicalDatastoreType.OPERATIONAL, ref.getValue())).transform( + NodeConnector::getId).orNull(); } public static Action createNxOfInPortAction(final int actionKey, final int inPortVal) { - - NxRegLoad r = new NxRegLoadBuilder().setDst(new DstBuilder().setDstChoice(new DstNxOfInPortCaseBuilder().setOfInPort(Boolean.TRUE).build()) - .setStart(Integer.valueOf(0)).setEnd(Integer.valueOf(15)).build()).setValue(BigInteger.valueOf(inPortVal)).build(); + NxRegLoad regLoad = new NxRegLoadBuilder() + .setDst(new DstBuilder().setDstChoice(new DstNxOfInPortCaseBuilder().setOfInPort(Boolean.TRUE).build()) + .setStart(0).setEnd(15).build()) + .setValue(BigInteger.valueOf(inPortVal)).build(); ActionBuilder abExt = new ActionBuilder(); abExt.setKey(new ActionKey(actionKey)); abExt.setOrder(actionKey); - abExt.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(r).build()); + abExt.setAction(new NxActionRegLoadNodesNodeTableFlowApplyActionsCaseBuilder().setNxRegLoad(regLoad).build()); return abExt.build(); }