Bug 3310 Bug 3316 Use iid stamp if we configured a connection, otherwise systemid
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / ovsdb / transact / TransactUtils.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.ovsdb.southbound.ovsdb.transact;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Set;
19
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;
40
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;
46
47 public class TransactUtils {
48     private static final Logger LOG = LoggerFactory.getLogger(TransactUtils.class);
49
50     public static Map<InstanceIdentifier<Node>,Node> extractNode(
51             Map<InstanceIdentifier<?>, DataObject> changes) {
52         Map<InstanceIdentifier<Node>,Node> result
53             = new HashMap<InstanceIdentifier<Node>,Node>();
54         if (changes != null && changes.entrySet() != 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);
63                     }
64                 }
65             }
66         }
67         return result;
68     }
69
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);
73     }
74
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);
78     }
79
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));
84         return result;
85     }
86
87     public static <T extends DataObject> Map<InstanceIdentifier<T>, T> extractCreatedOrUpdatedOrRemoved(
88             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
89             Class<T> klazz) {
90         Map<InstanceIdentifier<T>,T> result = extractCreatedOrUpdated(changes,klazz);
91         result.putAll(extractRemovedObjects(changes, klazz));
92         return result;
93     }
94
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);
98     }
99
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<InstanceIdentifier<T>>();
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;
108                     result.add(iidn);
109                 }
110             }
111         }
112         return result;
113     }
114
115     public static <T extends DataObject> Map<InstanceIdentifier<T>, T> extractRemovedObjects(
116             AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> changes,
117             Class<T> klazz) {
118         Set<InstanceIdentifier<T>> iids = extractRemoved(changes, klazz);
119         return Maps.filterKeys(extractOriginal(changes, klazz),Predicates.in(iids));
120     }
121
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<InstanceIdentifier<T>,T>();
125         if (changes != null && changes.entrySet() != 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);
135                     }
136                 }
137             }
138         }
139         return result;
140     }
141
142     public static List<Insert> extractInsert(TransactionBuilder transaction, GenericTableSchema schema) {
143         List<Operation> operations = transaction.getOperations();
144         List<Insert> inserts = new ArrayList<Insert>();
145         for (int count = 0;count < operations.size();count++) {
146             Operation operation = operations.get(count);
147             if (operation instanceof Insert && operation.getTableSchema().equals(schema)) {
148                 Insert insert = (Insert)operation;
149                 inserts.add(insert);
150             }
151         }
152         return inserts;
153     }
154
155     /**
156      * Extract the NamedUuid from the Insert.
157      * If the Insert does not have a NamedUuid set, a random one will be
158      * generated, set, and returned.
159      *
160      * @param insert - Insert from which to extract the NamedUuid
161      * @return UUID - NamedUUID of the Insert
162      */
163     public static UUID extractNamedUuid(Insert insert) {
164         String uuidString = insert.getUuidName() != null
165                 ? insert.getUuidName() : SouthboundMapper.getRandomUUID();
166         insert.setUuidName(uuidString);
167         UUID uuid = new UUID(uuidString);
168         return uuid;
169     }
170
171     public static <T  extends TableSchema<T>> void stampInstanceIdentifier(TransactionBuilder transaction,
172             InstanceIdentifier<?> iid, TableSchema<T> tableSchema,
173             ColumnSchema<T, Map<String,String>> columnSchema) {
174         Map<String,String> externalIdsMap = ImmutableMap.of(SouthboundConstants.IID_EXTERNAL_ID_KEY,
175                 SouthboundUtil.serializeInstanceIdentifier(iid));
176         Mutate<T> mutate = op.mutate(tableSchema)
177                 .addMutation(columnSchema,
178                     Mutator.INSERT,
179                     externalIdsMap);
180         Mutation deleteIidMutation = new Mutation(columnSchema.getName(),
181                 Mutator.DELETE,
182                 OvsdbSet.fromSet(Sets.newHashSet(SouthboundConstants.IID_EXTERNAL_ID_KEY)));
183         List<Mutation> mutations = Lists.newArrayList(Sets.newHashSet(deleteIidMutation));
184         mutations.addAll(mutate.getMutations());
185         mutate.setMutations(mutations);
186         transaction.add(mutate);
187     }
188 }