Esempio n. 1
0
        private void GetBasicInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;

            //Get setting of interest
            SelectedSetting = OIDSettings["DeviceBasicInfo"];

            //Define handle collection in order
            MappingHandlers = new List <Action <IList <string>, string, object> >();
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).Description = y; });
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).OIDobjectID = y; });
            MappingHandlers.Add(null);
            MappingHandlers.Add(null);
            MappingHandlers.Add((x, y, z) =>
            {
                EnumDeviceType dvt;
                IDeviceTopologyInfoDTO Data = (IDeviceTopologyInfoDTO)z;
                Data.DeviceName             = y;
                Data.DeviceType             = Enum.TryParse <EnumDeviceType>(Regex.Match(y, @"^.*?(?=[0-9])").ToString(), out dvt) ? dvt : EnumDeviceType.Unknown;
            });
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).Location = y; });
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).OSIImplementedLayers = (EnumOSILayers)Enum.Parse(typeof(EnumOSILayers), y); });

            //Add network known data
            TopologyInfo.DeviceIPAndMask = $"{Device.TargetIP}/{Device.NetworkMask}";

            if (RegardingObject.ARPTable.Any(x => x.Value == $"{Device.TargetIP}"))
            {
                TopologyInfo.DeviceMAC = RegardingObject.ARPTable.Where(x => x.Value == $"{Device.TargetIP}").First().Key;
            }

            //Collect data mapping with handlers
            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers);
        }
Esempio n. 2
0
        private void ShowDirectNeighbours(IDeviceTopologyInfoDTO data)
        {
            Console.WriteLine("\nComputed direct neighbours:\n");
            Console.WriteLine("Port ID \t MAC Address \t IP Address");

            foreach (KeyValuePair <string, IDictionary <string, string> > computedneigh in data.DeviceDirectNeighbours)
            {
                foreach (KeyValuePair <string, string> addrrlist in computedneigh.Value)
                {
                    Console.WriteLine($"{computedneigh.Key} \t {addrrlist.Key} \t {addrrlist.Value}");
                }
            }
        }
Esempio n. 3
0
        private void ShowData(IDeviceTopologyInfoDTO data)
        {
            ViewHelper.RedirectConsoleToFile(true);

            ShowPortInventory(data);
            ShowMACVolumetry(data);
            ShowMACsByPort(data);
            ShowDirectNeighbours(data);

            Console.WriteLine();

            ViewHelper.RedirectConsoleToFile(false);
        }
Esempio n. 4
0
        private void ShowPortInventory(IDeviceTopologyInfoDTO data)
        {
            Console.WriteLine("\nPort inventory by internal ID:\n");
            Console.WriteLine("{0,-40} {1,-40} {2,-40} {3,-40} {4,-40} {5,-40}", "Port ID", "Port Name", "MAC Address", "Port Type", "Port Referer", "VLAN");

            foreach (KeyValuePair <string, string> MACPort in data.PortMACAddress)
            {
                List <string> relVLANs;
                bool          existVLAN = data.PortVLANMapping.TryGetValue(MACPort.Key, out relVLANs);
                string        VLANList  = existVLAN ? string.Join(",", relVLANs.Select(x => data.VLANInventory[x])) : string.Empty;

                Console.WriteLine($"{MACPort.Key,-40} {data.PortDescriptions[MACPort.Key],-40} {MACPort.Value,-40} {data.PortSettings[MACPort.Key].First,-40} {data.PortSettings[MACPort.Key].Second,-40} {VLANList,-40}");
            }
        }
Esempio n. 5
0
        private void ShowMACVolumetry(IDeviceTopologyInfoDTO data)
        {
            if (data.DeviceType == EnumDeviceType.RT || data.DeviceType == EnumDeviceType.SW || data.DeviceType == EnumDeviceType.AP)
            {
                //Volumetry MACs by port
                Console.WriteLine("\nVolumetry MACs by port:\n");
                Console.WriteLine("Port ID \t Quantity");

                foreach (KeyValuePair <string, IDictionary <string, string> > portlearned in data.PortLearnedAddresses)
                {
                    Console.WriteLine($"{portlearned.Key} \t {portlearned.Value.Count}");
                }
            }
        }
