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