Ejemplo n.º 1
0
        public async Task <Boolean> Handle(UpdateDHCPv6ScopeCommand request, CancellationToken cancellationToken)
        {
            _logger.LogDebug("Handle started");

            var scope = _rootScope.GetScopeById(request.ScopeId);

            if (scope == DHCPv6Scope.NotFound)
            {
                return(false);
            }

            Guid?parentId          = scope.HasParentScope() == false ? new Guid?() : scope.ParentScope.Id;
            var  properties        = GetScopeProperties(request);
            var  addressProperties = GetScopeAddressProperties(request);

            if (request.Name != scope.Name)
            {
                _rootScope.UpdateScopeName(request.ScopeId, ScopeName.FromString(request.Name));
            }
            if (request.Description != scope.Description)
            {
                _rootScope.UpdateScopeDescription(request.ScopeId, ScopeDescription.FromString(request.Description));
            }
            if (request.ParentId != parentId)
            {
                _rootScope.UpdateParent(request.ScopeId, request.ParentId);
            }

            _rootScope.UpdateScopeResolver(request.ScopeId, GetResolverInformation(request));

            if (addressProperties != scope.AddressRelatedProperties)
            {
                _rootScope.UpdateAddressProperties(request.ScopeId, addressProperties);
            }

            if (properties != scope.Properties)
            {
                _rootScope.UpdateScopeProperties(request.ScopeId, properties);
            }

            Boolean result = await _store.Save(_rootScope);

            if (result == true)
            {
                var triggers = _rootScope.GetTriggers();

                if (triggers.Any() == true)
                {
                    await _serviceBus.Publish(new NewTriggerHappendMessage(triggers));

                    _rootScope.ClearTriggers();
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task Handle(Boolean storeResult, Boolean useDynamicTime)
        {
            Random random = new Random();

            Guid scopeId = random.NextGuid();
            Guid leaseId = random.NextGuid();

            String name        = random.GetAlphanumericString();
            String description = random.GetAlphanumericString();

            IPv6Address start = random.GetIPv6Address();
            IPv6Address end   = start + 100;

            String resolverName = random.GetAlphanumericString();

            Mock <IScopeResolver <DHCPv6Packet, IPv6Address> > resolverMock = new Mock <IScopeResolver <DHCPv6Packet, IPv6Address> >(MockBehavior.Strict);

            resolverMock.Setup(x => x.GetValues()).Returns(new Dictionary <string, string>());
            Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> > scopeResolverMock = new Mock <IScopeResolverManager <DHCPv6Packet, IPv6Address> >();

            scopeResolverMock.Setup(x => x.IsResolverInformationValid(It.Is <CreateScopeResolverInformation>(y =>
                                                                                                             y.Typename == resolverName
                                                                                                             ))).Returns(true).Verifiable();
            scopeResolverMock.Setup(x => x.InitializeResolver(It.Is <CreateScopeResolverInformation>(y =>
                                                                                                     y.Typename == resolverName
                                                                                                     ))).Returns(resolverMock.Object).Verifiable();

            Mock <ILoggerFactory> factoryMock = new Mock <ILoggerFactory>(MockBehavior.Strict);

            factoryMock.Setup(x => x.CreateLogger(It.IsAny <String>())).Returns(Mock.Of <ILogger <DHCPv6RootScope> >());

            DHCPv6RootScope rootScope = new DHCPv6RootScope(random.NextGuid(), scopeResolverMock.Object, factoryMock.Object);

            rootScope.Load(new DomainEvent[] { new DHCPv6ScopeAddedEvent {
                                                   Instructions = new DHCPv6ScopeCreateInstruction
                                                   {
                                                       Name                = random.GetAlphanumericString(),
                                                       Description         = random.GetAlphanumericString(),
                                                       Id                  = scopeId,
                                                       ParentId            = null,
                                                       ResolverInformation = new CreateScopeResolverInformation
                                                       {
                                                           Typename = resolverName,
                                                       },
                                                       ScopeProperties   = new DHCPv6ScopeProperties(),
                                                       AddressProperties = DHCPv6ScopeAddressProperties.Empty,
                                                   }
                                               },
                                               new DHCPv6LeaseCreatedEvent
                                               {
                                                   EntityId                      = leaseId,
                                                   Address                       = start - 100,
                                                   ScopeId                       = scopeId,
                                                   HasPrefixDelegation           = true,
                                                   PrefixLength                  = 64,
                                                   ClientIdentifier              = new UUIDDUID(random.NextGuid()),
                                                   IdentityAssocationIdForPrefix = random.NextUInt32(),
                                                   DelegatedNetworkAddress       = IPv6Address.FromString("fc70::0"),
                                               },
                                               new DHCPv6LeaseActivatedEvent
                                               {
                                                   EntityId = leaseId,
                                                   ScopeId  = scopeId,
                                               } });

            Mock <IDHCPv6StorageEngine> storageMock = new Mock <IDHCPv6StorageEngine>(MockBehavior.Strict);

            storageMock.Setup(x => x.Save(rootScope)).ReturnsAsync(storeResult).Verifiable();

            var command = new UpdateDHCPv6ScopeCommand(scopeId, name, description, null,
                                                       useDynamicTime == false ? new DHCPv6ScopeAddressPropertyReqest
            {
                Start                     = start.ToString(),
                End                       = end.ToString(),
                ExcludedAddresses         = Array.Empty <String>(),
                AcceptDecline             = random.NextBoolean(),
                AddressAllocationStrategy = DHCPv6ScopeAddressPropertyReqest.AddressAllocationStrategies.Next,
                InformsAreAllowd          = random.NextBoolean(),
                RapitCommitEnabled        = random.NextBoolean(),
                ReuseAddressIfPossible    = random.NextBoolean(),
                SupportDirectUnicast      = random.NextBoolean(),
                PreferredLifeTime         = TimeSpan.FromDays(0.5),
                ValidLifeTime             = TimeSpan.FromDays(1),
                PrefixDelgationInfo       = new DHCPv6PrefixDelgationInfoRequest
                {
                    AssingedPrefixLength = 80,
                    Prefix       = "fe80::0",
                    PrefixLength = 64
                },
                T1 = 0.3,
                T2 = 0.65,
            } : new DHCPv6ScopeAddressPropertyReqest
            {
                Start                     = start.ToString(),
                End                       = end.ToString(),
                ExcludedAddresses         = Array.Empty <String>(),
                AcceptDecline             = random.NextBoolean(),
                AddressAllocationStrategy = DHCPv6ScopeAddressPropertyReqest.AddressAllocationStrategies.Next,
                InformsAreAllowd          = random.NextBoolean(),
                RapitCommitEnabled        = random.NextBoolean(),
                ReuseAddressIfPossible    = random.NextBoolean(),
                SupportDirectUnicast      = random.NextBoolean(),
                PrefixDelgationInfo       = new DHCPv6PrefixDelgationInfoRequest
                {
                    AssingedPrefixLength = 80,
                    Prefix       = "fe80::0",
                    PrefixLength = 64
                },
                DynamicRenewTime = new DHCPv6DynamicRenewTimeRequest
                {
                    Hours              = 5,
                    Minutes            = 10,
                    MinutesToEndOfLife = 45,
                    MinutesToRebound   = 35,
                },
            },
                                                       new CreateScopeResolverRequest
            {
                PropertiesAndValues = new Dictionary <String, String>(),
                Typename            = resolverName,
            },
                                                       new[] { new DHCPv6AddressListScopePropertyRequest
                                                               {
                                                                   OptionCode = 24,
                                                                   Type       = Beer.DaAPI.Core.Scopes.DHCPv6.ScopeProperties.DHCPv6ScopePropertyType.AddressList,
                                                                   Addresses  = random.GetIPv6Addresses().Select(x => x.ToString()).ToArray(),
                                                               },
                                                               new DHCPv6AddressListScopePropertyRequest
                                                               {
                                                                   OptionCode = 64,
                                                                   MarkAsRemovedInInheritance = true,
                                                               } }
                                                       );

            var serviceBusMock = new Mock <IServiceBus>(MockBehavior.Strict);

            serviceBusMock.Setup(x => x.Publish(It.Is <NewTriggerHappendMessage>(y => y.Triggers.Count() == 1 &&
                                                                                 ((PrefixEdgeRouterBindingUpdatedTrigger)y.Triggers.First()).OldBinding.Prefix == IPv6Address.FromString("fc70::0")
                                                                                 ))).Returns(Task.CompletedTask);
            var handler = new UpdateDHCPv6ScopeCommandHandler(storageMock.Object, serviceBusMock.Object, rootScope,
                                                              Mock.Of <ILogger <UpdateDHCPv6ScopeCommandHandler> >());

            Boolean result = await handler.Handle(command, CancellationToken.None);

            Assert.Equal(storeResult, result);

            var scope = rootScope.GetRootScopes().First();

            Assert.Equal(name, scope.Name);
            Assert.Equal(description, scope.Description);
            Assert.NotNull(scope.Resolver);
            Assert.Equal(start, scope.AddressRelatedProperties.Start);

            Assert.True(scope.Properties.IsMarkedAsRemovedFromInheritance(64));
            Assert.False(scope.Properties.IsMarkedAsRemovedFromInheritance(24));

            if (useDynamicTime == true)
            {
                Assert.True(scope.AddressRelatedProperties.UseDynamicRewnewTime);
                Assert.Equal(5, scope.AddressRelatedProperties.DynamicRenewTime.Hour);
            }
            else
            {
                Assert.False(scope.AddressRelatedProperties.UseDynamicRewnewTime);
            }

            scopeResolverMock.Verify();
            storageMock.Verify();


            if (storeResult == true)
            {
                serviceBusMock.Verify();
                Assert.Empty(rootScope.GetTriggers());
            }
            else
            {
                Assert.Single(rootScope.GetTriggers());
            }
        }