Esempio n. 6
0
        private void ShowMACsByPort(IDeviceTopologyInfoDTO data)
        {
            if (data.DeviceType == EnumDeviceType.RT || data.DeviceType == EnumDeviceType.SW || data.DeviceType == EnumDeviceType.AP)
            {
                //Learned MACs by port
                Console.WriteLine("\nLearned MACs by port:\n");
                Console.WriteLine("Port ID \t MAC Address \t IP Address");

                foreach (KeyValuePair <string, IDictionary <string, string> > portlearned in data.PortLearnedAddresses)
                {
                    foreach (KeyValuePair <string, string> maclist in portlearned.Value)
                    {
                        Console.WriteLine($"{portlearned.Key} \t {maclist.Key} \t {maclist.Value}");
                    }
                }
            }
        }
Esempio n. 7
0
        private void LearnedAddressMapper(IList <string> IndexValues, string Value, object StrategyDTOobject)
        {
            IDeviceTopologyInfoDTO TopologyInfo = StrategyDTOobject as IDeviceTopologyInfoDTO;

            IDictionary <string, IDictionary <string, string> > LearnedAddress = TopologyInfo.PortLearnedAddresses;

            if (LearnedAddress.ContainsKey(Value))
            {
                if (!LearnedAddress[Value].ContainsKey(IndexValues[1]))
                {
                    LearnedAddress[Value].Add(IndexValues[1], RegardingObject.ARPTable.ContainsKey(IndexValues[1]) ? RegardingObject.ARPTable[IndexValues[1]] : null);
                }
            }
            else
            {
                LearnedAddress.Add(Value, new Dictionary <string, string>()
                {
                    { IndexValues[1], RegardingObject.ARPTable.ContainsKey(IndexValues[1]) ? RegardingObject.ARPTable[IndexValues[1]] : null }
                });
            }
        }
Esempio n. 8
0
        private void GetAggregateDestinations(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;

            //Get setting of interest
            SelectedSetting = OIDSettings["TrunkDestinationsCDP"];

            //Define handle collection in order
            MappingHandlers = new List <Action <IList <string>, string, object> >();
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortAggregateDestinations.Add(x[0], new CustomPair <string, string>(y, null)); });
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortAggregateDestinations[x[0]].Second = y; });

            //Define container if necesary
            TopologyInfo.PortAggregateDestinations = new Dictionary <string, CustomPair <string, string> >();

            //Collect data mapping with handlers
            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers);
        }
Esempio n. 9
0
        private void GetVLANInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            //Key: Port ID, Value: Tuple of VLAN ID and VLAN name
            //IDictionary<string, CustomPair<string, string>> VLANByInterfaceID { get; set; }

            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;
            IDictionary <string, string> VLANMappingResult;

            SelectedSetting = OIDSettings["VLANDescription"];
            MappingHandlers = new List <Action <IList <string>, string, object> >();

            #region VLAN description

            MappingHandlers.Add((x, y, z) => { ((IDictionary <string, string>)z).Add(x[0], y); });
            TopologyInfo.VLANInventory = new Dictionary <string, string>();

            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo.VLANInventory, SelectedSetting, MappingHandlers);

            #endregion

            #region VLAN Mapping

            //By default, all ports are access ports. Initialize dictionary
            MappingHandlers.Clear();
            MappingHandlers.Add((x, y, z) => { ((IDictionary <string, string>)z).Add(x[0], y); });

            VLANMappingResult = new Dictionary <string, string>();
            SelectedSetting   = OIDSettings["VLANMapping"];

            StrategyHelper.OIDEntryProcessor(Device, VLANMappingResult, SelectedSetting, MappingHandlers);

            TopologyInfo.PortVLANMapping = new Dictionary <string, List <string> >();

            if (VLANMappingResult.Count != 0)
            {
                foreach (KeyValuePair <string, string> VLANMaskInfo in VLANMappingResult)
                {
                    string[] positions = StrategyHelper.GetFlagArrayPositions(VLANMaskInfo.Value);

                    for (int i = 0; i < positions.Length; i++)
                    {
                        if (TopologyInfo.PortDescriptions.ContainsKey(positions[i]))
                        {
                            if (TopologyInfo.PortVLANMapping.ContainsKey(positions[i]))
                            {
                                TopologyInfo.PortVLANMapping[positions[i]].Add(VLANMaskInfo.Key);
                            }
                            else
                            {
                                TopologyInfo.PortVLANMapping.Add(positions[i], new List <string>()
                                {
                                    VLANMaskInfo.Key
                                });
                            }
                        }
                    }
                }
            }

            #endregion
        }
