2 * Copyright (c) 2013 Hewlett-Packard Development Company, L.P. and others
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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11 import org.opendaylight.ovsdb.openstack.netvirt.MdsalUtils;
12 import org.opendaylight.ovsdb.openstack.netvirt.NodeConfiguration;
13 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
18 import com.google.common.collect.Maps;
19 import java.util.List;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
28 public class VlanConfigurationCacheImpl implements VlanConfigurationCache {
29 static final Logger logger = LoggerFactory.getLogger(VlanConfigurationCacheImpl.class);
30 private Map<String, NodeConfiguration> configurationCache = Maps.newConcurrentMap();
31 private volatile TenantNetworkManager tenantNetworkManager;
34 logger.info(">>>>>> init {}", this.getClass());
37 private NodeConfiguration getNodeConfiguration(Node node){
38 String nodeUuid = getNodeUUID(node);
39 if (configurationCache.get(nodeUuid) != null) {
40 return configurationCache.get(nodeUuid);
44 initializeNodeConfiguration(node, nodeUuid);
46 return configurationCache.get(nodeUuid);
49 private String getNodeUUID(Node node) {
50 return MdsalUtils.getOvsdbNodeUUID(node);
53 private void initializeNodeConfiguration(Node node, String nodeUuid) {
54 NodeConfiguration nodeConfiguration = new NodeConfiguration();
56 String networkId = null;
57 List<OvsdbTerminationPointAugmentation> ports = MdsalUtils.getTerminationPointsOfBridge(node);
58 for (OvsdbTerminationPointAugmentation port : ports) {
59 vlan = port.getVlanTag().getValue();
60 networkId = tenantNetworkManager.getTenantNetwork(port).getNetworkUUID();
61 if (vlan != 0 && networkId != null) {
62 internalVlanInUse(nodeConfiguration, vlan);
63 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
65 logger.debug("Node: {} initialized without a vlan", node);
68 configurationCache.put(nodeUuid, nodeConfiguration);
72 * Return the currently mapped internal vlan or get the next
73 * free internal vlan from the available pool and map it to the networkId.
76 public Integer assignInternalVlan (Node node, String networkId) {
77 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
78 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
79 if (mappedVlan != null) {
82 mappedVlan = nodeConfiguration.getInternalVlans().poll();
83 if (mappedVlan != null) {
84 nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
90 * Return the mapped internal vlan to the available pool.
93 public Integer reclaimInternalVlan(Node node, String networkId) {
94 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
95 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
96 if (mappedVlan != null) {
97 nodeConfiguration.getTenantVlanMap().remove(networkId);
98 nodeConfiguration.getInternalVlans().add(mappedVlan);
104 private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
105 nodeConfiguration.getInternalVlans().remove(vlan);
109 public Integer getInternalVlan(Node node, String networkId) {
110 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
111 Integer vlan = nodeConfiguration.getTenantVlanMap().get(networkId);
112 return vlan == null ? 0 : vlan;