2 * Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
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
12 package org.opendaylight.ovsdb.openstack.netvirt.impl;
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.TenantNetworkManager;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
19 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
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;
25 import com.google.common.base.Preconditions;
26 import com.google.common.collect.Maps;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
33 public class VlanConfigurationCacheImpl implements VlanConfigurationCache {
34 static final Logger logger = LoggerFactory.getLogger(VlanConfigurationCacheImpl.class);
36 private Map<String, NodeConfiguration> configurationCache = Maps.newConcurrentMap();
38 private volatile TenantNetworkManager tenantNetworkManager;
39 private volatile OvsdbConfigurationService ovsdbConfigurationService;
41 private NodeConfiguration getNodeConfiguration(Node node){
42 String nodeUuid = getNodeUUID(node);
43 if (configurationCache.get(nodeUuid) != null) {
44 return configurationCache.get(nodeUuid);
48 initializeNodeConfiguration(nodeUuid, node);
50 return configurationCache.get(nodeUuid);
53 private String getNodeUUID(Node node) {
54 Preconditions.checkNotNull(ovsdbConfigurationService);
55 String nodeUuid = new String();
57 Map<String, Row> ovsTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
58 nodeUuid = (String)ovsTable.keySet().toArray()[0];
61 logger.error("Unable to get the Open_vSwitch table for Node {}", node, e);
67 private void initializeNodeConfiguration(String nodeUuid, Node node) {
69 NodeConfiguration nodeConfiguration = new NodeConfiguration();
71 String networkId = null;
74 Map<String, Row> portRows = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
76 if (portRows == null){
77 logger.debug("Port table is null for Node {}", node);
81 for (Row row : portRows.values()) {
82 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, row);
84 if (port.getTagColumn() == null) continue;
85 Set<Long> tags = port.getTagColumn().getData();
88 //There is only one tag here
89 vlan = tags.iterator().next().intValue();
92 logger.debug("This port ({}) has {} tags", port.getName(), tags.size());
96 for (UUID ifaceId : port.getInterfacesColumn().getData()) {
97 Row ifaceRow = ovsdbConfigurationService
98 .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
100 Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
103 logger.debug("Interface table is null");
107 networkId = tenantNetworkManager.getTenantNetwork(iface).getNetworkUUID();
109 if (networkId != null) break;
112 if (vlan != 0 && networkId != null) {
114 this.internalVlanInUse(nodeConfiguration, vlan);
115 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
118 logger.debug("Node: {} initialized without a vlan", node);
122 configurationCache.put(nodeUuid, nodeConfiguration);
124 catch (Exception e) {
125 logger.debug("Error getting Port table for Node {}", node, e);
130 * Return the currently mapped internal vlan or get the next
131 * free internal vlan from the available pool and map it to the networkId.
134 public Integer assignInternalVlan (Node node, String networkId) {
135 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
136 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
137 if (mappedVlan != null) {
140 mappedVlan = nodeConfiguration.getInternalVlans().poll();
141 if (mappedVlan != null) {
142 nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
148 * Return the mapped internal vlan to the available pool.
151 public Integer reclaimInternalVlan (Node node, String networkId) {
152 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
153 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
154 if (mappedVlan != null) {
155 nodeConfiguration.getTenantVlanMap().remove(networkId);
156 nodeConfiguration.getInternalVlans().add(mappedVlan);
162 private void internalVlanInUse (NodeConfiguration nodeConfiguration, Integer vlan) {
163 nodeConfiguration.getInternalVlans().remove(vlan);
167 public Integer getInternalVlan (Node node, String networkId) {
168 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
169 Integer vlan = nodeConfiguration.getTenantVlanMap().get(networkId);
170 if (vlan == null) return 0;