Example #1
0
        /// <summary>
        /// Rollback all virtual networks
        /// </summary>
        /// <param name="networkConfiguration">Network configuration</param>
        private void RollBackVirtualNetworks(NetworkConfiguration networkConfiguration)
        {
            Stopwatch swVirtualNetwork = new Stopwatch();

            swVirtualNetwork.Start();
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.NetworkConfiguration.ToString());
            // Get destination subscription network configuration
            NetworkGetConfigurationResponse destinationNetworkResponse = GetNetworkConfigurationFromMSAzure(
                importParameters.DestinationSubscriptionSettings.Credentials, importParameters.DestinationSubscriptionSettings.ServiceUrl);

            NetworkConfiguration destinationNetConfiguration = null;
            XmlSerializer        serializer = new XmlSerializer(typeof(NetworkConfiguration));

            if (destinationNetworkResponse != null && !(string.IsNullOrEmpty(destinationNetworkResponse.Configuration)))
            {
                var destinationReader = new StringReader(destinationNetworkResponse.Configuration);
                destinationNetConfiguration = (NetworkConfiguration)serializer.Deserialize(destinationReader);
            }

            try
            {
                if (networkConfiguration != null)
                {
                    if (networkConfiguration.VirtualNetworkConfiguration != null)
                    {
                        if (networkConfiguration.VirtualNetworkConfiguration.Dns != null &&
                            networkConfiguration.VirtualNetworkConfiguration.Dns.DnsServers != null &&
                            destinationNetConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration.Dns != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration.Dns.DnsServers != null)
                        {
                            foreach (var virtualNetworkSite in networkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites)
                            {
                                foreach (var dns in networkConfiguration.VirtualNetworkConfiguration.Dns.DnsServers)
                                {
                                    string dnsDestination = resourceImporter.GetDestinationResourceName(ResourceType.DnsServer, dns.name,
                                                                                                        ResourceType.VirtualNetworkSite,
                                                                                                        resourceImporter.GetDestinationResourceName(ResourceType.VirtualNetworkSite,
                                                                                                                                                    virtualNetworkSite.name));

                                    if (!string.IsNullOrEmpty(dnsDestination))
                                    {
                                        destinationNetConfiguration.VirtualNetworkConfiguration.Dns.DnsServers =
                                            destinationNetConfiguration.VirtualNetworkConfiguration.Dns.DnsServers.Where(s => s.name != dnsDestination).ToArray();
                                    }
                                }
                            }
                        }
                        if (networkConfiguration.VirtualNetworkConfiguration.LocalNetworkSites != null &&
                            destinationNetConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration.LocalNetworkSites != null)
                        {
                            foreach (var virtualNetworkSite in networkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites)
                            {
                                foreach (var localNetwork in networkConfiguration.VirtualNetworkConfiguration.LocalNetworkSites)
                                {
                                    string localNetworkDestination = resourceImporter.GetDestinationResourceName(ResourceType.LocalNetworkSite,
                                                                                                                 localNetwork.name,
                                                                                                                 ResourceType.VirtualNetworkSite,
                                                                                                                 resourceImporter.GetDestinationResourceName(ResourceType.VirtualNetworkSite,
                                                                                                                                                             virtualNetworkSite.name));
                                    if (!string.IsNullOrEmpty(localNetworkDestination))
                                    {
                                        destinationNetConfiguration.VirtualNetworkConfiguration.LocalNetworkSites =
                                            destinationNetConfiguration.VirtualNetworkConfiguration.LocalNetworkSites.Where(s => s.name != localNetworkDestination).ToArray();
                                    }
                                }
                            }
                        }
                        if (networkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites != null &&
                            destinationNetConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration != null &&
                            destinationNetConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites != null)
                        {
                            destinationNetConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites =
                                destinationNetConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.
                                Where(x => !networkConfiguration.VirtualNetworkConfiguration.VirtualNetworkSites.
                                      Any(x1 => resourceImporter.GetDestinationResourceName(ResourceType.VirtualNetworkSite, x1.name) == x.name)).ToArray();
                        }
                    }
                }

                MemoryStream memoryStream = new MemoryStream();
                XmlWriter    writer       = XmlWriter.Create(memoryStream, new XmlWriterSettings {
                    Encoding = Encoding.UTF8
                });
                serializer.Serialize(writer, destinationNetConfiguration);

                using (var vnetClient = new NetworkManagementClient(importParameters.DestinationSubscriptionSettings.Credentials,
                                                                    importParameters.DestinationSubscriptionSettings.ServiceUrl))
                {
                    OperationStatusResponse response = Retry.RetryOperation(() => vnetClient.Networks.SetConfiguration(
                                                                                new NetworkSetConfigurationParameters
                    {
                        Configuration = Encoding.UTF8.GetString(memoryStream.ToArray())
                    }), (BaseParameters)importParameters, ResourceType.NetworkConfiguration);
                }
                writer.Close();
                memoryStream.Close();
                resourceImporter.UpdateMedatadaFile(ResourceType.NetworkConfiguration, null, false);
                Logger.Info(methodName, ProgressResources.RollbackVirtualNetworksWaiting, ResourceType.NetworkConfiguration.ToString());
                dcMigrationManager.ReportProgress(ProgressResources.RollbackVirtualNetworksWaiting);
                Task.Delay(Constants.DelayTimeInMilliseconds_Rollback).Wait();
                Logger.Info(methodName, ProgressResources.RollbackVirtualNetwork, ResourceType.NetworkConfiguration.ToString());
                swVirtualNetwork.Stop();
                Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swVirtualNetwork.Elapsed.Days, swVirtualNetwork.Elapsed.Hours,
                                                      swVirtualNetwork.Elapsed.Minutes, swVirtualNetwork.Elapsed.Seconds), ResourceType.NetworkConfiguration.ToString());
            }
            catch (CloudException ex)
            {
                if (string.Compare(ex.ErrorCode, Constants.ResourceNotFound, StringComparison.CurrentCultureIgnoreCase) != 0)
                {
                    Logger.Error(methodName, ex, ResourceType.VirtualNetwork.ToString());
                }
                else
                {
                    throw;
                }
            }
        }
