Remove plugin dependencies
[ovsdb.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / VlanConfigurationCacheImpl.java
1 /*
2  * Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Authors: Dave Tucker
10 */
11
12 package org.opendaylight.ovsdb.openstack.netvirt.impl;
13
14 import org.opendaylight.ovsdb.lib.notation.Row;
15 import org.opendaylight.ovsdb.lib.notation.UUID;
16 import org.opendaylight.ovsdb.openstack.netvirt.NodeConfiguration;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.MdsalConsumer;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
20 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
21 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
22 import org.opendaylight.ovsdb.schema.openvswitch.Port;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Maps;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.util.Map;
31 import java.util.Set;
32
33 public class VlanConfigurationCacheImpl implements VlanConfigurationCache {
34     static final Logger logger = LoggerFactory.getLogger(VlanConfigurationCacheImpl.class);
35
36     private Map<String, NodeConfiguration> configurationCache = Maps.newConcurrentMap();
37
38     private volatile TenantNetworkManager tenantNetworkManager;
39     //private volatile OvsdbConfigurationService ovsdbConfigurationService;
40     private volatile MdsalConsumer mdsalConsumer; // TODO SB_MIGRATION
41
42     private NodeConfiguration getNodeConfiguration(Node node){
43         String nodeUuid = getNodeUUID(node);
44         if (configurationCache.get(nodeUuid) != null) {
45             return configurationCache.get(nodeUuid);
46         }
47
48         // Cache miss
49         initializeNodeConfiguration(nodeUuid, node);
50
51         return configurationCache.get(nodeUuid);
52     }
53
54     private String getNodeUUID(Node node) {
55         String nodeUuid = mdsalConsumer.getNodeUUID(node);
56         /* TODO SB_MIGRATION
57         Preconditions.checkNotNull(ovsdbConfigurationService);
58         String nodeUuid = new String();
59
60         try {
61             Map<String, Row> ovsTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
62             nodeUuid = (String)ovsTable.keySet().toArray()[0];
63         }
64         catch (Exception e) {
65             logger.error("Unable to get the Open_vSwitch table for Node {}", node, e);
66         }*/
67
68         return nodeUuid;
69     }
70
71     private void initializeNodeConfiguration(String nodeUuid, Node node) {
72         /* TODO SB_MIGRATION
73         NodeConfiguration nodeConfiguration = new NodeConfiguration();
74         Integer vlan;
75         String networkId = null;
76
77         try {
78             Map<String, Row> portRows = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
79
80             if (portRows == null){
81                 logger.debug("Port table is null for Node {}", node);
82                 return;
83             }
84
85             for (Row row : portRows.values()) {
86                 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, row);
87
88                 if (port.getTagColumn() == null) continue;
89                 Set<Long> tags = port.getTagColumn().getData();
90                 if (tags.size() == 1)
91                 {
92                     //There is only one tag here
93                     vlan = tags.iterator().next().intValue();
94                 }
95                 else {
96                    logger.debug("This port ({}) has {} tags", port.getName(), tags.size());
97                    continue;
98                 }
99
100                 for (UUID ifaceId : port.getInterfacesColumn().getData()) {
101                     Row ifaceRow = ovsdbConfigurationService
102                             .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
103                                     ifaceId.toString());
104                     Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
105
106                     if (iface == null) {
107                         logger.debug("Interface table is null");
108                         continue;
109                     }
110
111                     networkId = tenantNetworkManager.getTenantNetwork(iface).getNetworkUUID();
112
113                     if (networkId != null) break;
114                 }
115
116                 if (vlan != 0 && networkId != null) {
117
118                     this.internalVlanInUse(nodeConfiguration, vlan);
119                     nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
120
121                 } else {
122                     logger.debug("Node: {} initialized without a vlan", node);
123                 }
124             }
125
126             configurationCache.put(nodeUuid, nodeConfiguration);
127         }
128         catch (Exception e) {
129             logger.debug("Error getting Port table for Node {}", node, e);
130         }*/
131     }
132
133     /*
134      * Return the currently mapped internal vlan or get the next
135      * free internal vlan from the available pool and map it to the networkId.
136      */
137     @Override
138     public Integer assignInternalVlan (Node node, String networkId) {
139         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
140         Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
141         if (mappedVlan != null) {
142             return mappedVlan;
143         }
144         mappedVlan = nodeConfiguration.getInternalVlans().poll();
145         if (mappedVlan != null) {
146             nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
147         }
148         return mappedVlan;
149     }
150
151     /*
152      * Return the mapped internal vlan to the available pool.
153      */
154     @Override
155     public Integer reclaimInternalVlan (Node node, String networkId) {
156         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
157         Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
158         if (mappedVlan != null) {
159             nodeConfiguration.getTenantVlanMap().remove(networkId);
160             nodeConfiguration.getInternalVlans().add(mappedVlan);
161             return mappedVlan;
162         }
163         return 0;
164     }
165
166     private void internalVlanInUse (NodeConfiguration nodeConfiguration, Integer vlan) {
167         nodeConfiguration.getInternalVlans().remove(vlan);
168     }
169
170     @Override
171     public Integer getInternalVlan (Node node, String networkId) {
172         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
173         Integer vlan = nodeConfiguration.getTenantVlanMap().get(networkId);
174         if (vlan == null) return 0;
175         return vlan;
176     }
177
178 }