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.Nullable;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
20 import org.opendaylight.ovsdb.utils.mdsal.utils.ControllerMdsalUtils;
21 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class NeutronHostConfigChangeListener
38 extends AsyncDataTreeChangeListenerBase<Node,NeutronHostConfigChangeListener> {
39 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostConfigChangeListener.class);
40 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
41 private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
42 private static int HOST_TYPE_STR_LEN = 8;
50 private final DataBroker dataBroker;
51 private final SouthboundUtils southboundUtils;
52 private final ControllerMdsalUtils mdsalUtils;
55 public NeutronHostConfigChangeListener(final DataBroker dataBroker) {
56 super(Node.class,NeutronHostConfigChangeListener.class);
57 this.dataBroker = dataBroker;
58 this.mdsalUtils = new ControllerMdsalUtils(dataBroker);
59 this.southboundUtils = new SouthboundUtils(mdsalUtils);
65 LOG.info("{} init", getClass().getSimpleName());
66 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
70 protected InstanceIdentifier<Node> getWildCardPath() {
71 return InstanceIdentifier
72 .create(NetworkTopology.class)
73 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
78 protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
79 return NeutronHostConfigChangeListener.this;
84 protected void remove(InstanceIdentifier<Node> identifier, Node del) {
85 updateHostConfig(del, Action.DELETE);
89 protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
90 updateHostConfig(update, Action.UPDATE);
94 protected void add(InstanceIdentifier<Node> identifier, Node add) {
95 updateHostConfig(add, Action.ADD);
99 private void updateHostConfig(Node node, Action action) {
100 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
101 if (hostId == null) {
104 for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
105 updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
109 private Map<String, String> extractHostConfig(Node node) {
110 Map<String, String> config = new HashMap<>();
111 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
112 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
113 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
114 if (openvswitchExternalIds.getExternalIdKey() != null && openvswitchExternalIds.getExternalIdKey()
115 .startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
116 // Extract the host type. Max 8 characters after
117 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
118 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
119 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
120 if (hostType.length() > 0) {
121 if (hostType.length() > HOST_TYPE_STR_LEN) {
122 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
124 hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
125 if (null != openvswitchExternalIds.getExternalIdValue()) {
126 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
135 private void updateMdsal(Hostconfig hostConfig, Action action) {
137 InstanceIdentifier<Hostconfig> hostConfigId;
138 if (hostConfig == null) {
144 hostConfigId = createInstanceIdentifier(hostConfig);
145 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
146 LOG.trace("Add Node: result: {}", result);
149 hostConfigId = createInstanceIdentifier(hostConfig);
150 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
151 LOG.trace("Delete Node: result: {}", result);
154 LOG.warn("Invalid action: {}", action);
159 private Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
160 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
161 hostconfigBuilder.setHostId(hostId);
162 hostconfigBuilder.setHostType(hostType);
163 hostconfigBuilder.setConfig(hostConfig);
164 return hostconfigBuilder.build();
168 private String getExternalId(Node node, String key) {
169 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
170 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
171 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
172 if (key.equals(openvswitchExternalIds.getExternalIdKey())) {
173 return openvswitchExternalIds.getExternalIdValue();
181 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
182 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
183 if (ovsdbNode == null) {
184 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
185 if (nodeFromReadOvsdbNode != null) {
186 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
192 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
193 return InstanceIdentifier.create(Neutron.class)
194 .child(Hostconfigs.class)
195 .child(Hostconfig.class, hostconfig.key());