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.ConfigInterface;
12 import org.opendaylight.ovsdb.openstack.netvirt.NodeConfiguration;
13 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
16 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
20 import com.google.common.collect.Maps;
21 import java.util.List;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
32 public class VlanConfigurationCacheImpl implements ConfigInterface, VlanConfigurationCache {
33 static final Logger logger = LoggerFactory.getLogger(VlanConfigurationCacheImpl.class);
34 private Map<String, NodeConfiguration> configurationCache = Maps.newConcurrentMap();
35 private volatile TenantNetworkManager tenantNetworkManager;
36 private volatile Southbound southbound;
38 private NodeConfiguration getNodeConfiguration(Node node){
39 String nodeUuid = getNodeUUID(node);
40 if (configurationCache.get(nodeUuid) != null) {
41 return configurationCache.get(nodeUuid);
45 initializeNodeConfiguration(node, nodeUuid);
47 return configurationCache.get(nodeUuid);
50 private String getNodeUUID(Node node) {
51 return southbound.getOvsdbNodeUUID(node);
54 private void initializeNodeConfiguration(Node node, String nodeUuid) {
55 NodeConfiguration nodeConfiguration = new NodeConfiguration();
57 String networkId = null;
58 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
59 for (OvsdbTerminationPointAugmentation port : ports) {
60 vlan = port.getVlanTag().getValue();
61 networkId = tenantNetworkManager.getTenantNetwork(port).getNetworkUUID();
62 if (vlan != 0 && networkId != null) {
63 internalVlanInUse(nodeConfiguration, vlan);
64 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
66 logger.debug("Node: {} initialized without a vlan", node);
69 configurationCache.put(nodeUuid, nodeConfiguration);
73 * Return the currently mapped internal vlan or get the next
74 * free internal vlan from the available pool and map it to the networkId.
77 public Integer assignInternalVlan (Node node, String networkId) {
78 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
79 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
80 if (mappedVlan != null) {
83 mappedVlan = nodeConfiguration.getInternalVlans().poll();
84 if (mappedVlan != null) {
85 nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
91 * Return the mapped internal vlan to the available pool.
94 public Integer reclaimInternalVlan(Node node, String networkId) {
95 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
96 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
97 if (mappedVlan != null) {
98 nodeConfiguration.getTenantVlanMap().remove(networkId);
99 nodeConfiguration.getInternalVlans().add(mappedVlan);
105 private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
106 nodeConfiguration.getInternalVlans().remove(vlan);
110 public Integer getInternalVlan(Node node, String networkId) {
111 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
112 Integer vlan = nodeConfiguration.getTenantVlanMap().get(networkId);
113 return vlan == null ? 0 : vlan;
117 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
118 tenantNetworkManager =
119 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
121 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
125 public void setDependencies(Object impl) {