Esempio n. 10
0
        private void GetPortIDInfo(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;
            IDictionary <string, IList <string> >            LACPResults;
            IDictionary <string, IList <string> >            PortHierarchyResults;

            #region Port description

            SelectedSetting = OIDSettings["PhysPortDescription"];

            MappingHandlers = new List <Action <IList <string>, string, object> >();
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortDescriptions.Add(x[0], y); });

            TopologyInfo.PortDescriptions = new Dictionary <string, string>();
            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers);

            #endregion

            #region Port typology

            //By default, all ports are access ports. Initialize dictionary
            TopologyInfo.PortSettings = new Dictionary <string, CustomPair <EnumPhysPortType, string> >();

            foreach (string portID in TopologyInfo.PortMACAddress.Keys)
            {
                TopologyInfo.PortSettings.Add(portID, new CustomPair <EnumPhysPortType, string>(EnumPhysPortType.Access, null));
            }

            //Detect ports without MAC --> type loopback
            IEnumerable <string> LoopbackPorts = TopologyInfo.PortMACAddress.Where(x => string.IsNullOrEmpty(x.Value)).Select(x => x.Key);
            foreach (string loopbackitem in LoopbackPorts)
            {
                TopologyInfo.PortSettings[loopbackitem].First = EnumPhysPortType.Loopback;
            }

            // Adrress by port > LearnedMACThreshold --> InferedTrunks
            IEnumerable <string> InferedTrunks = TopologyInfo.PortLearnedAddresses.Where(x => x.Value.Count > LearnedMACThreshold).Select(x => x.Key);
            foreach (string inferedtrunkitem in InferedTrunks)
            {
                TopologyInfo.PortSettings[inferedtrunkitem].First = EnumPhysPortType.InferedTrunk;
            }

            // VirtualPort - Trunk
            MappingHandlers.Clear();
            MappingHandlers.Add(PortHierarchyMapper);
            PortHierarchyResults = new Dictionary <string, IList <string> >();
            SelectedSetting      = OIDSettings["PortHierarchy"];

            StrategyHelper.OIDEntryProcessor(Device, PortHierarchyResults, SelectedSetting, MappingHandlers);

            if (PortHierarchyResults.Count != 0)
            {
                //Virtual ports
                if (PortHierarchyResults.ContainsKey("0"))
                {
                    foreach (string vlanport in PortHierarchyResults["0"])
                    {
                        TopologyInfo.PortSettings[vlanport].First = EnumPhysPortType.VirtualPort;
                    }
                }

                //Trunk ports (any protocol)
                IEnumerable <KeyValuePair <string, IList <string> > > trunkports = PortHierarchyResults.Where(x => x.Value.Count > 1 && x.Key != "0").Where(x => !PortHierarchyResults["0"].Contains(x.Key));

                foreach (KeyValuePair <string, IList <string> > trunkentry in trunkports)
                {
                    TopologyInfo.PortSettings[trunkentry.Key].First = EnumPhysPortType.Trunk;

                    foreach (string aggregateentry in trunkentry.Value)
                    {
                        TopologyInfo.PortSettings[aggregateentry].First  = EnumPhysPortType.Aggregate;
                        TopologyInfo.PortSettings[aggregateentry].Second = trunkentry.Key;
                    }
                }
            }

            //LACP - Aggregate
            MappingHandlers.Clear();
            MappingHandlers.Add(LACPAssignmentMapper);
            LACPResults     = new Dictionary <string, IList <string> >();
            SelectedSetting = OIDSettings["LACPSetting"];

            StrategyHelper.OIDEntryProcessor(Device, LACPResults, SelectedSetting, MappingHandlers);

            if (LACPResults.Count != 0)
            {
                foreach (KeyValuePair <string, IList <string> > lacpentry in LACPResults)
                {
                    TopologyInfo.PortSettings[lacpentry.Key].First = EnumPhysPortType.LACP;

                    foreach (string groupedport in lacpentry.Value)
                    {
                        if (TopologyInfo.PortSettings.ContainsKey(groupedport))
                        {
                            TopologyInfo.PortSettings[groupedport].First  = EnumPhysPortType.Aggregate;
                            TopologyInfo.PortSettings[groupedport].Second = lacpentry.Key;
                        }
                    }
                }
            }

            #endregion
        }
