示例#1
0
        public void Test_TagFileArchive_Failed()
        {
            SetupDITfa();

            byte[] tagContent;
            using (var tagFileStream =
                       new FileStream(Path.Combine("TestData", "TAGFiles", "TestTAGFile.tag"),
                                      FileMode.Open, FileAccess.Read))
            {
                tagContent = new byte[tagFileStream.Length];
                tagFileStream.Read(tagContent, 0, (int)tagFileStream.Length);
            }

            var td = new TagFileDetail()
            {
                assetId        = Guid.Parse("{00000000-0000-0000-0000-000000000001}"),
                projectId      = Guid.Parse("{00000000-0000-0000-0000-000000000001}"),
                tagFileName    = "Test.tag",
                tagFileContent = null,
                tccOrgId       = "",
                IsJohnDoe      = false
            };

            Assert.False(TagFileRepository.ArchiveTagfileS3(td).Result, "Failed to validate null data archive");
        }
示例#2
0
        /// <summary>
        /// Receive a TAG file to be processed, validate TAG File Authorization for the file, and add it to the
        /// queue to be processed.
        /// </summary>
        /// <param name="projectId">Project ID to be used as an override to any project ID that may be determined via TAG file authorization</param>
        /// <param name="assetId">Asset ID to be used as an override to any Asset ID that may be determined via TAG file authorization</param>
        /// <param name="tagFileName">Name of the physical tag file for archiving and logging</param>
        /// <param name="tagFileContent">The content of the TAG file to be processed, expressed as a byte array</param>
        /// <param name="tccOrgId">Used by TFA service to match VL customer to TCC org when looking for project if multiple projects and/or machine ID not in tag file</param>
        /// <param name="treatAsJohnDoe">The TAG file will be processed as if it were a john doe machine is projectId is also specified</param>
        /// <param name="tagFileSubmissionFlags">A flag set controlling how certain aspects of managing a submitted TAG file should be managed</param>
        /// <param name="originSource">Indictaes the system of origin this file came from</param>
        public async Task <SubmitTAGFileResponse> ExecuteAsync(Guid?projectId, Guid?assetId, string tagFileName, byte[] tagFileContent,
                                                               string tccOrgId, bool treatAsJohnDoe, TAGFileSubmissionFlags tagFileSubmissionFlags, TAGFileOriginSource originSource)
        {
            if (OutputInformationalRequestLogging)
            {
                _log.LogInformation($"#In# SubmitTAGFileResponse. Processing {tagFileName} TAG file into ProjectUID:{projectId}, asset:{assetId}, John Doe?:{treatAsJohnDoe}, submission flags: {tagFileSubmissionFlags}, origin source:{originSource}");
            }

            var response = new SubmitTAGFileResponse
            {
                FileName = tagFileName,
                Success  = false,
                Message  = "TRex unknown result (SubmitTAGFileResponse.Execute)",
                Code     = (int)TRexTagFileResultCode.TRexUnknownException,
            };

            try
            {
                try
                {
                    // wrap up details into obj
                    var td = new TagFileDetail
                    {
                        assetId        = assetId,
                        projectId      = projectId,
                        tagFileName    = tagFileName,
                        tagFileContent = tagFileContent,
                        tccOrgId       = tccOrgId,
                        IsJohnDoe      = treatAsJohnDoe
                    };

                    ContractExecutionResult result;

                    if (originSource == TAGFileOriginSource.LegacyTAGFileSource)
                    {
                        // Validate tag file submission
                        result = TagfileValidator.PreScanTagFile(td, out var tagFilePreScan);

                        if (result.Code == (int)TRexTagFileResultCode.Valid)
                        {
                            if (_isDeviceGatewayEnabled)
                            {
                                SendDeviceStatusToDeviceGateway(td, tagFilePreScan);
                            }

                            result = await TagfileValidator.ValidSubmission(td, tagFilePreScan);
                        }
                    }
                    else
                    {
                        // For non legacy origins where we have no overt validation rules or need for device status notifications
                        // note the presented file as valid for processing
                        result = new ContractExecutionResult((int)TRexTagFileResultCode.Valid, "Success");
                    }

                    response.Code    = result.Code;
                    response.Message = result.Message;

                    if (result.Code == (int)TRexTagFileResultCode.Valid && td.projectId != null) // If OK add to process queue
                    {
                        // First archive the tag file
                        if (_tagFileArchiving && tagFileSubmissionFlags.HasFlag(TAGFileSubmissionFlags.AddToArchive)
                            // For now, GCS900/Earthworks style TAG files are the only ones archived
                            && originSource == TAGFileOriginSource.LegacyTAGFileSource)
                        {
                            _log.LogInformation($"#Progress# SubmitTAGFileResponse. Archiving tag file:{tagFileName}, ProjectUID:{td.projectId}");
                            if (!await TagFileRepository.ArchiveTagfileS3(td))
                            {
                                _log.LogError($"SubmitTAGFileResponse. Failed to archive tag file. Returning TRexQueueSubmissionError error. ProjectUID:{td.projectId}, AssetUID:{td.assetId}, Tagfile:{tagFileName}");
                                throw new ServiceException(HttpStatusCode.InternalServerError, new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError, $"SubmitTAGFileResponse. Failed to archive tag file {tagFileName} to S3"));
                            }
                        }

                        // switch from nullable to not nullable
                        var validProjectId = td.projectId ?? Guid.Empty;
                        var validAssetId   = td.assetId ?? Guid.Empty;

                        if (OutputInformationalRequestLogging)
                        {
                            _log.LogInformation($"#Progress# SubmitTAGFileResponse. Submitting tag file to TagFileBufferQueue. ProjectUID:{validProjectId}, AssetUID:{validAssetId}, Tagfile:{tagFileName}, JohnDoe:{td.IsJohnDoe} ");
                        }

                        var tagKey  = new TAGFileBufferQueueKey(tagFileName, validProjectId, validAssetId);
                        var tagItem = new TAGFileBufferQueueItem
                        {
                            InsertUTC       = DateTime.UtcNow,
                            ProjectID       = validProjectId,
                            AssetID         = validAssetId,
                            FileName        = tagFileName,
                            Content         = tagFileContent,
                            IsJohnDoe       = td.IsJohnDoe,
                            SubmissionFlags = tagFileSubmissionFlags,
                            OriginSource    = originSource
                        };

                        if (_queue == null)
                        {
                            throw new ServiceException(HttpStatusCode.InternalServerError, new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError, "SubmitTAGFileResponse. Processing queue not available"));
                        }

                        if (_queue.Add(tagKey, tagItem)) // Add tag file to queue
                        {
                            response.Success = true;
                            response.Message = "";
                            response.Code    = (int)TRexTagFileResultCode.Valid;

                            // Commented out top reduce logging
                            // Log.LogInformation($"Added TAG file {tagKey.FileName} representing asset {tagKey.AssetUID} within project {tagKey.ProjectUID} into the buffer queue");
                        }
                        else
                        {
                            response.Success = false;
                            response.Message = "SubmitTAGFileResponse. Failed to submit tag file to processing queue. Request already exists";
                            response.Code    = (int)TRexTagFileResultCode.TRexQueueSubmissionError;

                            _log.LogWarning(response.Message);
                        }
                    }
                    else
                    {
                        response.Success = false;
                    }
                }
                catch (Exception e) // catch all exceptions here
                {
                    _log.LogError(e, $"#Exception# SubmitTAGFileResponse. Exception occured processing {tagFileName} Exception:");
                    throw new ServiceException(HttpStatusCode.InternalServerError, new ContractExecutionResult(ContractExecutionStatesEnum.InternalProcessingError, $"SubmitTAGFileResponse. Exception {e.Message}"));
                }
            }
            finally
            {
                if (OutputInformationalRequestLogging)
                {
                    _log.LogInformation($"#Out# SubmitTAGFileResponse. Processed {tagFileName} Result: {response.Success}, Message:{response.Message} Code:{response.Code}");
                }
            }
            return(response);
        }