cfeeea34bc6b9ee92a8656e11148cc0dec604c5e
[neutron.git] / neutron-hostconfig / ovs / src / main / java / org / opendaylight / neutron / hostconfig / ovs / NeutronHostconfigOvsListener.java
1 /*
2  * Copyright (c) 2017 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.neutron.hostconfig.ovs;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13
14 import java.util.Collection;
15 import java.util.Locale;
16 import java.util.Map;
17 import javax.annotation.Nonnull;
18 import javax.annotation.PostConstruct;
19 import javax.annotation.PreDestroy;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.neutron.hostconfig.utils.NeutronHostconfigUtils;
30 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
31 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 @Singleton
45 public class NeutronHostconfigOvsListener implements ClusteredDataTreeChangeListener<Node> {
46     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostconfigOvsListener.class);
47     private final DataBroker dataBroker;
48     private final SouthboundUtils southboundUtils;
49     private final NeutronHostconfigUtils neutronHostconfig;
50     private ListenerRegistration<DataTreeChangeListener<Node>> listenerRegistration;
51     private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
52     private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
53     private static int HOST_TYPE_STR_LEN = 8;
54
55     @Inject
56     public NeutronHostconfigOvsListener(final DataBroker dataBroker) {
57         this.dataBroker = dataBroker;
58         MdsalUtils mdsalUtils = new MdsalUtils(dataBroker);
59         this.southboundUtils = new SouthboundUtils(mdsalUtils);
60         this.neutronHostconfig = new NeutronHostconfigUtils(dataBroker);
61     }
62
63     private void processChanges(Collection<DataTreeModification<Node>> changes) {
64         LOG.info("onDataTreeChanged: Received Data Tree Changed ...", changes);
65         for (DataTreeModification<Node> change : changes) {
66             final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
67             final DataObjectModification<Node> mod = change.getRootNode();
68             LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
69                     mod.getModificationType(), key);
70             switch (mod.getModificationType()) {
71                 case DELETE:
72                     updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.DELETE);
73                     break;
74                 case SUBTREE_MODIFIED:
75                     updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.UPDATE);
76                     break;
77                 case WRITE:
78                     updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.ADD);
79                     break;
80                 default:
81                     LOG.error("onDataTreeChanged: Invalid modification type={}",
82                             mod.getModificationType());
83                     break;
84             }
85         }
86     }
87
88     @Override
89     public void onDataTreeChanged(@Nonnull Collection<DataTreeModification<Node>> changes) {
90         Preconditions.checkNotNull(changes, "Changes may not be null!");
91         processChanges(changes);
92     }
93
94     private InstanceIdentifier<Node> createNodeIdentifier() {
95         return InstanceIdentifier
96                 .create(NetworkTopology.class)
97                 .child(Topology.class, new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
98                 .child(Node.class);
99     }
100
101     @PostConstruct
102     public void init() {
103         LOG.info("{} start", getClass().getSimpleName());
104         DataTreeIdentifier<Node> dataTreeIdentifier =
105                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, createNodeIdentifier());
106         LOG.info("Neutron Hostconfig DataChange listener registration {}", dataTreeIdentifier);
107         listenerRegistration = dataBroker.registerDataTreeChangeListener(dataTreeIdentifier, this);
108     }
109
110     @PreDestroy
111     public void close() throws Exception {
112         if (listenerRegistration != null) {
113             listenerRegistration.close();
114             LOG.trace("HostConfig listener Closed");
115         }
116     }
117
118     private void updateHostConfig(Node node, NeutronHostconfigUtils.Action action) {
119         String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
120         if (hostId == null) {
121             return;
122         }
123         for (Map.Entry<String, String> entry : extractHostConfig(node).entrySet()) {
124             neutronHostconfig.updateMdsal(neutronHostconfig.buildHostConfigInfo(hostId, entry.getKey(),
125                     entry.getValue()), action);
126         }
127     }
128
129     private Map<String, String> extractHostConfig(Node node) {
130         Map<String, String> config = Maps.newHashMap();
131         OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
132         if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
133             for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
134                 if (openvswitchExternalIds.getExternalIdKey().startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
135                     // Extract the host type. Max 8 characters after
136                     // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
137                     String hostType = openvswitchExternalIds.getExternalIdKey().substring(
138                             OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
139                     if (hostType.length() > 0) {
140                         if (hostType.length() > HOST_TYPE_STR_LEN) {
141                             hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
142                         }
143                         hostType = "ODL " + hostType.toUpperCase(Locale.ROOT);
144                         if (null != openvswitchExternalIds.getExternalIdValue()) {
145                             config.put(hostType, openvswitchExternalIds.getExternalIdValue());
146                         }
147                     }
148                 }
149             }
150         }
151         return config;
152     }
153
154     private String getExternalId(Node node, String key) {
155         OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
156         if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
157             for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
158                 if (openvswitchExternalIds.getExternalIdKey().equals(key)) {
159                     return openvswitchExternalIds.getExternalIdValue();
160                 }
161             }
162         }
163         return null;
164     }
165
166     private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
167         OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
168         if (ovsdbNode == null) {
169             Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
170             if (nodeFromReadOvsdbNode != null) {
171                 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
172             }
173         }
174         return ovsdbNode;
175     }
176 }