Remove OvsdbConfigurationService
[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.NodeConfiguration;
12 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
13 import org.opendaylight.ovsdb.openstack.netvirt.api.VlanConfigurationCache;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
16
17 import com.google.common.collect.Maps;
18 import java.util.List;
19 import java.util.Map;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @author Dave Tucker
25  * @author Sam Hague
26  */
27 public class VlanConfigurationCacheImpl implements VlanConfigurationCache {
28     static final Logger logger = LoggerFactory.getLogger(VlanConfigurationCacheImpl.class);
29     private Map<String, NodeConfiguration> configurationCache = Maps.newConcurrentMap();
30     private volatile TenantNetworkManager tenantNetworkManager;
31
32     void init() {
33         logger.info(">>>>>> init {}", this.getClass());
34     }
35
36     private NodeConfiguration getNodeConfiguration(Node node){
37         String nodeUuid = getNodeUUID(node);
38         if (configurationCache.get(nodeUuid) != null) {
39             return configurationCache.get(nodeUuid);
40         }
41
42         // Cache miss
43         initializeNodeConfiguration(node, nodeUuid);
44
45         return configurationCache.get(nodeUuid);
46     }
47
48     private String getNodeUUID(Node node) {
49         return MdsalUtils.getNodeUUID(node);
50     }
51
52     private void initializeNodeConfiguration(Node node, String nodeUuid) {
53         NodeConfiguration nodeConfiguration = new NodeConfiguration();
54         Integer vlan = 0;
55         String networkId = null;
56         List<OvsdbTerminationPointAugmentation> ports = MdsalUtils.getPorts(node);
57         for (OvsdbTerminationPointAugmentation port : ports) {
58             vlan = port.getVlanTag().getValue();
59             networkId = tenantNetworkManager.getTenantNetwork(port).getNetworkUUID();
60             if (vlan != 0 && networkId != null) {
61                 internalVlanInUse(nodeConfiguration, vlan);
62                 nodeConfiguration.getTenantVlanMap().put(networkId, vlan);
63             } else {
64                 logger.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 }