Merge "Add UT for SouthboundMapper and SouthboundProvider"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / VlanConfigurationCacheImpl.java
1 /*
2  * Copyright (c) 2013, 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10
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;
19
20 import com.google.common.collect.Maps;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.osgi.framework.ServiceReference;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * @author Dave Tucker
30  * @author Sam Hague
31  */
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;
37
38     private NodeConfiguration getNodeConfiguration(Node node){
39         String nodeUuid = getNodeUUID(node);
40         if (configurationCache.get(nodeUuid) != null) {
41             return configurationCache.get(nodeUuid);
42         }
43
44         // Cache miss
45         initializeNodeConfiguration(node, nodeUuid);
46
47         return configurationCache.get(nodeUuid);
48     }
49
50     private String getNodeUUID(Node node) {
51         return southbound.getOvsdbNodeUUID(node);
52     }
53
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);
63             } else {
64                 LOG.debug("Node: {} initialized without a vlan", node);
65             }
66         }
67         configurationCache.put(nodeUuid, nodeConfiguration);
68     }
69
70     /**
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.
73      */
74     @Override
75     public Integer assignInternalVlan (Node node, String networkId) {
76         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
77         Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
78         if (mappedVlan != null) {
79             return mappedVlan;
80         }
81         mappedVlan = nodeConfiguration.getInternalVlans().poll();
82         if (mappedVlan != null) {
83             nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
84         }
85         return mappedVlan;
86     }
87
88     /**
89      * Return the mapped internal vlan to the available pool.
90      */
91     @Override
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);
98             return mappedVlan;
99         }
100         return 0;
101     }
102
103     private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
104         nodeConfiguration.getInternalVlans().remove(vlan);
105     }
106
107     @Override
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;
112     }
113
114     @Override
115     public void setDependencies(ServiceReference serviceReference) {
116         tenantNetworkManager =
117                 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
118         southbound =
119                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
120     }
121
122     @Override
123     public void setDependencies(Object impl) {
124
125     }
126 }