2 * Copyright (c) 2013, 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at 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;
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
32 public class VlanConfigurationCacheImpl implements ConfigInterface, VlanConfigurationCache {
33 private static final Logger LOG = 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();
56 List<OvsdbTerminationPointAugmentation> ports = southbound.getTerminationPointsOfBridge(node);
57 for (OvsdbTerminationPointAugmentation port : ports) {
58 Integer vlan = port.getVlanTag().getValue();
59 String networkId = tenantNetworkManager.getTenantNetwork(port).getNetworkUUID();
60 if (vlan != 0 && networkId != null) {
61 internalVlanInUse(nodeConfiguration, vlan);
62 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
64 LOG.debug("Node: {} initialized without a vlan", node);
67 configurationCache.put(nodeUuid, nodeConfiguration);
71 * Return the currently mapped internal vlan or get the next
72 * free internal vlan from the available pool and map it to the networkId.
75 public Integer assignInternalVlan (Node node, String networkId) {
76 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
77 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
78 if (mappedVlan != null) {
81 mappedVlan = nodeConfiguration.getInternalVlans().poll();
82 if (mappedVlan != null) {
83 nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
89 * Return the mapped internal vlan to the available pool.
92 public Integer reclaimInternalVlan(Node node, String networkId) {
93 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
94 Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
95 if (mappedVlan != null) {
96 nodeConfiguration.getTenantVlanMap().remove(networkId);
97 nodeConfiguration.getInternalVlans().add(mappedVlan);
103 private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
104 nodeConfiguration.getInternalVlans().remove(vlan);
108 public Integer getInternalVlan(Node node, String networkId) {
109 NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
110 Integer vlan = nodeConfiguration.getTenantVlanMap().get(networkId);
111 return vlan == null ? 0 : vlan;
115 public void setDependencies(ServiceReference serviceReference) {
116 tenantNetworkManager =
117 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
119 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
123 public void setDependencies(Object impl) {