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