CLOUDSTACK-10007: Isolation methods (#2193)

Change isolation methods from an enum to a registry based construct to enhance pluggability
This commit is contained in:
dahn 2017-08-03 12:20:37 +02:00 committed by Rohit Yadav
parent 98dc4eb96a
commit 597df24b53
15 changed files with 252 additions and 79 deletions

View File

@ -16,12 +16,16 @@
// under the License.
package com.cloud.network;
import java.util.List;
import com.cloud.exception.CloudException;
import com.cloud.utils.Pair;
import com.cloud.utils.StringUtils;
import org.apache.cloudstack.api.Identity;
import org.apache.cloudstack.api.InternalIdentity;
import com.cloud.utils.Pair;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
/**
*
@ -32,8 +36,101 @@ public interface PhysicalNetwork extends Identity, InternalIdentity {
Disabled, Enabled;
}
public enum IsolationMethod {
VLAN, L3, GRE, STT, BCF_SEGMENT, MIDO, SSP, VXLAN, ODL, L3VPN, VSP, VCS;
public class IsolationMethod {
protected static final String UNKNOWN_PROVIDER = "Unknown";
static Set<IsolationMethod> registeredIsolationMethods = new HashSet<>();
String methodPrefix;
String provider;
public IsolationMethod(String prfx) {
this(prfx, UNKNOWN_PROVIDER);
}
public IsolationMethod(String prfx, String prvdr) {
methodPrefix = prfx;
provider = StringUtils.isNotBlank(prvdr)? prvdr : UNKNOWN_PROVIDER;
registeredIsolationMethods.add(this);
}
/**
* 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
* it will return the one with the unknown provider. if none is found it return null.
*
* @param prfx
* @return
*/
public static IsolationMethod getIsolationMethod(String prfx) throws IsolationMethodNotRegistered {
IsolationMethod rc = null;
for (IsolationMethod method: registeredIsolationMethods) {
if (method.methodPrefix.equals(prfx)) {
rc = method;
if(! rc.getProvider().equals(UNKNOWN_PROVIDER)) {
break;
}
}
}
if (rc == null) {
throw new IsolationMethodNotRegistered("No registration of prefix '" + prfx + "' found.");
}
return rc;
}
public static IsolationMethod getIsolationMethod(String prfx, String provider) throws IsolationMethodNotRegistered {
for (IsolationMethod method: registeredIsolationMethods) {
if (method.methodPrefix.equals(prfx) && method.provider.equals(provider)) {
return method;
}
}
throw new IsolationMethodNotRegistered("No registration of prefix '" + prfx + "' for provider '" + provider + "' found.");
}
static class IsolationMethodNotRegistered extends CloudException {
IsolationMethodNotRegistered (String message) {
super(message);
}
}
public String getMethodPrefix() {
return methodPrefix;
}
public String getProvider() {
return provider;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
IsolationMethod that = (IsolationMethod)o;
return Objects.equals(methodPrefix, that.methodPrefix) && Objects.equals(provider, that.provider);
}
@Override
public int hashCode() {
return Objects.hash(methodPrefix, provider);
}
@Override
public String toString() {
return methodPrefix;
}
public static boolean remove(String prfx, String prvdr) {
prvdr = StringUtils.isNotBlank(prvdr)? prvdr : UNKNOWN_PROVIDER;
try {
return remove(getIsolationMethod(prfx, prvdr));
} catch (IsolationMethodNotRegistered isolationMethodNotRegistered) {
return false;
}
}
public static boolean remove(IsolationMethod method) {
return registeredIsolationMethods.remove(method);
}
}
public enum BroadcastDomainRange {

View File

@ -0,0 +1,89 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
public class IsolationMethodTest {
@After
public void cleanTheRegistry() {
PhysicalNetwork.IsolationMethod.registeredIsolationMethods.removeAll(PhysicalNetwork.IsolationMethod.registeredIsolationMethods);
}
@Test
public void equalsTest() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
assertEquals(PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER, method.provider);
assertEquals(new PhysicalNetwork.IsolationMethod("bla", PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER), method);
}
@Test
public void toStringTest() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
assertEquals("bla", method.toString());
}
@Test
public void getGeneric() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
method = new PhysicalNetwork.IsolationMethod("bla");
assertEquals("blob",PhysicalNetwork.IsolationMethod.getIsolationMethod("bla").getProvider());
}
@Test
public void removeUnregistered() throws Exception {
assertFalse(PhysicalNetwork.IsolationMethod.remove("bla", "blob"));
}
@Test
public void removeSuccesfulGeneric() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
method = new PhysicalNetwork.IsolationMethod("bla");
PhysicalNetwork.IsolationMethod.remove("bla", "blob");
assertEquals(PhysicalNetwork.IsolationMethod.UNKNOWN_PROVIDER,PhysicalNetwork.IsolationMethod.getIsolationMethod("bla").getProvider());
}
@Test(expected= PhysicalNetwork.IsolationMethod.IsolationMethodNotRegistered.class)
public void removeSuccesfulSpecificByString() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla", "blob");
PhysicalNetwork.IsolationMethod.remove("bla", "blob");
PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
}
@Test(expected= PhysicalNetwork.IsolationMethod.IsolationMethodNotRegistered.class)
public void removeSuccesfulSpecificObject() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
PhysicalNetwork.IsolationMethod.remove(method);
PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
}
@Test
public void getIsolationMethodTest() throws Exception {
PhysicalNetwork.IsolationMethod method = new PhysicalNetwork.IsolationMethod("bla");
final PhysicalNetwork.IsolationMethod isolationMethod = PhysicalNetwork.IsolationMethod.getIsolationMethod("bla");
assertEquals(method, isolationMethod);
}
}

