Code ReOrganization and Re-Architecture changes
[ovsdb.git] / neutron / src / main / java / org / opendaylight / ovsdb / neutron / NodeConfiguration.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *    Dave Tucker (HP) - Replace tenantVlanMap with a per Node cache
10  *******************************************************************************/
11
12 package org.opendaylight.ovsdb.neutron;
13
14 import java.math.BigInteger;
15 import java.util.LinkedList;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
20 import org.opendaylight.controller.sal.core.Node;
21 import org.opendaylight.controller.sal.utils.ServiceHelper;
22 import org.opendaylight.ovsdb.lib.notation.UUID;
23 import org.opendaylight.ovsdb.lib.table.Interface;
24 import org.opendaylight.ovsdb.lib.table.Port;
25 import org.opendaylight.ovsdb.lib.table.Table;
26 import org.opendaylight.ovsdb.plugin.OVSDBConfigService;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class NodeConfiguration {
31     static final Logger logger = LoggerFactory.getLogger(NodeConfiguration.class);
32     private static final int MAX_VLAN = 4096;
33     private java.util.Queue<Integer> internalVlans = new LinkedList<>();
34     private ConcurrentMap<String, Integer> tenantVlanMap = new ConcurrentHashMap<>();
35
36     public NodeConfiguration(Node node) {
37         for (int i = 1; i < MAX_VLAN ; i++) {
38             internalVlans.add(i);
39         }
40
41         initializeNodeConfiguration(node);
42     }
43
44
45     private void initializeNodeConfiguration(Node node) {
46
47         int vlan = 0;
48         String networkId = new String();
49         OVSDBConfigService ovsdbTable = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
50
51         try {
52             Map<String, Table<?>> portRows = ovsdbTable.getRows(node, Port.NAME.getName());
53
54             if (portRows == null){
55                 logger.info("Interface table is null for Node {}", node);
56                 return;
57             }
58
59             for (Table<?> row : portRows.values()) {
60                 Port port = (Port)row;
61
62                 BigInteger[] tags = port.getTag().toArray(new BigInteger[0]);
63                 if (tags.length == 1)
64                 {
65                     //There is only one tag here
66                     vlan = tags[0].intValue();
67                 }
68                 else {
69                    logger.debug("This port has more {} interfaces", tags.length);
70                    continue;
71                 }
72
73                 for (UUID ifaceId : port.getInterfaces()) {
74                     Interface iface = (Interface)ovsdbTable.getRow(node, Interface.NAME.getName(), ifaceId.toString());
75
76                     if (iface == null) {
77                         logger.error("Interface table is null for Po");
78                         continue;
79                     }
80
81                     networkId = TenantNetworkManager.getManager().getTenantNetworkForInterface(iface).getNetworkUUID();
82
83                     if (networkId != null) break;
84                 }
85
86                 if (vlan != 0 && networkId != null) {
87
88                     this.internalVlanInUse(vlan);
89                     this.tenantVlanMap.put(networkId, vlan);
90
91                 }
92             }
93         }
94         catch (Exception e) {
95             logger.error("Error getting Port table for Node {}: {}", node, e);
96         }
97     }
98
99     public int assignInternalVlan (String networkId) {
100         Integer mappedVlan = tenantVlanMap.get(networkId);
101         if (mappedVlan != null) return mappedVlan;
102         mappedVlan = internalVlans.poll();
103         if (mappedVlan != null) tenantVlanMap.put(networkId, mappedVlan);
104         return mappedVlan;
105     }
106
107     public int reclaimInternalVlan (String networkId) {
108         Integer mappedVlan = tenantVlanMap.get(networkId);
109         if (mappedVlan != null) {
110             tenantVlanMap.remove(mappedVlan);
111             internalVlans.add(mappedVlan);
112             return mappedVlan;
113         }
114         return 0;
115     }
116
117     public void internalVlanInUse (int vlan) {
118         internalVlans.remove(vlan);
119     }
120
121     public int getInternalVlan (String networkId) {
122         Integer vlan = tenantVlanMap.get(networkId);
123         if (vlan == null) return 0;
124         return vlan.intValue();
125     }
126
127 }