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.PreDestroy;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.infrautils.utils.concurrent.Executors;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
21 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
22 import org.opendaylight.serviceutils.tools.listener.AbstractAsyncDataTreeChangeListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 public class NeutronHostConfigChangeListener extends AbstractAsyncDataTreeChangeListener<Node> {
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 MdsalUtils mdsalUtils;
55 public NeutronHostConfigChangeListener(final DataBroker dataBroker) {
56 super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(NetworkTopology.class)
57 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID)).child(Node.class),
58 Executors.newSingleThreadExecutor("NeutronHostConfigChangeListener", LOG));
59 this.dataBroker = dataBroker;
60 this.mdsalUtils = new MdsalUtils(dataBroker);
61 this.southboundUtils = new SouthboundUtils(mdsalUtils);
65 LOG.info("{} init", getClass().getSimpleName());
72 Executors.shutdownAndAwaitTermination(getExecutorService());
76 public void remove(InstanceIdentifier<Node> identifier, Node del) {
77 updateHostConfig(del, Action.DELETE);
81 public void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
82 updateHostConfig(update, Action.UPDATE);
86 public void add(InstanceIdentifier<Node> identifier, Node add) {
87 updateHostConfig(add, Action.ADD);
91 private void updateHostConfig(Node node, Action action) {
92 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
96 for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
97 updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
101 private Map<String, String> extractHostConfig(Node node) {
102 Map<String, String> config = new HashMap<>();
103 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
104 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
105 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
106 if (openvswitchExternalIds.getExternalIdKey() != null && openvswitchExternalIds.getExternalIdKey()
107 .startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
108 // Extract the host type. Max 8 characters after
109 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
110 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
111 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
112 if (hostType.length() > 0) {
113 if (hostType.length() > HOST_TYPE_STR_LEN) {
114 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
116 hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
117 if (null != openvswitchExternalIds.getExternalIdValue()) {
118 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
127 private void updateMdsal(Hostconfig hostConfig, Action action) {
129 InstanceIdentifier<Hostconfig> hostConfigId;
130 if (hostConfig == null) {
136 hostConfigId = createInstanceIdentifier(hostConfig);
137 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
138 LOG.trace("Add Node: result: {}", result);
141 hostConfigId = createInstanceIdentifier(hostConfig);
142 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
143 LOG.trace("Delete Node: result: {}", result);
146 LOG.warn("Invalid action: {}", action);
151 private Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
152 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
153 hostconfigBuilder.setHostId(hostId);
154 hostconfigBuilder.setHostType(hostType);
155 hostconfigBuilder.setConfig(hostConfig);
156 return hostconfigBuilder.build();
160 private String getExternalId(Node node, String key) {
161 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
162 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
163 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
164 if (key.equals(openvswitchExternalIds.getExternalIdKey())) {
165 return openvswitchExternalIds.getExternalIdValue();
173 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
174 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
175 if (ovsdbNode == null) {
176 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
177 if (nodeFromReadOvsdbNode != null) {
178 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
184 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
185 return InstanceIdentifier.create(Neutron.class)
186 .child(Hostconfigs.class)
187 .child(Hostconfig.class, hostconfig.key());