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.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;
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;
40 private volatile MdsalConsumer mdsalConsumer; // TODO SB_MIGRATION
42 private NodeConfiguration getNodeConfiguration(Node node){
43 String nodeUuid = getNodeUUID(node);
44 if (configurationCache.get(nodeUuid) != null) {
45 return configurationCache.get(nodeUuid);
49 initializeNodeConfiguration(nodeUuid, node);
51 return configurationCache.get(nodeUuid);
54 private String getNodeUUID(Node node) {
55 String nodeUuid = mdsalConsumer.getNodeUUID(node);
57 Preconditions.checkNotNull(ovsdbConfigurationService);
58 String nodeUuid = new String();
61 Map<String, Row> ovsTable = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
62 nodeUuid = (String)ovsTable.keySet().toArray()[0];
65 logger.error("Unable to get the Open_vSwitch table for Node {}", node, e);
71 private void initializeNodeConfiguration(String nodeUuid, Node node) {
73 NodeConfiguration nodeConfiguration = new NodeConfiguration();
75 String networkId = null;
78 Map<String, Row> portRows = ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Port.class));
80 if (portRows == null){
81 logger.debug("Port table is null for Node {}", node);
85 for (Row row : portRows.values()) {
86 Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, row);
88 if (port.getTagColumn() == null) continue;
89 Set<Long> tags = port.getTagColumn().getData();
92 //There is only one tag here
93 vlan = tags.iterator().next().intValue();
96 logger.debug("This port ({}) has {} tags", port.getName(), tags.size());
100 for (UUID ifaceId : port.getInterfacesColumn().getData()) {
101 Row ifaceRow = ovsdbConfigurationService
102 .getRow(node, ovsdbConfigurationService.getTableName(node, Interface.class),
104 Interface iface = ovsdbConfigurationService.getTypedRow(node, Interface.class, ifaceRow);
107 logger.debug("Interface table is null");
111 networkId = tenantNetworkManager.getTenantNetwork(iface).getNetworkUUID();
113 if (networkId != null) break;
116 if (vlan != 0 && networkId != null) {
118 this.internalVlanInUse(nodeConfiguration, vlan);
119 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
122 logger.debug("Node: {} initialized without a vlan", node);
126 configurationCache.put(nodeUuid, nodeConfiguration);
128 catch (Exception e) {
129 logger.debug("Error getting Port table for Node {}", node, e);
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.
138 public Integer assignInternalVlan (Node node, String networkId) {
139 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
140 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
141 if (mappedVlan != null) {
144 mappedVlan = nodeConfiguration.getInternalVlans().poll();
145 if (mappedVlan != null) {
146 nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
152 * Return the mapped internal vlan to the available pool.
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);
166 private void internalVlanInUse (NodeConfiguration nodeConfiguration, Integer vlan) {
167 nodeConfiguration.getInternalVlans().remove(vlan);
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;