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
9 * Dave Tucker (HP) - Replace tenantVlanMap with a per Node cache
10 *******************************************************************************/
12 package org.opendaylight.ovsdb.neutron;
14 import java.math.BigInteger;
15 import java.util.LinkedList;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
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;
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<>();
36 public NodeConfiguration(Node node) {
37 for (int i = 1; i < MAX_VLAN ; i++) {
41 initializeNodeConfiguration(node);
45 private void initializeNodeConfiguration(Node node) {
48 String networkId = new String();
49 OVSDBConfigService ovsdbTable = (OVSDBConfigService) ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
52 Map<String, Table<?>> portRows = ovsdbTable.getRows(node, Port.NAME.getName());
54 if (portRows == null){
55 logger.info("Interface table is null for Node {}", node);
59 for (Table<?> row : portRows.values()) {
60 Port port = (Port)row;
62 BigInteger[] tags = port.getTag().toArray(new BigInteger[0]);
65 //There is only one tag here
66 vlan = tags[0].intValue();
69 logger.debug("This port has more {} interfaces", tags.length);
73 for (UUID ifaceId : port.getInterfaces()) {
74 Interface iface = (Interface)ovsdbTable.getRow(node, Interface.NAME.getName(), ifaceId.toString());
77 logger.error("Interface table is null for Po");
81 networkId = TenantNetworkManager.getManager().getTenantNetworkForInterface(iface).getNetworkUUID();
83 if (networkId != null) break;
86 if (vlan != 0 && networkId != null) {
88 this.internalVlanInUse(vlan);
89 this.tenantVlanMap.put(networkId, vlan);
95 logger.error("Error getting Port table for Node {}: {}", node, e);
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);
107 public int reclaimInternalVlan (String networkId) {
108 Integer mappedVlan = tenantVlanMap.get(networkId);
109 if (mappedVlan != null) {
110 tenantVlanMap.remove(mappedVlan);
111 internalVlans.add(mappedVlan);
117 public void internalVlanInUse (int vlan) {
118 internalVlans.remove(vlan);
121 public int getInternalVlan (String networkId) {
122 Integer vlan = tenantVlanMap.get(networkId);
123 if (vlan == null) return 0;
124 return vlan.intValue();