示例#1
0
 public async Task <List <NetworkInterface> > Get(
     [FromServices] PnPServerContext dbContext,
     Guid networkDeviceTypeId
     )
 {
     return(await dbContext.NetworkInterfaces.Where(x => x.DeviceType.Id == networkDeviceTypeId).ToListAsync());
 }
示例#2
0
        public async Task <IActionResult> Get(
            [FromServices] PnPServerContext dbContext,
            Guid networkDeviceTypeId,
            Guid id
            )
        {
            var item = await dbContext.NetworkInterfaces
                       .Include("DeviceType")
                       .Where(
                x =>
                x.Id == id &&
                x.DeviceType.Id == networkDeviceTypeId
                )
                       .FirstOrDefaultAsync();

            if (item == null)
            {
                return(NotFound());
            }

            // TODO : Come up with a better solution for coping with cyclic references between interface and device type
            item.DeviceType.Interfaces = null;

            return(new ObjectResult(item));
        }
示例#3
0
        private void OnReadRequest(
            ITftpTransfer transfer,
            EndPoint client
            )
        {
            var connectionString = Startup.Configuration.GetValue <string>("Data:DefaultConnection:ConnectionString");
            var dbOptions        = new DbContextOptionsBuilder <PnPServerContext>();

            dbOptions.UseSqlServer(connectionString);

            var dbContext = new PnPServerContext(dbOptions.Options);

            string ipAddress = "";

            if (client.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                ipAddress = (client as IPEndPoint).Address.ToString();
            }
            else if (client.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                ipAddress = (client as IPEndPoint).Address.ToString();
            }
            var networkDevice = dbContext.NetworkDevices.Where(x => x.IPAddress == ipAddress).FirstOrDefault();

            if (networkDevice == null)
            {
                transfer.Cancel(TftpErrorPacket.FileNotFound);
            }
            else
            {
                var config = Task.Run <string>(() => { return(ConfigurationGenerator.Generator.Generate(ipAddress, dbContext)); }).Result;
                var data   = new MemoryStream(Encoding.ASCII.GetBytes(config));
                transfer.Start(data);
            }
        }
示例#4
0
        public async Task <IActionResult> Put(
            [FromServices] PnPServerContext dbContext,
            Guid id,
            [FromBody] Template template
            )
        {
            if (template == null || template.Id != id)
            {
                return(BadRequest());
            }

            var existingRecord = await dbContext.Templates.FirstOrDefaultAsync(x => x.Id == id);

            if (existingRecord == null)
            {
                return(NotFound());
            }

            existingRecord.Name    = template.Name;
            existingRecord.Content = template.Content;

            dbContext.Templates.Update(existingRecord);
            await dbContext.SaveChangesAsync();

            return(new NoContentResult());
        }
示例#5
0
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            [FromBody] NetworkDeviceType networkDeviceType
            )
        {
            if (networkDeviceType == null)
            {
                return(BadRequest());
            }

            var existingRecord = await dbContext.NetworkDeviceTypes
                                 .Where(x =>
                                        x.Name.Equals(networkDeviceType.Name, StringComparison.OrdinalIgnoreCase)
                                        )
                                 .FirstOrDefaultAsync();

            if (existingRecord != null)
            {
                // TODO : Make it so bed request explains duplicate record
                return(BadRequest());
            }

            await dbContext.NetworkDeviceTypes.AddAsync(networkDeviceType);

            await dbContext.SaveChangesAsync();

            return(new CreatedAtRouteResult("GetNetworkDeviceType", new { id = networkDeviceType.Id }, networkDeviceType));
        }
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            [FromBody] NetworkDeviceLink networkDeviceLink
            )
        {
            if (
                networkDeviceLink == null ||
                networkDeviceLink.Id != Guid.Empty ||
                networkDeviceLink.NetworkDevice == null ||
                networkDeviceLink.NetworkDevice.Id == Guid.Empty ||
                networkDeviceLink.ConnectedToDevice == null ||
                networkDeviceLink.ConnectedToDevice.Id == Guid.Empty
                )
            {
                return(BadRequest());
            }

            var networkDevice = await dbContext.NetworkDevices
                                .Include("Uplinks")
                                .Where(x => x.Id == networkDeviceLink.NetworkDevice.Id)
                                .FirstOrDefaultAsync();

            if (networkDevice == null)
            {
                System.Diagnostics.Debug.WriteLine("Invalid network device " + networkDeviceLink.NetworkDevice.Id.ToString());
                return(NotFound());
            }

            var connectedToDevice = await dbContext.NetworkDevices
                                    .Where(x => x.Id == networkDeviceLink.ConnectedToDevice.Id)
                                    .FirstOrDefaultAsync();

            if (connectedToDevice == null)
            {
                System.Diagnostics.Debug.WriteLine("Invalid connected to network device " + networkDeviceLink.NetworkDevice.Id.ToString());
                return(NotFound());
            }

            // TODO : Check other links to see if the source or destination ports are in use elsewhere

            var newLink = new NetworkDeviceLink
            {
                InterfaceIndex            = networkDeviceLink.InterfaceIndex,
                ConnectedToDevice         = connectedToDevice,
                ConnectedToInterfaceIndex = networkDeviceLink.ConnectedToInterfaceIndex
            };

            networkDevice.Uplinks.Add(newLink);

            dbContext.NetworkDevices.Update(networkDevice);
            await dbContext.SaveChangesAsync();

            newLink.NetworkDevice.Uplinks = null;

            return(new CreatedAtRouteResult("GetNetworkDeviceLink", new { id = newLink.Id }, newLink));
        }
