Skip to content

Commit 597df24

Browse files
DaanHooglandyadvr
authored andcommitted
CLOUDSTACK-10007: Isolation methods (#2193)
Change isolation methods from an enum to a registry based construct to enhance pluggability
1 parent 98dc4eb commit 597df24

15 files changed

Lines changed: 252 additions & 79 deletions

File tree

api/src/com/cloud/network/PhysicalNetwork.java

Lines changed: 102 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616
// under the License.
1717
package com.cloud.network;
1818

19-
import java.util.List;
20-
19+
import com.cloud.exception.CloudException;
20+
import com.cloud.utils.Pair;
21+
import com.cloud.utils.StringUtils;
2122
import org.apache.cloudstack.api.Identity;
2223
import org.apache.cloudstack.api.InternalIdentity;
2324

24-
import com.cloud.utils.Pair;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Objects;
28+
import java.util.Set;
2529

2630
/**
2731
*
@@ -32,8 +36,101 @@ public enum State {
3236
Disabled, Enabled;
3337
}
3438

35-
public enum IsolationMethod {
36-
VLAN, L3, GRE, STT, BCF_SEGMENT, MIDO, SSP, VXLAN, ODL, L3VPN, VSP, VCS;
39+
public class IsolationMethod {
40+
protected static final String UNKNOWN_PROVIDER = "Unknown";
41+
static Set<IsolationMethod> registeredIsolationMethods = new HashSet<>();
42+
43+
String methodPrefix;
44+
String provider;
45+
46+
public IsolationMethod(String prfx) {
47+
this(prfx, UNKNOWN_PROVIDER);
48+
}
49+
50+
public IsolationMethod(String prfx, String prvdr) {
51+
methodPrefix = prfx;
52+
provider = StringUtils.isNotBlank(prvdr)? prvdr : UNKNOWN_PROVIDER;
53+
registeredIsolationMethods.add(this);
54+
}
55+
56+
/**
57+
* gets a IsolationMethod object that defines this prefix and if any it returns the first one found that has a known provider. If none has a known provider
58+
* it will return the one with the unknown provider. if none is found it return null.
59+
*
60+
* @param prfx
61+
* @return
62+
*/
63+
public static IsolationMethod getIsolationMethod(String prfx) throws IsolationMethodNotRegistered {
64+
IsolationMethod rc = null;
65+
for (IsolationMethod method: registeredIsolationMethods) {
66+
if (method.methodPrefix.equals(prfx)) {
67+
rc = method;
68+
if(! rc.getProvider().equals(UNKNOWN_PROVIDER)) {
69+
break;
70+
}
71+
}
72+
}
73+
if (rc == null) {
74+
throw new IsolationMethodNotRegistered("No registration of prefix '" + prfx + "' found.");
75+
}
76+
return rc;
77+
}
78+
79+
public static IsolationMethod getIsolationMethod(String prfx, String provider) throws IsolationMethodNotRegistered {
80+
for (IsolationMethod method: registeredIsolationMethods) {
81+
if (method.methodPrefix.equals(prfx) && method.provider.equals(provider)) {
82+
return method;
83+
}
84+
}
85+
throw new IsolationMethodNotRegistered("No registration of prefix '" + prfx + "' for provider '" + provider + "' found.");
86+
}
87+
88+
static class IsolationMethodNotRegistered extends CloudException {
89+
IsolationMethodNotRegistered (String message) {
90+
super(message);
91+
}
92+
}
93+
94+
public String getMethodPrefix() {
95+
return methodPrefix;
96+
}
97+
98+
public String getProvider() {
99+
return provider;
100+
}
101+
102+
@Override
103+
public boolean equals(Object o) {
104+
if (this == o)
105+
return true;
106+
if (o == null || getClass() != o.getClass())
107+
return false;
108+
IsolationMethod that = (IsolationMethod)o;
109+
return Objects.equals(methodPrefix, that.methodPrefix) && Objects.equals(provider, that.provider);
110+
}
111+
112+
@Override
113+
public int hashCode() {
114+
return Objects.hash(methodPrefix, provider);
115+
}
116+
117+
@Override
118+
public String toString() {
119+
return methodPrefix;
120+
}
121+
122+
public static boolean remove(String prfx, String prvdr) {
123+
prvdr = StringUtils.isNotBlank(prvdr)? prvdr : UNKNOWN_PROVIDER;
124+
125+
try {
126+
return remove(getIsolationMethod(prfx, prvdr));
127+
} catch (IsolationMethodNotRegistered isolationMethodNotRegistered) {
128+
return false;
129+
}
130+
}
131+
public static boolean remove(IsolationMethod method) {
132+
return registeredIsolationMethods.remove(method);
133+
}
37134
}
38135

