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 com.google.common.collect.Maps;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
15 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
16 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.Hostconfigs;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NeutronHostConfigChangeListener extends AsyncDataTreeChangeListenerBase<Node,
32 NeutronHostConfigChangeListener> implements AutoCloseable {
33 private static final Logger LOG = LoggerFactory.getLogger(NeutronHostConfigChangeListener.class);
34 private final DataBroker dataBroker;
35 private final SouthboundUtils southboundUtils;
36 private final MdsalUtils mdsalUtils;
37 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
38 private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
39 private static int HOST_TYPE_STR_LEN = 8;
47 public NeutronHostConfigChangeListener(final DataBroker dataBroker){
48 super(Node.class,NeutronHostConfigChangeListener.class);
49 this.dataBroker = dataBroker;
50 this.mdsalUtils = new MdsalUtils(dataBroker);
51 this.southboundUtils = new SouthboundUtils(mdsalUtils);
55 LOG.info("{} start", getClass().getSimpleName());
56 registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
60 protected InstanceIdentifier<Node> getWildCardPath(){
61 return InstanceIdentifier
62 .create(NetworkTopology.class)
63 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
68 protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
69 return NeutronHostConfigChangeListener.this;
74 protected void remove(InstanceIdentifier<Node>identifier, Node del){
75 updateHostConfig(del, Action.DELETE);
79 protected void update(InstanceIdentifier<Node>identifier, Node original, Node update){
80 updateHostConfig(update, Action.UPDATE);
84 protected void add(InstanceIdentifier<Node>identifier, Node add){
85 updateHostConfig(add, Action.ADD);
89 private void updateHostConfig(Node node, Action action) {
90 String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
94 for(Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
95 updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
99 private Map<String, String> extractHostConfig(Node node) {
100 Map<String, String> config = Maps.newHashMap();
101 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
102 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
103 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
104 if (openvswitchExternalIds.getExternalIdKey().startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
105 // Extract the host type. Max 8 characters after
106 // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
107 String hostType = openvswitchExternalIds.getExternalIdKey().substring(
108 OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
109 if (null != hostType && hostType.length() > 0) {
110 if (hostType.length() > HOST_TYPE_STR_LEN) {
111 hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
113 hostType = "ODL " + hostType.toUpperCase();
114 if (null != openvswitchExternalIds.getExternalIdValue())
115 config.put(hostType, openvswitchExternalIds.getExternalIdValue());
123 private void updateMdsal(Hostconfig hostConfig, Action action) {
125 InstanceIdentifier<Hostconfig> hostConfigId;
126 if (hostConfig == null) {
132 hostConfigId = createInstanceIdentifier(hostConfig);
133 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
134 LOG.trace("Add Node: result: {}", result);
137 hostConfigId = createInstanceIdentifier(hostConfig);
138 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
139 LOG.trace("Delete Node: result: {}", result);
144 private Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
145 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
146 hostconfigBuilder.setHostId(hostId);
147 hostconfigBuilder.setHostType(hostType);
148 hostconfigBuilder.setConfig(hostConfig);
149 return hostconfigBuilder.build();
152 private String getExternalId(Node node, String key) {
153 OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
154 if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
155 for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
156 if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
157 return openvswitchExternalIds.getExternalIdValue();
164 private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node)
166 OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
167 if (ovsdbNode == null) {
168 Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
169 if (nodeFromReadOvsdbNode != null) {
170 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
176 private InstanceIdentifier<Hostconfig> createInstanceIdentifier() {
177 return InstanceIdentifier.create(Neutron.class)
178 .child(Hostconfigs.class)
179 .child(Hostconfig.class);
182 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
183 return InstanceIdentifier.create(Neutron.class)
184 .child(Hostconfigs.class)
185 .child(Hostconfig.class, hostconfig.getKey());