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