Esempio n. 11
0
        private void GetPortMACAddress(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;

            //Get setting of interest
            SelectedSetting = OIDSettings["PhysPortMACAddress"];

            //Define handle collection in order
            MappingHandlers = new List <Action <IList <string>, string, object> >();
            MappingHandlers.Add((x, y, z) => { ((IDeviceTopologyInfoDTO)z).PortMACAddress.Add(x[0], y); });

            //Define container if necesary
            TopologyInfo.PortMACAddress = new Dictionary <string, string>();

            //Collect data mapping with handlers
            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers);
        }
Esempio n. 12
0
        private void GetLearnedMACAddresses(ISNMPDeviceDataDTO Device, IDictionary <string, IOIDSettingDTO> OIDSettings, IDeviceTopologyInfoDTO TopologyInfo)
        {
            IOIDSettingDTO SelectedSetting;
            IList <Action <IList <string>, string, object> > MappingHandlers;

            //Get setting of interest
            SelectedSetting = OIDSettings["LearnedMACByPhysPortID"];

            //Define handle collection in order
            MappingHandlers = new List <Action <IList <string>, string, object> >();
            MappingHandlers.Add(LearnedAddressMapper);

            //Initialize container if necesary
            TopologyInfo.PortLearnedAddresses = new Dictionary <string, IDictionary <string, string> >();

            //Collect data mapping with handlers
            StrategyHelper.OIDEntryProcessor(Device, TopologyInfo, SelectedSetting, MappingHandlers);
        }
Esempio n. 13
0
        private void BuildTopology()
        {
            List <string> TargetBuffer;

            Dictionary <string, int> DevMACvolumetry;
            GlobalTopologyInfo       Result;

            Result = new GlobalTopologyInfo();
            RegardingObject.AttachSNMPProcessedValue(typeof(GlobalTopologyInfo), Result);

            TargetBuffer = new List <string>();

            //Get volumetry of MACs learned by device
            DevMACvolumetry = RegardingObject.DeviceData.Values
                              .ToDictionary(
                x => x.TargetIP.ToString(),
                y => ((IDeviceTopologyInfoDTO)y.SNMPProcessedData[nameof(IDeviceTopologyInfoDTO)].Data).PortLearnedAddresses.Sum(z => z.Value.Count));

            //If no elements are found, avoid continue with processing
            if (DevMACvolumetry.Count == 0)
            {
                return;
            }

            //Get device of MAX MACs learned --> Define as pivot
            TargetBuffer.Add(DevMACvolumetry.Aggregate((p, c) => p.Value < c.Value ? c : p).Key);

            //Loop on root node and scans by rows
            for (int i = 0; i < TargetBuffer.Count; i++)
            {
                IDeviceTopologyInfoDTO orignodeOBJ = (IDeviceTopologyInfoDTO)RegardingObject.DeviceData[TargetBuffer[i]].SNMPProcessedData[nameof(IDeviceTopologyInfoDTO)].Data;

                //Add node neighbours to results
                foreach (var nodeCONN in orignodeOBJ.DeviceDirectNeighbours)
                {
                    Tuple <string, string, string, string, string>         origTopoMatrix = new Tuple <string, string, string, string, string>($"{orignodeOBJ.DeviceType}", orignodeOBJ.DeviceIPAndMask, orignodeOBJ.PortMACAddress[nodeCONN.Key], nodeCONN.Key, orignodeOBJ.PortDescriptions[nodeCONN.Key]);
                    List <Tuple <string, string, string, string, string> > destTopoMatrix = new List <Tuple <string, string, string, string, string> >();

                    if (Result.TopologyMatrix.ContainsKey(origTopoMatrix))
                    {
                        Result.TopologyMatrix[origTopoMatrix] = destTopoMatrix;
                    }
                    else
                    {
                        Result.TopologyMatrix.Add(origTopoMatrix, destTopoMatrix);
                    }

                    //Allow multiple node for each connection despite should exist only one
                    foreach (KeyValuePair <string, string> PortMACIPtuple in nodeCONN.Value)
                    {
                        //Check if destination es an scanned device or not
                        //Expression eval left to right. Exception avoidance
                        if (PortMACIPtuple.Value != null && RegardingObject.DeviceData.ContainsKey(PortMACIPtuple.Value))
                        {
                            //Retrieve data for building tuple
                            IDeviceTopologyInfoDTO destnodeOBJ = (IDeviceTopologyInfoDTO)RegardingObject.DeviceData[PortMACIPtuple.Value].SNMPProcessedData[nameof(IDeviceTopologyInfoDTO)].Data;
                            string destport = destnodeOBJ.PortMACAddress.Where(x => x.Value == PortMACIPtuple.Key).FirstOrDefault().Key;

                            //Update target buffer for drilling down
                            if (!TargetBuffer.Contains(ModelHelper.ExtractIPAddress(destnodeOBJ.DeviceIPAndMask).ToString()))
                            {
                                TargetBuffer.Add(ModelHelper.ExtractIPAddress(destnodeOBJ.DeviceIPAndMask).ToString());
                            }

                            //Add to results
                            destTopoMatrix.Add(new Tuple <string, string, string, string, string>($"{destnodeOBJ.DeviceType}", destnodeOBJ.DeviceIPAndMask, PortMACIPtuple.Key, destnodeOBJ.PortMACAddress[destport], destnodeOBJ.PortDescriptions[destport]));
                        }
                        else
                        {
                            //Add to results
                            destTopoMatrix.Add(new Tuple <string, string, string, string, string>($"{EnumDeviceType.Unknown}", PortMACIPtuple.Value, PortMACIPtuple.Key, "unknown", "unknown"));
                        }
                    }
                }
            }
        }
