2 * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
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.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yangtools.yang.binding.Identifiable;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 import java.util.concurrent.ConcurrentHashMap;
34 public class HwvtepSouthboundUtil {
36 private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundUtil.class);
37 private static final String SCHEMA_VERSION_MISMATCH =
38 "{} column for {} table is not supported by this version of the {} schema: {}";
40 private static InstanceIdentifierCodec instanceIdentifierCodec;
42 private HwvtepSouthboundUtil() {
43 // Prevent instantiating a utility class
46 public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
47 instanceIdentifierCodec = iidc;
50 public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
51 return instanceIdentifierCodec;
54 public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
55 return instanceIdentifierCodec.serialize(iid);
58 public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
59 InstanceIdentifier<?> result = null;
61 result = instanceIdentifierCodec.bindingDeserializer(iidString);
62 } catch (DeserializationException e) {
63 LOG.warn("Unable to deserialize iidString", e);
68 public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
69 ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
70 Optional<D> node = Optional.absent();
72 node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
73 } catch (final ReadFailedException e) {
74 LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
79 public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
80 HwvtepPhysicalSwitchAttributes pNode) {
81 Preconditions.checkNotNull(pNode);
82 Optional<HwvtepGlobalAugmentation> result = null;
83 HwvtepGlobalRef ref = pNode.getManagedBy();
84 if (ref != null && ref.getValue() != null) {
85 result = getManagingNode(db, ref);
87 LOG.warn("Cannot find client for PhysicalSwitch without a specified ManagedBy {}", pNode);
88 return Optional.absent();
90 if (!result.isPresent()) {
91 LOG.warn("Failed to find managing node for PhysicalSwitch {}", pNode);
96 public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
98 ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
99 @SuppressWarnings("unchecked")
100 // Note: erasure makes this safe in combination with the typecheck
102 InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
104 CheckedFuture<Optional<Node>, ReadFailedException> nf =
105 transaction.read(LogicalDatastoreType.OPERATIONAL, path);
107 Optional<Node> optional = nf.get();
108 if (optional != null && optional.isPresent()) {
109 HwvtepGlobalAugmentation hwvtepNode = null;
110 Node node = optional.get();
111 if (node instanceof HwvtepGlobalAugmentation) {
112 hwvtepNode = (HwvtepGlobalAugmentation) node;
113 } else if (node != null) {
114 hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
116 if (hwvtepNode != null) {
117 return Optional.of(hwvtepNode);
119 LOG.warn("Hwvtep switch claims to be managed by {} but " + "that HwvtepNode does not exist",
121 return Optional.absent();
124 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
125 return Optional.absent();
127 } catch (Exception e) {
128 LOG.warn("Failed to get HwvtepNode {}", ref, e);
129 return Optional.absent();
132 public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
133 return String.valueOf(
134 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
138 public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
139 LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, "hw_vtep", ex.getMessage());
142 public static <KeyType, D> void updateData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
143 Class<? extends Identifiable> cls, KeyType key, D data) {
147 if (!map.containsKey(cls)) {
148 map.put(cls, new ConcurrentHashMap<>());
150 map.get(cls).put(key, data);
153 public static <KeyType, D> D getData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
154 Class<? extends Identifiable> cls, KeyType key) {
158 if (map.containsKey(cls)) {
159 return map.get(cls).get(key);
164 public static <KeyType, D> boolean containsKey(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
165 Class<? extends Identifiable> cls, KeyType key) {
169 if (map.containsKey(cls)) {
170 return map.get(cls).containsKey(key);
175 public static <KeyType, D> void clearData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
176 Class<? extends Identifiable> cls, KeyType key) {
180 if (map.containsKey(cls)) {
181 map.get(cls).remove(key);