NETVIRT-1630 migrate to md-sal APIs
[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.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;
36
37 @Singleton
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;
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 MdsalUtils mdsalUtils;
53
54     @Inject
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);
62     }
63
64     public void init() {
65         LOG.info("{} init", getClass().getSimpleName());
66     }
67
68     @Override
69     @PreDestroy
70     public void close() {
71         super.close();
72         Executors.shutdownAndAwaitTermination(getExecutorService());
73     }
74
75     @Override
76     public void remove(InstanceIdentifier<Node> identifier, Node del) {
77         updateHostConfig(del, Action.DELETE);
78     }
79
80     @Override
81     public void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
82         updateHostConfig(update, Action.UPDATE);
83     }
84
85     @Override
86     public void add(InstanceIdentifier<Node> identifier, Node add) {
87         updateHostConfig(add, Action.ADD);
88
89     }
90
91     private void updateHostConfig(Node node, Action action) {
92         String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
93         if (hostId == null) {
94             return;
95         }
96         for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
97             updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
98         }
99     }
100
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);
115                         }
116                         hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
117                         if (null != openvswitchExternalIds.getExternalIdValue()) {
118                             config.put(hostType, openvswitchExternalIds.getExternalIdValue());
119                         }
120                     }
121                 }
122             }
123         }
124         return config;
125     }
126
127     private void updateMdsal(Hostconfig hostConfig, Action action) {
128         boolean result;
129         InstanceIdentifier<Hostconfig> hostConfigId;
130         if (hostConfig == null) {
131             return;
132         }
133         switch (action) {
134             case ADD:
135             case UPDATE:
136                 hostConfigId = createInstanceIdentifier(hostConfig);
137                 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
138                 LOG.trace("Add Node: result: {}", result);
139                 break;
140             case DELETE:
141                 hostConfigId = createInstanceIdentifier(hostConfig);
142                 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
143                 LOG.trace("Delete Node: result: {}", result);
144                 break;
145             default:
146                 LOG.warn("Invalid action: {}", action);
147                 break;
148         }
149     }
150
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();
157     }
158
159     @Nullable
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();
166                 }
167             }
168         }
169         return null;
170     }
171
172     @Nullable
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);
179             }
180         }
181         return ovsdbNode;
182     }
183
184     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
185         return InstanceIdentifier.create(Neutron.class)
186                 .child(Hostconfigs.class)
187                 .child(Hostconfig.class, hostconfig.key());
188     }
189 }