Squashed commit of the following:
[ovsdb.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.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;
17
18 import com.google.common.collect.Maps;
19 import java.util.List;
20 import java.util.Map;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * @author Dave Tucker
26  * @author Sam Hague
27  */
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;
32
33     void init() {
34         logger.info(">>>>>> init {}", this.getClass());
35     }
36
37     private NodeConfiguration getNodeConfiguration(Node node){
38         String nodeUuid = getNodeUUID(node);
39         if (configurationCache.get(nodeUuid) != null) {
40             return configurationCache.get(nodeUuid);
41         }
42
43         // Cache miss
44         initializeNodeConfiguration(node, nodeUuid);
45
46         return configurationCache.get(nodeUuid);
47     }
48
49     private String getNodeUUID(Node node) {
50         return MdsalUtils.getOvsdbNodeUUID(node);
51     }
52
53     private void initializeNodeConfiguration(Node node, String nodeUuid) {
54         NodeConfiguration nodeConfiguration = new NodeConfiguration();
55         Integer vlan = 0;
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);
64             } else {
65                 logger.debug("Node: {} initialized without a vlan", node);
66             }
67         }
68         configurationCache.put(nodeUuid, nodeConfiguration);
69     }
70
71     /**
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.
74      */
75     @Override
76     public Integer assignInternalVlan (Node node, String networkId) {
77         NodeConfiguration nodeConfiguration = getNodeConfiguration(node);
78         Integer mappedVlan = nodeConfiguration.getTenantVlanMap().get(networkId);
79         if (mappedVlan != null) {
80             return mappedVlan;
81         }
82         mappedVlan = nodeConfiguration.getInternalVlans().poll();
83         if (mappedVlan != null) {
84             nodeConfiguration.getTenantVlanMap().put(networkId, mappedVlan);
85         }
86         return mappedVlan;
87     }
88
89     /**
90      * Return the mapped internal vlan to the available pool.
91      */
92     @Override
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);
99             return mappedVlan;
100         }
101         return 0;
102     }
103
104     private void internalVlanInUse(NodeConfiguration nodeConfiguration, Integer vlan) {
105         nodeConfiguration.getInternalVlans().remove(vlan);
106     }
107
108     @Override
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;
113     }
114 }