39136
public enum BroadcastDomainRange {
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.network;
18+
19+
import org.junit.After;
20+
import org.junit.Test;
21+
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertFalse;
24+
25+
public class IsolationMethodTest {
26+
@After
27+
public void cleanTheRegistry() {
28+
PhysicalNetwork.IsolationMethod.registeredIsolationMethods.removeAll(PhysicalNetwork.IsolationMethod.registeredIsolationMethods);
29+
}
30+
31+
@Test
32+
public void equalsTest() throws Exception {
33+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
34+
assertEquals(PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER, method.provider);
35+
assertEquals(new PhysicalNetwork.IsolationMethod("bla", PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER), method);
36+
}
37+
38+
@Test
39+
public void toStringTest() throws Exception {
40+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
41+
assertEquals("bla", method.toString());
42+
43+
}
44+
45+
@Test
46+
public void getGeneric() throws Exception {
47+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
48+
method = new PhysicalNetwork.IsolationMethod("bla");
49+
50+
assertEquals("blob",PhysicalNetwork.IsolationMethod.getIsolationMethod("bla").getProvider());
51+
}
52+
53+
@Test
54+
public void removeUnregistered() throws Exception {
55+
assertFalse(PhysicalNetwork.IsolationMethod.remove("bla", "blob"));
56+
}
57+
58+
@Test
59+
public void removeSuccesfulGeneric() throws Exception {
60+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
61+
method = new PhysicalNetwork.IsolationMethod("bla");
62+
63+
PhysicalNetwork.IsolationMethod.remove("bla", "blob");
64+
assertEquals(PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER,PhysicalNetwork.IsolationMethod.getIsolationMethod("bla").getProvider());
65+
}
66+
67+
@Test(expected= PhysicalNetwork.IsolationMethod.IsolationMethodNotRegistered.class)
68+
public void removeSuccesfulSpecificByString() throws Exception {
69+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
70+
71+
PhysicalNetwork.IsolationMethod.remove("bla", "blob");
72+
PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
73+
}
74+
75+
@Test(expected= PhysicalNetwork.IsolationMethod.IsolationMethodNotRegistered.class)
76+
public void removeSuccesfulSpecificObject() throws Exception {
77+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
78+
79+
PhysicalNetwork.IsolationMethod.remove(method);
80+
PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
81+
}
82+
83+
@Test
84+
public void getIsolationMethodTest() throws Exception {
85+
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
86+
final PhysicalNetwork.IsolationMethod isolationMethod = PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
87+
assertEquals(method, isolationMethod);
88+
}
89+
}

plugins/network-elements/bigswitch/src/com/cloud/network/guru/BigSwitchBcfGuestNetworkGuru.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public class BigSwitchBcfGuestNetworkGuru extends GuestNetworkGuru implements Ne
129129

130130
public BigSwitchBcfGuestNetworkGuru() {
131131
super();
132-
_isolationMethods = new IsolationMethod[] {IsolationMethod.BCF_SEGMENT};
132+
_isolationMethods = new IsolationMethod[] {new IsolationMethod("BCF_SEGMENT")};
133133
}
134134

135135
@Override

plugins/network-elements/brocade-vcs/src/com/cloud/network/guru/BrocadeVcsGuestNetworkGuru.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
// under the License.
1717
package com.cloud.network.guru;
1818

19-
import java.util.List;
20-
21-
import javax.inject.Inject;
22-
23-
import org.apache.log4j.Logger;
2419

2520
import com.cloud.agent.AgentManager;
2621
import com.cloud.agent.api.AssociateMacToNetworkAnswer;
@@ -61,6 +56,10 @@
6156
import com.cloud.vm.NicProfile;
6257
import com.cloud.vm.ReservationContext;
6358
import com.cloud.vm.VirtualMachineProfile;
59+
import org.apache.log4j.Logger;
60+
61+
import javax.inject.Inject;
62+
import java.util.List;
6463

6564
public class BrocadeVcsGuestNetworkGuru extends GuestNetworkGuru {
6665
private static final Logger s_logger = Logger.getLogger(BrocadeVcsGuestNetworkGuru.class);
@@ -82,7 +81,7 @@ public class BrocadeVcsGuestNetworkGuru extends GuestNetworkGuru {
8281

8382
public BrocadeVcsGuestNetworkGuru() {
8483
super();
85-
_isolationMethods = new IsolationMethod[] {IsolationMethod.VCS};
84+
_isolationMethods = new IsolationMethod[] {new IsolationMethod("VCS")};
8685
}
8786

8887
@Override

plugins/network-elements/midonet/src/com/cloud/network/guru/MidoNetGuestNetworkGuru.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919

2020
package com.cloud.network.guru;
2121

22-
import javax.inject.Inject;
23-
24-
import org.apache.log4j.Logger;
25-
import org.springframework.stereotype.Component;
2622

2723
import com.cloud.dc.DataCenter.NetworkType;
2824
import com.cloud.deploy.DeployDestination;
@@ -42,6 +38,11 @@
4238
import com.cloud.vm.NicProfile;
4339
import com.cloud.vm.ReservationContext;
4440
import com.cloud.vm.VirtualMachineProfile;
41+
import org.apache.log4j.Logger;
42+
import org.springframework.stereotype.Component;
43+
44+
import javax.ejb.Local;
45+
import javax.inject.Inject;
4546

4647
@Component
4748
public class MidoNetGuestNetworkGuru extends GuestNetworkGuru {
@@ -52,7 +53,7 @@ public class MidoNetGuestNetworkGuru extends GuestNetworkGuru {
5253

5354
public MidoNetGuestNetworkGuru() {
5455
super();
55-
_isolationMethods = new PhysicalNetwork.IsolationMethod[] {PhysicalNetwork.IsolationMethod.MIDO};
56+
_isolationMethods = new PhysicalNetwork.IsolationMethod[] {new PhysicalNetwork.IsolationMethod("MIDO")};
5657
}
5758

5859
@Override

plugins/network-elements/nicira-nvp/src/main/java/com/cloud/network/guru/NiciraNvpGuestNetworkGuru.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru implements Netwo
113113

114114
public NiciraNvpGuestNetworkGuru() {
115115
super();
116-
_isolationMethods = new IsolationMethod[] { IsolationMethod.STT, IsolationMethod.VXLAN };
116+
_isolationMethods = new IsolationMethod[] { new IsolationMethod("STT", "NiciraNvp"), new IsolationMethod("VXLAN","NiciraNvp") };
117117
}
118118

119119
@Override

plugins/network-elements/nuage-vsp/src/com/cloud/network/element/NuageVspElement.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@
8181
import com.cloud.network.Network.Service;
8282
import com.cloud.network.NetworkModel;
8383
import com.cloud.network.Networks;
84-
import com.cloud.network.PhysicalNetwork;
8584
import com.cloud.network.PhysicalNetworkServiceProvider;
8685
import com.cloud.network.PublicIpAddress;
8786
import com.cloud.network.dao.FirewallRulesCidrsDao;
@@ -694,7 +693,7 @@ private Long getPhysicalNetworkId(Long zoneId) {
694693
Long guestPhysicalNetworkId = 0L;
695694
List<PhysicalNetworkVO> physicalNetworkList = _physicalNetworkDao.listByZone(zoneId);
696695
for (PhysicalNetworkVO phyNtwk : physicalNetworkList) {
697-
if (phyNtwk.getIsolationMethods().contains(PhysicalNetwork.IsolationMethod.VSP.name())) {
696+
if (phyNtwk.getIsolationMethods().contains("VSP")) {
698697
guestPhysicalNetworkId = phyNtwk.getId();
699698
break;
700699
}

plugins/network-elements/nuage-vsp/src/com/cloud/network/guru/NuageVspGuestNetworkGuru.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class NuageVspGuestNetworkGuru extends GuestNetworkGuru {
131131

132132
public NuageVspGuestNetworkGuru() {
133133
super();
134-
_isolationMethods = new IsolationMethod[] {IsolationMethod.VSP};
134+
_isolationMethods = new IsolationMethod[] {new IsolationMethod("VSP")};
135135
}
136136

137137
@Override
@@ -591,6 +591,4 @@ private Long getDefaultNetwork(long vmId) {
591591
if (defaultNic != null) return defaultNic.getNetworkId();
592592
return null;
593593
}
594-
595-
596594
}

plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ public HostVO getNuageVspHost(long physicalNetworkId) {
822822
PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
823823
List<PhysicalNetworkVO> physicalNetworksInZone = _physicalNetworkDao.listByZone(physicalNetwork.getDataCenterId());
824824
for (PhysicalNetworkVO physicalNetworkInZone : physicalNetworksInZone) {
825-
if (physicalNetworkInZone.getIsolationMethods().contains(PhysicalNetwork.IsolationMethod.VSP.name())) {
825+
if (physicalNetworkInZone.getIsolationMethods().contains("VSP")) {
826826
nuageVspDevices = _nuageVspDao.listByPhysicalNetwork(physicalNetworkInZone.getId());
827827
break;
828828
}

plugins/network-elements/nuage-vsp/test/com/cloud/network/element/NuageVspElementTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import com.cloud.network.Networks.BroadcastDomainType;
5757
import com.cloud.network.Networks.TrafficType;
5858
import com.cloud.network.NuageVspDeviceVO;
59-
import com.cloud.network.PhysicalNetwork;
6059
import com.cloud.network.dao.FirewallRulesDao;
6160
import com.cloud.network.dao.IPAddressDao;
6261
import com.cloud.network.dao.IPAddressVO;
@@ -332,7 +331,7 @@ public void testShutdownVpc() throws Exception {
332331
when(context.getAccount()).thenReturn(acc);
333332

334333
PhysicalNetworkVO physNet = mock(PhysicalNetworkVO.class);
335-
when(physNet.getIsolationMethods()).thenReturn(Lists.newArrayList(PhysicalNetwork.IsolationMethod.VSP.name()));
334+
when(physNet.getIsolationMethods()).thenReturn(Lists.newArrayList("VSP"));
336335
when(physNet.getId()).thenReturn(NETWORK_ID);
337336
when(_physicalNetworkDao.listByZone(NETWORK_ID)).thenReturn(Lists.newArrayList(physNet));
338337

0 commit comments

Comments
 (0)