Rename MdsalUtils to be Southbound and remove statics
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / VlanConfigurationCacheImpl.java
1 /*
2  * Copyright (c) 2013 Hewlett-Packard Development Company, L.P. and others
3  *
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
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 import org.osgi.framework.BundleContext;
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     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;
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         Integer vlan = 0;
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);
65             } else {
66                 logger.debug("Node: {} initialized without a vlan", node);
67             }
68         }
69         configurationCache.put(nodeUuid, nodeConfiguration);
70     }
71
72     /**
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.
75      */
76     @Override
77     public Integer assignInternalVlan (Node node, String networkId) {
78         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
79         Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
80         if (mappedVlan != null) {
81             return mappedVlan;
82         }
83         mappedVlan = nodeConfiguration.getInternalVlans().poll();
84         if (mappedVlan != null) {
85             nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
86         }
87         return mappedVlan;
88     }
89
90     /**
91      * Return the mapped internal vlan to the available pool.
92      */
93     @Override
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);
100             return mappedVlan;
101         }
102         return 0;
103     }
104
105     private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
106         nodeConfiguration.getInternalVlans().remove(vlan);
107     }
108
109     @Override
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;
114     }
115
116     @Override
117     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
118         tenantNetworkManager =
119                 (TenantNetworkManager) ServiceHelper.getGlobalInstance(TenantNetworkManager.class, this);
120         southbound =
121                 (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
122     }
123
124     @Override
125     public void setDependencies(Object impl) {
126
127     }
128 }