You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by hu...@apache.org on 2014/07/21 10:50:18 UTC
[2/4] CLOUDSTACK-6845 : NuageVsp Network plugin
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java b/plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java
new file mode 100644
index 0000000..fed970e
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/com/cloud/network/manager/NuageVspManagerImpl.java
@@ -0,0 +1,353 @@
+//
+// 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.manager;
+
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+import javax.ejb.Local;
+import javax.inject.Inject;
+import javax.naming.ConfigurationException;
+
+import org.apache.cloudstack.framework.config.ConfigKey;
+import org.apache.cloudstack.framework.config.Configurable;
+import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
+import org.apache.cloudstack.network.ExternalNetworkDeviceManager;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.log4j.Logger;
+
+import com.cloud.api.ApiDBUtils;
+import com.cloud.api.commands.AddNuageVspDeviceCmd;
+import com.cloud.api.commands.DeleteNuageVspDeviceCmd;
+import com.cloud.api.commands.IssueNuageVspResourceRequestCmd;
+import com.cloud.api.commands.ListNuageVspDevicesCmd;
+import com.cloud.api.response.NuageVspDeviceResponse;
+import com.cloud.exception.InvalidParameterValueException;
+import com.cloud.host.DetailVO;
+import com.cloud.host.Host;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.host.dao.HostDetailsDao;
+import com.cloud.network.Network;
+import com.cloud.network.Networks;
+import com.cloud.network.NuageVspDeviceVO;
+import com.cloud.network.PhysicalNetwork;
+import com.cloud.network.PhysicalNetworkServiceProvider;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.network.dao.NuageVspDao;
+import com.cloud.network.dao.PhysicalNetworkDao;
+import com.cloud.network.dao.PhysicalNetworkServiceProviderDao;
+import com.cloud.network.dao.PhysicalNetworkServiceProviderVO;
+import com.cloud.network.dao.PhysicalNetworkVO;
+import com.cloud.network.resource.NuageVspResource;
+import com.cloud.network.sync.NuageVspSync;
+import com.cloud.network.vpc.dao.VpcDao;
+import com.cloud.network.vpc.dao.VpcOfferingDao;
+import com.cloud.network.vpc.dao.VpcOfferingServiceMapDao;
+import com.cloud.resource.ResourceManager;
+import com.cloud.resource.ResourceState;
+import com.cloud.resource.ServerResource;
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.db.Transaction;
+import com.cloud.utils.db.TransactionCallback;
+import com.cloud.utils.db.TransactionStatus;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+@Local(value = {NuageVspManager.class})
+public class NuageVspManagerImpl extends ManagerBase implements NuageVspManager, Configurable {
+
+ private static final Logger s_logger = Logger.getLogger(NuageVspManagerImpl.class);
+
+ private static final int ONE_MINUTE_MULTIPLIER = 60 * 1000;
+
+ @Inject
+ ResourceManager _resourceMgr;
+ @Inject
+ HostDetailsDao _hostDetailsDao;
+ @Inject
+ HostDao _hostDao;
+ @Inject
+ PhysicalNetworkDao _physicalNetworkDao;
+ @Inject
+ PhysicalNetworkServiceProviderDao _physicalNetworkServiceProviderDao;
+ @Inject
+ ConfigurationDao _configDao;
+ @Inject
+ NuageVspDao _nuageVspDao;
+ @Inject
+ NetworkDao _networkDao;
+ @Inject
+ VpcOfferingDao _vpcOffDao;
+ @Inject
+ VpcOfferingServiceMapDao _vpcOffSvcMapDao;
+ @Inject
+ VpcDao _vpcDao;
+ @Inject
+ NuageVspDao nuageVspDao;
+ @Inject
+ NuageVspSync nuageVspSync;
+
+ private ScheduledExecutorService scheduler;
+
+ @Override
+ public List<Class<?>> getCommands() {
+ List<Class<?>> cmdList = new ArrayList<Class<?>>();
+ cmdList.add(AddNuageVspDeviceCmd.class);
+ cmdList.add(DeleteNuageVspDeviceCmd.class);
+ cmdList.add(ListNuageVspDevicesCmd.class);
+ cmdList.add(IssueNuageVspResourceRequestCmd.class);
+
+ return cmdList;
+ }
+
+ @Override
+ public NuageVspDeviceVO addNuageVspDevice(AddNuageVspDeviceCmd cmd) {
+ ServerResource resource = new NuageVspResource();
+ final String deviceName = Network.Provider.NuageVsp.getName();
+ ExternalNetworkDeviceManager.NetworkDevice networkDevice = ExternalNetworkDeviceManager.NetworkDevice.getNetworkDevice(deviceName);
+ final Long physicalNetworkId = cmd.getPhysicalNetworkId();
+ PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
+ if (physicalNetwork == null) {
+ throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
+ }
+ long zoneId = physicalNetwork.getDataCenterId();
+
+ final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(),
+ networkDevice.getNetworkServiceProvder());
+ if (ntwkSvcProvider == null) {
+ throw new CloudRuntimeException("Network Service Provider: " + networkDevice.getNetworkServiceProvder() + " is not added in the physical network: " + physicalNetworkId
+ + "to add this device");
+ } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
+ throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is in shutdown state in the physical network: "
+ + physicalNetworkId + "to add this device");
+ }
+
+ if (_nuageVspDao.listByPhysicalNetwork(physicalNetworkId).size() != 0) {
+ throw new CloudRuntimeException("A NuageVsp device is already configured on this physical network");
+ }
+
+ Map<String, String> params = new HashMap<String, String>();
+ params.put("guid", UUID.randomUUID().toString());
+ params.put("zoneId", String.valueOf(physicalNetwork.getDataCenterId()));
+ params.put("physicalNetworkId", String.valueOf(physicalNetwork.getId()));
+ params.put("name", "Nuage VSD - " + cmd.getHostName());
+ params.put("hostname", cmd.getHostName());
+ params.put("cmsuser", cmd.getUserName());
+ String cmsUserPasswordBase64 = org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.encodeBase64(cmd.getPassword().getBytes(Charset.forName("UTF-8"))));
+ params.put("cmsuserpass", cmsUserPasswordBase64);
+ int port = cmd.getPort();
+ if (0 == port) {
+ port = 443;
+ }
+ params.put("port", String.valueOf(port));
+ params.put("apirelativepath", "/nuage/api/" + cmd.getApiVersion());
+ params.put("retrycount", String.valueOf(cmd.getApiRetryCount()));
+ params.put("retryinterval", String.valueOf(cmd.getApiRetryInterval()));
+
+ Map<String, Object> hostdetails = new HashMap<String, Object>();
+ hostdetails.putAll(params);
+
+ try {
+ resource.configure(cmd.getHostName(), hostdetails);
+
+ final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params);
+ if (host != null) {
+ return Transaction.execute(new TransactionCallback<NuageVspDeviceVO>() {
+ @Override
+ public NuageVspDeviceVO doInTransaction(TransactionStatus status) {
+ NuageVspDeviceVO nuageVspDevice = new NuageVspDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName);
+ _nuageVspDao.persist(nuageVspDevice);
+
+ DetailVO detail = new DetailVO(host.getId(), "nuagevspdeviceid", String.valueOf(nuageVspDevice.getId()));
+ _hostDetailsDao.persist(detail);
+
+ return nuageVspDevice;
+ }
+ });
+ } else {
+ throw new CloudRuntimeException("Failed to add Nuage Vsp Device due to internal error.");
+ }
+ } catch (ConfigurationException e) {
+ throw new CloudRuntimeException(e.getMessage());
+ }
+ }
+
+ @Override
+ public NuageVspDeviceResponse createNuageVspDeviceResponse(NuageVspDeviceVO nuageVspDeviceVO) {
+ HostVO nuageVspHost = _hostDao.findById(nuageVspDeviceVO.getHostId());
+ _hostDao.loadDetails(nuageVspHost);
+
+ NuageVspDeviceResponse response = new NuageVspDeviceResponse();
+ response.setDeviceName(nuageVspDeviceVO.getDeviceName());
+ PhysicalNetwork pnw = ApiDBUtils.findPhysicalNetworkById(nuageVspDeviceVO.getPhysicalNetworkId());
+ if (pnw != null) {
+ response.setPhysicalNetworkId(pnw.getUuid());
+ }
+ response.setId(nuageVspDeviceVO.getUuid());
+ response.setProviderName(nuageVspDeviceVO.getProviderName());
+ response.setHostName(nuageVspHost.getDetail("hostname"));
+ response.setPort(Integer.parseInt(nuageVspHost.getDetail("port")));
+ String apiRelativePath = nuageVspHost.getDetail("apirelativepath");
+ response.setApiVersion(apiRelativePath.substring(apiRelativePath.lastIndexOf('/') + 1));
+ response.setApiRetryCount(Integer.parseInt(nuageVspHost.getDetail("retrycount")));
+ response.setApiRetryInterval(Long.parseLong(nuageVspHost.getDetail("retryinterval")));
+ response.setObjectName("nuagevspdevice");
+ return response;
+ }
+
+ @Override
+ public boolean deleteNuageVspDevice(DeleteNuageVspDeviceCmd cmd) {
+ Long nuageDeviceId = cmd.getNuageVspDeviceId();
+ NuageVspDeviceVO nuageVspDevice = _nuageVspDao.findById(nuageDeviceId);
+ if (nuageVspDevice == null) {
+ throw new InvalidParameterValueException("Could not find a Nuage Vsp device with id " + nuageDeviceId);
+ }
+
+ // Find the physical network we work for
+ Long physicalNetworkId = nuageVspDevice.getPhysicalNetworkId();
+ PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
+ if (physicalNetwork != null) {
+ // Lets see if there are networks that use us
+ // Find the NuageVsp on this physical network
+ List<NetworkVO> networkList = _networkDao.listByPhysicalNetwork(physicalNetworkId);
+
+ // Networks with broadcast type lswitch are ours
+ for (NetworkVO network : networkList) {
+ if (network.getBroadcastDomainType() == Networks.BroadcastDomainType.Vsp) {
+ if ((network.getState() != Network.State.Shutdown) && (network.getState() != Network.State.Destroy)) {
+ throw new CloudRuntimeException("This Nuage Vsp device can not be deleted as there are one or more logical networks provisioned by Cloudstack.");
+ }
+ }
+ }
+ }
+
+ HostVO nuageHost = _hostDao.findById(nuageVspDevice.getHostId());
+ Long hostId = nuageHost.getId();
+
+ nuageHost.setResourceState(ResourceState.Maintenance);
+ _hostDao.update(hostId, nuageHost);
+ _resourceMgr.deleteHost(hostId, false, false);
+
+ _nuageVspDao.remove(nuageDeviceId);
+ return true;
+ }
+
+ @Override
+ public List<NuageVspDeviceVO> listNuageVspDevices(ListNuageVspDevicesCmd cmd) {
+ Long physicalNetworkId = cmd.getPhysicalNetworkId();
+ Long nuageVspDeviceId = cmd.getNuageVspDeviceId();
+ List<NuageVspDeviceVO> responseList = new ArrayList<NuageVspDeviceVO>();
+
+ if (physicalNetworkId == null && nuageVspDeviceId == null) {
+ throw new InvalidParameterValueException("Either physical network Id or Nuage device Id must be specified");
+ }
+
+ if (nuageVspDeviceId != null) {
+ NuageVspDeviceVO nuageVspDevice = _nuageVspDao.findById(nuageVspDeviceId);
+ if (nuageVspDevice == null) {
+ throw new InvalidParameterValueException("Could not find Nuage Vsp device with id: " + nuageVspDeviceId);
+ }
+ responseList.add(nuageVspDevice);
+ } else {
+ PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
+ if (physicalNetwork == null) {
+ throw new InvalidParameterValueException("Could not find a physical network with id: " + physicalNetworkId);
+ }
+ responseList = _nuageVspDao.listByPhysicalNetwork(physicalNetworkId);
+ }
+
+ return responseList;
+ }
+
+ @Override
+ public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+ try {
+ initNuageScheduledTasks();
+ } catch (Exception ce) {
+ s_logger.warn("Failed to load NuageVsp configuration properties. Check if the NuageVsp properties are configured correctly");
+ }
+ return true;
+ }
+
+ private void initNuageScheduledTasks() {
+ Integer numOfSyncThreads = Integer.valueOf(_configDao.getValue(NuageVspManager.NuageVspSyncWorkers.key()));
+ Integer syncUpIntervalInMinutes = Integer.valueOf(_configDao.getValue(NuageVspManager.NuageVspSyncInterval.key()));
+
+ if (numOfSyncThreads != null && syncUpIntervalInMinutes != null) {
+ ThreadFactory threadFactory = new ThreadFactory() {
+ public Thread newThread(Runnable runnable) {
+ Thread thread = new Thread(runnable, "Nuage Vsp sync task");
+ if (thread.isDaemon())
+ thread.setDaemon(false);
+ if (thread.getPriority() != Thread.NORM_PRIORITY)
+ thread.setPriority(Thread.NORM_PRIORITY);
+ return thread;
+ }
+ };
+ scheduler = Executors.newScheduledThreadPool(numOfSyncThreads, threadFactory);
+ scheduler.scheduleWithFixedDelay(new NuageVspSyncTask("FLOATING_IP"), ONE_MINUTE_MULTIPLIER * 15, ONE_MINUTE_MULTIPLIER * syncUpIntervalInMinutes,
+ TimeUnit.MILLISECONDS);
+ scheduler.scheduleWithFixedDelay(new NuageVspSyncTask("ENTERPRISE_NTWK_MACRO"), ONE_MINUTE_MULTIPLIER * 15, ONE_MINUTE_MULTIPLIER * syncUpIntervalInMinutes,
+ TimeUnit.MILLISECONDS);
+ scheduler
+ .scheduleWithFixedDelay(new NuageVspSyncTask("ENTERPRISE"), ONE_MINUTE_MULTIPLIER * 15, ONE_MINUTE_MULTIPLIER * syncUpIntervalInMinutes, TimeUnit.MILLISECONDS);
+ } else {
+ s_logger.warn("NuageVsp configuration for syncWorkers=" + numOfSyncThreads + " syncInterval=" + syncUpIntervalInMinutes
+ + " could not be read properly. So, check if the properties are configured properly in global properties");
+ }
+ }
+
+ public class NuageVspSyncTask implements Runnable {
+
+ private String nuageVspEntity;
+
+ public NuageVspSyncTask(String nuageVspEntity) {
+ this.nuageVspEntity = nuageVspEntity;
+ }
+
+ public String getNuageVspEntity() {
+ return nuageVspEntity;
+ }
+
+ @Override
+ public void run() {
+ nuageVspSync.syncWithNuageVsp(nuageVspEntity);
+ }
+ }
+
+ @Override
+ public String getConfigComponentName() {
+ return NuageVspManager.class.getSimpleName();
+ }
+
+ @Override
+ public ConfigKey<?>[] getConfigKeys() {
+ return new ConfigKey<?>[] {NuageVspSyncWorkers, NuageVspSyncInterval};
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/com/cloud/network/resource/NuageVspResource.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/com/cloud/network/resource/NuageVspResource.java b/plugins/network-elements/nuage-vsp/src/com/cloud/network/resource/NuageVspResource.java
new file mode 100644
index 0000000..65c8ae9
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/com/cloud/network/resource/NuageVspResource.java
@@ -0,0 +1,467 @@
+//
+// 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.resource;
+
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+import javax.naming.ConfigurationException;
+
+import net.nuage.vsp.acs.NuageVspPluginClientLoader;
+import net.nuage.vsp.acs.client.NuageVspApiClient;
+import net.nuage.vsp.acs.client.NuageVspElementClient;
+import net.nuage.vsp.acs.client.NuageVspGuruClient;
+import net.nuage.vsp.acs.client.NuageVspSyncClient;
+
+import org.apache.commons.codec.binary.Base64;
+import org.apache.log4j.Logger;
+
+import com.cloud.agent.IAgentControl;
+import com.cloud.agent.api.Answer;
+import com.cloud.agent.api.Command;
+import com.cloud.agent.api.MaintainAnswer;
+import com.cloud.agent.api.MaintainCommand;
+import com.cloud.agent.api.PingCommand;
+import com.cloud.agent.api.ReadyAnswer;
+import com.cloud.agent.api.ReadyCommand;
+import com.cloud.agent.api.StartupCommand;
+import com.cloud.agent.api.StartupVspCommand;
+import com.cloud.agent.api.VspResourceAnswer;
+import com.cloud.agent.api.VspResourceCommand;
+import com.cloud.agent.api.element.ApplyAclRuleVspAnswer;
+import com.cloud.agent.api.element.ApplyAclRuleVspCommand;
+import com.cloud.agent.api.element.ApplyStaticNatVspAnswer;
+import com.cloud.agent.api.element.ApplyStaticNatVspCommand;
+import com.cloud.agent.api.element.ShutDownVpcVspAnswer;
+import com.cloud.agent.api.element.ShutDownVpcVspCommand;
+import com.cloud.agent.api.guru.DeallocateVmVspAnswer;
+import com.cloud.agent.api.guru.DeallocateVmVspCommand;
+import com.cloud.agent.api.guru.ImplementNetworkVspAnswer;
+import com.cloud.agent.api.guru.ImplementNetworkVspCommand;
+import com.cloud.agent.api.guru.ReleaseVmVspAnswer;
+import com.cloud.agent.api.guru.ReleaseVmVspCommand;
+import com.cloud.agent.api.guru.ReserveVmInterfaceVspAnswer;
+import com.cloud.agent.api.guru.ReserveVmInterfaceVspCommand;
+import com.cloud.agent.api.guru.TrashNetworkVspAnswer;
+import com.cloud.agent.api.guru.TrashNetworkVspCommand;
+import com.cloud.agent.api.sync.SyncVspAnswer;
+import com.cloud.agent.api.sync.SyncVspCommand;
+import com.cloud.host.Host;
+import com.cloud.resource.ServerResource;
+import com.cloud.utils.component.ManagerBase;
+import com.cloud.utils.exception.CloudRuntimeException;
+
+public class NuageVspResource extends ManagerBase implements ServerResource {
+ private static final Logger s_logger = Logger.getLogger(NuageVspResource.class);
+
+ private String _name;
+ private String _guid;
+ private String _zoneId;
+ private String[] _cmsUserInfo;
+ private String _relativePath;
+ private int _numRetries;
+ private int _retryInterval;
+
+ protected NuageVspApiClient _nuageVspApiClient;
+ protected NuageVspGuruClient _nuageVspGuruClient;
+ protected NuageVspElementClient _nuageVspElementClient;
+ protected NuageVspSyncClient _nuageVspSyncClient;
+ protected boolean _isNuageVspClientLoaded;
+
+ private static final String CMS_USER_ENTEPRISE_NAME = "CSP";
+ private static final String NUAGE_VSP_PLUGIN_ERROR_MESSAGE = "Nuage Vsp plugin client is not installed";
+ private static final String NUAGE_PLUGIN_CLIENT_JAR_FILE = "/usr/share/nuagevsp/lib/nuage-vsp-acs-client.jar";
+ private static final String NUAGE_VSP_API_CLIENT_IMPL = "net.nuage.vsp.acs.client.impl.NuageVspApiClientImpl";
+ private static final String NUAGE_VSP_SYNC_CLIENT_IMPL = "net.nuage.vsp.acs.client.impl.NuageVspSyncClientImpl";
+ private static final String NUAGE_VSP_ELEMENT_CLIENT_IMPL = "net.nuage.vsp.acs.client.impl.NuageVspElementClientImpl";
+ private static final String NUAGE_VSP_GURU_CLIENT_IMPL = "net.nuage.vsp.acs.client.impl.NuageVspGuruClientImpl";
+
+ @Override
+ public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
+
+ _name = (String)params.get("name");
+ if (_name == null) {
+ throw new ConfigurationException("Unable to find name");
+ }
+
+ _guid = (String)params.get("guid");
+ if (_guid == null) {
+ throw new ConfigurationException("Unable to find the guid");
+ }
+
+ _zoneId = (String)params.get("zoneId");
+ if (_zoneId == null) {
+ throw new ConfigurationException("Unable to find zone");
+ }
+
+ String hostname = (String)params.get("hostname");
+ if (hostname == null) {
+ throw new ConfigurationException("Unable to find hostname");
+ }
+
+ String cmsUser = (String)params.get("cmsuser");
+ if (cmsUser == null) {
+ throw new ConfigurationException("Unable to find CMS username");
+ }
+
+ String cmsUserPassBase64 = (String)params.get("cmsuserpass");
+ if (cmsUserPassBase64 == null) {
+ throw new ConfigurationException("Unable to find CMS password");
+ }
+
+ String port = (String)params.get("port");
+ if (port == null) {
+ throw new ConfigurationException("Unable to find port");
+ }
+
+ String apiRelativePath = (String)params.get("apirelativepath");
+ if ((apiRelativePath != null) && (!apiRelativePath.isEmpty())) {
+ String apiVersion = apiRelativePath.substring(apiRelativePath.lastIndexOf('/') + 1);
+ if (!Pattern.matches("v\\d+_\\d+", apiVersion)) {
+ throw new ConfigurationException("Incorrect API version");
+ }
+ } else {
+ throw new ConfigurationException("Unable to find API version");
+ }
+
+ String retryCount = (String)params.get("retrycount");
+ if ((retryCount != null) && (!retryCount.isEmpty())) {
+ try {
+ _numRetries = Integer.parseInt(retryCount);
+ } catch (NumberFormatException ex) {
+ throw new ConfigurationException("Number of retries has to be between 1 and 10");
+ }
+ if ((_numRetries < 1) || (_numRetries > 10)) {
+ throw new ConfigurationException("Number of retries has to be between 1 and 10");
+ }
+ } else {
+ throw new ConfigurationException("Unable to find number of retries");
+ }
+
+ String retryInterval = (String)params.get("retryinterval");
+ if ((retryInterval != null) && (!retryInterval.isEmpty())) {
+ try {
+ _retryInterval = Integer.parseInt(retryInterval);
+ } catch (NumberFormatException ex) {
+ throw new ConfigurationException("Retry interval has to be between 0 and 10000 ms");
+ }
+ if ((_retryInterval < 0) || (_retryInterval > 10000)) {
+ throw new ConfigurationException("Retry interval has to be between 0 and 10000 ms");
+ }
+ } else {
+ throw new ConfigurationException("Unable to find retry interval");
+ }
+
+ _relativePath = new StringBuffer().append("https://").append(hostname).append(":").append(port).append(apiRelativePath).toString();
+
+ String cmsUserPass = org.apache.commons.codec.binary.StringUtils.newStringUtf8(Base64.decodeBase64(cmsUserPassBase64));
+ _cmsUserInfo = new String[] {CMS_USER_ENTEPRISE_NAME, cmsUser, cmsUserPass};
+
+ try {
+ loadNuageClient();
+ } catch (Exception e) {
+ throw new CloudRuntimeException("Failed to login to Nuage VSD on " + name + " as user " + cmsUser, e);
+ }
+
+ try {
+ login();
+ } catch (Exception e) {
+ s_logger.error("Failed to login to Nuage VSD on " + name + " as user " + cmsUser + " Exception " + e.getMessage());
+ throw new CloudRuntimeException("Failed to login to Nuage VSD on " + name + " as user " + cmsUser, e);
+ }
+
+ return true;
+ }
+
+ protected void login() throws Exception {
+ isNuageVspApiLoaded();
+ _nuageVspApiClient.login();
+ }
+
+ protected <A extends NuageVspApiClient, B extends NuageVspElementClient, C extends NuageVspSyncClient, D extends NuageVspGuruClient> void loadNuageClient() throws Exception {
+
+ try {
+ ClassLoader loader = NuageVspPluginClientLoader.getClassLoader(NUAGE_PLUGIN_CLIENT_JAR_FILE);
+
+ Class<?> nuageVspApiClientClass = Class.forName(NUAGE_VSP_API_CLIENT_IMPL, true, loader);
+ Class<?> nuageVspSyncClientClass = Class.forName(NUAGE_VSP_SYNC_CLIENT_IMPL, true, loader);
+ Class<?> nuageVspGuruClientClass = Class.forName(NUAGE_VSP_GURU_CLIENT_IMPL, true, loader);
+ Class<?> nuageVspElementClientClass = Class.forName(NUAGE_VSP_ELEMENT_CLIENT_IMPL, true, loader);
+
+ //Instantiate the instances
+ _nuageVspApiClient = (NuageVspApiClient)nuageVspApiClientClass.newInstance();
+ _nuageVspApiClient.setNuageVspHost(_relativePath, _cmsUserInfo, _numRetries, _retryInterval);
+ _nuageVspSyncClient = (NuageVspSyncClient)nuageVspSyncClientClass.newInstance();
+ _nuageVspSyncClient.setNuageVspApiClient(_nuageVspApiClient);
+ _nuageVspGuruClient = (NuageVspGuruClient)nuageVspGuruClientClass.newInstance();
+ _nuageVspGuruClient.setNuageVspApiClient(_nuageVspApiClient);
+ _nuageVspElementClient = (NuageVspElementClient)nuageVspElementClientClass.newInstance();
+ _nuageVspElementClient.setNuageVspApiClient(_nuageVspApiClient);
+ _isNuageVspClientLoaded = true;
+ } catch (Exception e) {
+ _isNuageVspClientLoaded = false;
+ String errorMessage = "Nuage Vsp Plugin client is not yet installed. Please install NuageVsp plugin client to use NuageVsp plugin in Cloudstack. ";
+ s_logger.warn(errorMessage + e.getMessage());
+ throw new Exception(errorMessage);
+ }
+
+ }
+
+ @Override
+ public boolean start() {
+ return true;
+ }
+
+ @Override
+ public boolean stop() {
+ return true;
+ }
+
+ @Override
+ public String getName() {
+ return _name;
+ }
+
+ @Override
+ public Host.Type getType() {
+ return Host.Type.L2Networking;
+ }
+
+ @Override
+ public StartupCommand[] initialize() {
+ StartupVspCommand sc = new StartupVspCommand();
+ sc.setGuid(_guid);
+ sc.setName(_name);
+ sc.setDataCenter(_zoneId);
+ sc.setPod("");
+ sc.setPrivateIpAddress("");
+ sc.setStorageIpAddress("");
+ sc.setVersion(NuageVspResource.class.getPackage().getImplementationVersion());
+ return new StartupCommand[] {sc};
+ }
+
+ @Override
+ public PingCommand getCurrentStatus(long id) {
+ if ((_relativePath == null) || (_relativePath.isEmpty()) || (_cmsUserInfo == null) || (_cmsUserInfo.length == 0)) {
+ s_logger.error("Failed to ping to Nuage VSD");
+ return null;
+ }
+ try {
+ login();
+ } catch (Exception e) {
+ s_logger.error("Failed to ping to Nuage VSD on " + _name + " as user " + _cmsUserInfo[1] + " Exception " + e.getMessage());
+ return null;
+ }
+ return new PingCommand(Host.Type.L2Networking, id);
+ }
+
+ @Override
+ public Answer executeRequest(Command cmd) {
+ if (cmd instanceof ReadyCommand) {
+ return executeRequest((ReadyCommand)cmd);
+ } else if (cmd instanceof MaintainCommand) {
+ return executeRequest((MaintainCommand)cmd);
+ } else if (cmd instanceof VspResourceCommand) {
+ return executeRequest((VspResourceCommand)cmd);
+ }
+ //Guru commands
+ else if (cmd instanceof ImplementNetworkVspCommand) {
+ return executeRequest((ImplementNetworkVspCommand)cmd);
+ } else if (cmd instanceof ReserveVmInterfaceVspCommand) {
+ return executeRequest((ReserveVmInterfaceVspCommand)cmd);
+ } else if (cmd instanceof ReleaseVmVspCommand) {
+ return executeRequest((ReleaseVmVspCommand)cmd);
+ } else if (cmd instanceof DeallocateVmVspCommand) {
+ return executeRequest((DeallocateVmVspCommand)cmd);
+ } else if (cmd instanceof TrashNetworkVspCommand) {
+ return executeRequest((TrashNetworkVspCommand)cmd);
+ }
+ //Element commands
+ else if (cmd instanceof ApplyAclRuleVspCommand) {
+ return executeRequest((ApplyAclRuleVspCommand)cmd);
+ } else if (cmd instanceof ApplyStaticNatVspCommand) {
+ return executeRequest((ApplyStaticNatVspCommand)cmd);
+ } else if (cmd instanceof ShutDownVpcVspCommand) {
+ return executeRequest((ShutDownVpcVspCommand)cmd);
+ }
+ //Sync Commands
+ else if (cmd instanceof SyncVspCommand) {
+ return executeRequest((SyncVspCommand)cmd);
+ }
+ s_logger.debug("Received unsupported command " + cmd.toString());
+ return Answer.createUnsupportedCommandAnswer(cmd);
+ }
+
+ @Override
+ public void disconnected() {
+ }
+
+ @Override
+ public IAgentControl getAgentControl() {
+ return null;
+ }
+
+ @Override
+ public void setAgentControl(IAgentControl agentControl) {
+ }
+
+ private Answer executeRequest(ReadyCommand cmd) {
+ return new ReadyAnswer(cmd);
+ }
+
+ private Answer executeRequest(MaintainCommand cmd) {
+ return new MaintainAnswer(cmd);
+ }
+
+ private Answer executeRequest(VspResourceCommand cmd) {
+ try {
+ isNuageVspApiLoaded();
+ if (cmd.getRequestType().equalsIgnoreCase("GETALL") || cmd.getRequestType().equalsIgnoreCase("GET") || cmd.getRequestType().equalsIgnoreCase("GETRELATED")) {
+ String resourceInfo = _nuageVspApiClient.executeRestApi(cmd.getRequestType(), cmd.getResource(), cmd.getResourceId(), cmd.getChildResource(),
+ cmd.getEntityDetails(), cmd.getResourceFilter(), cmd.getProxyUserUuid(), cmd.getProxyUserDomainuuid());
+ return new VspResourceAnswer(cmd, resourceInfo, "Executed Issue Resource command");
+ }
+ return new VspResourceAnswer(cmd, false, cmd.getRequestType() + " is not yet supported");
+ } catch (Exception e) {
+ return new VspResourceAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ImplementNetworkVspCommand cmd) {
+ try {
+ isNuageVspGuruLoaded();
+ _nuageVspGuruClient.implement(cmd.getNetworkDomainName(), cmd.getNetworkDomainPath(), cmd.getNetworkDomainUuid(), cmd.getNetworkAccountName(),
+ cmd.getNetworkAccountUuid(), cmd.getNetworkName(), cmd.getNetworkCidr(), cmd.getNetworkGateway(), cmd.getNetworkUuid(), cmd.isL3Network(), cmd.getVpcName(),
+ cmd.getVpcUuid(), cmd.isDefaultEgressPolicy(), cmd.getIpAddressRange());
+ return new ImplementNetworkVspAnswer(cmd, true, "Created Nuage VSP network mapping to " + cmd.getNetworkName());
+ } catch (Exception e) {
+ return new ImplementNetworkVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ReserveVmInterfaceVspCommand cmd) {
+ try {
+ isNuageVspGuruLoaded();
+ List<Map<String, String>> vmInterfaceInfo = _nuageVspGuruClient.reserve(cmd.getNicUuid(), cmd.getNicMacAddress(), cmd.getNetworkUuid(), cmd.isL3Network(),
+ cmd.getVpcUuid(), cmd.getNetworkDomainUuid(), cmd.getNetworksAccountUuid(), cmd.isDomainRouter(), cmd._getDomainRouterIp(), cmd._getVmInstanceName(),
+ cmd._getVmUuid());
+ return new ReserveVmInterfaceVspAnswer(cmd, vmInterfaceInfo, "Created NIC in VSP that maps to nicUuid" + cmd.getNicUuid());
+ } catch (Exception e) {
+ return new ReserveVmInterfaceVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ReleaseVmVspCommand cmd) {
+ try {
+ isNuageVspGuruLoaded();
+ _nuageVspGuruClient.release(cmd.getNetworkUuid(), cmd.getVmUuid(), cmd.getVmInstanceName());
+ return new ReleaseVmVspAnswer(cmd, true, "VM has been deleted from VSP.");
+ } catch (Exception e) {
+ return new ReleaseVmVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(DeallocateVmVspCommand cmd) {
+ try {
+ isNuageVspGuruLoaded();
+ _nuageVspGuruClient.deallocate(cmd.getNetworkUuid(), cmd.getNicFrmDdUuid(), cmd.getNicMacAddress(), cmd.getNicIp4Address(), cmd.isL3Network(), cmd.getVpcUuid(),
+ cmd.getNetworksDomainUuid(), cmd.getVmInstanceName(), cmd.getVmUuid());
+ return new DeallocateVmVspAnswer(cmd, true, "Deallocated VM from Nuage VSP.");
+ } catch (Exception e) {
+ return new DeallocateVmVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(TrashNetworkVspCommand cmd) {
+ try {
+ isNuageVspGuruLoaded();
+ _nuageVspGuruClient.trash(cmd.getDomainUuid(), cmd.getNetworkUuid(), cmd.isL3Network(), cmd.getVpcUuid());
+ return new TrashNetworkVspAnswer(cmd, true, "Deleted Nuage VSP network mapping to " + cmd.getNetworkUuid());
+ } catch (Exception e) {
+ return new TrashNetworkVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ApplyStaticNatVspCommand cmd) {
+ try {
+ isNuageVspElementLoaded();
+ _nuageVspElementClient.applyStaticNats(cmd.getNetworkDomainUuid(), cmd.getVpcOrSubnetUuid(), cmd.isL3Network(), cmd.getStaticNatDetails());
+ return new ApplyStaticNatVspAnswer(cmd, true, "Applied Static NAT to VSP network mapping to " + cmd.getVpcOrSubnetUuid());
+ } catch (Exception e) {
+ return new ApplyStaticNatVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ApplyAclRuleVspCommand cmd) {
+ try {
+ isNuageVspElementLoaded();
+ _nuageVspElementClient.applyAclRules(cmd.getNetworkUuid(), cmd.getNetworkDomainUuid(), cmd.getVpcOrSubnetUuid(), cmd.isL3Network(), cmd.getAclRules(), cmd.isVpc(),
+ cmd.getNetworkId());
+ return new ApplyAclRuleVspAnswer(cmd, true, "Applied ACL Rule to VSP network mapping to " + cmd.getVpcOrSubnetUuid());
+ } catch (Exception e) {
+ return new ApplyAclRuleVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(ShutDownVpcVspCommand cmd) {
+ try {
+ isNuageVspElementLoaded();
+ _nuageVspElementClient.shutDownVpc(cmd.getDomainUuid(), cmd.getVpcUuid());
+ return new ShutDownVpcVspAnswer(cmd, true, "Shutdown VPC " + cmd.getVpcUuid());
+ } catch (Exception e) {
+ return new ShutDownVpcVspAnswer(cmd, e);
+ }
+ }
+
+ private Answer executeRequest(SyncVspCommand cmd) {
+ try {
+ isNuageVspSyncLoaded();
+ _nuageVspSyncClient.syncWithNuageVsp(cmd.getNuageVspEntity());
+ return new SyncVspAnswer(cmd, true, "Synced " + cmd.getNuageVspEntity() + " in VSP");
+ } catch (Exception e) {
+ return new SyncVspAnswer(cmd, e);
+ }
+ }
+
+ protected void isNuageVspApiLoaded() throws Exception {
+ if (!_isNuageVspClientLoaded || _nuageVspApiClient == null) {
+ throw new Exception(NUAGE_VSP_PLUGIN_ERROR_MESSAGE);
+ }
+ }
+
+ protected void isNuageVspGuruLoaded() throws Exception {
+ if (!_isNuageVspClientLoaded || _nuageVspGuruClient == null) {
+ throw new Exception(NUAGE_VSP_PLUGIN_ERROR_MESSAGE);
+ }
+ }
+
+ protected void isNuageVspElementLoaded() throws Exception {
+ if (!_isNuageVspClientLoaded || _nuageVspElementClient == null) {
+ throw new Exception(NUAGE_VSP_PLUGIN_ERROR_MESSAGE);
+ }
+ }
+
+ protected void isNuageVspSyncLoaded() throws Exception {
+ if (!_isNuageVspClientLoaded || _nuageVspSyncClient == null) {
+ throw new Exception(NUAGE_VSP_PLUGIN_ERROR_MESSAGE);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSync.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSync.java b/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSync.java
new file mode 100644
index 0000000..4e1cc43
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSync.java
@@ -0,0 +1,26 @@
+//
+// 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.sync;
+
+
+public interface NuageVspSync {
+
+ public void syncWithNuageVsp(String nuageVspEntity);
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSyncImpl.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSyncImpl.java b/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSyncImpl.java
new file mode 100644
index 0000000..f3d19c1
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/com/cloud/network/sync/NuageVspSyncImpl.java
@@ -0,0 +1,72 @@
+//
+// 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.sync;
+
+import java.util.List;
+
+import javax.inject.Inject;
+
+import org.apache.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.sync.SyncVspAnswer;
+import com.cloud.agent.api.sync.SyncVspCommand;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.network.NuageVspDeviceVO;
+import com.cloud.network.dao.NuageVspDao;
+
+@Component
+public class NuageVspSyncImpl implements NuageVspSync {
+
+ private static final Logger s_logger = Logger.getLogger(NuageVspSyncImpl.class);
+
+ @Inject
+ NuageVspDao _nuageVspDao;
+ @Inject
+ HostDao _hostDao;
+ @Inject
+ AgentManager _agentMgr;
+
+ public void syncWithNuageVsp(String nuageVspEntity) {
+ //Get the NuageVspDevice and get the host information.
+ //This information is used to query VSP and synch the corresponding
+ //entities
+ List<NuageVspDeviceVO> nuageVspDevices = _nuageVspDao.listAll();
+ for (NuageVspDeviceVO nuageVspDevice : nuageVspDevices) {
+
+ try {
+ HostVO nuageVspHost = _hostDao.findById(nuageVspDevice.getHostId());
+ _hostDao.loadDetails(nuageVspHost);
+ SyncVspCommand cmd = new SyncVspCommand(nuageVspEntity);
+ SyncVspAnswer answer = (SyncVspAnswer)_agentMgr.easySend(nuageVspHost.getId(), cmd);
+ if (answer == null || !answer.getResult()) {
+ s_logger.error("SyncNuageVspCommand for Nuage VSP Host " + nuageVspHost.getUuid() + " failed");
+ if ((null != answer) && (null != answer.getDetails())) {
+ s_logger.error(answer.getDetails());
+ }
+ }
+ } catch (Exception e) {
+ s_logger.warn("Failed to clean up " + nuageVspEntity + " in Vsp " + e.getMessage());
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/NuageVspPluginClientLoader.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/NuageVspPluginClientLoader.java b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/NuageVspPluginClientLoader.java
new file mode 100644
index 0000000..76b9823
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/NuageVspPluginClientLoader.java
@@ -0,0 +1,46 @@
+//
+// 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 net.nuage.vsp.acs;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+
+public class NuageVspPluginClientLoader {
+
+ private static NuageVspPluginClientLoader nuageVspPluginClientClassloader;
+ private ClassLoader loader = null;
+
+ private NuageVspPluginClientLoader(String nuagePluginClientJarLocation) {
+ try {
+ loader = URLClassLoader.newInstance(new URL[] {new URL("jar:file:" + nuagePluginClientJarLocation + "!/")},
+ getClass().getClassLoader());
+ } catch (MalformedURLException e) {
+ e.printStackTrace();
+ }
+ }
+
+ public static ClassLoader getClassLoader(String nuagePluginClientJarLocation) {
+ if (nuageVspPluginClientClassloader == null) {
+ nuageVspPluginClientClassloader = new NuageVspPluginClientLoader(nuagePluginClientJarLocation);
+ }
+ return nuageVspPluginClientClassloader.loader;
+ }
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspApiClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspApiClient.java b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspApiClient.java
new file mode 100644
index 0000000..d584c6f
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspApiClient.java
@@ -0,0 +1,30 @@
+//
+// 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 net.nuage.vsp.acs.client;
+
+public interface NuageVspApiClient {
+
+ public void login() throws Exception;
+
+ public void setNuageVspHost(String restRelativePath, String[] cmsUserInfo, int noofRetry, int retryInterval);
+
+ public String executeRestApi(String method, String resource, String resourceId, String childResource, Object entityDetails, String resourceFilter, String proxyUserUuid,
+ String proxyUserDomainuuid) throws Exception;
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspElementClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspElementClient.java b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspElementClient.java
new file mode 100644
index 0000000..27c613b
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspElementClient.java
@@ -0,0 +1,36 @@
+//
+// 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 net.nuage.vsp.acs.client;
+
+import java.util.List;
+import java.util.Map;
+
+public interface NuageVspElementClient {
+
+ public void applyStaticNats(String networkDomainUuid, String vpcOrSubnetUuid, boolean isL3Network, List<Map<String, Object>> staticNatDetails) throws Exception;
+
+ public void applyAclRules(String networkUuid, String networkDomainUuid, String vpcOrSubnetUuid, boolean isL3Network, List<Map<String, Object>> aclRules, boolean isVpc, long networkId)
+ throws Exception;
+
+ public void shutDownVpc(String domainUuid, String vpcUuid) throws Exception;
+
+ public <C extends NuageVspApiClient> void setNuageVspApiClient(NuageVspApiClient nuageVspApiClient);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspGuruClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspGuruClient.java b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspGuruClient.java
new file mode 100644
index 0000000..3c7f33d
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspGuruClient.java
@@ -0,0 +1,44 @@
+//
+// 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 net.nuage.vsp.acs.client;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+public interface NuageVspGuruClient {
+
+ public void implement(String networkDomainName, String networkDomainPath, String networkDomainUuid, String networkAccountName, String networkAccountUuid, String networkName,
+ String networkCidr, String networkGateway, String networkUuid, boolean isL3Network, String vpcName, String vpcUuid, boolean defaultEgressPolicy,
+ Collection<String> ipAddressRange) throws Exception;
+
+ public List<Map<String, String>> reserve(String nicUuid, String nicMacAddress, String networkUuid, boolean isL3Network, String vpcUuid, String networkDomainUuid,
+ String networksAccountUuid, boolean isDomainRouter, String domainRouterIp, String vmInstanceName, String vmUuid) throws Exception;
+
+ public void release(String networkUuid, String vmUuid, String vmInstanceName) throws Exception;
+
+ public void deallocate(String networkUuid, String nicFrmDdUuid, String nicMacAddress, String nicIp4Address, boolean isL3Network, String vpcUuid, String networksDomainUuid,
+ String vmInstanceName, String vmUuid) throws Exception;
+
+ public void trash(String domainUuid, String networkUuid, boolean isL3Network, String vpcUuid) throws Exception;
+
+ public void setNuageVspApiClient(NuageVspApiClient nuageVspApiClient);
+
+}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspSyncClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspSyncClient.java b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspSyncClient.java
new file mode 100644
index 0000000..b9400f1
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/src/net/nuage/vsp/acs/client/NuageVspSyncClient.java
@@ -0,0 +1,27 @@
+//
+// 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 net.nuage.vsp.acs.client;
+
+public interface NuageVspSyncClient {
+
+ public void syncWithNuageVsp(String nuageVspEntity) throws Exception;
+
+ public void setNuageVspApiClient(NuageVspApiClient nuageVspApiClient);
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/test/com/cloud/network/element/NuageVspElementTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/test/com/cloud/network/element/NuageVspElementTest.java b/plugins/network-elements/nuage-vsp/test/com/cloud/network/element/NuageVspElementTest.java
new file mode 100644
index 0000000..08d685f
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/test/com/cloud/network/element/NuageVspElementTest.java
@@ -0,0 +1,280 @@
+//
+// 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.element;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.naming.ConfigurationException;
+
+import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Command;
+import com.cloud.agent.api.element.ApplyAclRuleVspAnswer;
+import com.cloud.agent.api.element.ApplyStaticNatVspAnswer;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.domain.Domain;
+import com.cloud.domain.DomainVO;
+import com.cloud.domain.dao.DomainDao;
+import com.cloud.exception.CloudException;
+import com.cloud.exception.ConcurrentOperationException;
+import com.cloud.exception.InsufficientCapacityException;
+import com.cloud.exception.ResourceUnavailableException;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.network.Network.Provider;
+import com.cloud.network.Network.Service;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.NuageVspDeviceVO;
+import com.cloud.network.dao.NetworkServiceMapDao;
+import com.cloud.network.dao.NuageVspDao;
+import com.cloud.network.rules.FirewallRule;
+import com.cloud.network.rules.StaticNat;
+import com.cloud.network.vpc.NetworkACLItem;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.NetworkOfferingVO;
+import com.cloud.offerings.dao.NetworkOfferingDao;
+import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
+import com.cloud.resource.ResourceManager;
+import com.cloud.user.Account;
+import com.cloud.vm.ReservationContext;
+
+public class NuageVspElementTest {
+
+ private static final long NETWORK_ID = 42L;
+ NuageVspElement element = new NuageVspElement();
+ NetworkOrchestrationService networkManager = mock(NetworkOrchestrationService.class);
+ NetworkModel networkModel = mock(NetworkModel.class);
+ NetworkServiceMapDao ntwkSrvcDao = mock(NetworkServiceMapDao.class);
+ AgentManager agentManager = mock(AgentManager.class);
+ HostDao hostDao = mock(HostDao.class);
+ NuageVspDao nuageVspDao = mock(NuageVspDao.class);
+ DomainDao domainDao = mock(DomainDao.class);
+ NetworkOfferingDao ntwkOfferingDao = mock(NetworkOfferingDao.class);
+ NetworkOfferingServiceMapDao ntwkOfferingSrvcDao = mock(NetworkOfferingServiceMapDao.class);
+
+ Answer<Object> genericAnswer = new Answer<Object>() {
+ public Object answer(InvocationOnMock invocation) {
+ return null;
+ }
+ };
+
+ @Before
+ public void setUp() throws ConfigurationException {
+ element._resourceMgr = mock(ResourceManager.class);
+ element._ntwkSrvcDao = ntwkSrvcDao;
+ element._networkModel = networkModel;
+ element._agentMgr = agentManager;
+ element._hostDao = hostDao;
+ element._nuageVspDao = nuageVspDao;
+ element._ntwkOfferingSrvcDao = ntwkOfferingSrvcDao;
+ element._domainDao = domainDao;
+ element._ntwkOfferingDao = ntwkOfferingDao;
+
+ // Standard responses
+ when(networkModel.isProviderForNetwork(Provider.NuageVsp, NETWORK_ID)).thenReturn(true);
+
+ element.configure("NuageVspTestElement", Collections.<String, Object> emptyMap());
+ }
+
+ @Test
+ public void testCcanHandle() {
+ final Network net = mock(Network.class);
+ when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vsp);
+ when(net.getId()).thenReturn(NETWORK_ID);
+
+ when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NuageVsp)).thenReturn(true);
+ // Golden path
+ assertTrue(element.canHandle(net, Service.Connectivity));
+
+ when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vlan);
+ // Only broadcastdomaintype Vsp is supported
+ assertFalse(element.canHandle(net, Service.Connectivity));
+
+ when(net.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vsp);
+ when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NuageVsp)).thenReturn(false);
+ // No NuageVsp provider in the network
+ assertFalse(element.canHandle(net, Service.Connectivity));
+
+ when(networkModel.isProviderForNetwork(Provider.NuageVsp, NETWORK_ID)).thenReturn(false);
+ when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NuageVsp)).thenReturn(true);
+ // NusageVsp provider does not provide Connectivity for this network
+ assertFalse(element.canHandle(net, Service.Connectivity));
+
+ when(networkModel.isProviderForNetwork(Provider.NuageVsp, NETWORK_ID)).thenReturn(true);
+ // Only service Connectivity is supported
+ assertFalse(element.canHandle(net, Service.Dhcp));
+
+ }
+
+ @Test
+ public void testImplement() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException, URISyntaxException {
+ final Network network = mock(Network.class);
+ when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vsp);
+ when(network.getId()).thenReturn(NETWORK_ID);
+ when(network.getBroadcastUri()).thenReturn(new URI(""));
+ when(networkModel.isProviderForNetwork(Provider.NuageVsp, NETWORK_ID)).thenReturn(true);
+ when(ntwkSrvcDao.canProviderSupportServiceInNetwork(NETWORK_ID, Service.Connectivity, Provider.NuageVsp)).thenReturn(true);
+
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+
+ DeployDestination deployDest = mock(DeployDestination.class);
+
+ final Domain dom = mock(Domain.class);
+ when(dom.getName()).thenReturn("domain");
+ final Account acc = mock(Account.class);
+ when(acc.getAccountName()).thenReturn("accountname");
+ final ReservationContext context = mock(ReservationContext.class);
+ when(context.getDomain()).thenReturn(dom);
+ when(context.getAccount()).thenReturn(acc);
+
+ assertTrue(element.implement(network, offering, deployDest, context));
+ }
+
+ @Test
+ public void testVerifyServiceCombination() {
+ Set<Service> services = new HashSet<Service>();
+ services.add(Service.Dhcp);
+ services.add(Service.StaticNat);
+ services.add(Service.SourceNat);
+ services.add(Service.Connectivity);
+ services.add(Service.Firewall);
+ assertTrue(element.verifyServicesCombination(services));
+
+ services = new HashSet<Service>();
+ services.add(Service.Dhcp);
+ services.add(Service.StaticNat);
+ services.add(Service.Connectivity);
+ services.add(Service.Firewall);
+ assertFalse(element.verifyServicesCombination(services));
+ }
+
+ @Test
+ public void testApplyStaticNats() throws CloudException {
+ final Network network = mock(Network.class);
+ when(network.getUuid()).thenReturn("aaaaaa");
+ when(network.getVpcId()).thenReturn(null);
+ when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(network.getDomainId()).thenReturn(NETWORK_ID);
+
+ final DomainVO domVo = mock(DomainVO.class);
+ when(domainDao.findById(41l)).thenReturn(domVo);
+
+ final NetworkOfferingVO ntwkoffer = mock(NetworkOfferingVO.class);
+ when(ntwkoffer.getId()).thenReturn(NETWORK_ID);
+ when(ntwkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffer);
+ when(element.isL3Network(NETWORK_ID)).thenReturn(true);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ when(domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
+ final ApplyStaticNatVspAnswer answer = mock(ApplyStaticNatVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+ assertTrue(element.applyStaticNats(network, new ArrayList<StaticNat>()));
+ }
+
+ @Test
+ public void testApplyFWRules() throws Exception {
+ final Network network = mock(Network.class);
+ when(network.getUuid()).thenReturn("aaaaaa");
+ when(network.getVpcId()).thenReturn(null);
+ when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(network.getDomainId()).thenReturn(NETWORK_ID);
+
+ final NetworkOfferingVO ntwkoffer = mock(NetworkOfferingVO.class);
+ when(ntwkoffer.getId()).thenReturn(NETWORK_ID);
+ when(ntwkoffer.getEgressDefaultPolicy()).thenReturn(true);
+ when(ntwkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffer);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ when(domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
+
+ final ApplyAclRuleVspAnswer answer = mock(ApplyAclRuleVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+ assertTrue(element.applyFWRules(network, new ArrayList<FirewallRule>()));
+ }
+
+ @Test
+ public void testApplyNetworkACL() throws Exception {
+ final Network network = mock(Network.class);
+ when(network.getUuid()).thenReturn("aaaaaa");
+ when(network.getVpcId()).thenReturn(null);
+ when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(network.getDomainId()).thenReturn(NETWORK_ID);
+
+ final NetworkOfferingVO ntwkoffer = mock(NetworkOfferingVO.class);
+ when(ntwkoffer.getId()).thenReturn(NETWORK_ID);
+ when(ntwkoffer.getEgressDefaultPolicy()).thenReturn(true);
+ when(ntwkOfferingDao.findById(NETWORK_ID)).thenReturn(ntwkoffer);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ when(domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
+ final ApplyAclRuleVspAnswer answer = mock(ApplyAclRuleVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+ assertTrue(element.applyNetworkACLs(network, new ArrayList<NetworkACLItem>()));
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/03de9cc3/plugins/network-elements/nuage-vsp/test/com/cloud/network/guru/NuageVspGuestNetworkGuruTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/nuage-vsp/test/com/cloud/network/guru/NuageVspGuestNetworkGuruTest.java b/plugins/network-elements/nuage-vsp/test/com/cloud/network/guru/NuageVspGuestNetworkGuruTest.java
new file mode 100644
index 0000000..f879f09
--- /dev/null
+++ b/plugins/network-elements/nuage-vsp/test/com/cloud/network/guru/NuageVspGuestNetworkGuruTest.java
@@ -0,0 +1,386 @@
+//
+// 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.guru;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Map;
+
+import org.apache.cloudstack.engine.orchestration.service.NetworkOrchestrationService;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.cloud.agent.AgentManager;
+import com.cloud.agent.api.Command;
+import com.cloud.agent.api.guru.ImplementNetworkVspAnswer;
+import com.cloud.agent.api.guru.ReleaseVmVspAnswer;
+import com.cloud.agent.api.guru.ReserveVmInterfaceVspAnswer;
+import com.cloud.dc.DataCenter;
+import com.cloud.dc.DataCenter.NetworkType;
+import com.cloud.dc.DataCenterVO;
+import com.cloud.dc.dao.DataCenterDao;
+import com.cloud.deploy.DeployDestination;
+import com.cloud.deploy.DeploymentPlan;
+import com.cloud.domain.Domain;
+import com.cloud.domain.DomainVO;
+import com.cloud.domain.dao.DomainDao;
+import com.cloud.exception.InsufficientAddressCapacityException;
+import com.cloud.exception.InsufficientVirtualNetworkCapacityException;
+import com.cloud.host.HostVO;
+import com.cloud.host.dao.HostDao;
+import com.cloud.network.Network;
+import com.cloud.network.Network.GuestType;
+import com.cloud.network.Network.Service;
+import com.cloud.network.NetworkModel;
+import com.cloud.network.Networks.BroadcastDomainType;
+import com.cloud.network.Networks.Mode;
+import com.cloud.network.Networks.TrafficType;
+import com.cloud.network.NuageVspDeviceVO;
+import com.cloud.network.dao.NetworkDao;
+import com.cloud.network.dao.NetworkVO;
+import com.cloud.network.dao.NuageVspDao;
+import com.cloud.network.dao.PhysicalNetworkDao;
+import com.cloud.network.dao.PhysicalNetworkVO;
+import com.cloud.offering.NetworkOffering;
+import com.cloud.offerings.NetworkOfferingVO;
+import com.cloud.offerings.dao.NetworkOfferingDao;
+import com.cloud.offerings.dao.NetworkOfferingServiceMapDao;
+import com.cloud.user.Account;
+import com.cloud.user.AccountVO;
+import com.cloud.user.dao.AccountDao;
+import com.cloud.vm.NicProfile;
+import com.cloud.vm.NicVO;
+import com.cloud.vm.ReservationContext;
+import com.cloud.vm.VirtualMachine;
+import com.cloud.vm.VirtualMachine.State;
+import com.cloud.vm.VirtualMachineProfile;
+import com.cloud.vm.dao.NicDao;
+
+public class NuageVspGuestNetworkGuruTest {
+ private static final long NETWORK_ID = 42L;
+ PhysicalNetworkDao physnetdao = mock(PhysicalNetworkDao.class);
+ DataCenterDao dcdao = mock(DataCenterDao.class);
+ NetworkOfferingServiceMapDao nosd = mock(NetworkOfferingServiceMapDao.class);
+ AgentManager agentManager = mock(AgentManager.class);
+ NetworkOrchestrationService netmgr = mock(NetworkOrchestrationService.class);
+ NetworkModel networkModel = mock(NetworkModel.class);
+ AccountDao accountDao = mock(AccountDao.class);
+ DomainDao domainDao = mock(DomainDao.class);
+ NicDao nicDao = mock(NicDao.class);
+ NetworkOfferingDao ntwkOfferDao = mock(NetworkOfferingDao.class);
+ NuageVspDao nuageVspDao = mock(NuageVspDao.class);
+ HostDao hostDao = mock(HostDao.class);
+ NetworkDao networkDao = mock(NetworkDao.class);
+
+ NetworkDao netdao = mock(NetworkDao.class);
+ NuageVspGuestNetworkGuru guru;
+
+ @Before
+ public void setUp() {
+ guru = new NuageVspGuestNetworkGuru();
+ ((GuestNetworkGuru)guru)._physicalNetworkDao = physnetdao;
+ guru._physicalNetworkDao = physnetdao;
+ guru._nuageVspDao = nuageVspDao;
+ guru._dcDao = dcdao;
+ guru._ntwkOfferingSrvcDao = nosd;
+ guru._networkModel = networkModel;
+ guru._hostDao = hostDao;
+ guru._agentMgr = agentManager;
+ guru._networkDao = netdao;
+ guru._networkDao = networkDao;
+ guru._accountDao = accountDao;
+ guru._domainDao = domainDao;
+ guru._nicDao = nicDao;
+ guru._ntwkOfferingDao = ntwkOfferDao;
+
+ final DataCenterVO dc = mock(DataCenterVO.class);
+ when(dc.getNetworkType()).thenReturn(NetworkType.Advanced);
+ when(dc.getGuestNetworkCidr()).thenReturn("10.1.1.1/24");
+
+ when(dcdao.findById((Long)any())).thenReturn(dc);
+ }
+
+ @Test
+ public void testCanHandle() {
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+
+ final PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+ when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] {"VSP"}));
+ when(physnet.getId()).thenReturn(NETWORK_ID);
+
+ when(nosd.areServicesSupportedByNetworkOffering(NETWORK_ID, Service.Connectivity)).thenReturn(true);
+
+ assertTrue(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+
+ // Not supported TrafficType != Guest
+ when(offering.getTrafficType()).thenReturn(TrafficType.Management);
+ assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+
+ // Not supported: GuestType Shared
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Shared);
+ assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+
+ // Not supported: Basic networking
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+ assertFalse(guru.canHandle(offering, NetworkType.Basic, physnet) == true);
+
+ // Not supported: IsolationMethod != STT
+ when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] {"VLAN"}));
+ assertFalse(guru.canHandle(offering, NetworkType.Advanced, physnet) == true);
+
+ }
+
+ @Test
+ public void testDesign() {
+ final PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+ when(physnetdao.findById((Long)any())).thenReturn(physnet);
+ when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] {"VSP"}));
+ when(physnet.getId()).thenReturn(NETWORK_ID);
+
+ final NuageVspDeviceVO device = mock(NuageVspDeviceVO.class);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {device}));
+ when(device.getId()).thenReturn(1L);
+
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+
+ when(nosd.areServicesSupportedByNetworkOffering(NETWORK_ID, Service.Connectivity)).thenReturn(true);
+
+ final DeploymentPlan plan = mock(DeploymentPlan.class);
+ final Network network = mock(Network.class);
+ final Account account = mock(Account.class);
+
+ final Network designednetwork = guru.design(offering, plan, network, account);
+ assertTrue(designednetwork != null);
+ assertTrue(designednetwork.getBroadcastDomainType() == BroadcastDomainType.Vsp);
+ }
+
+ @Test
+ public void testDesignNoElementOnPhysicalNetwork() {
+ final PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+ when(physnetdao.findById((Long)any())).thenReturn(physnet);
+ when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] {"STT"}));
+ when(physnet.getId()).thenReturn(NETWORK_ID);
+
+ mock(NuageVspDeviceVO.class);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Collections.<NuageVspDeviceVO> emptyList());
+
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+
+ final DeploymentPlan plan = mock(DeploymentPlan.class);
+ final Network network = mock(Network.class);
+ final Account account = mock(Account.class);
+
+ final Network designednetwork = guru.design(offering, plan, network, account);
+ assertTrue(designednetwork == null);
+ }
+
+ @Test
+ public void testDesignNoIsolationMethodVSP() {
+ final PhysicalNetworkVO physnet = mock(PhysicalNetworkVO.class);
+ when(physnetdao.findById((Long)any())).thenReturn(physnet);
+ when(physnet.getIsolationMethods()).thenReturn(Arrays.asList(new String[] {"VLAN"}));
+ when(physnet.getId()).thenReturn(NETWORK_ID);
+
+ mock(NuageVspDeviceVO.class);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Collections.<NuageVspDeviceVO> emptyList());
+
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getGuestType()).thenReturn(GuestType.Isolated);
+
+ final DeploymentPlan plan = mock(DeploymentPlan.class);
+ final Network network = mock(Network.class);
+ final Account account = mock(Account.class);
+
+ final Network designednetwork = guru.design(offering, plan, network, account);
+ assertTrue(designednetwork == null);
+ }
+
+ @Test
+ public void testReserve() throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException, URISyntaxException {
+ final Network network = mock(Network.class);
+ when(network.getUuid()).thenReturn("aaaaaa");
+ when(network.getDataCenterId()).thenReturn(NETWORK_ID);
+ when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(network.getDomainId()).thenReturn(NETWORK_ID);
+ when(network.getAccountId()).thenReturn(NETWORK_ID);
+ when(network.getVpcId()).thenReturn(null);
+ when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
+
+ final DataCenterVO dataCenter = mock(DataCenterVO.class);
+ when(dcdao.findById(NETWORK_ID)).thenReturn(dataCenter);
+ final AccountVO networksAccount = mock(AccountVO.class);
+ when(networksAccount.getUuid()).thenReturn("aaaa-abbbb");
+ when(networksAccount.getType()).thenReturn(Account.ACCOUNT_TYPE_NORMAL);
+ when(accountDao.findById(NETWORK_ID)).thenReturn(networksAccount);
+ final DomainVO networksDomain = mock(DomainVO.class);
+ when(networksDomain.getUuid()).thenReturn("aaaaa-bbbbb");
+ when(domainDao.findById(NETWORK_ID)).thenReturn(networksDomain);
+
+ final NicVO nicvo = mock(NicVO.class);
+ when(nicvo.getId()).thenReturn(NETWORK_ID);
+ when(nicvo.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
+ when(nicvo.getUuid()).thenReturn("aaaa-fffff");
+ when(nicDao.findById(NETWORK_ID)).thenReturn(nicvo);
+
+ final VirtualMachineProfile vm = mock(VirtualMachineProfile.class);
+ when(vm.getType()).thenReturn(VirtualMachine.Type.User);
+ when(vm.getInstanceName()).thenReturn("");
+ when(vm.getUuid()).thenReturn("aaaa-bbbbb");
+
+ NicProfile nicProfile = mock(NicProfile.class);
+ when(nicProfile.getUuid()).thenReturn("aaa-bbbb");
+ when(nicProfile.getId()).thenReturn(NETWORK_ID);
+ when(nicProfile.getMacAddress()).thenReturn("aa-aa-aa-aa-aa-aa");
+
+ final NetworkOfferingVO ntwkoffering = mock(NetworkOfferingVO.class);
+ when(ntwkoffering.getId()).thenReturn(NETWORK_ID);
+ when(ntwkOfferDao.findById(NETWORK_ID)).thenReturn(ntwkoffering);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ when(domainDao.findById(NETWORK_ID)).thenReturn(mock(DomainVO.class));
+ final ReserveVmInterfaceVspAnswer answer = mock(ReserveVmInterfaceVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(answer.getInterfaceDetails()).thenReturn(new ArrayList<Map<String, String>>());
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+
+ guru.reserve(nicProfile, network, vm, mock(DeployDestination.class), mock(ReservationContext.class));
+ }
+
+ @Test
+ public void testRelease() {
+ final NicProfile nicProfile = mock(NicProfile.class);
+ when(nicProfile.getNetworkId()).thenReturn(NETWORK_ID);
+ final NetworkVO network = mock(NetworkVO.class);
+ when(network.getUuid()).thenReturn("aaaaaa-ffffff");
+ when(network.getName()).thenReturn("aaaaaa");
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(networkDao.findById(NETWORK_ID)).thenReturn(network);
+
+ final VirtualMachineProfile vm = mock(VirtualMachineProfile.class);
+ when(vm.getType()).thenReturn(VirtualMachine.Type.User);
+ when(vm.getInstanceName()).thenReturn("");
+ when(vm.getUuid()).thenReturn("aaaa-bbbbb");
+
+ final VirtualMachine virtualMachine = mock(VirtualMachine.class);
+ when(vm.getVirtualMachine()).thenReturn(virtualMachine);
+ when(virtualMachine.getState()).thenReturn(State.Stopping);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ final ReleaseVmVspAnswer answer = mock(ReleaseVmVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+
+ guru.release(nicProfile, vm, "aaaaa-fffff");
+ }
+
+ @Test
+ public void testImplementNetwork() throws URISyntaxException, InsufficientVirtualNetworkCapacityException {
+ final Network network = mock(Network.class);
+ when(network.getId()).thenReturn(NETWORK_ID);
+ when(network.getUuid()).thenReturn("aaaaaa");
+ when(network.getDataCenterId()).thenReturn(NETWORK_ID);
+ when(network.getNetworkOfferingId()).thenReturn(NETWORK_ID);
+ when(network.getPhysicalNetworkId()).thenReturn(NETWORK_ID);
+ when(network.getDomainId()).thenReturn(NETWORK_ID);
+ when(network.getAccountId()).thenReturn(NETWORK_ID);
+ when(network.getVpcId()).thenReturn(null);
+ when(network.getState()).thenReturn(com.cloud.network.Network.State.Implementing);
+ when(network.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(network.getMode()).thenReturn(Mode.Static);
+ when(network.getBroadcastDomainType()).thenReturn(BroadcastDomainType.Vsp);
+ when(network.getBroadcastUri()).thenReturn(new URI("vsp://aaaaaa-aavvv/10.1.1.1"));
+ when(network.getGateway()).thenReturn("10.1.1.1");
+ when(network.getCidr()).thenReturn("10.1.1.0/24");
+ when(network.getName()).thenReturn("iso");
+
+ final NetworkOffering offering = mock(NetworkOffering.class);
+ when(offering.getId()).thenReturn(NETWORK_ID);
+ when(offering.getTrafficType()).thenReturn(TrafficType.Guest);
+ when(offering.getTags()).thenReturn("aaaa");
+ when(offering.getEgressDefaultPolicy()).thenReturn(true);
+
+ when(networkModel.findPhysicalNetworkId(NETWORK_ID, "aaa", TrafficType.Guest)).thenReturn(NETWORK_ID);
+
+ final ReservationContext reserveContext = mock(ReservationContext.class);
+ final Domain domain = mock(Domain.class);
+ when(reserveContext.getDomain()).thenReturn(domain);
+ when(domain.getName()).thenReturn("aaaaa");
+ final Account account = mock(Account.class);
+ when(account.getAccountId()).thenReturn(NETWORK_ID);
+ when(reserveContext.getAccount()).thenReturn(account);
+ final DomainVO domainVo = mock(DomainVO.class);
+ when(domainDao.findById(NETWORK_ID)).thenReturn(domainVo);
+ final AccountVO accountVo = mock(AccountVO.class);
+ when(accountDao.findById(NETWORK_ID)).thenReturn(accountVo);
+
+ final HostVO host = mock(HostVO.class);
+ when(host.getId()).thenReturn(NETWORK_ID);
+ final NuageVspDeviceVO nuageVspDevice = mock(NuageVspDeviceVO.class);
+ when(nuageVspDevice.getHostId()).thenReturn(NETWORK_ID);
+ when(nuageVspDao.listByPhysicalNetwork(NETWORK_ID)).thenReturn(Arrays.asList(new NuageVspDeviceVO[] {nuageVspDevice}));
+ when(hostDao.findById(NETWORK_ID)).thenReturn(host);
+
+ final ImplementNetworkVspAnswer answer = mock(ImplementNetworkVspAnswer.class);
+ when(answer.getResult()).thenReturn(true);
+ when(agentManager.easySend(eq(NETWORK_ID), (Command)any())).thenReturn(answer);
+
+ final DataCenter dc = mock(DataCenter.class);
+ when(dc.getId()).thenReturn(NETWORK_ID);
+ final DeployDestination deployDest = mock(DeployDestination.class);
+ when(deployDest.getDataCenter()).thenReturn(dc);
+ guru.implement(network, offering, deployDest, reserveContext);
+ }
+
+}