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
9 package org.opendaylight.netvirt.openstack.netvirt.impl;
11 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.netvirt.openstack.netvirt.ClusterAwareMdsalUtils;
14 import org.opendaylight.netvirt.openstack.netvirt.ConfigInterface;
15 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
16 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbInventoryListener;
17 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbInventoryService;
18 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
19 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbTables;
20 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
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.rev150712.Neutron;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.osgi.framework.ServiceReference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.util.List;
36 public class HostConfigService implements OvsdbInventoryListener, ConfigInterface {
37 private static final Logger LOG = LoggerFactory.getLogger(HostConfigService.class);
39 private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
40 private static final String OS_HOST_CONFIG_HOST_TYPE_KEY = "odl_os_hostconfig_hosttype";
41 private static final String OS_HOST_CONFIG_CONFIG_KEY = "odl_os_hostconfig_config";
43 private final DataBroker databroker;
44 private final ClusterAwareMdsalUtils mdsalUtils;
45 private volatile OvsdbInventoryService ovsdbInventoryService;
46 private volatile Southbound southbound;
48 public HostConfigService(DataBroker dataBroker) {
49 this.databroker = dataBroker;
50 mdsalUtils = new ClusterAwareMdsalUtils(dataBroker);
54 public void ovsdbUpdate(Node node, DataObject resourceAugmentationData, OvsdbType ovsdbType, Action action) {
56 Hostconfig hostConfig;
57 InstanceIdentifier<Hostconfig> hostConfigId;
59 if (ovsdbType != OvsdbType.NODE) {
62 hostConfig = buildHostConfigInfo(node);
63 if (hostConfig == null) {
66 LOG.trace("ovsdbUpdate: {} - {} - <<{}>> <<{}>>", ovsdbType, action, node, resourceAugmentationData);
70 hostConfigId = createInstanceIdentifier(hostConfig);
71 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
72 LOG.trace("Add Node: result: {}", result);
75 hostConfigId = createInstanceIdentifier(hostConfig);
76 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
77 LOG.trace("Delete Node: result: {}", result);
83 public void triggerUpdates() {
84 List<Node> ovsdbNodes = southbound.readOvsdbTopologyNodes();
85 for (Node node : ovsdbNodes) {
86 ovsdbUpdate(node, node.getAugmentation(OvsdbNodeAugmentation.class),
87 OvsdbInventoryListener.OvsdbType.NODE, Action.ADD);
91 private Hostconfig buildHostConfigInfo(Node node) {
92 HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
95 value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_HOST_ID_KEY);
99 hostconfigBuilder.setHostId(value);
100 value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_HOST_TYPE_KEY);
104 hostconfigBuilder.setHostType(value);
105 value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_CONFIG_KEY);
109 hostconfigBuilder.setConfig(value);
110 return hostconfigBuilder.build();
113 private InstanceIdentifier<Hostconfig> createInstanceIdentifier() {
114 return InstanceIdentifier.create(Neutron.class)
115 .child(Hostconfigs.class)
116 .child(Hostconfig.class);
119 private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
120 return InstanceIdentifier.create(Neutron.class)
121 .child(Hostconfigs.class)
122 .child(Hostconfig.class, hostconfig.getKey());
126 public void setDependencies(ServiceReference serviceReference) {
128 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
129 ovsdbInventoryService =
130 (OvsdbInventoryService) ServiceHelper.getGlobalInstance(OvsdbInventoryService.class, this);
131 ovsdbInventoryService.listenerAdded(this);
135 public void setDependencies(Object impl) {