View File

@ -129,7 +129,7 @@ public class BigSwitchBcfGuestNetworkGuru extends GuestNetworkGuru implements Ne
public BigSwitchBcfGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.BCF_SEGMENT};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("BCF_SEGMENT")};
}
@Override

View File

@ -16,11 +16,6 @@
// under the License.
package com.cloud.network.guru;
import java.util.List;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.AssociateMacToNetworkAnswer;
@ -61,6 +56,10 @@ import com.cloud.user.Account;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.log4j.Logger;
import javax.inject.Inject;
import java.util.List;
public class BrocadeVcsGuestNetworkGuru extends GuestNetworkGuru {
private static final Logger s_logger = Logger.getLogger(BrocadeVcsGuestNetworkGuru.class);
@ -82,7 +81,7 @@ public class BrocadeVcsGuestNetworkGuru extends GuestNetworkGuru {
public BrocadeVcsGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.VCS};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("VCS")};
}
@Override

View File

@ -19,10 +19,6 @@
package com.cloud.network.guru;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.deploy.DeployDestination;
@ -42,6 +38,11 @@ import com.cloud.user.dao.AccountDao;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.ejb.Local;
import javax.inject.Inject;
@Component
public class MidoNetGuestNetworkGuru extends GuestNetworkGuru {
@ -52,7 +53,7 @@ public class MidoNetGuestNetworkGuru extends GuestNetworkGuru {
public MidoNetGuestNetworkGuru() {
super();
_isolationMethods = new PhysicalNetwork.IsolationMethod[] {PhysicalNetwork.IsolationMethod.MIDO};
_isolationMethods = new PhysicalNetwork.IsolationMethod[] {new PhysicalNetwork.IsolationMethod("MIDO")};
}
@Override

View File

@ -113,7 +113,7 @@ public class NiciraNvpGuestNetworkGuru extends GuestNetworkGuru implements Netwo
public NiciraNvpGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] { IsolationMethod.STT, IsolationMethod.VXLAN };
_isolationMethods = new IsolationMethod[] { new IsolationMethod("STT", "NiciraNvp"), new IsolationMethod("VXLAN","NiciraNvp") };
}
@Override

View File

