2 * Copyright (c) 2017 Intel Corporation. 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
8 package org.opendaylight.neutron.hostconfig.ovs;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.Maps;
12 import java.util.Collection;
13 import java.util.Locale;
15 import javax.annotation.PostConstruct;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.DataObjectModification;
23 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
24 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
25 import org.opendaylight.mdsal.binding.api.DataTreeModification;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
28 import org.opendaylight.neutron.hostconfig.utils.NeutronHostconfigUtils;
29 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
30 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
37 import org.opendaylight.yangtools.concepts.ListenerRegistration;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class NeutronHostconfigOvsListener implements ClusteredDataTreeChangeListener<Node> {
44 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigOvsListener.class);
45 private final DataBroker dataBroker;
46 private final SouthboundUtils southboundUtils;
47 private final NeutronHostconfigUtils neutronHostconfig;
48 private ListenerRegistration<DataTreeChangeListener<Node>> listenerRegistration;
49 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
50 private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
51 private static int HOST_TYPE_STR_LEN = 8;
54 public NeutronHostconfigOvsListener(final DataBroker dataBroker) {
55 this.dataBroker = dataBroker;
56 this.southboundUtils = new SouthboundUtils(new MdsalUtils(dataBroker));
57 this.neutronHostconfig = new NeutronHostconfigUtils(dataBroker);
60 private void processChanges(Collection<DataTreeModification<Node>> changes)
61 throws TransactionCommitFailedException {
62 LOG.info("onDataTreeChanged: Received Data Tree Changes: {}", changes);
63 for (DataTreeModification<Node> change : changes) {
64 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
65 final DataObjectModification<Node> mod = change.getRootNode();
66 LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
67 mod.getModificationType(), key);
68 switch (mod.getModificationType()) {
70 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.DELETE);
72 case SUBTREE_MODIFIED:
73 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.UPDATE);
76 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.ADD);
79 LOG.error("onDataTreeChanged: Invalid modification type={}",
80 mod.getModificationType());
87 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<Node>> changes) {
88 Preconditions.checkNotNull(changes, "Changes may not be null!");
90 processChanges(changes);
91 } catch (TransactionCommitFailedException e) {
92 LOG.error("Transaction commit failed; ignorining changes: {}", changes, e);
96 private static InstanceIdentifier<Node> createNodeIdentifier() {
97 return InstanceIdentifier
98 .create(NetworkTopology.class)
99 .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
105 LOG.info("{} start", getClass().getSimpleName());
106 DataTreeIdentifier<Node> dataTreeIdentifier =
107 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, createNodeIdentifier());
108 LOG.info("Neutron Hostconfig DataChange listener registration {}", dataTreeIdentifier);
109 listenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier, this);
113 public void close() throws Exception {
114 if (listenerRegistration != null) {
115 listenerRegistration.close();
116 LOG.trace("HostConfig listener Closed");
120 private void updateHostConfig(Node node, NeutronHostconfigUtils.Action action)
121 throws TransactionCommitFailedException {
122 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
123 if (hostId == null) {
126 for (Map.Entry<String, String> entry : extractHostConfig(node).entrySet()) {
127 neutronHostconfig.updateMdsal(neutronHostconfig.buildHostConfigInfo(hostId, entry.getKey(),
128 entry.getValue()), action);
132 private Map<String, String> extractHostConfig(Node node) {
133 Map<String, String> config = Maps.newHashMap();
134 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
135 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
136 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
137 if (openvswitchExternalIds.getExternalIdKey().startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
138 // Extract the host type. Max 8 characters after
139 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
140 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
141 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
142 if (hostType.length() > 0) {
143 if (hostType.length() > HOST_TYPE_STR_LEN) {
144 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
146 hostType = "ODL " + hostType.toUpperCase(Locale.ROOT);
147 if (null != openvswitchExternalIds.getExternalIdValue()) {
148 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
157 private String getExternalId(Node node, String key) {
158 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
159 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
160 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
161 if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
162 return openvswitchExternalIds.getExternalIdValue();
169 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
170 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
171 if (ovsdbNode == null) {
172 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
173 if (nodeFromReadOvsdbNode != null) {
174 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);