Merge "Remove ovsdb related in resources"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / netvirt / openstack / netvirt / impl / HostConfigService.java
1 /*
2  * Copyright (c) 2016 Intel Corporation.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netvirt.openstack.netvirt.impl;
10
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;
32
33 import java.util.List;
34
35
36 public class HostConfigService implements OvsdbInventoryListener, ConfigInterface {
37     private static final Logger LOG = LoggerFactory.getLogger(HostConfigService.class);
38
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";
42
43     private final DataBroker databroker;
44     private final ClusterAwareMdsalUtils mdsalUtils;
45     private volatile OvsdbInventoryService ovsdbInventoryService;
46     private volatile Southbound southbound;
47
48     public HostConfigService(DataBroker dataBroker) {
49         this.databroker = dataBroker;
50         mdsalUtils = new ClusterAwareMdsalUtils(dataBroker);
51     }
52
53     @Override
54     public void ovsdbUpdate(Node node, DataObject resourceAugmentationData, OvsdbType ovsdbType, Action action) {
55         boolean result;
56         Hostconfig hostConfig;
57         InstanceIdentifier<Hostconfig> hostConfigId;
58
59         if (ovsdbType != OvsdbType.NODE) {
60             return;
61         }
62         hostConfig = buildHostConfigInfo(node);
63         if (hostConfig == null) {
64               return;
65         }
66         LOG.trace("ovsdbUpdate: {} - {} - <<{}>> <<{}>>", ovsdbType, action, node, resourceAugmentationData);
67         switch (action) {
68             case ADD:
69             case UPDATE:
70                     hostConfigId = createInstanceIdentifier(hostConfig);
71                     result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
72                     LOG.trace("Add Node: result: {}", result);
73                 break;
74             case DELETE:
75                     hostConfigId = createInstanceIdentifier(hostConfig);
76                     result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
77                     LOG.trace("Delete Node: result: {}", result);
78                 break;
79         }
80     }
81
82     @Override
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);
88         }
89     }
90
91     private Hostconfig buildHostConfigInfo(Node node) {
92         HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
93         String value;
94
95         value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_HOST_ID_KEY);
96         if (value == null){
97             return null;
98         }
99         hostconfigBuilder.setHostId(value);
100         value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_HOST_TYPE_KEY);
101         if (value == null) {
102             return null;
103         }
104         hostconfigBuilder.setHostType(value);
105         value = southbound.getExternalId(node, OvsdbTables.OPENVSWITCH, OS_HOST_CONFIG_CONFIG_KEY);
106         if (value == null) {
107             return null;
108         }
109         hostconfigBuilder.setConfig(value);
110         return hostconfigBuilder.build();
111     }
112
113     private InstanceIdentifier<Hostconfig> createInstanceIdentifier() {
114         return InstanceIdentifier.create(Neutron.class)
115                 .child(Hostconfigs.class)
116                 .child(Hostconfig.class);
117     }
118
119     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
120         return InstanceIdentifier.create(Neutron.class)
121                 .child(Hostconfigs.class)
122                 .child(Hostconfig.class, hostconfig.getKey());
123     }
124
125     @Override
126     public void setDependencies(ServiceReference serviceReference) {
127         southbound =
128                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
129         ovsdbInventoryService =
130                 (OvsdbInventoryService) ServiceHelper.getGlobalInstance(OvsdbInventoryService.class, this);
131         ovsdbInventoryService.listenerAdded(this);
132     }
133
134     @Override
135     public void setDependencies(Object impl) {
136     }
137 }