@ -81,7 +81,6 @@ import com.cloud.network.Network.Provider;
import com.cloud.network.Network.Service;
import com.cloud.network.NetworkModel;
import com.cloud.network.Networks;
import com.cloud.network.PhysicalNetwork;
import com.cloud.network.PhysicalNetworkServiceProvider;
import com.cloud.network.PublicIpAddress;
import com.cloud.network.dao.FirewallRulesCidrsDao;
@ -694,7 +693,7 @@ public class NuageVspElement extends AdapterBase implements ConnectivityProvider
Long guestPhysicalNetworkId = 0L;
List<PhysicalNetworkVO> physicalNetworkList = _physicalNetworkDao.listByZone(zoneId);
for (PhysicalNetworkVO phyNtwk : physicalNetworkList) {
if (phyNtwk.getIsolationMethods().contains(PhysicalNetwork.IsolationMethod.VSP.name())) {
if (phyNtwk.getIsolationMethods().contains("VSP")) {
guestPhysicalNetworkId = phyNtwk.getId();
break;
}

View File

@ -131,7 +131,7 @@ public class NuageVspGuestNetworkGuru extends GuestNetworkGuru {
public NuageVspGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.VSP};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("VSP")};
}
@Override
@ -591,6 +591,4 @@ public class NuageVspGuestNetworkGuru extends GuestNetworkGuru {
if (defaultNic != null) return defaultNic.getNetworkId();
return null;
}
}

View File

