|
20 | 20 | import java.util.HashMap; |
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Map; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
23 | 24 |
|
24 | 25 | import org.apache.log4j.Logger; |
25 | 26 |
|
|
36 | 37 | public class DistributedVirtualSwitchMO extends BaseMO { |
37 | 38 | @SuppressWarnings("unused") |
38 | 39 | private static final Logger s_logger = Logger.getLogger(DistributedVirtualSwitchMO.class); |
| 40 | + private static ConcurrentHashMap<String, List<String>> s_dvPortGroupCacheMap = null; |
39 | 41 |
|
40 | 42 | public DistributedVirtualSwitchMO(VmwareContext context, ManagedObjectReference morDvs) { |
41 | 43 | super(context, morDvs); |
| 44 | + s_dvPortGroupCacheMap = new ConcurrentHashMap<String, List<String>>(); |
42 | 45 | } |
43 | 46 |
|
44 | 47 | public DistributedVirtualSwitchMO(VmwareContext context, String morType, String morValue) { |
45 | 48 | super(context, morType, morValue); |
| 49 | + s_dvPortGroupCacheMap = new ConcurrentHashMap<String, List<String>>(); |
46 | 50 | } |
47 | 51 |
|
48 | 52 | public void createDVPortGroup(DVPortgroupConfigSpec dvPortGroupSpec) throws Exception { |
49 | 53 | List<DVPortgroupConfigSpec> dvPortGroupSpecArray = new ArrayList<DVPortgroupConfigSpec>(); |
50 | 54 | dvPortGroupSpecArray.add(dvPortGroupSpec); |
51 | | - _context.getService().addDVPortgroupTask(_mor, dvPortGroupSpecArray); |
| 55 | + boolean dvPortGroupExists = false; |
| 56 | + String dvSwitchInstance = _mor.getValue(); |
| 57 | + String dvPortGroupName = dvPortGroupSpec.getName(); |
| 58 | + String uniquedvPortGroupPerDvs = dvSwitchInstance + dvPortGroupName; |
| 59 | + List<String> dvPortGroupList = null; |
| 60 | + synchronized (uniquedvPortGroupPerDvs.intern()) { |
| 61 | + // Looking up local cache rather than firing another API call to see if dvPortGroup exists already. |
| 62 | + if (s_dvPortGroupCacheMap.containsKey(dvSwitchInstance)) { |
| 63 | + dvPortGroupList = s_dvPortGroupCacheMap.get(dvSwitchInstance); |
| 64 | + if (dvPortGroupList != null && dvPortGroupList.contains(dvPortGroupName)) { |
| 65 | + dvPortGroupExists = true; |
| 66 | + } |
| 67 | + } |
| 68 | + if (!dvPortGroupExists) { |
| 69 | + ManagedObjectReference task = _context.getService().addDVPortgroupTask(_mor, dvPortGroupSpecArray); |
| 70 | + if (!_context.getVimClient().waitForTask(task)) { |
| 71 | + throw new Exception("Failed to create dvPortGroup " + dvPortGroupSpec.getName()); |
| 72 | + } else { |
| 73 | + if (s_dvPortGroupCacheMap.containsKey(dvSwitchInstance)) { |
| 74 | + dvPortGroupList = s_dvPortGroupCacheMap.get(dvSwitchInstance); |
| 75 | + if (dvPortGroupList == null) { |
| 76 | + dvPortGroupList = new ArrayList<String>(); |
| 77 | + } |
| 78 | + dvPortGroupList.add(dvPortGroupName); //does this update map? |
| 79 | + } else { |
| 80 | + dvPortGroupList = new ArrayList<String>(); |
| 81 | + dvPortGroupList.add(dvPortGroupName); |
| 82 | + s_dvPortGroupCacheMap.put(dvSwitchInstance, dvPortGroupList); |
| 83 | + } |
| 84 | + } |
| 85 | + if (s_logger.isTraceEnabled()) { |
| 86 | + s_logger.trace("Created dvPortGroup. dvPortGroup cache is :" + s_dvPortGroupCacheMap); |
| 87 | + } |
| 88 | + } else if (s_logger.isDebugEnabled()) { |
| 89 | + s_logger.debug("Detected dvPortGroup [" + dvPortGroupName + "] already present. Not attempting to create again."); |
| 90 | + } |
| 91 | + } |
52 | 92 | } |
53 | 93 |
|
54 | 94 | public void updateDvPortGroup(ManagedObjectReference dvPortGroupMor, DVPortgroupConfigSpec dvPortGroupSpec) throws Exception { |
55 | | - // TODO(sateesh): Update numPorts |
56 | | - _context.getService().reconfigureDVPortgroupTask(dvPortGroupMor, dvPortGroupSpec); |
| 95 | + synchronized (dvPortGroupMor.getValue().intern()) { |
| 96 | + ManagedObjectReference task = _context.getService().reconfigureDVPortgroupTask(dvPortGroupMor, dvPortGroupSpec); |
| 97 | + if (!_context.getVimClient().waitForTask(task)) { |
| 98 | + throw new Exception("Failed to update dvPortGroup " + dvPortGroupMor.getValue()); |
| 99 | + } |
| 100 | + } |
57 | 101 | } |
58 | 102 |
|
59 | 103 | public void updateVMWareDVSwitch(ManagedObjectReference dvSwitchMor, VMwareDVSConfigSpec dvsSpec) throws Exception { |
|
0 commit comments