2 * Copyright (c) 2016 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.netvirt.neutronvpn;
10 import java.util.HashMap;
11 import java.util.Locale;
13 import javax.annotation.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
20 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class NeutronHostConfigChangeListener
37 extends AsyncDataTreeChangeListenerBase<Node,NeutronHostConfigChangeListener> {
38 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostConfigChangeListener.class);
39 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
40 private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
41 private static int HOST_TYPE_STR_LEN = 8;
49 private final DataBroker dataBroker;
50 private final SouthboundUtils southboundUtils;
51 private final MdsalUtils mdsalUtils;
54 public NeutronHostConfigChangeListener(final DataBroker dataBroker) {
55 super(Node.class,NeutronHostConfigChangeListener.class);
56 this.dataBroker = dataBroker;
57 this.mdsalUtils = new MdsalUtils(dataBroker);
58 this.southboundUtils = new SouthboundUtils(mdsalUtils);
64 LOG.info("{} init", getClass().getSimpleName());
65 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
69 protected InstanceIdentifier<Node> getWildCardPath() {
70 return InstanceIdentifier
71 .create(NetworkTopology.class)
72 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
77 protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
78 return NeutronHostConfigChangeListener.this;
83 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
84 updateHostConfig(del, Action.DELETE);
88 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
89 updateHostConfig(update, Action.UPDATE);
93 protected void add(InstanceIdentifier<Node> identifier, Node add) {
94 updateHostConfig(add, Action.ADD);
98 private void updateHostConfig(Node node, Action action) {
99 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
100 if (hostId == null) {
103 for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
104 updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
108 private Map<String, String> extractHostConfig(Node node) {
109 Map<String, String> config = new HashMap<>();
110 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
111 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
112 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
113 if (openvswitchExternalIds.getExternalIdKey() != null && openvswitchExternalIds.getExternalIdKey()
114 .startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
115 // Extract the host type. Max 8 characters after
116 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
117 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
118 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
119 if (hostType.length() > 0) {
120 if (hostType.length() > HOST_TYPE_STR_LEN) {
121 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
123 hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
124 if (null != openvswitchExternalIds.getExternalIdValue()) {
125 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
134 private void updateMdsal(Hostconfig hostConfig, Action action) {
136 InstanceIdentifier<Hostconfig> hostConfigId;
137 if (hostConfig == null) {
143 hostConfigId = createInstanceIdentifier(hostConfig);
144 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
145 LOG.trace("Add Node: result: {}", result);
148 hostConfigId = createInstanceIdentifier(hostConfig);
149 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
150 LOG.trace("Delete Node: result: {}", result);
153 LOG.warn("Invalid action: {}", action);
158 private Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
159 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
160 hostconfigBuilder.setHostId(hostId);
161 hostconfigBuilder.setHostType(hostType);
162 hostconfigBuilder.setConfig(hostConfig);
163 return hostconfigBuilder.build();
166 private String getExternalId(Node node, String key) {
167 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
168 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
169 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
170 if (key.equals(openvswitchExternalIds.getExternalIdKey())) {
171 return openvswitchExternalIds.getExternalIdValue();
178 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
179 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
180 if (ovsdbNode == null) {
181 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
182 if (nodeFromReadOvsdbNode != null) {
183 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
189 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
190 return InstanceIdentifier.create(Neutron.class)
191 .child(Hostconfigs.class)
192 .child(Hostconfig.class, hostconfig.key());