private void SortMplsFibRows()
 {
     MPLS_FIB_Rows.Sort((x, y) =>
     {
         int compare = x.RouterName.CompareTo(y.RouterName);
         if (compare != 0)
         {
             return(compare);
         }
         compare = x.FEC.ToString().CompareTo(y.FEC.ToString());
         if (compare != 0)
         {
             return(compare);
         }
         return(x.DestAddress.ToString().CompareTo(y.DestAddress.ToString()));
     });
 }
        private void MPLS_FIB_AddButton_Click(object sender, RoutedEventArgs e)
        {
            var routerName  = MPLS_FIB_RouterName.Text;
            var destAddress = MPLS_FIB_DestAddress.Text;
            var FEC         = MPLS_FIB_FEC.Text;

            if (routerName.Length == 0 || destAddress.Length == 0 || FEC.Length == 0)
            {
                return;
            }

            var isProperMPLS_FIB_Rule = CheckIfIsProperMPLS_FIB_Entry(destAddress, FEC);

            if (!isProperMPLS_FIB_Rule || !Manager.RouterNameToMPLS_FIB_Table.ContainsKey(routerName))
            {
                AddLog("You are trying to add an incorrect rule!", LogType.Error);
                return;
            }

            var foundRow = MPLS_FIB_Rows.Find(x => x.RouterName.Equals(routerName) &&
                                              x.DestAddress.Equals(IPAddress.Parse(destAddress)));

            if (foundRow != null)
            {
                AddLog("FEC for such destination adress already exists!", LogType.Error);
                return;
            }

            var rule = new MplsFibTableRow(destAddress + " " + FEC);

            rule.RouterName = routerName;

            Manager.RouterNameToMPLS_FIB_Table[routerName].Add(rule);
            MPLS_FIB_Rows.Add(rule);
            Manager.SendRow(routerName, rule, ManagementActions.ADD_MPLS_FIB_ENTRY);
            SortMplsFibRows();
            MPLS_FIB_Table.Items.Refresh();
            MPLS_FIB_RouterName.Clear();
            MPLS_FIB_DestAddress.Clear();
            MPLS_FIB_FEC.Clear();
            MPLS_FIB_RouterName.Focus();
        }
        private void MPLS_FIB_DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            int index = MPLS_FIB_Table.SelectedIndex;

            if (index == -1) // selected empty entry
            {
                return;
            }
            var selectedEntry = MPLS_FIB_Table.SelectedItems[0] as MplsFibTableRow;
            var routerName    = selectedEntry.RouterName;
            var destAddress   = selectedEntry.DestAddress;
            var FEC           = selectedEntry.FEC;

            if (Manager.RouterNameToMPLS_FIB_Table.ContainsKey(routerName))
            {
                try
                {
                    // remove item from the list that is binded to GUI's listView
                    var foundRow = MPLS_FIB_Rows.Find(x => x.RouterName.Equals(routerName) &&
                                                      x.DestAddress.Equals(destAddress) && x.FEC.Equals(FEC));
                    Manager.SendRow(routerName, foundRow, ManagementActions.REMOVE_MPLS_FIB_ENTRY);
                    MPLS_FIB_Rows.Remove(foundRow);

                    // remove item from the list that is in dictionary
                    var row = Manager.RouterNameToMPLS_FIB_Table[routerName].Find(x => x.RouterName.Equals(routerName) &&
                                                                                  x.DestAddress.Equals(destAddress) && x.FEC.Equals(FEC));
                    Manager.RouterNameToMPLS_FIB_Table[routerName].Remove(row);
                    MPLS_FIB_Table.Items.Refresh();
                }
                catch (Exception)
                {
                    AddLog($"Router {routerName} is not connected to MS!\n", LogType.Error);
                }
            }
            else
            {
                AddLog("Network topology does not contain such router!\n", LogType.Error);
            }
        }