Fixup Augmentable and Identifiable methods changing
[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.PostConstruct;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
19 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
20 import org.opendaylight.ovsdb.utils.southbound.utils.SouthboundUtils;
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.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.OpenvswitchExternalIds;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 @Singleton
36 public class NeutronHostConfigChangeListener
37         extends AsyncDataTreeChangeListenerBase<Node,NeutronHostConfigChangeListener> {
38     private static final Logger LOG = LoggerFactory.getLogger(NeutronHostConfigChangeListener.class);
39     private static final String OS_HOST_CONFIG_HOST_ID_KEY = "odl_os_hostconfig_hostid";
40     private static final String OS_HOST_CONFIG_CONFIG_KEY_PREFIX = "odl_os_hostconfig_config_odl_";
41     private static int HOST_TYPE_STR_LEN = 8;
42
43     private enum Action {
44         ADD,
45         UPDATE,
46         DELETE
47     }
48
49     private final DataBroker dataBroker;
50     private final SouthboundUtils southboundUtils;
51     private final MdsalUtils mdsalUtils;
52
53     @Inject
54     public NeutronHostConfigChangeListener(final DataBroker dataBroker) {
55         super(Node.class,NeutronHostConfigChangeListener.class);
56         this.dataBroker = dataBroker;
57         this.mdsalUtils = new MdsalUtils(dataBroker);
58         this.southboundUtils = new SouthboundUtils(mdsalUtils);
59     }
60
61     @Override
62     @PostConstruct
63     public void init() {
64         LOG.info("{} init", getClass().getSimpleName());
65         registerListener(LogicalDatastoreType.OPERATIONAL, dataBroker);
66     }
67
68     @Override
69     protected InstanceIdentifier<Node> getWildCardPath() {
70         return InstanceIdentifier
71                 .create(NetworkTopology.class)
72                 .child(Topology.class,new TopologyKey(SouthboundUtils.OVSDB_TOPOLOGY_ID))
73                 .child(Node.class);
74     }
75
76     @Override
77     protected NeutronHostConfigChangeListener getDataTreeChangeListener() {
78         return NeutronHostConfigChangeListener.this;
79     }
80
81
82     @Override
83     protected void remove(InstanceIdentifier<Node> identifier, Node del) {
84         updateHostConfig(del, Action.DELETE);
85     }
86
87     @Override
88     protected void update(InstanceIdentifier<Node> identifier, Node original, Node update) {
89         updateHostConfig(update, Action.UPDATE);
90     }
91
92     @Override
93     protected void add(InstanceIdentifier<Node> identifier, Node add) {
94         updateHostConfig(add, Action.ADD);
95
96     }
97
98     private void updateHostConfig(Node node, Action action) {
99         String hostId = getExternalId(node, OS_HOST_CONFIG_HOST_ID_KEY);
100         if (hostId == null) {
101             return;
102         }
103         for (Map.Entry<String,String> entry : extractHostConfig(node).entrySet()) {
104             updateMdsal(buildHostConfigInfo(hostId, entry.getKey(), entry.getValue()), action);
105         }
106     }
107
108     private Map<String, String> extractHostConfig(Node node) {
109         Map<String, String> config = new HashMap<>();
110         OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
111         if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
112             for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
113                 if (openvswitchExternalIds.getExternalIdKey() != null && openvswitchExternalIds.getExternalIdKey()
114                         .startsWith(OS_HOST_CONFIG_CONFIG_KEY_PREFIX)) {
115                     // Extract the host type. Max 8 characters after
116                     // suffix OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length()
117                     String hostType = openvswitchExternalIds.getExternalIdKey().substring(
118                             OS_HOST_CONFIG_CONFIG_KEY_PREFIX.length());
119                     if (hostType.length() > 0) {
120                         if (hostType.length() > HOST_TYPE_STR_LEN) {
121                             hostType = hostType.substring(0, HOST_TYPE_STR_LEN);
122                         }
123                         hostType = "ODL " + hostType.toUpperCase(Locale.getDefault());
124                         if (null != openvswitchExternalIds.getExternalIdValue()) {
125                             config.put(hostType, openvswitchExternalIds.getExternalIdValue());
126                         }
127                     }
128                 }
129             }
130         }
131         return config;
132     }
133
134     private void updateMdsal(Hostconfig hostConfig, Action action) {
135         boolean result;
136         InstanceIdentifier<Hostconfig> hostConfigId;
137         if (hostConfig == null) {
138             return;
139         }
140         switch (action) {
141             case ADD:
142             case UPDATE:
143                 hostConfigId = createInstanceIdentifier(hostConfig);
144                 result = mdsalUtils.put(LogicalDatastoreType.OPERATIONAL, hostConfigId, hostConfig);
145                 LOG.trace("Add Node: result: {}", result);
146                 break;
147             case DELETE:
148                 hostConfigId = createInstanceIdentifier(hostConfig);
149                 result = mdsalUtils.delete(LogicalDatastoreType.OPERATIONAL, hostConfigId);
150                 LOG.trace("Delete Node: result: {}", result);
151                 break;
152             default:
153                 LOG.warn("Invalid action: {}", action);
154                 break;
155         }
156     }
157
158     private Hostconfig buildHostConfigInfo(String hostId, String hostType, String hostConfig) {
159         HostconfigBuilder hostconfigBuilder = new HostconfigBuilder();
160         hostconfigBuilder.setHostId(hostId);
161         hostconfigBuilder.setHostType(hostType);
162         hostconfigBuilder.setConfig(hostConfig);
163         return hostconfigBuilder.build();
164     }
165
166     private String getExternalId(Node node, String key) {
167         OvsdbNodeAugmentation ovsdbNode = getOvsdbNodeAugmentation(node);
168         if (ovsdbNode != null && ovsdbNode.getOpenvswitchExternalIds() != null) {
169             for (OpenvswitchExternalIds openvswitchExternalIds : ovsdbNode.getOpenvswitchExternalIds()) {
170                 if (key.equals(openvswitchExternalIds.getExternalIdKey())) {
171                     return openvswitchExternalIds.getExternalIdValue();
172                 }
173             }
174         }
175         return null;
176     }
177
178     private OvsdbNodeAugmentation getOvsdbNodeAugmentation(Node node) {
179         OvsdbNodeAugmentation ovsdbNode = southboundUtils.extractOvsdbNode(node);
180         if (ovsdbNode == null) {
181             Node nodeFromReadOvsdbNode = southboundUtils.readOvsdbNode(node);
182             if (nodeFromReadOvsdbNode != null) {
183                 ovsdbNode = southboundUtils.extractOvsdbNode(nodeFromReadOvsdbNode);
184             }
185         }
186         return ovsdbNode;
187     }
188
189     private InstanceIdentifier<Hostconfig> createInstanceIdentifier(Hostconfig hostconfig) {
190         return InstanceIdentifier.create(Neutron.class)
191                 .child(Hostconfigs.class)
192                 .child(Hostconfig.class, hostconfig.key());
193     }
194 }