Esempio n. 14
0
        private void ComputeDirectNeighbours()
        {
            foreach (ISNMPDeviceDataDTO Device in RegardingObject.DeviceData.Values)
            {
                IDeviceTopologyInfoDTO DeviceTopology = (IDeviceTopologyInfoDTO)Device.SNMPProcessedData[nameof(IDeviceTopologyInfoDTO)].Data;
                DeviceTopology.DeviceDirectNeighbours = new Dictionary <string, IDictionary <string, string> >();

                //1) Get access ports
                //1.2) Get Learned MAC from access port
                IEnumerable <string> AccessPorts = DeviceTopology.PortSettings.Where(x => x.Value.First == EnumPhysPortType.Access).Select(x => x.Key);

                foreach (string acports in AccessPorts)
                {
                    if (DeviceTopology.PortLearnedAddresses.ContainsKey(acports))
                    {
                        DeviceTopology.DeviceDirectNeighbours.Add(acports, DeviceTopology.PortLearnedAddresses[acports]);
                    }
                }

                //2) Get Aggregates - InferedTrunks
                //2.2) Get CISCO OID for SWITCH MAC and port
                //2.3) Get Interface MAC linked to port of SWITCH MAC

                foreach (KeyValuePair <string, CustomPair <string, string> > aggports in DeviceTopology.PortAggregateDestinations)
                {
                    string deviceip;

                    if (RegardingObject.ARPTable.TryGetValue(aggports.Value.First, out deviceip))
                    {
                        string PortMACAddr = null;
                        IDeviceTopologyInfoDTO      targdevice = null;
                        Dictionary <string, string> aggres     = new Dictionary <string, string>();

                        if (RegardingObject.DeviceData.ContainsKey(deviceip))
                        {
                            targdevice = (IDeviceTopologyInfoDTO)RegardingObject.DeviceData[deviceip].SNMPProcessedData[nameof(IDeviceTopologyInfoDTO)].Data;
                        }

                        if (targdevice != null)
                        {
                            //Try search by index first, otherwise by port description
                            if (targdevice.PortMACAddress.TryGetValue(aggports.Value.Second, out PortMACAddr))
                            {
                                aggres.Add(PortMACAddr, RegardingObject.ARPTable[aggports.Value.First]);
                            }
                            else
                            {
                                //Get Port ID associated to port description
                                KeyValuePair <string, string> PortMapTupple = targdevice.PortMACAddress.Where(x => x.Value == aggports.Value.First).FirstOrDefault();

                                if (targdevice.PortMACAddress.TryGetValue(PortMapTupple.Key, out PortMACAddr))
                                {
                                    aggres.Add(PortMACAddr, RegardingObject.ARPTable[aggports.Value.First]);
                                }
                            }
                        }
                        else
                        {
                            aggres.Add("Aggregate NA", "Aggregate NA");
                        }

                        //MJE - Revisar porque duplica clave a veces...
                        if (!DeviceTopology.DeviceDirectNeighbours.ContainsKey(aggports.Key))
                        {
                            DeviceTopology.DeviceDirectNeighbours.Add(aggports.Key, aggres);
                        }
                    }
                }
            }
        }