public async Task <EdgeAgentDeploymentPayload> PrepareDeploymentTemplateForInternalAgentIdAsync(string plantId, string internalAgentId)
        {
            using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name))
            {
                try
                {
                    EdgeAgentDeploymentPayload agentPayload = new EdgeAgentDeploymentPayload();
                    JTokenWrapper deploymentJsonObject      = new JTokenWrapper();
                    var           deploymentTemplate        = await _edgeAgentDeploymentRepository.GetDeploymentTemplateAsync();

                    deploymentJsonObject.Value = JObject.Parse(deploymentTemplate.DeploymentTemplate);

                    var edgeAgent = await _edgeAgentRepository.GetEdgeAgentByIdAsync(plantId, internalAgentId);

                    var edgeConfiguration = await _edgeConfigurationRepository.GetEdgeConfigurationByInternalAgentIdAsync(plantId, internalAgentId);

                    var edgeGatewayConfiguration = await _edgeGatewayConfigurationRepository.GetEdgeGatewayConfigurationByInternalAgentIdAsync(plantId, internalAgentId);

                    var modulesOfAgent = await _edgeMessagePipelineRepository.GetEdgeModuleConfigurationsByAgentIdAsync(plantId, internalAgentId);

                    var defaultModulesOfAgent = await GetDefaultModulesOfAgentAsync(plantId, internalAgentId);

                    var finalModulesOfAgent = GetFinalModulesOfAgent(defaultModulesOfAgent, modulesOfAgent);

                    var routesForTemplate = await GetRoutesForTemplateAsync(plantId, internalAgentId, defaultModulesOfAgent, finalModulesOfAgent);

                    var modulesForTemplate = GetModulesForTemplate(defaultModulesOfAgent, modulesOfAgent, edgeConfiguration);

                    JObject modules = JObject.FromObject(modulesForTemplate);
                    var     edgeAgentDesiredPropertiesModules = GetEdgeAgentDesiredPropertiesModules(deploymentJsonObject).Value;
                    edgeAgentDesiredPropertiesModules.Replace(modules);

                    JObject routes = JObject.FromObject(routesForTemplate);
                    var     edgeHubDesiresPropertiesRoutes = GetEdgeHubDesiredPropertiesRoutes(deploymentJsonObject).Value;
                    edgeHubDesiresPropertiesRoutes.Replace(routes);

                    SetModuleDesiredProperties(finalModulesOfAgent, deploymentJsonObject, edgeAgent, edgeConfiguration);
                    UpdateEdgeHub(deploymentJsonObject, edgeConfiguration, edgeGatewayConfiguration);
                    UpdateEdgeAgent(deploymentJsonObject, edgeConfiguration, edgeGatewayConfiguration);
                    SetRegistryCredentials(deploymentJsonObject);
                    agentPayload.DeploymentTemplate      = deploymentJsonObject.Value;
                    agentPayload.AgentConfigurationName  = string.Format(AgentonfigurationNameTemplate, edgeAgent.AgentName, DateTime.UtcNow.ToString());
                    agentPayload.DeploymentConfiguration = await PrepareDeploymentConfigurationForInternalAgentIdAsync(plantId, internalAgentId);

                    return(agentPayload);
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                    throw;
                }
            }
        }
        public async Task <EdgeAgentDeploymentPayload> GetDeployedTemplateByIdAsync(string plantId, string internalAgentId, string id)
        {
            using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name))
            {
                try
                {
                    var edgeAgentPayload = await _orchestrateRepository.GetDeployedTemplateAsync(plantId, internalAgentId, id);

                    return(edgeAgentPayload);
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                    throw;
                }
            }
        }
 public OrchestrateService(
     ILoggerTransaction loggerTransaction,
     IConfigurationService configuration,
     IEdgeAgentDeploymentService edgeAgentDeploymentService,
     IEdgeMessagePipelineService edgeMessagePipelineService,
     IModuleService moduleService)
 {
     _loggerTransaction                  = loggerTransaction;
     _configuration                      = configuration;
     _edgeAgentRepository                = edgeAgentDeploymentService.EdgeAgentRepository;
     _edgeMessagePipelineRepository      = edgeAgentDeploymentService.EdgeMessagePipelineRepository;
     _edgeConfigurationRepository        = edgeAgentDeploymentService.EdgeConfigurationRepository;
     _edgeGatewayConfigurationRepository = edgeAgentDeploymentService.EdgeGatewayConfigurationRepository;
     _edgeAgentDeploymentRepository      = edgeAgentDeploymentService.EdgeAgentDeploymentRepository;
     _ioTHubRepository                   = edgeAgentDeploymentService.IoTHubRepository;
     _orchestrateRepository              = edgeAgentDeploymentService.OrchestrateRepository;
     _edgeMessagePipelineService         = edgeMessagePipelineService;
     _moduleService                      = moduleService;
 }
        public async Task <EdgeAgentDeploymentConfiguration> PrepareDeploymentConfigurationForInternalAgentIdAsync(string plantId, string internalAgentId)
        {
            using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name))
            {
                try
                {
                    var edgeAgent = await _edgeAgentRepository.GetEdgeAgentByIdAsync(plantId, internalAgentId);

                    EdgeAgentDeploymentConfiguration edgeAgentConfigurationTemplate = new EdgeAgentDeploymentConfiguration();
                    edgeAgentConfigurationTemplate.Name             = edgeAgent.AgentName;
                    edgeAgentConfigurationTemplate.MessagePipelines = JObject.FromObject(await GetMessagePipelinesAsync(plantId, internalAgentId));
                    return(edgeAgentConfigurationTemplate);
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                    throw;
                }
            }
        }
        public async Task <EdgeAgentDeploymentPayload> DeployTemplateOnAgentAsync(string plantId, string internalAgentId, EdgeAgentDeploymentPayload edgeAgentPayload)
        {
            using (ILoggerTransaction logger = _loggerTransaction.LogTransaction(GetType().Name))
            {
                try
                {
                    var edgeAgent = await _edgeAgentRepository.GetEdgeAgentByIdAsync(plantId, internalAgentId);

                    EdgeAgentDeploymentPayloadDto edgeAgentPayloadDto = new EdgeAgentDeploymentPayloadDto(edgeAgentPayload);
                    edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent = await _ioTHubRepository.SetDeviceModuleAsync(edgeAgent.AgentId, edgeAgentPayload.DeploymentTemplate.ToString());

                    var edgeAgentDeployedTemplates = await _orchestrateRepository.GetDeployedTemplatesAsync(plantId, internalAgentId);

                    edgeAgent.UpdatedDate     = DateTime.UtcNow.ToString();
                    edgeAgent.ManagedBy       = Constants.ManagedByEdgeV2;
                    edgeAgent.VisibleOnScreen = Constants.VisibleOnScreenEdgeV2;

                    if (edgeAgentDeployedTemplates == null || !edgeAgentDeployedTemplates.Any())
                    {
                        edgeAgent.StartDate = DateTime.UtcNow.ToString();
                    }

                    await _edgeAgentRepository.UpdateEdgeAgentAsync(edgeAgent.Id, edgeAgent);

                    var deployTemplates = await _orchestrateRepository.GetDeployedTemplatesAsync(plantId, internalAgentId);

                    if (deployTemplates?.Any() == true)
                    {
                        var fileterdeployTemplates = deployTemplates.Where(template => template.IsLatestDeployedTemplate);
                        foreach (var template in fileterdeployTemplates)
                        {
                            template.IsLatestDeployedTemplate = false;
                            await _orchestrateRepository.UpdateDeployedTemplateAsync(template.Id, template);
                        }
                    }

                    if (string.IsNullOrEmpty(edgeAgentPayloadDto.Id))
                    {
                        edgeAgentPayloadDto.IsDeleted                = false;
                        edgeAgentPayloadDto.PlantId                  = plantId;
                        edgeAgentPayloadDto.InternalAgentId          = internalAgentId;
                        edgeAgentPayloadDto.IsLatestDeployedTemplate = true;
                        edgeAgentPayloadDto.AgentConfigurationName   = string.Format(AgentonfigurationNameTemplate, edgeAgent.AgentName, DateTime.UtcNow.ToString());
                        await _orchestrateRepository.AddDeployedTemplateAsync(edgeAgentPayloadDto);
                    }
                    else
                    {
                        var deployedTemplate = deployTemplates.FirstOrDefault(template => template.Id == edgeAgentPayloadDto.Id);
                        deployedTemplate.UserComments                       = edgeAgentPayloadDto.UserComments;
                        deployedTemplate.DeploymentConfiguration            = edgeAgentPayloadDto.DeploymentConfiguration;
                        deployedTemplate.DeploymentTemplate                 = edgeAgentPayloadDto.DeploymentTemplate;
                        deployedTemplate.IsLatestDeployedTemplate           = true;
                        deployedTemplate.AgentConfigurationName             = edgeAgentPayloadDto.AgentConfigurationName;
                        deployedTemplate.IsApplyConfigurationContentOnAgent = edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent;
                        await _orchestrateRepository.UpdateDeployedTemplateAsync(edgeAgentPayloadDto.Id, deployedTemplate);
                    }

                    return(new EdgeAgentDeploymentPayload
                    {
                        Id = edgeAgentPayloadDto.Id,
                        IsApplyConfigurationContentOnAgent = edgeAgentPayloadDto.IsApplyConfigurationContentOnAgent,
                        DeploymentTemplate = edgeAgentPayloadDto.DeploymentTemplate,
                        AgentConfigurationName = edgeAgentPayloadDto.AgentConfigurationName,
                        DeploymentConfiguration = edgeAgentPayload.DeploymentConfiguration,
                        UserComments = edgeAgentPayloadDto.UserComments,
                        IsLatestDeployedTemplate = edgeAgentPayloadDto.IsLatestDeployedTemplate
                    });
                }
                catch (Exception ex)
                {
                    logger.LogException(ex);
                    throw;
                }
            }
        }