0a21cb73a0d62d81d2cd45d46274f55b439d8099
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepSouthboundUtil.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
19 import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchAttributes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.LogicalSwitches;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteMcastMacs;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.RemoteUcastMacs;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.Identifiable;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
37 import com.google.common.util.concurrent.CheckedFuture;
38
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Objects;
43 import java.util.concurrent.ConcurrentHashMap;
44
45 public class HwvtepSouthboundUtil {
46
47     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundUtil.class);
48     private static final String SCHEMA_VERSION_MISMATCH =
49             "{} column for {} table is not supported by this version of the {} schema: {}";
50
51     private static InstanceIdentifierCodec instanceIdentifierCodec;
52
53     private HwvtepSouthboundUtil() {
54         // Prevent instantiating a utility class
55     }
56
57     public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
58         instanceIdentifierCodec = iidc;
59     }
60
61     public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
62         return instanceIdentifierCodec;
63     }
64
65     public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
66         return instanceIdentifierCodec.serialize(iid);
67     }
68
69     public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
70         InstanceIdentifier<?> result = null;
71         try {
72             result = instanceIdentifierCodec.bindingDeserializer(iidString);
73         } catch (DeserializationException e) {
74             LOG.warn("Unable to deserialize iidString", e);
75         }
76         return result;
77     }
78
79     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
80                     ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
81         Optional<D> node = Optional.absent();
82         try {
83             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
84         } catch (final ReadFailedException e) {
85             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
86         }
87         return node;
88     }
89
90     public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
91                     HwvtepPhysicalSwitchAttributes pNode) {
92         Preconditions.checkNotNull(pNode);
93         Optional<HwvtepGlobalAugmentation> result = null;
94         HwvtepGlobalRef ref = pNode.getManagedBy();
95         if (ref != null && ref.getValue() != null) {
96             result = getManagingNode(db, ref);
97         } else {
98             LOG.warn("Cannot find client for PhysicalSwitch without a specified ManagedBy {}", pNode);
99             return Optional.absent();
100         }
101         if (!result.isPresent()) {
102             LOG.warn("Failed to find managing node for PhysicalSwitch {}", pNode);
103         }
104         return result;
105     }
106
107     public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
108         try {
109             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
110             @SuppressWarnings("unchecked")
111             // Note: erasure makes this safe in combination with the typecheck
112             // below
113             InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
114
115             CheckedFuture<Optional<Node>, ReadFailedException> nf =
116                             transaction.read(LogicalDatastoreType.OPERATIONAL, path);
117             transaction.close();
118             Optional<Node> optional = nf.get();
119             if (optional != null && optional.isPresent()) {
120                 HwvtepGlobalAugmentation hwvtepNode = null;
121                 Node node = optional.get();
122                 if (node instanceof HwvtepGlobalAugmentation) {
123                     hwvtepNode = (HwvtepGlobalAugmentation) node;
124                 } else if (node != null) {
125                     hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
126                 }
127                 if (hwvtepNode != null) {
128                     return Optional.of(hwvtepNode);
129                 } else {
130                     LOG.warn("Hwvtep switch claims to be managed by {} but " + "that HwvtepNode does not exist",
131                                     ref.getValue());
132                     return Optional.absent();
133                 }
134             } else {
135                 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
136                 return Optional.absent();
137             }
138         } catch (Exception e) {
139             LOG.warn("Failed to get HwvtepNode {}", ref, e);
140             return Optional.absent();
141         }
142     }
143     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
144         return String.valueOf(
145                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
146     }
147
148
149     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
150         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, "hw_vtep", ex.getMessage());
151     }
152
153     public static <KeyType, D> void updateData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
154                                                Class<? extends Identifiable> cls, KeyType key, D data) {
155         if (key == null) {
156             return;
157         }
158         if (!map.containsKey(cls)) {
159             map.put(cls, new ConcurrentHashMap<>());
160         }
161         map.get(cls).put(key, data);
162     }
163
164     public static <KeyType, D> D getData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
165                                          Class<? extends Identifiable> cls, KeyType key) {
166         if (key == null) {
167             return null;
168         }
169         if (map.containsKey(cls)) {
170             return map.get(cls).get(key);
171         }
172         return null;
173     }
174
175     public static <KeyType, D> boolean containsKey(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
176                                                    Class<? extends Identifiable> cls, KeyType key) {
177         if (key == null) {
178             return false;
179         }
180         if (map.containsKey(cls)) {
181             return map.get(cls).containsKey(key);
182         }
183         return false;
184     }
185
186     public static <KeyType, D> void clearData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
187                                               Class<? extends Identifiable> cls, KeyType key) {
188         if (key == null) {
189             return;
190         }
191         if (map.containsKey(cls)) {
192             map.get(cls).remove(key);
193         }
194     }
195
196     public static <T> boolean isEmpty(Collection<T> list) {
197         return list == null || list.isEmpty();
198     }
199
200     public static boolean isEmptyMap(Map map) {
201         return map == null || map.isEmpty();
202     }
203 }