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

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

Huawei's EMS ONT SIP INFO support class of Next Generation Network'a (NGN's) data model.

   1:  using System;
   2:  using System.Collections;
   3:  using System.Collections.Generic;
   4:  using System.Linq;
   5:  using System.Data;
   6:  using System.Data.Entity;
   7:  using System.Text;
   8:   
   9:  namespace Ia.Ngn.Cl.Model.Data.Huawei
  10:  {
  11:      ////////////////////////////////////////////////////////////////////////////
  12:   
  13:      /// <summary publish="true">
  14:      /// Huawei's EMS ONT SIP INFO support class of Next Generation Network'a (NGN's) data model.
  15:      /// </summary>
  16:      /// 
  17:      /// <remarks> 
  18:      /// Copyright © 2016-2017 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  19:      ///
  20:      /// 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
  21:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  22:      ///
  23:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  24:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  25:      /// 
  26:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  27:      /// 
  28:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  29:      /// </remarks> 
  30:      public partial class OntSipInfo
  31:      {
  32:          /// <summary/>
  33:          public OntSipInfo() { }
  34:   
  35:          ////////////////////////////////////////////////////////////////////////////
  36:   
  37:          /// <summary>
  38:          /// Update a single EmsOntSipInfo item
  39:          /// </summary>
  40:          public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Response response, out Ia.Cl.Model.Result result)
  41:          {
  42:              bool isUpdated;
  43:              int queryFn, querySn, queryPn, queryOntId, readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount, tableRowIndex, tel;
  44:              string id, ontId, queryCommand, queryDev, sipUserName, sipName;
  45:              DataColumnCollection columns;
  46:              DataRow columnDataRow;
  47:              Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Pon pon;
  48:              Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo ontSipInfo, newOntSipInfo;
  49:              Ia.Ngn.Cl.Model.Huawei.EmsOnt emsOnt;
  50:              List<string> itemIdList;
  51:              List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> ontSipInfoList;
  52:   
  53:              isUpdated = false;
  54:              tableRowIndex = 0;
  55:              readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
  56:              result = new Ia.Cl.Model.Result();
  57:   
  58:              if (response.QueryDataTable != null)
  59:              {
  60:                  queryCommand = response.CommandFromCorrelationTagDictionaryByCtag;
  61:   
  62:                  // ADD-VOIPPSTNUSER::DEV=MDU-JBA-943-002,FN=0,SN=3,PN=10:{ctag}::MGID=0,TID=1,DN=96524602285;
  63:                  queryDev = Ia.Cl.Model.Default.Match(queryCommand, @"DEV=(.+?),");
  64:                  queryFn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"FN=(\d+)"));
  65:                  querySn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"SN=(\d+)"));
  66:                  queryPn = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"PN=(\d+)"));
  67:                  queryOntId = int.Parse(Ia.Cl.Model.Default.Match(queryCommand, @"ONTID=(\d+)"));
  68:   
  69:                  columns = response.QueryDataTable.Columns;
  70:                  readItemCount = response.QueryDataTable.Rows.Count;
  71:   
  72:                  pon = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.PonByOltEmsNameAndSnAndPn(queryDev, querySn, queryPn);
  73:   
  74:                  if (pon != null)
  75:                  {
  76:                      ontId = Ia.Ngn.Cl.Model.Business.Huawei.Ont.OntId(pon.Id, queryOntId);
  77:   
  78:                      itemIdList = new List<string>(response.QueryDataTable.Rows.Count + 1);
  79:   
  80:                      using (var db = new Ia.Ngn.Cl.Model.Ngn())
  81:                      {
  82:                          emsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault();
  83:   
  84:                          if (emsOnt != null)
  85:                          {
  86:                              ontSipInfoList = (from eo in db.EmsOntSipInfos where eo.EmsOnt.Id == emsOnt.Id select eo).ToList();
  87:   
  88:                              existingItemCount = ontSipInfoList.Count;
  89:   
  90:                              if (response.QueryDataTable.Rows.Count >= 1)
  91:                              {
  92:                                  columnDataRow = response.QueryDataTable.Rows[0];
  93:   
  94:                                  foreach (DataRow dataRow in response.QueryDataTable.Rows)
  95:                                  {
  96:                                      tableRowIndex++;
  97:   
  98:                                      sipUserName = columns.Contains("SIPUSERNAME") ? dataRow[columns.IndexOf("SIPUSERNAME")].ToString() : string.Empty;
  99:                                      sipName = columns.Contains("SIPNAME") ? dataRow[columns.IndexOf("SIPNAME")].ToString() : string.Empty;
 100:   
 101:                                      // we will check that the records is for a real number, otherwise we will not send it to the database
 102:                                      if ((Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToImpi(sipName) || Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToPridUser(sipName)) && Ia.Ngn.Cl.Model.Business.NumberFormatConverter.IsMatchToImpuAid(sipUserName))
 103:                                      {
 104:                                          tel = tableRowIndex;
 105:   
 106:                                          id = Ia.Ngn.Cl.Model.Business.Huawei.OntSipInfo.OntSipInfoId(ontId, tel);
 107:   
 108:                                          ontSipInfo = (from eo in ontSipInfoList where eo.Id == id select eo).SingleOrDefault();
 109:   
 110:                                          newOntSipInfo = new Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo()
 111:                                          {
 112:                                              Id = id,
 113:                                              SIPUSERNAME = sipUserName,
 114:                                              SIPUSERPWD = columns.Contains("SIPUSERPWD") ? dataRow[columns.IndexOf("SIPUSERPWD")].ToString() : string.Empty,
 115:                                              SIPNAME = sipName,
 116:                                              TEL = tel,
 117:                                              EmsOnt = (from eo in db.EmsOnts where eo.Id == ontId select eo).SingleOrDefault()
 118:                                          };
 119:   
 120:                                          if (ontSipInfo == null)
 121:                                          {
 122:                                              newOntSipInfo.Created = newOntSipInfo.Updated = DateTime.UtcNow.AddHours(3);
 123:   
 124:                                              db.EmsOntSipInfos.Add(newOntSipInfo);
 125:   
 126:                                              insertedItemCount++;
 127:                                          }
 128:                                          else // update
 129:                                          {
 130:                                              // below: copy values from newOntSipInfo to ontSipInfo
 131:   
 132:                                              if (ontSipInfo.Update(newOntSipInfo))
 133:                                              {
 134:                                                  db.EmsOntSipInfos.Attach(ontSipInfo);
 135:                                                  db.Entry(ontSipInfo).State = System.Data.Entity.EntityState.Modified;
 136:   
 137:                                                  updatedItemCount++;
 138:                                              }
 139:                                          }
 140:   
 141:                                          itemIdList.Add(id); // keep at the end
 142:                                      }
 143:                                  }
 144:   
 145:                                  // below: this function will remove values that were not present in the reading
 146:                                  if (ontSipInfoList.Count > 0)
 147:                                  {
 148:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
 149:                                      {
 150:                                          if (!itemIdList.Contains(o.Id))
 151:                                          {
 152:                                              ontSipInfo = (from osi in db.EmsOntSipInfos where osi.Id == o.Id select osi).SingleOrDefault();
 153:   
 154:                                              db.EmsOntSipInfos.Remove(ontSipInfo);
 155:   
 156:                                              deletedItemCount++;
 157:                                          }
 158:                                      }
 159:                                  }
 160:                              }
 161:                              else //if (dataTable.Rows.Count == 0)
 162:                              {
 163:                                  // note sometimes the response is ONT IS OFFLINE
 164:                                  // delete all ONT SIP INFO elements for this ONT
 165:   
 166:                                  /*
 167:                                  if (ontSipInfoList.Count > 0)
 168:                                  {
 169:                                      foreach (Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo o in ontSipInfoList)
 170:                                      {
 171:                                          ontSipInfo = (from osi in db.EmsOntSipInfos where osi.Id == o.Id select osi).SingleOrDefault();
 172:  
 173:                                          db.EmsOntSipInfos.Remove(ontSipInfo);
 174:  
 175:                                          deletedItemCount++;
 176:                                      }
 177:                                  }
 178:                                  */
 179:                              }
 180:   
 181:                              db.SaveChanges();
 182:                          }
 183:                          else
 184:                          {
 185:                              result.AddWarning("EmsOnt is null for ontId: " + ontId);
 186:                          }
 187:                      }
 188:   
 189:                      if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
 190:                      else isUpdated = false;
 191:   
 192:                      result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
 193:                  }
 194:                  else
 195:                  {
 196:                      result.AddError("NDD PON is null");
 197:                  }
 198:              }
 199:              else
 200:              {
 201:                  result.AddWarning("response.QueryDataTable is null");
 202:              }
 203:   
 204:              return isUpdated;
 205:          }
 206:   
 207:          ////////////////////////////////////////////////////////////////////////////
 208:   
 209:          /// <summary>
 210:          ///
 211:          /// </summary>
 212:          public static List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> List()
 213:          {
 214:              List<Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo> list;
 215:   
 216:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 217:              {
 218:                  list = (from o in db.EmsOntSipInfos select o).ToList();
 219:              }
 220:   
 221:              return list;
 222:          }
 223:   
 224:          ////////////////////////////////////////////////////////////////////////////
 225:   
 226:          /// <summary>
 227:          /// List of service number ids within EmsOntSipInfos within a SIP designated OLT
 228:          /// </summary>
 229:          public static List<string> ServiceIdWithinSipOltList()
 230:          {
 231:              int serviceType;
 232:              string service, serviceId;
 233:              List<int> sipOltIdList;
 234:              List<string> list, serviceIdList;
 235:   
 236:              serviceType = 1;
 237:              sipOltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.SipOltIdList;
 238:   
 239:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 240:              {
 241:                  list = (from o in db.EmsOntSipInfos where o.EmsOnt.Access != null && sipOltIdList.Contains(o.EmsOnt.Access.Olt) select o.SIPUSERNAME).ToList();
 242:              }
 243:   
 244:              if (list != null && list.Count > 0)
 245:              {
 246:                  serviceIdList = new List<string>(list.Count);
 247:   
 248:                  foreach (string s in list)
 249:                  {
 250:                      service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
 251:   
 252:                      serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
 253:   
 254:                      serviceIdList.Add(serviceId);
 255:                  }
 256:              }
 257:              else
 258:              {
 259:                  serviceIdList = new List<string>();
 260:              }
 261:   
 262:              return serviceIdList;
 263:          }
 264:   
 265:          ////////////////////////////////////////////////////////////////////////////
 266:   
 267:          /// <summary>
 268:          ///
 269:          /// </summary>
 270:          public static Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo Read(string id)
 271:          {
 272:              Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo item;
 273:   
 274:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 275:              {
 276:                  item = (from o in db.EmsOntSipInfos where o.Id == id select o).Include(o => o.EmsOnt).SingleOrDefault();
 277:              }
 278:   
 279:              return item;
 280:          }
 281:   
 282:          ////////////////////////////////////////////////////////////////////////////
 283:   
 284:          /// <summary>
 285:          ///
 286:          /// </summary>
 287:          public static Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo ReadByService(string service) //, Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Ont nddOnt)
 288:          {
 289:              string huaweiSipName, nokiaSipName;
 290:              Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo item;
 291:   
 292:              // send priv_96525212254 for Nokia switch and +96524602283@ims.moc.kw for Huawei switch
 293:              //if (nddOnt.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei)
 294:              //{
 295:              huaweiSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Impi(service);
 296:              //}
 297:              //else //if(ont.Pon.PonGroup.Olt.Odf.Router.Vendor == Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Nokia)
 298:              //{
 299:              nokiaSipName = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.PrividUser(service);
 300:              //}
 301:   
 302:              using (var db = new Ia.Ngn.Cl.Model.Ngn())
 303:              {
 304:                  item = (from o in db.EmsOntSipInfos where o.SIPNAME == huaweiSipName || o.SIPNAME == nokiaSipName select o).Include(o => o.EmsOnt).Include(o => o.EmsOnt.Access).FirstOrDefault(); //.SingleOrDefault();
 305:              }
 306:   
 307:              return item;
 308:          }
 309:   
 310:          ////////////////////////////////////////////////////////////////////////////
 311:   
 312:          /// <summary>
 313:          ///
 314:          /// </summary>
 315:          public static Dictionary<string, string> IdToOntIdDictionary
 316:          {
 317:              get
 318:              {
 319:                  Dictionary<string, string> dictionary;
 320:   
 321:                  using (var db = new Ia.Ngn.Cl.Model.Ngn())
 322:                  {
 323:                      dictionary = (from s in db.EmsOntSipInfos select new { s.Id, OntId = s.EmsOnt.Id }).ToDictionary(u => u.Id, u => u.OntId);
 324:                  }
 325:   
 326:                  return dictionary.ToDictionary(u => u.Key, u => u.Value);
 327:              }
 328:          }
 329:   
 330:          ////////////////////////////////////////////////////////////////////////////
 331:          ////////////////////////////////////////////////////////////////////////////
 332:   
 333:          /// <summary>
 334:          ///
 335:          /// </summary>
 336:          public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.EmsOntSipInfo emsOntSipInfo)
 337:          {
 338:              StringBuilder sb;
 339:   
 340:              sb = new StringBuilder();
 341:   
 342:              //sb.AppendLine("Vendor: " + Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Huawei.Name);
 343:              sb.AppendLine("TEL: " + emsOntSipInfo.TEL);
 344:              sb.AppendLine("SIPUSERNAME: " + emsOntSipInfo.SIPUSERNAME);
 345:              sb.AppendLine("SIPUSERPWD: " + emsOntSipInfo.SIPUSERPWD);
 346:              sb.AppendLine("SIPNAME: " + emsOntSipInfo.SIPNAME);
 347:   
 348:              return sb.ToString();
 349:          }
 350:   
 351:          ////////////////////////////////////////////////////////////////////////////
 352:          ////////////////////////////////////////////////////////////////////////////
 353:      }
 354:   
 355:      ////////////////////////////////////////////////////////////////////////////
 356:      ////////////////////////////////////////////////////////////////////////////
 357:  }