示例#7
0
 public async Task <List <TemplateProperty> > Get(
     [FromServices] PnPServerContext dbContext,
     Guid templateId,
     Guid configurationId
     )
 {
     return(await dbContext.TemplateProperties
            .Where(x =>
                   x.TemplateConfiguration.Id == configurationId
                   )
            .ToListAsync());
 }
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            Guid templateId,
            [FromBody] TemplateConfiguration templateConfiguration
            )
        {
            if (
                templateId == Guid.Empty ||
                templateConfiguration == null ||
                (
                    templateConfiguration.Template != null &&
                    templateConfiguration.Template.Id != Guid.Empty &&
                    templateConfiguration.Template.Id != templateId
                ) ||
                templateConfiguration.NetworkDevice == null ||
                templateConfiguration.NetworkDevice.Id == Guid.Empty
                )
            {
                System.Diagnostics.Debug.WriteLine("Invalid parameters passed");
                return(BadRequest());
            }

            var template = await dbContext.Templates.Where(x => x.Id == templateId).FirstOrDefaultAsync();

            if (template == null)
            {
                System.Diagnostics.Debug.WriteLine("Invalid template id specified " + templateId.ToString());
                return(NotFound());
            }

            var networkDevice = await dbContext.NetworkDevices.Where(x => x.Id == templateConfiguration.NetworkDevice.Id).FirstOrDefaultAsync();

            if (networkDevice == null)
            {
                System.Diagnostics.Debug.WriteLine("Invalid network device specified " + templateConfiguration.NetworkDevice.Id.ToString());
                return(NotFound());
            }

            templateConfiguration.Template      = template;
            templateConfiguration.NetworkDevice = networkDevice;

            await dbContext.TemplateConfigurations.AddAsync(templateConfiguration);

            await dbContext.SaveChangesAsync();

            // TODO : Better solution to handling cyclic reference
            for (var i = 0; i < templateConfiguration.Properties.Count; i++)
            {
                templateConfiguration.Properties[i].TemplateConfiguration = null;
            }

            return(new CreatedAtRouteResult("GetTemplateConfiguration", new { id = templateConfiguration.Id }, templateConfiguration));
        }
        public async Task <List <NetworkDeviceLink> > Get(
            [FromServices] PnPServerContext dbContext,
            Guid networkDeviceId
            )
        {
            // TODO : Make sure to get network device and connected to device from the database
            var result = await dbContext.NetworkDeviceLinks
                         .Where(x =>
                                x.NetworkDevice.Id == networkDeviceId
                                )
                         .ToListAsync();

            return(result);
        }
示例#10
0
        public async Task <IActionResult> Get(
            [FromServices] PnPServerContext dbContext,
            Guid id
            )
        {
            var item = await dbContext.NetworkDeviceTypes.FindAsync(id);

            if (item == null)
            {
                return(NotFound());
            }

            return(new ObjectResult(item));
        }
示例#11
0
        public async Task <IEnumerable <WeatherForecast> > WeatherForecasts(
            [FromServices] PnPServerContext dbContext
            )
        {
            var value = await dbContext.NetworkDevices.ToListAsync();

            var rng = new Random();

            return(Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                DateFormatted = DateTime.Now.AddDays(index).ToString("d"),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            }));
        }
        public async Task <IActionResult> TestConfiguration(
            [FromServices] PnPServerContext dbContext,
            Guid id
            )
        {
            var item = await dbContext.NetworkDevices.FindAsync(id);

            if (item == null)
            {
                return(NotFound());
            }

            var config = await Services.ConfigurationGenerator.Generator.Generate(item.IPAddress, dbContext);

            return(new ObjectResult(new { text = config }));
        }
