Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Port

Public general use code classes and xml files that we've compiled and used over the years:

Huawei's Port support class of Next Generation Network'a (NGN's) data model.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Data;
   5:   
   6:  namespace Ia.Ngn.Cl.Model.Data.Huawei
   7:  {
   8:      ////////////////////////////////////////////////////////////////////////////
   9:   
  10:      /// <summary publish="true">
  11:      /// Huawei's Port support class of Next Generation Network'a (NGN's) data model.
  12:      /// </summary>
  13:      /// 
  14:      /// <remarks> 
  15:      /// Copyright © 2016-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  16:      ///
  17:      /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
  18:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  19:      ///
  20:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  21:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  22:      /// 
  23:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  24:      /// 
  25:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  26:      /// </remarks> 
  27:      public partial class Port
  28:      {
  29:          /// <summary/>
  30:          public Port() { }
  31:   
  32:          ////////////////////////////////////////////////////////////////////////////
  33:   
  34:          /// <summary>
  35:          ///
  36:          /// </summary>
  37:          public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Response response, out Ia.Cl.Model.Result result)
  38:          {
  39:              bool isUpdated;
  40:              int did, fn, sn, pn, queryFn, querySn, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
  41:              string id, queryCommand, queryDev;
  42:              DataColumnCollection columns;
  43:              DataRow columnDataRow;
  44:              Dictionary<string, int> devToDidDictionary;
  45:              Ia.Ngn.Cl.Model.Huawei.EmsPort port, newPort;
  46:              Ia.Ngn.Cl.Model.Huawei.EmsBoard board;
  47:              List<string> itemIdList;
  48:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> portList;
  49:   
  50:              isUpdated = false;
  51:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
  52:              result = new Ia.Cl.Model.Result();
  53:   
  54:              if (response.QueryDataTable != null)
  55:              {
  56:                  queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
  57:   
  58:                  // LST-PORT::DEV=MDU-SAA-838-001,FN=0,SN=1:{ctag}::;
  59:                  queryDev = Ia.Cl.Model.Default.Match(queryCommand, @"DEV=(.+?),");
  60:                  queryFn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"FN=(\d+)"));
  61:                  querySn = int.TryParse(Ia.Cl.Model.Default.Match(queryCommand, @"SN=(\d+)"), out int i) ? i : 0;
  62:   
  63:                  columns = response.QueryDataTable.Columns;
  64:                  readItemCount = response.QueryDataTable.Rows.Count;
  65:   
  66:                  devToDidDictionary = Ia.Ngn.Cl.Model.Data.Huawei.Dev.DevToDidDictionary;
  67:   
  68:                  if (devToDidDictionary.ContainsKey(queryDev))
  69:                  {
  70:                      did = devToDidDictionary[queryDev];
  71:   
  72:                      board = Ia.Ngn.Cl.Model.Data.Huawei.Board.ReadByDidFnSn(did, queryFn, querySn);
  73:   
  74:                      if (board != null)
  75:                      {
  76:                          itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
  77:   
  78:                          using (var db = new Ia.Ngn.Cl.Model.Ngn())
  79:                          {
  80:                              portList = (from ep in db.EmsPorts where ep.EmsBoard.Id == board.Id select ep).ToList();
  81:                              // or portList = Ia.Ngn.Cl.Model.Data.Huawei.Port.List(board.Id);
  82:   
  83:                              existingItemCount = portList.Count;
  84:   
  85:                              if (response.QueryDataTable.Rows.Count >= 1)
  86:                              {
  87:                                  columnDataRow = response.QueryDataTable.Rows[0];
  88:   
  89:                                  foreach (DataRow dataRow in response.QueryDataTable.Rows)
  90:                                  {
  91:                                      did = int.Parse(dataRow[columns.IndexOf("DID")].ToString());
  92:                                      fn = int.Parse(dataRow[columns.IndexOf("FN")].ToString());
  93:                                      sn = int.Parse(dataRow[columns.IndexOf("SN")].ToString());
  94:                                      pn = int.Parse(dataRow[columns.IndexOf("PN")].ToString());
  95:   
  96:                                      id = Ia.Ngn.Cl.Model.Business.Huawei.Port.PortId(board.Id, pn);
  97:   
  98:                                      port = (from p in portList where p.Id == id select p).SingleOrDefault();
  99:   
 100:                                      newPort = new Ia.Ngn.Cl.Model.Huawei.EmsPort()
 101:                                      {
 102:                                          Id = id,
 103:                                          DID = did,
 104:                                          FN = fn,
 105:                                          SN = sn,
 106:                                          PN = pn,
 107:                                          ALIAS = columns.Contains("ALIAS") ? dataRow[columns.IndexOf("ALIAS")].ToString() : string.Empty,
 108:                                          PT = columns.Contains("PT") ? dataRow[columns.IndexOf("PT")].ToString() : string.Empty,
 109:                                          PSTAT = columns.Contains("PSTAT") ? dataRow[columns.IndexOf("PSTAT")].ToString() : string.Empty,
 110:                                          NAME = columns.Contains("NAME") ? dataRow[columns.IndexOf("NAME")].ToString() : string.Empty,
 111:                                          PSPEED = columns.Contains("PSPEED") ? dataRow[columns.IndexOf("PSPEED")].ToString() : string.Empty,
 112:                                          PDIRECT = columns.Contains("PDIRECT") ? dataRow[columns.IndexOf("PDIRECT")].ToString() : string.Empty,
 113:                                          LOOPSTAT = columns.Contains("LOOPSTAT") ? dataRow[columns.IndexOf("LOOPSTAT")].ToString() : string.Empty,
 114:                                          TESTSTAT = columns.Contains("TESTSTAT") ? dataRow[columns.IndexOf("TESTSTAT")].ToString() : string.Empty,
 115:                                          FEC = columns.Contains("FEC") ? dataRow[columns.IndexOf("FEC")].ToString() : string.Empty,
 116:                                          RN = columns.Contains("RN") ? dataRow[columns.IndexOf("RN")].ToString() : string.Empty,
 117:                                          AState = columns.Contains("AState") ? dataRow[columns.IndexOf("AState")].ToString() : string.Empty,
 118:                                          EmsBoard = (from eb in db.EmsBoards where eb.Id == board.Id select eb).SingleOrDefault()
 119:                                      };
 120:   
 121:                                      if (port == null)
 122:                                      {
 123:                                          newPort.Created = newPort.Updated = DateTime.UtcNow.AddHours(3);
 124:   
 125:                                          db.EmsPorts.Add(newPort);
 126:   
 127:                                          insertedItemCount++;
 128:                                      }
 129:                                      else // update
 130:                                      {
 131:                                          // below: copy values from newPort to port
 132:   
 133:                                          if (port.Update(newPort))
 134:                                          {
 135:                                              db.EmsPorts.Attach(port);
 136:                                              db.Entry(port).State = System.Data.Entity.EntityState.Modified;
 137:   
 138:                                              updatedItemCount++;
 139:                                          }
 140:                                      }
 141:   
 142:                                      itemIdList.Add(id); // keep at the end
 143:                                  }
 144:   
 145:                                  // below: this function will remove values that were not present in the reading
 146:                                  if (portList.Count > 0)
 147:                                  {
 148:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsPort po in portList)
 149:                                      {
 150:                                          if (!itemIdList.Contains(po.Id))
 151:                                          {
 152:                                              port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
 153:   
 154:                                              db.EmsPorts.Remove(port);
 155:   
 156:                                              deletedItemCount++;
 157:                                          }
 158:                                      }
 159:                                  }
 160:                              }
 161:                              else
 162:                              {
 163:                                  // below: remove all values since reading was empty
 164:   
 165:                                  if (portList.Count > 0)
 166:                                  {
 167:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsPort po in portList)
 168:                                      {
 169:                                          port = (from ep in db.EmsPorts where ep.Id == po.Id select ep).SingleOrDefault();
 170:   
 171:                                          db.EmsPorts.Remove(port);
 172:   
 173:                                          deletedItemCount++;
 174:                                      }
 175:                                  }
 176:                              }
 177:   
 178:                              db.SaveChanges();
 179:                          }
 180:   
 181:                          if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 182:                          else isUpdated = false;
 183:   
 184:                          result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
 185:                      }
 186:                      else
 187:                      {
 188:                          result.AddWarning("board is null");
 189:                      }
 190:                  }
 191:                  else
 192:                  {
 193:                      result.AddError("devToDidDictionary does not contain key: " + queryDev);
 194:                  }
 195:              }
 196:              else
 197:              {
 198:                  result.AddWarning("dataTable is null");
 199:              }
 200:   
 201:              return isUpdated;
 202:          }
 203:   
 204:          ////////////////////////////////////////////////////////////////////////////
 205:   
 206:          /// <summary>
 207:          ///
 208:          /// </summary>
 209:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> List()
 210:          {
 211:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 212:   
 213:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 214:              {
 215:                  list = (from p in db.EmsPorts select p).ToList();
 216:              }
 217:   
 218:              return list;
 219:          }
 220:   
 221:          ////////////////////////////////////////////////////////////////////////////
 222:   
 223:          /// <summary>
 224:          ///
 225:          /// </summary>
 226:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> List(string boardId)
 227:          {
 228:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 229:   
 230:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 231:              {
 232:                  list = (from p in db.EmsPorts where p.EmsBoard.Id == boardId select p).ToList();
 233:              }
 234:   
 235:              return list;
 236:          }
 237:   
 238:          ////////////////////////////////////////////////////////////////////////////
 239:   
 240:          /// <summary>
 241:          ///
 242:          /// </summary>
 243:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> ListByDevId(int devId)
 244:          {
 245:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 246:   
 247:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 248:              {
 249:                  list = (from p in db.EmsPorts where p.EmsBoard.EmsDev.Id == devId select p).ToList();
 250:              }
 251:   
 252:              return list;
 253:          }
 254:   
 255:          ////////////////////////////////////////////////////////////////////////////
 256:   
 257:          /// <summary>
 258:          ///
 259:          /// </summary>
 260:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsPort> ListByDid(int did)
 261:          {
 262:              List<Ia.Ngn.Cl.Model.Huawei.EmsPort> list;
 263:   
 264:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 265:              {
 266:                  list = (from p in db.EmsPorts where p.DID == did select p).ToList();
 267:              }
 268:   
 269:              return list;
 270:          }
 271:   
 272:          ////////////////////////////////////////////////////////////////////////////
 273:   
 274:          /// <summary>
 275:          ///
 276:          /// </summary>
 277:          public static Ia.Ngn.Cl.Model.Huawei.EmsPort ReadByDidFnSnPn(int did, int fn, int sn, int pn)
 278:          {
 279:              Ia.Ngn.Cl.Model.Huawei.EmsPort item;
 280:   
 281:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 282:              {
 283:                  item = (from p in db.EmsPorts where p.DID == did && p.FN == fn && p.SN == sn && p.PN == pn select p).SingleOrDefault();
 284:              }
 285:   
 286:              return item;
 287:          }
 288:   
 289:          ////////////////////////////////////////////////////////////////////////////
 290:          ////////////////////////////////////////////////////////////////////////////
 291:      }
 292:   
 293:      ////////////////////////////////////////////////////////////////////////////
 294:      ////////////////////////////////////////////////////////////////////////////
 295:  }