Example #2
0
        /// <summary>
        /// Validates and converts input paramters into <see cref="ExportParameters"/> class.
        /// </summary>
        /// <param name="parameters">Collection of input parameters stored in key value pairs
        /// <example> Operation "Export" SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" SourceDCName "East Asia"
        /// ExportMetadataFolderPath "D:\\DataCenterMigration" SourcePublishSettingsFilePath  "D:\\PublishSettings.PublishSettings"
        /// QuietMode "True" </example> </param>
        /// <returns>Parameters required for export functionality</returns>
        internal static ExportParameters ValidateAndConvertExportParameters(IDictionary <string, string> parameters)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            Logger.Info(methodName, ProgressResources.ParameterValidationStarted);

            #region Check for missing required parameters
            // PublishSettingsFilePath / CertificateThumbprint
            if (!parameters.Keys.Contains(Constants.Parameters.SourcePublishSettingsFilePath) && !parameters.Keys.Contains(
                    Constants.Parameters.SourceCertificateThumbprint))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source,
                                                            Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (!parameters.Keys.Contains(Constants.Parameters.ExportMetadataFolderPath))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.ExportMetadataFolderPath, Constants.AppConfigArguments));
            }
            // SourceSubscriptionID
            if (!parameters.Keys.Contains(Constants.Parameters.SourceSubscriptionID))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.SourceSubscriptionID, Constants.AppConfigArguments));
            }

            // SourceDCName
            if (!parameters.Keys.Contains(Constants.Parameters.SourceDCName))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.SourceDCName, Constants.AppConfigArguments));
            }

            #endregion

            #region Check for null or empty values

            // PublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourcePublishSettingsFilePath])) ||
                (parameters.ContainsKey(Constants.Parameters.SourceCertificateThumbprint) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourceCertificateThumbprint])))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source, Constants.AppConfigArguments));
            }

            // ExportMetadataFolderPath
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.ExportMetadataFolderPath));
            }
            // SourceSubscriptionID
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceSubscriptionID]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceSubscriptionID));
            }

            // SourceDCName
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.SourceDCName]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.SourceDCName));
            }

            #endregion

            #region Validate parameter's value

            // Validate PublishSettingsFilePath
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && !File.Exists(parameters[Constants.Parameters.SourcePublishSettingsFilePath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.SourcePublishSettingsFilePath),
                                              new FileNotFoundException(StringResources.PublishSettingsFilePathParamException));
            }

            // Validate ExportMetadataFolderPath
            string folderPath = parameters[Constants.Parameters.ExportMetadataFolderPath];

            if (!Directory.Exists(parameters[Constants.Parameters.ExportMetadataFolderPath]))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue, Constants.Parameters.ExportMetadataFolderPath),
                                              new DirectoryNotFoundException(StringResources.ExportMetadataFolderPathParamException));
            }

            int    retryCount;
            Double minBackOff;
            Double maxBackOff;
            Double deltaBackOff;
            bool   generateMapperXmlValue;

            if (!parameters.Keys.Contains(Constants.Parameters.RetryCount))
            {
                retryCount = Int32.Parse(Constants.RetryCountDefault);
            }
            else
            {
                Int32.TryParse(parameters[Constants.Parameters.RetryCount], out retryCount);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MinBackoff))
            {
                minBackOff = Double.Parse(Constants.MinBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MinBackoff], out minBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MaxBackoff))
            {
                maxBackOff = Double.Parse(Constants.MaxBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MaxBackoff], out maxBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.DeltaBackoff))
            {
                deltaBackOff = Double.Parse(Constants.DeltaBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.DeltaBackoff], out deltaBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.GenerateMapperXml))
            {
                generateMapperXmlValue = false;
            }
            else
            {
                bool.TryParse(parameters[Constants.Parameters.GenerateMapperXml], out generateMapperXmlValue);
            }

            // Validate SourceSubscriptionID
            PublishSettings publishSettingsFile = null;
            PublishSetting  subscription        = null;
            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath))
            {
                try
                {
                    var fileContents = File.ReadAllText(parameters[Constants.Parameters.SourcePublishSettingsFilePath]);
                    publishSettingsFile = new PublishSettings(fileContents);
                }
                catch (Exception xmlEx)
                {
                    throw new ValidationException(string.Format(StringResources.XMLParsingException + " =>" + xmlEx.Message,
                                                                parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }

                // Check whether subscriptionId exists in publish settings file
                subscription = publishSettingsFile.Subscriptions.FirstOrDefault(a =>
                                                                                string.Compare(a.Id, parameters[Constants.Parameters.SourceSubscriptionID], StringComparison.InvariantCultureIgnoreCase) == 0);
                if (subscription == null)
                {
                    throw new ValidationException(string.Format(StringResources.SubscriptionIdParamException,
                                                                parameters[Constants.Parameters.SourceSubscriptionID], parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }
            }
            else
            {
                string           thumbprint  = parameters[Constants.Parameters.SourceCertificateThumbprint];
                X509Certificate2 certificate = GetStoreCertificate(thumbprint);
                if (!certificate.HasPrivateKey)
                {
                    throw new ValidationException(string.Format(
                                                      StringResources.MissingPrivateKeyInCertificate, StringResources.Source, thumbprint));
                }
                var sourceCredentials = new CertificateCloudCredentials(parameters[Constants.Parameters.SourceSubscriptionID],
                                                                        certificate);
                string sourceSubscriptionName = null;
                using (var client = new ManagementClient(sourceCredentials))
                {
                    BaseParameters baseParams = new BaseParameters()
                    {
                        RetryCount   = retryCount,
                        MinBackOff   = TimeSpan.FromSeconds(minBackOff),
                        MaxBackOff   = TimeSpan.FromSeconds(maxBackOff),
                        DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff)
                    };
                    SubscriptionGetResponse subscriptionResponse = Retry.RetryOperation(() => client.Subscriptions.Get(),
                                                                                        baseParams, ResourceType.None);
                    sourceSubscriptionName = subscriptionResponse.SubscriptionName;
                }
                subscription = new PublishSetting()
                {
                    Id          = parameters[Constants.Parameters.SourceSubscriptionID],
                    Name        = sourceSubscriptionName,
                    ServiceUrl  = new Uri(Constants.ServiceManagementUrlValue),
                    Credentials = sourceCredentials
                };
            }


            List <string> locations = ExportDataCenterLocations(subscription.Credentials, retryCount, minBackOff, maxBackOff, deltaBackOff);
            // Check whether SourceDc name exists in subscription
            bool sourceLocationNamePresent = locations.Any((l => string.Compare(l, parameters[Constants.Parameters.SourceDCName],
                                                                                StringComparison.CurrentCultureIgnoreCase) == 0));

            if (!sourceLocationNamePresent)
            {
                throw new ValidationException(string.Format(StringResources.DCParamException, parameters[Constants.Parameters.SourceDCName]));
            }

            Logger.Info(methodName, ProgressResources.ParametersValidationCompleted);

            #endregion

            // Stores validated parameters in class.
            ExportParameters exportParams = new ExportParameters()
            {
                SourceDCName             = parameters[Constants.Parameters.SourceDCName],
                ExportMetadataFolderPath = Path.Combine(parameters[Constants.Parameters.ExportMetadataFolderPath],
                                                        string.Format(Constants.MetadataFileName, parameters[Constants.Parameters.SourceDCName],
                                                                      DateTime.Now.ToString(Constants.ExportMetadataFileNameFormat))),
                SourceSubscriptionSettings = subscription,
                RetryCount            = retryCount,
                MinBackOff            = TimeSpan.FromSeconds(minBackOff),
                MaxBackOff            = TimeSpan.FromSeconds(maxBackOff),
                DeltaBackOff          = TimeSpan.FromSeconds(deltaBackOff),
                GenerateMapperXml     = generateMapperXmlValue,
                DestinationPrefixName = parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName) ?
                                        parameters[Constants.Parameters.DestinationPrefixName].ToLower() : Constants.DestinationPrefixValue,
            };
            return(exportParams);
        }
Example #3
0
        /// <summary>
        /// Rollback all imported cloud services.
        /// </summary>
        /// <param name="cloudServices">Cloud services to be deleted.</param>
        private void RollBackServices(List <string> cloudServices)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted, ResourceType.CloudService.ToString());
            Logger.Info(methodName, ProgressResources.RollbackCloudServices, ResourceType.CloudService.ToString());
            dcMigrationManager.ReportProgress(ProgressResources.RollbackCloudServices);
            Stopwatch swTotalServices = new Stopwatch();

            swTotalServices.Start();
            //string origServiceName = null;
            if (cloudServices.Count > 0)
            {
                using (var client = new ComputeManagementClient(importParameters.DestinationSubscriptionSettings.Credentials))
                {
                    Parallel.ForEach(cloudServices, cloudService =>
                    {
                        try
                        {
                            Stopwatch swService = new Stopwatch();
                            swService.Start();
                            string origServiceName = resourceImporter.GetSourceResourceName(ResourceType.CloudService, cloudService);
                            Retry.RetryOperation(() => client.HostedServices.DeleteAll(cloudService),
                                                 (BaseParameters)importParameters, ResourceType.CloudService, cloudService, ignoreResourceNotFoundEx: true);

                            CloudService service = subscription.DataCenters.FirstOrDefault().CloudServices.
                                                   Where(ser => (ser.CloudServiceDetails.ServiceName == origServiceName)).FirstOrDefault();
                            if (service.DeploymentDetails != null)
                            {
                                string deploymentName = resourceImporter.GetDestinationResourceName(
                                    ResourceType.Deployment, service.DeploymentDetails.Name, ResourceType.CloudService,
                                    resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                    );
                                resourceImporter.UpdateMedatadaFile(ResourceType.Deployment, deploymentName, false,
                                                                    resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                                                    );
                                Logger.Info(methodName, string.Format(ProgressResources.RollbackDeployment, service.DeploymentDetails.Name,
                                                                      service.DeploymentDetails.Name), ResourceType.Deployment.ToString(), service.DeploymentDetails.Name);

                                foreach (VirtualMachine vm in service.DeploymentDetails.VirtualMachines)
                                {
                                    string virtualmachineName = resourceImporter.GetDestinationResourceName(
                                        ResourceType.VirtualMachine, vm.VirtualMachineDetails.RoleName, ResourceType.CloudService,
                                        resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                        );

                                    resourceImporter.UpdateMedatadaFile(ResourceType.VirtualMachine, virtualmachineName, false,
                                                                        resourceImporter.GetDestinationResourceName(ResourceType.CloudService, service.CloudServiceDetails.ServiceName)
                                                                        );
                                    Logger.Info(methodName, string.Format(ProgressResources.RollbackVirtualMachine, vm.VirtualMachineDetails.RoleName),
                                                ResourceType.VirtualMachine.ToString(), vm.VirtualMachineDetails.RoleName);
                                }
                            }
                            resourceImporter.UpdateMedatadaFile(ResourceType.CloudService, cloudService, false);
                            swService.Stop();
                            Logger.Info(methodName, string.Format(ProgressResources.RollbackCloudService, cloudService, swService.Elapsed.Days, swService.Elapsed.Hours,
                                                                  swService.Elapsed.Minutes, swService.Elapsed.Seconds), ResourceType.CloudService.ToString(), cloudService);
                        }
                        catch (AggregateException exAgg)
                        {
                            foreach (var ex in exAgg.InnerExceptions)
                            {
                                Logger.Error(methodName, exAgg, ResourceType.CloudService.ToString(), cloudService);
                            }
                            throw;
                        }
                    });
                    Logger.Info(methodName, ProgressResources.RollbackCloudServicesWaiting, ResourceType.CloudService.ToString());
                    dcMigrationManager.ReportProgress(ProgressResources.RollbackCloudServicesWaiting);
                    Task.Delay(Constants.DelayTimeInMilliseconds_Rollback).Wait();
                }
            }
            swTotalServices.Stop();
            Logger.Info(methodName, string.Format(ProgressResources.ExecutionCompletedWithTime, swTotalServices.Elapsed.Days, swTotalServices.Elapsed.Hours, swTotalServices.Elapsed.Minutes,
                                                  swTotalServices.Elapsed.Seconds), ResourceType.CloudService.ToString());
        }