@ -822,7 +822,7 @@ public class NuageVspManagerImpl extends ManagerBase implements NuageVspManager,
PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
List<PhysicalNetworkVO> physicalNetworksInZone = _physicalNetworkDao.listByZone(physicalNetwork.getDataCenterId());
for (PhysicalNetworkVO physicalNetworkInZone : physicalNetworksInZone) {
if (physicalNetworkInZone.getIsolationMethods().contains(PhysicalNetwork.IsolationMethod.VSP.name())) {
if (physicalNetworkInZone.getIsolationMethods().contains("VSP")) {
nuageVspDevices = _nuageVspDao.listByPhysicalNetwork(physicalNetworkInZone.getId());
break;
}

View File

@ -56,7 +56,6 @@ import com.cloud.network.Network.Service;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.NuageVspDeviceVO;
import com.cloud.network.PhysicalNetwork;
import com.cloud.network.dao.FirewallRulesDao;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.IPAddressVO;
@ -332,7 +331,7 @@ public class NuageVspElementTest extends NuageTest {
when(context.getAccount()).thenReturn(acc);
PhysicalNetworkVO physNet = mock(PhysicalNetworkVO.class);
when(physNet.getIsolationMethods()).thenReturn(Lists.newArrayList(PhysicalNetwork.IsolationMethod.VSP.name()));
when(physNet.getIsolationMethods()).thenReturn(Lists.newArrayList("VSP"));
when(physNet.getId()).thenReturn(NETWORK_ID);
when(_physicalNetworkDao.listByZone(NETWORK_ID)).thenReturn(Lists.newArrayList(physNet));

View File

@ -19,26 +19,6 @@
package org.apache.cloudstack.network.opendaylight;
import java.util.List;
import java.util.UUID;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.apache.cloudstack.network.opendaylight.agent.commands.AddHypervisorCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.ConfigureNetworkCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.ConfigurePortCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.DestroyNetworkCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.DestroyPortCommand;
import org.apache.cloudstack.network.opendaylight.agent.responses.AddHypervisorAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.ConfigureNetworkAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.ConfigurePortAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.DestroyNetworkAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.DestroyPortAnswer;
import org.apache.cloudstack.network.opendaylight.dao.OpenDaylightControllerMappingDao;
import org.apache.cloudstack.network.opendaylight.dao.OpenDaylightControllerVO;
import com.cloud.agent.AgentManager;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
@ -68,6 +48,23 @@ import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.network.opendaylight.agent.commands.AddHypervisorCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.ConfigureNetworkCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.ConfigurePortCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.DestroyNetworkCommand;
import org.apache.cloudstack.network.opendaylight.agent.commands.DestroyPortCommand;
import org.apache.cloudstack.network.opendaylight.agent.responses.AddHypervisorAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.ConfigureNetworkAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.ConfigurePortAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.DestroyNetworkAnswer;
import org.apache.cloudstack.network.opendaylight.agent.responses.DestroyPortAnswer;
import org.apache.cloudstack.network.opendaylight.dao.OpenDaylightControllerMappingDao;
import org.apache.cloudstack.network.opendaylight.dao.OpenDaylightControllerVO;
import org.apache.log4j.Logger;
import javax.inject.Inject;
import java.util.List;
import java.util.UUID;
public class OpendaylightGuestNetworkGuru extends GuestNetworkGuru {
private static final Logger s_logger = Logger.getLogger(OpendaylightGuestNetworkGuru.class);
@ -86,7 +83,7 @@ public class OpendaylightGuestNetworkGuru extends GuestNetworkGuru {
NetworkDao networkDao;
public OpendaylightGuestNetworkGuru() {
_isolationMethods = new IsolationMethod[] {IsolationMethod.ODL};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("ODL")};
}
@Override

View File

@ -16,14 +16,6 @@
// under the License.
package com.cloud.network.guru;
import com.cloud.network.vpc.VpcVO;
import com.cloud.network.vpc.dao.VpcDao;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.context.CallContext;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
@ -45,12 +37,19 @@ import com.cloud.network.PhysicalNetwork.IsolationMethod;
import com.cloud.network.dao.NetworkVO;
import com.cloud.network.dao.PhysicalNetworkVO;
import com.cloud.network.ovs.OvsTunnelManager;
import com.cloud.network.vpc.VpcVO;
import com.cloud.network.vpc.dao.VpcDao;
import com.cloud.offering.NetworkOffering;
import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
import com.cloud.user.Account;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import javax.inject.Inject;
@Component
public class OvsGuestNetworkGuru extends GuestNetworkGuru {
@ -66,8 +65,8 @@ public class OvsGuestNetworkGuru extends GuestNetworkGuru {
OvsGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.GRE,
IsolationMethod.L3, IsolationMethod.VLAN};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("GRE"),
new IsolationMethod("L3"), new IsolationMethod("VLAN")};
}
@Override

View File

@ -16,13 +16,6 @@
// under the License.
package org.apache.cloudstack.network.guru;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.apache.cloudstack.network.element.SspElement;
import org.apache.cloudstack.network.element.SspManager;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.deploy.DeployDestination;
import com.cloud.exception.InsufficientAddressCapacityException;
@ -40,6 +33,11 @@ import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.ReservationContextImpl;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.network.element.SspElement;
import org.apache.cloudstack.network.element.SspManager;
import org.apache.log4j.Logger;
import javax.inject.Inject;
/**
* Stratosphere SDN Platform NetworkGuru
@ -56,7 +54,7 @@ public class SspGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigr
public SspGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.SSP};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("SSP")};
}
@Override

View File

@ -17,11 +17,6 @@
package com.cloud.network.guru;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.apache.cloudstack.context.CallContext;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.deploy.DeployDestination;
@ -44,6 +39,9 @@ import com.cloud.user.Account;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.context.CallContext;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@Component
public class VxlanGuestNetworkGuru extends GuestNetworkGuru {
@ -51,7 +49,7 @@ public class VxlanGuestNetworkGuru extends GuestNetworkGuru {
public VxlanGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.VXLAN};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("VXLAN")};
}
@Override

View File

@ -16,13 +16,6 @@
// under the License.
package com.cloud.network.guru;
import java.util.List;
import javax.inject.Inject;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
@ -63,6 +56,12 @@ import com.cloud.vm.NicProfile;
import com.cloud.vm.NicVO;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
import org.apache.log4j.Logger;
import javax.inject.Inject;
import java.util.List;
public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
private static final Logger s_logger = Logger.getLogger(ExternalGuestNetworkGuru.class);
@ -85,7 +84,7 @@ public class ExternalGuestNetworkGuru extends GuestNetworkGuru {
public ExternalGuestNetworkGuru() {
super();
_isolationMethods = new IsolationMethod[] {IsolationMethod.GRE, IsolationMethod.L3, IsolationMethod.VLAN};
_isolationMethods = new IsolationMethod[] {new IsolationMethod("GRE"), new IsolationMethod("L3"), new IsolationMethod("VLAN")};
}
@Override