bug 6579 added dependency queue
[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.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;
30
31 import java.util.List;
32 import java.util.Map;
33 import java.util.concurrent.ConcurrentHashMap;
34
35 public class HwvtepSouthboundUtil {
36
37     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundUtil.class);
38     private static final String SCHEMA_VERSION_MISMATCH =
39             "{} column for {} table is not supported by this version of the {} schema: {}";
40
41     private static InstanceIdentifierCodec instanceIdentifierCodec;
42
43     private HwvtepSouthboundUtil() {
44         // Prevent instantiating a utility class
45     }
46
47     public static void setInstanceIdentifierCodec(InstanceIdentifierCodec iidc) {
48         instanceIdentifierCodec = iidc;
49     }
50
51     public static InstanceIdentifierCodec getInstanceIdentifierCodec() {
52         return instanceIdentifierCodec;
53     }
54
55     public static String serializeInstanceIdentifier(InstanceIdentifier<?> iid) {
56         return instanceIdentifierCodec.serialize(iid);
57     }
58
59     public static InstanceIdentifier<?> deserializeInstanceIdentifier(String iidString) {
60         InstanceIdentifier<?> result = null;
61         try {
62             result = instanceIdentifierCodec.bindingDeserializer(iidString);
63         } catch (DeserializationException e) {
64             LOG.warn("Unable to deserialize iidString", e);
65         }
66         return result;
67     }
68
69     public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> Optional<D> readNode(
70                     ReadWriteTransaction transaction, final InstanceIdentifier<D> connectionIid) {
71         Optional<D> node = Optional.absent();
72         try {
73             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
74         } catch (final ReadFailedException e) {
75             LOG.warn("Read Operational/DS for Node failed! {}", connectionIid, e);
76         }
77         return node;
78     }
79
80     public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
81                     HwvtepPhysicalSwitchAttributes pNode) {
82         Preconditions.checkNotNull(pNode);
83         Optional<HwvtepGlobalAugmentation> result = null;
84         HwvtepGlobalRef ref = pNode.getManagedBy();
85         if (ref != null && ref.getValue() != null) {
86             result = getManagingNode(db, ref);
87         } else {
88             LOG.warn("Cannot find client for PhysicalSwitch without a specified ManagedBy {}", pNode);
89             return Optional.absent();
90         }
91         if (!result.isPresent()) {
92             LOG.warn("Failed to find managing node for PhysicalSwitch {}", pNode);
93         }
94         return result;
95     }
96
97     public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
98         try {
99             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
100             @SuppressWarnings("unchecked")
101             // Note: erasure makes this safe in combination with the typecheck
102             // below
103             InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
104
105             CheckedFuture<Optional<Node>, ReadFailedException> nf =
106                             transaction.read(LogicalDatastoreType.OPERATIONAL, path);
107             transaction.close();
108             Optional<Node> optional = nf.get();
109             if (optional != null && optional.isPresent()) {
110                 HwvtepGlobalAugmentation hwvtepNode = null;
111                 Node node = optional.get();
112                 if (node instanceof HwvtepGlobalAugmentation) {
113                     hwvtepNode = (HwvtepGlobalAugmentation) node;
114                 } else if (node != null) {
115                     hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
116                 }
117                 if (hwvtepNode != null) {
118                     return Optional.of(hwvtepNode);
119                 } else {
120                     LOG.warn("Hwvtep switch claims to be managed by {} but " + "that HwvtepNode does not exist",
121                                     ref.getValue());
122                     return Optional.absent();
123                 }
124             } else {
125                 LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
126                 return Optional.absent();
127             }
128         } catch (Exception e) {
129             LOG.warn("Failed to get HwvtepNode {}", ref, e);
130             return Optional.absent();
131         }
132     }
133     public static String connectionInfoToString(final ConnectionInfo connectionInfo) {
134         return String.valueOf(
135                 connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
136     }
137
138
139     public static void schemaMismatchLog(String column, String table, SchemaVersionMismatchException ex) {
140         LOG.debug(SCHEMA_VERSION_MISMATCH, column, table, "hw_vtep", ex.getMessage());
141     }
142
143     public static <KeyType, D> void updateData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
144                                                Class<? extends Identifiable> cls, KeyType key, D data) {
145         if (key == null) {
146             return;
147         }
148         if (!map.containsKey(cls)) {
149             map.put(cls, new ConcurrentHashMap<>());
150         }
151         map.get(cls).put(key, data);
152     }
153
154     public static <KeyType, D> D getData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
155                                          Class<? extends Identifiable> cls, KeyType key) {
156         if (key == null) {
157             return null;
158         }
159         if (map.containsKey(cls)) {
160             return map.get(cls).get(key);
161         }
162         return null;
163     }
164
165     public static <KeyType, D> boolean containsKey(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
166                                                    Class<? extends Identifiable> cls, KeyType key) {
167         if (key == null) {
168             return false;
169         }
170         if (map.containsKey(cls)) {
171             return map.get(cls).containsKey(key);
172         }
173         return false;
174     }
175
176     public static <KeyType, D> void clearData(Map<Class<? extends Identifiable>, Map<KeyType, D>> map,
177                                               Class<? extends Identifiable> cls, KeyType key) {
178         if (key == null) {
179             return;
180         }
181         if (map.containsKey(cls)) {
182             map.get(cls).remove(key);
183         }
184     }
185
186     public static <T> boolean isEmpty(List<T> list) {
187         return list == null || list.isEmpty();
188     }
189 }