Adjust to the ovsdb MdsalUtils change
[netvirt.git] / neutronvpn / impl / src / main / java / org / opendaylight / netvirt / neutronvpn / NeutronHostConfigChangeListener.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 package org.opendaylight.netvirt.neutronvpn;
9
10 import java.util.HashMap;
11 import java.util.Locale;
12 import java.util.Map;
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;
35
36 @Singleton
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;
43
44     private enum Action {
45         ADD,
46         UPDATE,
47         DELETE
48     }
49
50     private final DataBroker dataBroker;
51     private final SouthboundUtils southboundUtils;
52     private final ControllerMdsalUtils mdsalUtils;
53
54     @Inject
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);
60     }
61
62     @Override
63     @PostConstruct
64     public void init() {
65         LOG.info("{} init", getClass().getSimpleName());
66         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
67     }
68
69     @Override
70     protected InstanceIdentifier<Node> getWildCardPath() {
71         return InstanceIdentifier
72                 .create(NetworkTopology.class)
73                 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
74                 .child(Node.class);
75     }
76
77     @Override
78     protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
79         return NeutronHostConfigChangeListener.this;
80     }
81
82
83     @Override
84     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
85         updateHostConfig(del, Action.DELETE);
86     }
87
88     @Override
89     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
90         updateHostConfig(update, Action.UPDATE);
91     }
92
93     @Override
94     protected void add(InstanceIdentifier<Node> identifier, Node add) {
95         updateHostConfig(add, Action.ADD);
96
97     }
98
99     private void updateHostConfig(Node node, Action action) {
100         String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
101         if (hostId == null) {
102             return;
103         }
104         for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
105             updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
106         }
107     }
108
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);
123                         }
124                         hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
125                         if (null != openvswitchExternalIds.getExternalIdValue()) {
126                             config.put(hostType, openvswitchExternalIds.getExternalIdValue());
127                         }
128                     }
129                 }
130             }
131         }
132         return config;
133     }
134
135     private void updateMdsal(Hostconfig hostConfig, Action action) {
136         boolean result;
137         InstanceIdentifier<Hostconfig> hostConfigId;
138         if (hostConfig == null) {
139             return;
140         }
141         switch (action) {
142             case ADD:
143             case UPDATE:
144                 hostConfigId = createInstanceIdentifier(hostConfig);
145                 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
146                 LOG.trace("Add Node: result: {}", result);
147                 break;
148             case DELETE:
149                 hostConfigId = createInstanceIdentifier(hostConfig);
150                 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
151                 LOG.trace("Delete Node: result: {}", result);
152                 break;
153             default:
154                 LOG.warn("Invalid action: {}", action);
155                 break;
156         }
157     }
158
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();
165     }
166
167     @Nullable
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();
174                 }
175             }
176         }
177         return null;
178     }
179
180     @Nullable
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);
187             }
188         }
189         return ovsdbNode;
190     }
191
192     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
193         return InstanceIdentifier.create(Neutron.class)
194                 .child(Hostconfigs.class)
195                 .child(Hostconfig.class, hostconfig.key());
196     }
197 }