X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=southbound%2Fsouthbound-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fovsdb%2Fsouthbound%2Fovsdb%2Ftransact%2FTransactUtils.java;h=0d37f5ecefa06c66b8ce03b03d35439b1a561934;hb=df08ae0d239824f7bce568a5dbaa7de48ad9ae31;hp=9568248259b71fef1c62653b13c63c6285b5d307;hpb=d252e98799b9aeb0b7660c927b69ab372f45dfea;p=ovsdb.git diff --git a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TransactUtils.java b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TransactUtils.java index 956824825..0d37f5ece 100644 --- a/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TransactUtils.java +++ b/southbound/southbound-impl/src/main/java/org/opendaylight/ovsdb/southbound/ovsdb/transact/TransactUtils.java @@ -7,27 +7,51 @@ */ package org.opendaylight.ovsdb.southbound.ovsdb.transact; +import static org.opendaylight.ovsdb.lib.operations.Operations.op; + +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; +import org.opendaylight.ovsdb.lib.notation.Mutation; +import org.opendaylight.ovsdb.lib.notation.Mutator; +import org.opendaylight.ovsdb.lib.notation.OvsdbSet; +import org.opendaylight.ovsdb.lib.notation.UUID; +import org.opendaylight.ovsdb.lib.operations.Insert; +import org.opendaylight.ovsdb.lib.operations.Mutate; +import org.opendaylight.ovsdb.lib.operations.Operation; +import org.opendaylight.ovsdb.lib.operations.TransactionBuilder; +import org.opendaylight.ovsdb.lib.schema.ColumnSchema; +import org.opendaylight.ovsdb.lib.schema.GenericTableSchema; +import org.opendaylight.ovsdb.lib.schema.TableSchema; +import org.opendaylight.ovsdb.southbound.SouthboundConstants; +import org.opendaylight.ovsdb.southbound.SouthboundMapper; +import org.opendaylight.ovsdb.southbound.SouthboundUtil; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Predicates; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; + public class TransactUtils { private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class); public static Map,Node> extractNode( Map, DataObject> changes) { Map,Node> result - = new HashMap,Node>(); - if (changes != null && changes.entrySet() != null) { + = new HashMap<>(); + if (changes != null) { for (Entry, DataObject> created : changes.entrySet()) { if (created.getValue() instanceof Node) { Node value = (Node) created.getValue(); @@ -60,6 +84,14 @@ public class TransactUtils { return result; } + public static Map, T> extractCreatedOrUpdatedOrRemoved( + AsyncDataChangeEvent, DataObject> changes, + Class klazz) { + Map,T> result = extractCreatedOrUpdated(changes,klazz); + result.putAll(extractRemovedObjects(changes, klazz)); + return result; + } + public static Map,T> extractOriginal( AsyncDataChangeEvent,DataObject> changes,Class klazz) { return extract(changes.getOriginalData(),klazz); @@ -67,7 +99,7 @@ public class TransactUtils { public static Set> extractRemoved( AsyncDataChangeEvent,DataObject> changes,Class klazz) { - Set> result = new HashSet>(); + Set> result = new HashSet<>(); if (changes != null && changes.getRemovedPaths() != null) { for (InstanceIdentifier iid : changes.getRemovedPaths()) { if (iid.getTargetType().equals(klazz)) { @@ -80,10 +112,17 @@ public class TransactUtils { return result; } + public static Map, T> extractRemovedObjects( + AsyncDataChangeEvent, DataObject> changes, + Class klazz) { + Set> iids = extractRemoved(changes, klazz); + return Maps.filterKeys(extractOriginal(changes, klazz),Predicates.in(iids)); + } + public static Map,T> extract( Map, DataObject> changes, Class klazz) { - Map,T> result = new HashMap,T>(); - if (changes != null && changes.entrySet() != null) { + Map,T> result = new HashMap<>(); + if (changes != null) { for (Entry, DataObject> created : changes.entrySet()) { if (klazz.isInstance(created.getValue())) { @SuppressWarnings("unchecked") @@ -99,4 +138,55 @@ public class TransactUtils { } return result; } + + public static List extractInsert(TransactionBuilder transaction, GenericTableSchema schema) { + List operations = transaction.getOperations(); + List inserts = new ArrayList<>(); + for (Operation operation : operations) { + if (operation instanceof Insert && operation.getTableSchema().equals(schema)) { + inserts.add((Insert) operation); + } + } + return inserts; + } + + /** + * Extract the NamedUuid from the Insert. + * If the Insert does not have a NamedUuid set, a random one will be + * generated, set, and returned. + * + * @param insert - Insert from which to extract the NamedUuid + * @return UUID - NamedUUID of the Insert + */ + public static UUID extractNamedUuid(Insert insert) { + String uuidString = insert.getUuidName() != null + ? insert.getUuidName() : SouthboundMapper.getRandomUUID(); + insert.setUuidName(uuidString); + return new UUID(uuidString); + } + + public static > void stampInstanceIdentifier(TransactionBuilder transaction, + InstanceIdentifier iid, TableSchema tableSchema, + ColumnSchema> columnSchema) { + transaction.add(stampInstanceIdentifierMutation(transaction,iid, + tableSchema,columnSchema)); + } + + public static > Mutate stampInstanceIdentifierMutation(TransactionBuilder transaction, + InstanceIdentifier iid, TableSchema tableSchema, + ColumnSchema> columnSchema) { + Map externalIdsMap = ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY, + SouthboundUtil.serializeInstanceIdentifier(iid)); + Mutate mutate = op.mutate(tableSchema) + .addMutation(columnSchema, + Mutator.INSERT, + externalIdsMap); + Mutation deleteIidMutation = new Mutation(columnSchema.getName(), + Mutator.DELETE, + OvsdbSet.fromSet(Sets.newHashSet(SouthboundConstants.IID_EXTERNAL_ID_KEY))); + List mutations = Lists.newArrayList(Sets.newHashSet(deleteIidMutation)); + mutations.addAll(mutate.getMutations()); + mutate.setMutations(mutations); + return mutate; + } }