Example #4
0
        /// <summary>
        /// Validates and converts input paramters into <see cref="ImportParameters"/> class.
        /// </summary>
        /// <param name="parameters">Collection of input parameters stored in key value pairs
        /// <example> Operation "Import" SourceSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07"
        /// DestinationSubscriptionID "5d14d4a2-8c5a-4fc5-8d7d-86efb48b3a07" DestinationDCName "West US"
        /// SourcePublishSettingsFilePath "D:\\PublishSettings.PublishSettings"
        /// DestinationPublishSettingsFilePath "D:\\PublishSettings.PublishSettings"
        /// ImportMetadataFilePath "D:\\DataCenterMigration\mydata.json" DestinationPrefixName "dc" QuietMode "True"
        /// RollBackOnFailure "True" ResumeImport "True" </example> </param>
        /// <param name="validateForImport">True if the function will be called for imort functionality.
        /// False if it is called for Migrate functionality</param>
        /// <returns>Parameters required for import functionality</returns>
        internal static ImportParameters ValidateAndConvertImportParameters(IDictionary <string, string> parameters,
                                                                            bool validateForImport)
        {
            string methodName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            Logger.Info(methodName, ProgressResources.ExecutionStarted);
            Logger.Info(methodName, ProgressResources.ParameterValidationStarted);

            #region Check for missing required parameters
            // SourcePublishSettingsFilePath/ CertificateThumbprint
            if (!parameters.Keys.Contains(Constants.Parameters.SourcePublishSettingsFilePath) && !parameters.Keys.Contains(Constants.Parameters.SourceCertificateThumbprint))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source, Constants.AppConfigArguments));
            }

            // DestinationPublishSettingsFilePath
            if (!parameters.Keys.Contains(Constants.Parameters.DestinationPublishSettingsFilePath) && !parameters.Keys.Contains(Constants.Parameters.DestinationCertificateThumbprint))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Destination, Constants.AppConfigArguments));
            }

            // SourceSubscriptionID
            if (!parameters.Keys.Contains(Constants.Parameters.SourceSubscriptionID))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.SourceSubscriptionID, Constants.AppConfigArguments));
            }

            // DestinationSubscriptionID
            if (!parameters.Keys.Contains(Constants.Parameters.DestinationSubscriptionID))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.DestinationSubscriptionID, Constants.AppConfigArguments));
            }

            // DestinationDCName
            if (!parameters.Keys.Contains(Constants.Parameters.DestinationDCName))
            {
                throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                            Constants.Parameters.DestinationDCName, Constants.AppConfigArguments));
            }

            //DestinationPrefixName & MapperXmlFilePath.
            if (!validateForImport)
            {
                if (!parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName))
                {
                    throw new ValidationException(string.Format(StringResources.MissingRequiredParameter,
                                                                Constants.Parameters.DestinationPrefixName, Constants.AppConfigArguments));
                }
            }
            else
            {
                if (!parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName) &&
                    !parameters.Keys.Contains(Constants.Parameters.MapperXmlFilePath))
                {
                    throw new ValidationException(string.Format(StringResources.MissingMapperAndPrefix,
                                                                StringResources.Destination, Constants.AppConfigArguments));
                }
            }

            #endregion

            #region Check for null or empty values
            //SourcePublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourcePublishSettingsFilePath])) ||
                (parameters.ContainsKey(Constants.Parameters.SourceCertificateThumbprint) && string.IsNullOrEmpty(parameters[Constants.Parameters.SourceCertificateThumbprint])))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Source, Constants.AppConfigArguments));
            }

            // DestinationPublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.DestinationPublishSettingsFilePath) && string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationPublishSettingsFilePath])) ||
                (parameters.ContainsKey(Constants.Parameters.DestinationCertificateThumbprint) && string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationCertificateThumbprint])))
            {
                throw new ValidationException(string.Format(StringResources.MissingCredentialsFile, StringResources.Destination, Constants.AppConfigArguments));
            }

            // ImportMetadataFilePath
            if (validateForImport && (string.IsNullOrEmpty(parameters[Constants.Parameters.ImportMetadataFilePath])))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter,
                                                            Constants.Parameters.ImportMetadataFilePath));
            }
            // DestinationSubscriptionID
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationSubscriptionID]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter,
                                                            Constants.Parameters.DestinationSubscriptionID));
            }

            // DestinationDCName
            if (string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationDCName]))
            {
                throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.DestinationDCName));
            }

            //DestinationPrefixName & MapperXmlFilePath.
            if (!validateForImport)
            {
                if (string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationPrefixName]))
                {
                    throw new ValidationException(string.Format(StringResources.EmptyOrNullParameter, Constants.Parameters.DestinationPrefixName));
                }
            }
            else
            {
                if ((parameters.ContainsKey(Constants.Parameters.DestinationPrefixName) &&
                     string.IsNullOrEmpty(parameters[Constants.Parameters.DestinationPrefixName])) ||
                    (parameters.ContainsKey(Constants.Parameters.MapperXmlFilePath) &&
                     string.IsNullOrEmpty(parameters[Constants.Parameters.MapperXmlFilePath])))
                {
                    throw new ValidationException(string.Format(StringResources.MissingMapperAndPrefix,
                                                                StringResources.Destination, Constants.AppConfigArguments));
                }
            }

            #endregion

            #region Validate parameter's value.

            // Validate SourcePublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath) && !File.Exists(parameters[Constants.Parameters.SourcePublishSettingsFilePath])))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue,
                                                            Constants.Parameters.SourcePublishSettingsFilePath),
                                              new FileNotFoundException(StringResources.PublishSettingsFilePathParamException));
            }

            // Validate DestinationPublishSettingsFilePath
            if ((parameters.ContainsKey(Constants.Parameters.DestinationPublishSettingsFilePath) && !File.Exists(parameters[Constants.Parameters.DestinationPublishSettingsFilePath])))
            {
                throw new ValidationException(string.Format(StringResources.InvalidParameterValue,
                                                            Constants.Parameters.DestinationPublishSettingsFilePath),
                                              new FileNotFoundException(StringResources.PublishSettingsFilePathParamException));
            }

            string importMapperXmlFilePath = string.Empty;
            string destinationPrefix       = string.Empty;
            // Validate MapperXmlFilePath if provided
            if (validateForImport && parameters.ContainsKey(Constants.Parameters.MapperXmlFilePath))
            {
                importMapperXmlFilePath = parameters[Constants.Parameters.MapperXmlFilePath];

                if (!File.Exists(importMapperXmlFilePath))
                {
                    throw new ValidationException(string.Format(StringResources.InvalidParameterValue,
                                                                Constants.Parameters.MapperXmlFilePath),
                                                  new FileNotFoundException(StringResources.MapperFilePathParamException));
                }
                if (!Path.GetExtension(importMapperXmlFilePath).Equals(Constants.MapperFileExtension, StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new ValidationException(string.Format(StringResources.InvalidExtensionMapperFile,
                                                                Constants.Parameters.MapperXmlFilePath),
                                                  new FileNotFoundException(StringResources.MapperFilePathParamException));
                }
            }
            destinationPrefix = parameters.Keys.Contains(Constants.Parameters.DestinationPrefixName) ?
                                parameters[Constants.Parameters.DestinationPrefixName].ToLower() :
                                Constants.DestinationPrefixValue;

            // Validate ImportMetadataFilePath
            if (validateForImport)
            {
                if (!string.IsNullOrEmpty(parameters[Constants.Parameters.ImportMetadataFilePath]))
                {
                    string filePath = parameters[Constants.Parameters.ImportMetadataFilePath];

                    if (!File.Exists(filePath))
                    {
                        throw new ValidationException(string.Format(StringResources.InvalidParameterValue,
                                                                    Constants.Parameters.ImportMetadataFilePath),
                                                      new FileNotFoundException(StringResources.ImportMetadataFilePathParamException));
                    }
                    if (!Path.GetExtension(filePath).Equals(Constants.MetadataFileExtension, StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new ValidationException(string.Format(StringResources.InvalidExtensionMetadataFile,
                                                                    Constants.Parameters.ImportMetadataFilePath),
                                                      new FileNotFoundException(StringResources.ImportMetadataFilePathParamException));
                    }
                }
            }

            bool   rollBackBoolValue;
            bool   resumeImportBoolValue;
            int    retryCount;
            Double minBackOff;
            Double maxBackOff;
            Double deltaBackOff;

            if (!parameters.Keys.Contains(Constants.Parameters.RollBackOnFailure))
            {
                rollBackBoolValue = false;
            }
            else
            {
                bool.TryParse(parameters[Constants.Parameters.RollBackOnFailure], out rollBackBoolValue);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.ResumeImport))
            {
                resumeImportBoolValue = false;
            }
            else
            {
                bool.TryParse(parameters[Constants.Parameters.ResumeImport], out resumeImportBoolValue);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.RetryCount))
            {
                retryCount = Int32.Parse(Constants.RetryCountDefault);
            }
            else
            {
                Int32.TryParse(parameters[Constants.Parameters.RetryCount], out retryCount);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MinBackoff))
            {
                minBackOff = Double.Parse(Constants.MinBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MinBackoff], out minBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.MaxBackoff))
            {
                maxBackOff = Double.Parse(Constants.MaxBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.MaxBackoff], out maxBackOff);
            }

            if (!parameters.Keys.Contains(Constants.Parameters.DeltaBackoff))
            {
                deltaBackOff = Double.Parse(Constants.DeltaBackoffDefault);
            }
            else
            {
                Double.TryParse(parameters[Constants.Parameters.DeltaBackoff], out deltaBackOff);
            }
            // Validate SourcePublishSettings File
            PublishSettings sourcePublishSettingsFile = null;
            PublishSetting  sourceSubscription        = null;

            if (parameters.ContainsKey(Constants.Parameters.SourcePublishSettingsFilePath))
            {
                try
                {
                    var fileContents = File.ReadAllText(parameters[Constants.Parameters.SourcePublishSettingsFilePath]);
                    sourcePublishSettingsFile = new PublishSettings(fileContents);
                }
                catch (Exception xmlEx)
                {
                    throw new ValidationException(string.Format(StringResources.XMLParsingException + " =>" + xmlEx.Message,
                                                                parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }

                // Check whether sourceSubscriptionId exists in publish settings file
                sourceSubscription = sourcePublishSettingsFile.Subscriptions.FirstOrDefault(a =>
                                                                                            string.Compare(a.Id, parameters[Constants.Parameters.SourceSubscriptionID], StringComparison.InvariantCultureIgnoreCase) == 0);
                if (sourceSubscription == null)
                {
                    throw new ValidationException(string.Format(StringResources.SubscriptionIdParamException, parameters[Constants.Parameters.SourceSubscriptionID], parameters[Constants.Parameters.SourcePublishSettingsFilePath]));
                }
            }
            else
            {
                string           thumbprint  = parameters[Constants.Parameters.SourceCertificateThumbprint];
                X509Certificate2 certificate = GetStoreCertificate(thumbprint);
                if (!certificate.HasPrivateKey)
                {
                    throw new ValidationException(string.Format(
                                                      StringResources.MissingPrivateKeyInCertificate, StringResources.Source, thumbprint));
                }
                var sourceCredentials = new CertificateCloudCredentials(parameters[Constants.Parameters.SourceSubscriptionID],
                                                                        certificate);
                string sourceSubscriptionName = null;
                using (var client = new ManagementClient(sourceCredentials))
                {
                    BaseParameters baseParams = new BaseParameters()
                    {
                        RetryCount   = retryCount,
                        MinBackOff   = TimeSpan.FromSeconds(minBackOff),
                        MaxBackOff   = TimeSpan.FromSeconds(maxBackOff),
                        DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff)
                    };
                    SubscriptionGetResponse subscriptionResponse = Retry.RetryOperation(() => client.Subscriptions.Get(), baseParams, ResourceType.None);
                    sourceSubscriptionName = subscriptionResponse.SubscriptionName;
                }
                sourceSubscription = new PublishSetting()
                {
                    Id          = parameters[Constants.Parameters.SourceSubscriptionID],
                    Name        = sourceSubscriptionName,
                    ServiceUrl  = new Uri(Constants.ServiceManagementUrlValue),
                    Credentials = sourceCredentials
                };
            }

            // Validate DestinationPublishSettings File
            PublishSettings destPublishSettingsFile = null;
            PublishSetting  destSubscription        = null;

            if (parameters.ContainsKey(Constants.Parameters.DestinationPublishSettingsFilePath))
            {
                try
                {
                    var fileContents = File.ReadAllText(parameters[Constants.Parameters.DestinationPublishSettingsFilePath]);
                    destPublishSettingsFile = new PublishSettings(fileContents);
                }
                catch (Exception xmlEx)
                {
                    throw new ValidationException(string.Format(StringResources.XMLParsingException + " =>" + xmlEx.Message,
                                                                parameters[Constants.Parameters.DestinationPublishSettingsFilePath]));
                }
                // Check whether destSubscriptionId exists in publish settings file
                destSubscription = destPublishSettingsFile.Subscriptions.FirstOrDefault(a =>
                                                                                        string.Compare(a.Id, parameters[Constants.Parameters.DestinationSubscriptionID], StringComparison.InvariantCultureIgnoreCase) == 0);
                if (destSubscription == null)
                {
                    throw new ValidationException(string.Format(StringResources.SubscriptionIdParamException, parameters[Constants.Parameters.DestinationSubscriptionID], parameters[Constants.Parameters.DestinationPublishSettingsFilePath]));
                }
            }
            else
            {
                string           thumbprint  = parameters[Constants.Parameters.DestinationCertificateThumbprint];
                X509Certificate2 certificate = GetStoreCertificate(thumbprint);
                if (!certificate.HasPrivateKey)
                {
                    throw new ValidationException(string.Format(
                                                      StringResources.MissingPrivateKeyInCertificate, StringResources.Destination, thumbprint));
                }
                var destCredentials = new CertificateCloudCredentials(parameters[Constants.Parameters.DestinationSubscriptionID],
                                                                      certificate);
                string destSubscriptionName = null;
                using (var client = new ManagementClient(destCredentials))
                {
                    BaseParameters baseParams = new BaseParameters()
                    {
                        RetryCount   = retryCount,
                        MinBackOff   = TimeSpan.FromSeconds(minBackOff),
                        MaxBackOff   = TimeSpan.FromSeconds(maxBackOff),
                        DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff)
                    };
                    SubscriptionGetResponse subscriptionResponse = Retry.RetryOperation(() => client.Subscriptions.Get(),
                                                                                        baseParams, ResourceType.None);
                    destSubscriptionName = subscriptionResponse.SubscriptionName;
                }
                destSubscription = new PublishSetting()
                {
                    Id          = parameters[Constants.Parameters.DestinationSubscriptionID],
                    Name        = destSubscriptionName,
                    ServiceUrl  = new Uri(Constants.ServiceManagementUrlValue),
                    Credentials = destCredentials
                };
            }

            // Check whether DestinationDCName exists in subscription
            List <string> locations = ExportDataCenterLocations(destSubscription.Credentials, retryCount, minBackOff, maxBackOff, deltaBackOff);

            bool destinationLocationNamePresent = locations.Any((l => string.Compare(l,
                                                                                     parameters[Constants.Parameters.DestinationDCName],
                                                                                     StringComparison.CurrentCultureIgnoreCase) == 0));

            if (!destinationLocationNamePresent)
            {
                throw new ValidationException(string.Format(StringResources.DCParamException,
                                                            parameters[Constants.Parameters.DestinationDCName]));
            }

            // Valiadte DestinationPrefixName
            if (parameters.ContainsKey(Constants.Parameters.DestinationPrefixName) &&
                (parameters[Constants.Parameters.DestinationPrefixName].Length < 1 || parameters[Constants.Parameters.DestinationPrefixName].Length > 5))
            {
                throw new ValidationException(string.Format(StringResources.InvalidDestinationPrefixName,
                                                            parameters[Constants.Parameters.DestinationPrefixName]));
            }
            #endregion

            // Stores validated parameters in class.
            ImportParameters importParams = new ImportParameters()
            {
                DestinationDCName               = parameters[Constants.Parameters.DestinationDCName],
                ImportMetadataFilePath          = (!validateForImport) ? null : parameters[Constants.Parameters.ImportMetadataFilePath],
                MapperXmlFilePath               = importMapperXmlFilePath,
                DestinationPrefixName           = destinationPrefix,
                DestinationSubscriptionSettings = destSubscription,
                RollBackOnFailure               = rollBackBoolValue,
                ResumeImport = resumeImportBoolValue,
                RetryCount   = retryCount,
                MinBackOff   = TimeSpan.FromSeconds(minBackOff),
                MaxBackOff   = TimeSpan.FromSeconds(maxBackOff),
                DeltaBackOff = TimeSpan.FromSeconds(deltaBackOff),
                SourceSubscriptionSettings = sourceSubscription
            };

            Logger.Info(methodName, ProgressResources.ParametersValidationCompleted);
            return(importParams);
        }