Exemple #1
0
        public void Test_TAGFileBufferQueue_AddingTAGFile()
        {
            EnsureServer();

            var queue = new TAGFileBufferQueue();

            Assert.NotNull(queue);

            // Load a TAG file and add it to the queue. Verify the TAG file appears in the cache

            var tagFileName = "TestTAGFile-TAGFile-Read-Stream.tag";

            var projectUid = Guid.NewGuid();
            var assetUid   = Guid.NewGuid();

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

            var tagKey  = new TAGFileBufferQueueKey(tagFileName, projectUid, assetUid);
            var tagItem = new TAGFileBufferQueueItem
            {
                InsertUTC = DateTime.UtcNow,
                ProjectID = projectUid,
                AssetID   = assetUid,
                FileName  = tagFileName,
                Content   = tagContent
            };

            // Perform the actual add
            queue.Add(tagKey, tagItem);

            // Read it back from the cache to ensure it was added as expected.
            var queueCache = _ignite.GetCache <ITAGFileBufferQueueKey, TAGFileBufferQueueItem>(TRexCaches.TAGFileBufferQueueCacheName());

            var tagItem2 = queueCache.Get(tagKey);

            Assert.True(tagItem2 != null, "Tag item read back from buffer queue cache was null");
            Assert.True(tagItem.Content.Length == tagItem2.Content.Length, "Tag content lengths different");
            Assert.True(tagItem.InsertUTC == tagItem2.InsertUTC, "Tag insert UTCs different");
            Assert.True(tagItem.AssetID == tagItem2.AssetID, "Tag AssetUIDs different");
            Assert.True(tagItem.FileName == tagItem2.FileName, "Tag FileNames different");
            Assert.True(tagItem.ProjectID == tagItem2.ProjectID, "Tag ProjectUIDs different");
        }
Exemple #2
0
        public void Test_TAGFileBufferQueueItem()
        {
            var testDate = DateTime.SpecifyKind(new DateTime(2000, 1, 1, 1, 1, 1, 1), DateTimeKind.Utc);
            var filter   = new TAGFileBufferQueueItem
            {
                Content   = new byte[] { 1, 10, 25, 100 },
                InsertUTC = testDate,
                ProjectID = Guid.NewGuid(),
                AssetID   = Guid.NewGuid(),
                IsJohnDoe = true,
                FileName  = "fileName"
            };

            var result = SimpleBinarizableInstanceTester.TestClass(filter, "Custom TAGFileBufferQueueItem not same after round trip serialisation");

            Assert.True(result.member.FileName.Equals("fileName") &&
                        result.member.InsertUTC.Equals(testDate),
                        "Post IEquality<T> comparer based comparison failure");
        }
Exemple #3
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);
        }
Exemple #4
0
        public void Test_TAGFileBufferQueueItem_Creation()
        {
            var item = new TAGFileBufferQueueItem();

            item.Should().NotBeNull();
        }
Exemple #5
0
 /// <summary>
 /// Adds a new TAG file to the buffer queue.
 /// </summary>
 /// <param name="key"></param>
 /// <param name="value"></param>
 /// <returns>If an element with this key already exists in the cache this method will false, true otherwise</returns>
 public bool Add(ITAGFileBufferQueueKey key, TAGFileBufferQueueItem value)
 {
     return(_queueCache.PutIfAbsent(key, value));
 }