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
9 package org.opendaylight.neutron.hostconfig.ovs;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
14 import java.util.Collection;
15 import java.util.Locale;
17 import javax.annotation.Nonnull;
18 import javax.annotation.PostConstruct;
19 import javax.annotation.PreDestroy;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.neutron.hostconfig.utils.NeutronHostconfigUtils;
30 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
31 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 public class NeutronHostconfigOvsListener implements ClusteredDataTreeChangeListener<Node> {
46 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigOvsListener.class);
47 private final DataBroker dataBroker;
48 private final SouthboundUtils southboundUtils;
49 private final NeutronHostconfigUtils neutronHostconfig;
50 private ListenerRegistration<DataTreeChangeListener<Node>> listenerRegistration;
51 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
52 private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
53 private static int HOST_TYPE_STR_LEN = 8;
56 public NeutronHostconfigOvsListener(final DataBroker dataBroker) {
57 this.dataBroker = dataBroker;
58 MdsalUtils mdsalUtils = new MdsalUtils(dataBroker);
59 this.southboundUtils = new SouthboundUtils(mdsalUtils);
60 this.neutronHostconfig = new NeutronHostconfigUtils(dataBroker);
63 private void processChanges(Collection<DataTreeModification<Node>> changes) {
64 LOG.info("onDataTreeChanged: Received Data Tree Changed ...", changes);
65 for (DataTreeModification<Node> change : changes) {
66 final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
67 final DataObjectModification<Node> mod = change.getRootNode();
68 LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
69 mod.getModificationType(), key);
70 switch (mod.getModificationType()) {
72 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.DELETE);
74 case SUBTREE_MODIFIED:
75 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.UPDATE);
78 updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.ADD);
81 LOG.error("onDataTreeChanged: Invalid modification type={}",
82 mod.getModificationType());
89 public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
90 Preconditions.checkNotNull(changes, "Changes may not be null!");
91 processChanges(changes);
94 private InstanceIdentifier<Node> createNodeIdentifier() {
95 return InstanceIdentifier
96 .create(NetworkTopology.class)
97 .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
103 LOG.info("{} start", getClass().getSimpleName());
104 DataTreeIdentifier<Node> dataTreeIdentifier =
105 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, createNodeIdentifier());
106 LOG.info("Neutron Hostconfig DataChange listener registration {}", dataTreeIdentifier);
107 listenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier, this);
111 public void close() throws Exception {
112 if (listenerRegistration != null) {
113 listenerRegistration.close();
114 LOG.trace("HostConfig listener Closed");
118 private void updateHostConfig(Node node, NeutronHostconfigUtils.Action action) {
119 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
120 if (hostId == null) {
123 for (Map.Entry<String, String> entry : extractHostConfig(node).entrySet()) {
124 neutronHostconfig.updateMdsal(neutronHostconfig.buildHostConfigInfo(hostId, entry.getKey(),
125 entry.getValue()), action);
129 private Map<String, String> extractHostConfig(Node node) {
130 Map<String, String> config = Maps.newHashMap();
131 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
132 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
133 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
134 if (openvswitchExternalIds.getExternalIdKey().startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
135 // Extract the host type. Max 8 characters after
136 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
137 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
138 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
139 if (hostType.length() > 0) {
140 if (hostType.length() > HOST_TYPE_STR_LEN) {
141 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
143 hostType = "ODL " + hostType.toUpperCase(Locale.ROOT);
144 if (null != openvswitchExternalIds.getExternalIdValue()) {
145 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
154 private String getExternalId(Node node, String key) {
155 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
156 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
157 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
158 if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
159 return openvswitchExternalIds.getExternalIdValue();
166 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
167 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
168 if (ovsdbNode == null) {
169 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
170 if (nodeFromReadOvsdbNode != null) {
171 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);