2 * Copyright (c) 2014 Cisco Systems, Inc. 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
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
17 import java.util.Map.Entry;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
21 import org.opendaylight.ovsdb.lib.notation.Mutation;
22 import org.opendaylight.ovsdb.lib.notation.Mutator;
23 import org.opendaylight.ovsdb.lib.notation.OvsdbSet;
24 import org.opendaylight.ovsdb.lib.notation.UUID;
25 import org.opendaylight.ovsdb.lib.operations.Insert;
26 import org.opendaylight.ovsdb.lib.operations.Mutate;
27 import org.opendaylight.ovsdb.lib.operations.Operation;
28 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
29 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
30 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
31 import org.opendaylight.ovsdb.lib.schema.TableSchema;
32 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
33 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
34 import org.opendaylight.ovsdb.southbound.SouthboundUtil;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 import com.google.common.base.Predicates;
42 import com.google.common.collect.ImmutableMap;
43 import com.google.common.collect.Lists;
44 import com.google.common.collect.Maps;
45 import com.google.common.collect.Sets;
47 public class TransactUtils {
48 private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
50 public static Map<InstanceIdentifier<Node>,Node> extractNode(
51 Map<InstanceIdentifier<?>, DataObject> changes) {
52 Map<InstanceIdentifier<Node>,Node> result
54 if (changes != null) {
55 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
56 if (created.getValue() instanceof Node) {
57 Node value = (Node) created.getValue();
58 Class<?> type = created.getKey().getTargetType();
59 if (type.equals(Node.class)) {
60 @SuppressWarnings("unchecked") // Actually checked above
61 InstanceIdentifier<Node> iid = (InstanceIdentifier<Node>) created.getKey();
62 result.put(iid, value);
70 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractCreated(
71 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,Class<T> klazz) {
72 return extract(changes.getCreatedData(),klazz);
75 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractUpdated(
76 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
77 return extract(changes.getUpdatedData(),klazz);
80 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractCreatedOrUpdated(
81 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
82 Map<InstanceIdentifier<T>,T> result = extractUpdated(changes,klazz);
83 result.putAll(extractCreated(changes,klazz));
87 public static <T extends DataObject> Map<InstanceIdentifier<T>, T> extractCreatedOrUpdatedOrRemoved(
88 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
90 Map<InstanceIdentifier<T>,T> result = extractCreatedOrUpdated(changes,klazz);
91 result.putAll(extractRemovedObjects(changes, klazz));
95 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extractOriginal(
96 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
97 return extract(changes.getOriginalData(),klazz);
100 public static <T extends DataObject> Set<InstanceIdentifier<T>> extractRemoved(
101 AsyncDataChangeEvent<InstanceIdentifier<?>,DataObject> changes,Class<T> klazz) {
102 Set<InstanceIdentifier<T>> result = new HashSet<>();
103 if (changes != null && changes.getRemovedPaths() != null) {
104 for (InstanceIdentifier<?> iid : changes.getRemovedPaths()) {
105 if (iid.getTargetType().equals(klazz)) {
106 @SuppressWarnings("unchecked") // Actually checked above
107 InstanceIdentifier<T> iidn = (InstanceIdentifier<T>)iid;
115 public static <T extends DataObject> Map<InstanceIdentifier<T>, T> extractRemovedObjects(
116 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
118 Set<InstanceIdentifier<T>> iids = extractRemoved(changes, klazz);
119 return Maps.filterKeys(extractOriginal(changes, klazz),Predicates.in(iids));
122 public static <T extends DataObject> Map<InstanceIdentifier<T>,T> extract(
123 Map<InstanceIdentifier<?>, DataObject> changes, Class<T> klazz) {
124 Map<InstanceIdentifier<T>,T> result = new HashMap<>();
125 if (changes != null) {
126 for (Entry<InstanceIdentifier<?>, DataObject> created : changes.entrySet()) {
127 if (klazz.isInstance(created.getValue())) {
128 @SuppressWarnings("unchecked")
129 T value = (T) created.getValue();
130 Class<?> type = created.getKey().getTargetType();
131 if (type.equals(klazz)) {
132 @SuppressWarnings("unchecked") // Actually checked above
133 InstanceIdentifier<T> iid = (InstanceIdentifier<T>) created.getKey();
134 result.put(iid, value);
142 public static List<Insert> extractInsert(TransactionBuilder transaction, GenericTableSchema schema) {
143 List<Operation> operations = transaction.getOperations();
144 List<Insert> inserts = new ArrayList<>();
145 for (Operation operation : operations) {
146 if (operation instanceof Insert && operation.getTableSchema().equals(schema)) {
147 inserts.add((Insert) operation);
154 * Extract the NamedUuid from the Insert.
155 * If the Insert does not have a NamedUuid set, a random one will be
156 * generated, set, and returned.
158 * @param insert - Insert from which to extract the NamedUuid
159 * @return UUID - NamedUUID of the Insert
161 public static UUID extractNamedUuid(Insert insert) {
162 String uuidString = insert.getUuidName() != null
163 ? insert.getUuidName() : SouthboundMapper.getRandomUUID();
164 insert.setUuidName(uuidString);
165 return new UUID(uuidString);
168 public static <T extends TableSchema<T>> void stampInstanceIdentifier(TransactionBuilder transaction,
169 InstanceIdentifier<?> iid, TableSchema<T> tableSchema,
170 ColumnSchema<T, Map<String,String>> columnSchema) {
171 transaction.add(stampInstanceIdentifierMutation(transaction,iid,
172 tableSchema,columnSchema));
175 public static <T extends TableSchema<T>> Mutate<T> stampInstanceIdentifierMutation(TransactionBuilder transaction,
176 InstanceIdentifier<?> iid, TableSchema<T> tableSchema,
177 ColumnSchema<T, Map<String,String>> columnSchema) {
178 Map<String,String> externalIdsMap = ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY,
179 SouthboundUtil.serializeInstanceIdentifier(iid));
180 Mutate<T> mutate = op.mutate(tableSchema)
181 .addMutation(columnSchema,
184 Mutation deleteIidMutation = new Mutation(columnSchema.getName(),
186 OvsdbSet.fromSet(Sets.newHashSet(SouthboundConstants.IID_EXTERNAL_ID_KEY)));
187 List<Mutation> mutations = Lists.newArrayList(Sets.newHashSet(deleteIidMutation));
188 mutations.addAll(mutate.getMutations());
189 mutate.setMutations(mutations);