示例#13
0
        public async Task <IActionResult> Get(
            [FromServices] PnPServerContext dbContext,
            Guid id
            )
        {
            var item = await dbContext.Templates
                       .Where(x => x.Id == id)
                       .FirstOrDefaultAsync();

            if (item == null)
            {
                return(NotFound());
            }

            return(new ObjectResult(item));
        }
示例#14
0
        public async Task <IActionResult> Delete(
            [FromServices] PnPServerContext dbContext,
            Guid id
            )
        {
            var item = await dbContext.Templates.FirstOrDefaultAsync(x => x.Id == id);

            if (item == null)
            {
                return(NotFound());
            }

            dbContext.Templates.Remove(item);
            await dbContext.SaveChangesAsync();

            return(new NoContentResult());
        }
示例#15
0
        public async Task <IActionResult> Delete(
            [FromServices] PnPServerContext dbContext,
            Guid id
            )
        {
            var item = await dbContext.NetworkDeviceTypes.FindAsync(id);

            if (item == null)
            {
                return(NotFound());
            }

            dbContext.NetworkDeviceTypes.Remove(item);
            await dbContext.SaveChangesAsync();

            return(new NoContentResult());
        }
示例#16
0
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            Guid networkDeviceTypeId,
            [FromBody] NetworkInterface networkInterface
            )
        {
            if (networkDeviceTypeId == Guid.Empty ||
                networkInterface == null ||
                string.IsNullOrEmpty(networkInterface.Name) ||
                (networkInterface.DeviceType != null && networkInterface.DeviceType.Id != networkDeviceTypeId)
                )
            {
                return(BadRequest());
            }

            var networkDeviceType = await dbContext.NetworkDeviceTypes.Include("Interfaces").FirstOrDefaultAsync(x => x.Id == networkDeviceTypeId);

            if (networkDeviceType == null)
            {
                System.Diagnostics.Debug.WriteLine("Network device type " + networkDeviceTypeId.ToString() + " does not exist");
                return(BadRequest());
            }

            var existingRecord = await dbContext.NetworkInterfaces
                                 .FirstOrDefaultAsync(x =>
                                                      x.Name == networkInterface.Name &&
                                                      x.DeviceType.Id == networkDeviceTypeId
                                                      );

            if (existingRecord != null)
            {
                System.Diagnostics.Debug.WriteLine("Network interface " + networkInterface.Name + " for device type " + networkDeviceType.Name + " already exists");
                return(BadRequest());
            }

            networkDeviceType.Interfaces.Add(networkInterface);
            dbContext.Update(networkDeviceType);

            await dbContext.SaveChangesAsync();

            // TODO : Come up with a better solution for coping with cyclic references between interface and device type
            networkInterface.DeviceType.Interfaces = null;

            return(new CreatedAtRouteResult("GetNetworkInterface", new { networkDeviceTypeId = networkDeviceTypeId, id = networkInterface.Id }, networkInterface));
        }
        public async Task <List <TemplateConfiguration> > Get(
            [FromServices] PnPServerContext dbContext,
            Guid templateId
            )
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            var result = await dbContext.TemplateConfigurations
                         .Include("NetworkDevice")
                         .Include("Template")
                         .Where(x =>
                                x.Template.Id == templateId
                                )
                         .ToListAsync();

            return(result);
        }
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            [FromBody] NetworkDevice networkDevice
            )
        {
            if (networkDevice == null || networkDevice.DeviceType == null || networkDevice.DeviceType.Id == null)
            {
                return(BadRequest());
            }

            var existingRecord = await dbContext.NetworkDevices
                                 .Where(x =>
                                        x.Hostname.Equals(networkDevice.Hostname, StringComparison.OrdinalIgnoreCase) &&
                                        x.DomainName.Equals(networkDevice.DomainName, StringComparison.OrdinalIgnoreCase)
                                        )
                                 .FirstOrDefaultAsync();

            if (existingRecord != null)
            {
                System.Diagnostics.Debug.WriteLine("Network device with name " + networkDevice.Hostname + "." + networkDevice.DomainName + " already exists");
                return(BadRequest());
            }

            var networkDeviceType = await dbContext.NetworkDeviceTypes.FindAsync(networkDevice.DeviceType.Id);

            if (networkDeviceType == null)
            {
                System.Diagnostics.Debug.WriteLine("Network device type " + networkDevice.DeviceType.Id.ToString() + " does not exist");
                return(BadRequest());
            }

            networkDevice.DeviceType = networkDeviceType;

            await dbContext.NetworkDevices.AddAsync(networkDevice);

            await dbContext.SaveChangesAsync();

            return(new CreatedAtRouteResult("GetNetworkDevice", new { id = networkDevice.Id }, networkDevice));
        }
