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