private void SortFtnRows()
 {
     FTN_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.NHLFE_ID.ToString().CompareTo(y.NHLFE_ID.ToString()));
     });
 }
        private void FTN_AddButton_Click(object sender, RoutedEventArgs e)
        {
            var routerName = FTN_RouterName.Text;
            var FEC        = FTN_FEC.Text;
            var NHLFE_ID   = FTN_NHLFE_ID.Text;

            if (routerName.Length == 0 || FEC.Length == 0 || NHLFE_ID.Length == 0)
            {
                return;
            }
            var isProperRule = CheckIfIsProperFTN_Entry(FEC, NHLFE_ID);

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

            var foundRow = FTN_Rows.Find(x => x.RouterName.Equals(routerName) &&
                                         x.FEC.Equals(int.Parse(FEC)));

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

            var rule = new FtnTableRow(FEC + " " + NHLFE_ID);

            rule.RouterName = routerName;
            Manager.RouterNameToFTN_Table[routerName].Add(rule);
            FTN_Rows.Add(rule);
            Manager.SendRow(routerName, rule, ManagementActions.ADD_FTN_ENTRY);
            SortFtnRows();

            FTN_Table.Items.Refresh();
            FTN_RouterName.Clear();
            FTN_FEC.Clear();
            FTN_NHLFE_ID.Clear();
            FTN_RouterName.Focus();
        }
        private void FTN_DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            int index = FTN_Table.SelectedIndex;

            if (index == -1) // selected empty entry
            {
                return;
            }

            var selectedEntry = FTN_Table.SelectedItems[0] as FtnTableRow;
            var routerName    = selectedEntry.RouterName;
            var FEC           = selectedEntry.FEC;
            var NHLFE_ID      = selectedEntry.NHLFE_ID;

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

                    // remove item from the list that is in dictionary
                    var row = Manager.RouterNameToFTN_Table[routerName].Find(x => x.RouterName.Equals(routerName) &&
                                                                             x.FEC.Equals(FEC) && x.NHLFE_ID.Equals(NHLFE_ID));
                    Manager.RouterNameToFTN_Table[routerName].Remove(row);
                    FTN_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);
            }
        }