示例#19
0
        public async static Task <string> Generate(
            string ipAddress,
            PnPServerContext dbContext
            )
        {
            var templateConfig = await dbContext.TemplateConfigurations
                                 .Include("Template")
                                 .Include("NetworkDevice")
                                 .Include("Properties")
                                 .Where(x => x.NetworkDevice.IPAddress == ipAddress)
                                 .FirstOrDefaultAsync();

            if (templateConfig == null)
            {
                // TODO : Throw
                return(string.Empty);
            }

            string template = templateConfig.Template.Content;

            var context = new VelocityContext();

            foreach (var prop in templateConfig.Properties)
            {
                context.Put(prop.Name, prop.Value);
            }

            var engine = new VelocityEngine();

            engine.Init();

            var outputWriter = new StringWriter();

            engine.Evaluate(context, outputWriter, "eval1", template);
            var templateText = outputWriter.GetStringBuilder().ToString();

            return(templateText);
        }
示例#20
0
        public async Task <IActionResult> Post(
            [FromServices] PnPServerContext dbContext,
            [FromBody] Template template
            )
        {
            if (template == null || string.IsNullOrEmpty(template.Name) || string.IsNullOrEmpty(template.Content) || template.Id != Guid.Empty)
            {
                return(BadRequest());
            }

            var existingRecord = await dbContext.Templates.FirstOrDefaultAsync(x => x.Name == template.Name);

            if (existingRecord != null)
            {
                // TODO : Proper error for duplicate name record
                return(BadRequest());
            }

            await dbContext.Templates.AddAsync(template);

            await dbContext.SaveChangesAsync();

            return(new CreatedAtRouteResult("GetTemplate", new { id = template.Id }, template));
        }
示例#21
0
        public async Task <IActionResult> PostRange(
            [FromServices] PnPServerContext dbContext,
            Guid networkDeviceTypeId,
            [FromBody] PostNetworkInterfaceRangeViewModel range
            )
        {
            // TODO : Additional verification on range validity
            if (networkDeviceTypeId == Guid.Empty ||
                range == null ||
                string.IsNullOrEmpty(range.Name)
                )
            {
                return(BadRequest());
            }

            var interfaceName = InterfaceName.tryParse(range.Name);

            if (interfaceName == null)
            {
                System.Diagnostics.Debug.WriteLine("Invalid format for interface name : " + range.Name);
                return(BadRequest());
            }

            var networkDeviceType = await dbContext.NetworkDeviceTypes.Include("Interfaces").FirstOrDefaultAsync(x => x.Id == networkDeviceTypeId);

            if (networkDeviceType == null)
            {
                System.Diagnostics.Debug.WriteLine("Network device type " + networkDeviceTypeId.ToString() + " does not exist");
                return(BadRequest());
            }

            var interfaceList = new List <string>();
            var networkInterfaceRecordList = new List <NetworkInterface>();

            for (var i = 0; i < range.Count; i++)
            {
                var newInterfaceName = interfaceName.subsequent(i).ToString();
                interfaceList.Add(newInterfaceName);
                networkInterfaceRecordList.Add(new NetworkInterface
                {
                    Name           = newInterfaceName,
                    InterfaceIndex = range.FirstIndex + i
                });
            }

            var conflictingRecords = await dbContext.NetworkInterfaces
                                     .Where(x =>
                                            interfaceList.Contains(x.Name, StringComparer.OrdinalIgnoreCase) &&
                                            x.DeviceType.Id == networkDeviceTypeId
                                            )
                                     .ToListAsync();

            if (conflictingRecords.Count() != 0)
            {
                System.Diagnostics.Debug.WriteLine("Conflicting network interface names found");
                return(BadRequest());
            }

            networkDeviceType.Interfaces.AddRange(networkInterfaceRecordList);
            dbContext.Update(networkDeviceType);

            await dbContext.SaveChangesAsync();

            foreach (var networkInterface in networkInterfaceRecordList)
            {
                networkInterface.DeviceType.Interfaces = null;
            }

            return(new CreatedAtRouteResult("GetNetworkInterfaces", networkInterfaceRecordList));
        }
示例#22
0
 public async Task <List <Template> > Get(
     [FromServices] PnPServerContext dbContext
     )
 {
     return(await dbContext.Templates.ToListAsync());
 }
示例#23
0
 public async Task <List <NetworkDeviceType> > Get(
     [FromServices] PnPServerContext dbContext
     )
 {
     return(await dbContext.NetworkDeviceTypes.ToListAsync());
 }