شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة ش.ش.و.

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Ems

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

Optical Fiber Network Management Intranet Portal (OFN) client support class for Huawei's Next Generation Network (NGN) EMS client model.

   1:  using Dart.PowerTCP.Telnet;
   2:  using System;
   3:  using System.Collections.Generic;
   4:  using System.Diagnostics;
   5:   
   6:  namespace Ia.Ngn.Cl.Model.Client.Huawei
   7:  {
   8:      ////////////////////////////////////////////////////////////////////////////
   9:   
  10:      /// <summary publish="true">
  11:      /// Optical Fiber Network Management Intranet Portal (OFN) client support class for Huawei's Next Generation Network (NGN) EMS client model.
  12:      /// </summary>
  13:      /// 
  14:      /// <remarks> 
  15:      /// Copyright © 2017-2018 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated 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 class Ems
  28:      {
  29:          private bool isLoggedIn;
  30:   
  31:          // interactive Telnet does not require a very large buffer, so 1024 is fine
  32:          private readonly byte[] buffer = new byte[1024];
  33:          private string receiveString;
  34:          internal Telnet telnet;
  35:   
  36:          /// <summary/>
  37:          public static int WaitAfterSendInMillisecond { get { return 4000; } }
  38:          public static int WaitAfterSendForCfgCommandInMillisecond { get { return 10000; } }
  39:   
  40:          public string LastSentCommand { get; private set; }
  41:   
  42:          public Queue<string> ReceiveQueue { get; set; }
  43:   
  44:          public Queue<string> SendQueue { get; set; }
  45:   
  46:          ////////////////////////////////////////////////////////////////////////////
  47:   
  48:          /// <summary>
  49:          /// 
  50:          /// </summary>
  51:          public enum ResultCode : long
  52:          {
  53:              // I'm basically using the almost exact document phrasing of error codes
  54:              Unknown = -1,
  55:              Succeeded = 0,
  56:              DeviceDoesNotExist1 = 2686058531,
  57:              DeviceDoesNotExist2 = 15658136501,
  58:              ResourceDoesNotExist = 2686058552,
  59:              OntIsNotOnline = 2689012370,
  60:              CommunicatingWithDmFailed = 2686058603,
  61:              OntIsOffline = 2689014724,
  62:              OntLoadingCommandIsBeingExecutedNow = 2689014933,
  63:              SystemIsBusy1 = 2686058516,
  64:              SystemIsBusy2 = 2689017157,
  65:              AttempetingTimeout = 2686058596,
  66:              CommandSyntaxError = 2686058497,
  67:              UnknownCommand = 2686058499,
  68:              TaskTimeout = 2686058508,
  69:              ValueOfParameterIsWrong = 2686058500,
  70:              DbaProfileDoesNotExist1 = 102690820,
  71:              DbaProfileDoesNotExist2 = 2689014791,
  72:              NodeParameterSetFailed = 1615069195,
  73:              VersionDoesNotMatchOrCommunicationWithDeviceFailed = 1615462437,
  74:              FailedToCommunicateWithDevice1 = 1610614842,
  75:              FailedToCommunicateWithDevice2 = 1616445483,
  76:              FailedToCommunicateWithDevice3 = 1616445484,
  77:              FailedToCommunicateWithDevice4 = 1616445485,
  78:              LoginAddressIsNotInUserAcl = 76546022,
  79:              LoginAddressIsNotInSystemAcl = 76546023,
  80:              DeviceOfflineError = 1610612765,
  81:              OperationIsNotSupported = 2689008176,
  82:              ParametersConflictWithVasProfile = 1615069206,
  83:              ProfileAlreadyExists = 2689023090,
  84:              TrafficProfileDoesNotExist = 2689020327,
  85:              MgParameterIsConflicting = 2688880284,
  86:              NoOnuTypeIsSetInGeneralProfile = 1618280538,
  87:              OntIsUsedByOthers = 1618214927,
  88:              UserDoesNotLogIn = 2686058521,
  89:              PortDoesNotExist = 2689015666,
  90:              TelephoneNumberAlreadyExists = 2689015764,
  91:              ThePasswordHasExpiredOrNeedsToBeChangedOnNextLogin = 1618280766,
  92:              TheUserAccountIsLocked = 76546024,
  93:              TheOntDoesNotSuppportConfigeringValueAddedServiceWithoutBindingGeneralVasProfile = 1615069214,
  94:          }
  95:   
  96:          ////////////////////////////////////////////////////////////////////////////
  97:   
  98:          /// <summary>
  99:          ///
 100:          /// </summary>
 101:          public static string ResultCodeString(long _resultCode)
 102:          {
 103:              // see: ColoredResultCodeString()
 104:              string resultCodeString;
 105:              Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode resultCode;
 106:   
 107:              resultCode = (Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode)_resultCode;
 108:   
 109:              resultCodeString = resultCode.ToString();
 110:              resultCodeString = Ia.Cl.Model.Default.CamelToProperCase(resultCodeString);
 111:              resultCodeString = Ia.Cl.Model.Default.ToTitleCase(resultCodeString);
 112:              resultCodeString = resultCodeString.Replace("Ont", "ONT");
 113:   
 114:              return resultCodeString;
 115:          }
 116:   
 117:          ////////////////////////////////////////////////////////////////////////////
 118:   
 119:          /// <summary>
 120:          ///
 121:          /// </summary>
 122:          public static string ColoredResultCodeString(long _resultCode)
 123:          {
 124:              // see: ResultCodeString()
 125:              string resultCodeString, coloredResultCodeString;
 126:              Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode resultCode;
 127:   
 128:              resultCode = (Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode)_resultCode;
 129:   
 130:              resultCodeString = resultCode.ToString();
 131:              resultCodeString = Ia.Cl.Model.Default.CamelToProperCase(resultCodeString);
 132:              resultCodeString = Ia.Cl.Model.Default.ToTitleCase(resultCodeString);
 133:              resultCodeString = resultCodeString.Replace("Ont", "ONT");
 134:   
 135:              switch (resultCode)
 136:              {
 137:                  case Ia.Ngn.Cl.Model.Client.Huawei.Ems.ResultCode.Succeeded: coloredResultCodeString = "<span style=\"color:Green\">" + resultCodeString + "</span>"; break;
 138:                  default: coloredResultCodeString = "<span style=\"color:Red\">" + resultCodeString + "</span>"; break;
 139:              }
 140:   
 141:              return coloredResultCodeString;
 142:          }
 143:   
 144:          ////////////////////////////////////////////////////////////////////////////
 145:   
 146:          /// <summary>
 147:          ///
 148:          /// </summary>
 149:          public Ems()
 150:          {
 151:              this.telnet = new Dart.PowerTCP.Telnet.Telnet(); // this.components);
 152:   
 153:              this.telnet.ClientOptions.AddRange(new Dart.PowerTCP.Telnet.Option[] {
 154:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn),
 155:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.WindowSize, new byte[] {((byte)(0)),((byte)(80)),((byte)(0)),((byte)(24))}, Dart.PowerTCP.Telnet.OptionState.RequestOn),
 156:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.TerminalType, new byte[] {((byte)(0)),((byte)(120)),((byte)(116)),((byte)(101)),((byte)(114)),((byte)(109))}, Dart.PowerTCP.Telnet.OptionState.RequestOn)});
 157:   
 158:              this.telnet.ServerOptions.AddRange(new Dart.PowerTCP.Telnet.Option[] {
 159:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn),
 160:              new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.Echo, null, Dart.PowerTCP.Telnet.OptionState.RequestOn)});
 161:   
 162:              //this.telnet.SynchronizingObject = this;
 163:              this.telnet.TerminalType = "xterm";
 164:              //this.telnet.WindowSize = new System.Drawing.Size(80, 24);
 165:              this.telnet.EndReceive += new Dart.PowerTCP.Telnet.SegmentEventHandler(this.Telnet_EndReceive);
 166:              this.telnet.ConnectedChangedEx += new Dart.PowerTCP.Telnet.EventHandlerEx(this.Telnet_ConnectedChangedEx);
 167:   
 168:              receiveString = string.Empty;
 169:              ReceiveQueue = new Queue<string>(100);
 170:              SendQueue = new Queue<string>(100);
 171:   
 172:              isLoggedIn = false;
 173:          }
 174:   
 175:          ////////////////////////////////////////////////////////////////////////////
 176:   
 177:          /// <summary>
 178:          ///
 179:          /// </summary>
 180:          ~Ems()
 181:          {
 182:              Dispose();
 183:          }
 184:   
 185:          ////////////////////////////////////////////////////////////////////////////
 186:   
 187:          /// <summary>
 188:          ///
 189:          /// </summary>
 190:          public void Connect(out Ia.Cl.Model.Result result)
 191:          {
 192:              result = new Ia.Cl.Model.Result();
 193:   
 194:              try
 195:              {
 196:                  telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 197:                  telnet.ClientOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.TerminalType, new System.Byte[] { ((System.Byte)(0)), ((System.Byte)(116)), ((System.Byte)(116)), ((System.Byte)(121)) }, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 198:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.SuppressGoAheads, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 199:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.Echo, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 200:                  telnet.ServerOptions.Add(new Dart.PowerTCP.Telnet.Option(Dart.PowerTCP.Telnet.OptionCode.OutputPageSize, null, Dart.PowerTCP.Telnet.OptionState.RequestOn));
 201:   
 202:                  telnet.Connect(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Host, Ia.Ngn.Cl.Model.Business.Huawei.Ems.Port.ToString());
 203:   
 204:                  // receive using asynchronous technique
 205:                  telnet.ReceiveTimeout = 0;
 206:                  telnet.BeginReceive(buffer);
 207:   
 208:                  result.AddSuccess("Connected. ");
 209:              }
 210:              catch (Exception ex)
 211:              {
 212:                  result.AddError("Exception: " + ex.Message);
 213:   
 214:                  telnet.Close();
 215:              }
 216:          }
 217:   
 218:          ////////////////////////////////////////////////////////////////////////////
 219:   
 220:          /// <summary>
 221:          ///
 222:          /// </summary>
 223:          public void Disconnect(out Ia.Cl.Model.Result result)
 224:          {
 225:              result = new Ia.Cl.Model.Result();
 226:   
 227:              try
 228:              {
 229:                  telnet.Close();
 230:   
 231:                  result.AddSuccess("Disconnected. ");
 232:              }
 233:              catch (Exception ex)
 234:              {
 235:                  result.AddError(ex.Message);
 236:              }
 237:          }
 238:   
 239:          ////////////////////////////////////////////////////////////////////////////
 240:   
 241:          /// <summary>
 242:          ///
 243:          /// </summary>
 244:          public bool IsConnected
 245:          {
 246:              get
 247:              {
 248:                  return telnet.Connected;
 249:              }
 250:          }
 251:   
 252:          ////////////////////////////////////////////////////////////////////////////
 253:   
 254:          /// <summary>
 255:          ///
 256:          /// </summary>
 257:          public bool IsLoggedIn { get { return isLoggedIn; } set { isLoggedIn = value; } }
 258:   
 259:          ////////////////////////////////////////////////////////////////////////////
 260:   
 261:          /// <summary>
 262:          ///
 263:          /// </summary>
 264:          public void Dispose()
 265:          {
 266:              telnet.Dispose();
 267:          }
 268:   
 269:          ////////////////////////////////////////////////////////////////////////////
 270:   
 271:          /// <summary>
 272:          ///
 273:          /// </summary>
 274:          public void Login()
 275:          {
 276:              // sendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.Semicolon); did not make any different
 277:   
 278:              // below: I need this because NCE somehow needs to be triggered before I send login information
 279:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.EmsKeepAliveCommand);
 280:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.EmsKeepAliveCommand);
 281:   
 282:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.LoginUser);
 283:          }
 284:   
 285:          ////////////////////////////////////////////////////////////////////////////
 286:   
 287:          /// <summary>
 288:          ///
 289:          /// </summary>
 290:          public void Logout()
 291:          {
 292:              SendQueue.Enqueue(Ia.Ngn.Cl.Model.Business.Huawei.Ems.LogoutUser);
 293:          }
 294:   
 295:          ////////////////////////////////////////////////////////////////////////////
 296:   
 297:          /// <summary>
 298:          ///
 299:          /// </summary>
 300:          public void Send(string text, bool skipSleep, out Ia.Cl.Model.Result result)
 301:          {
 302:              result = new Ia.Cl.Model.Result();
 303:   
 304:              try
 305:              {
 306:                  if (telnet.Connected)
 307:                  {
 308:                      if (text != null)
 309:                      {
 310:                          LastSentCommand = text;
 311:   
 312:                          //this.textBox.AppendText("\r\n/* Sending: " + text + " */\r\n");
 313:   
 314:                          telnet.Send(text);
 315:   
 316:                          //result.AddSuccess("Sent: " + text);
 317:                      }
 318:                      else
 319:                      {
 320:                          result.AddError("No text to send. ");
 321:                      }
 322:                  }
 323:                  else
 324:                  {
 325:                      result.AddError("No established telnet connection. ");
 326:                      //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
 327:                  }
 328:              }
 329:              catch (Exception ex)
 330:              {
 331:                  //if(processRunning) waitToConnectionCounter = waitToConnectionCounterSeconds;
 332:   
 333:                  result.AddError(ex.Message);
 334:              }
 335:   
 336:              if (!skipSleep)
 337:              {
 338:                  // this is meant to give the Telnet_EndReceive() time to receive and process data from session
 339:                  // we will wait according to the type of commands send
 340:   
 341:                  if (Ia.Ngn.Cl.Model.Business.Huawei.Ems.IsACfgCommand(LastSentCommand))
 342:                  {
 343:                      System.Threading.Thread.Sleep(Ia.Ngn.Cl.Model.Client.Huawei.Ems.WaitAfterSendForCfgCommandInMillisecond);
 344:                  }
 345:                  else
 346:                  {
 347:                      System.Threading.Thread.Sleep(Ia.Ngn.Cl.Model.Client.Huawei.Ems.WaitAfterSendInMillisecond);
 348:                  }
 349:              }
 350:          }
 351:   
 352:          ////////////////////////////////////////////////////////////////////////////
 353:   
 354:          /// <summary>
 355:          ///
 356:          /// </summary>
 357:          private void Telnet_EndReceive(object sender, Dart.PowerTCP.Telnet.SegmentEventArgs e)
 358:          {
 359:              string message, entry;
 360:              string[] receiveStringSplit;
 361:   
 362:              Debug.WriteLine(" ");
 363:              Debug.WriteLine("============================================");
 364:   
 365:              if (e.Exception == null)
 366:              {
 367:                  receiveString += e.Segment.ToString();
 368:   
 369:                  Debug.WriteLine("Telnet_EndReceive(): e.Segment: [" + e.Segment.ToString() + "], receiveString: [" + receiveString + "]");
 370:   
 371:                  if (!string.IsNullOrEmpty(receiveString))
 372:                  {
 373:                      receiveStringSplit = receiveString.Split(new string[] { "\r\n;" }, StringSplitOptions.None);
 374:   
 375:                      if (receiveStringSplit.Length > 0)
 376:                      {
 377:                          for (int i = 0; i < receiveStringSplit.Length; i++)
 378:                          {
 379:                              entry = receiveStringSplit[i];
 380:   
 381:                              if (!string.IsNullOrEmpty(entry) && !string.IsNullOrWhiteSpace(entry))
 382:                              {
 383:                                  // will not enqueue an empty entry
 384:   
 385:                                  if (i == receiveStringSplit.Length - 1)
 386:                                  {
 387:                                      // if there is a non empty last entry that means its incomplete and we will assign it to receiveString
 388:   
 389:                                      receiveString = entry;
 390:                                  }
 391:                                  else
 392:                                  {
 393:                                      message = entry + "\r\n;"; // important
 394:   
 395:                                      if (!ReceiveQueue.Contains(message))
 396:                                      {
 397:                                          // will not enqueue duplicate
 398:   
 399:                                          ReceiveQueue.Enqueue(message);
 400:   
 401:                                          Debug.WriteLine("Telnet_EndReceive(): ReceiveQueue.Enqueue(message): [" + message + "]");
 402:                                      }
 403:   
 404:                                      receiveString = string.Empty;
 405:                                  }
 406:                              }
 407:                          }
 408:                      }
 409:   
 410:                      Debug.WriteLine("Telnet_EndReceive(): receiveString: [" + receiveString + "]");
 411:                  }
 412:                  else
 413:                  {
 414:   
 415:                  }
 416:   
 417:                  if (telnet.Connected) telnet.BeginReceive(buffer);
 418:              }
 419:              else
 420:              {
 421:                  Debug.WriteLine("Telnet_EndReceive(): Exception: " + e.Exception.Message);
 422:   
 423:                  // I will not throw or handle the exception from the event handler
 424:                  // https://stackoverflow.com/questions/3114543/should-event-handlers-in-c-sharp-ever-raise-exceptions
 425:                  //throw new Exception("Telnet_EndReceive(): " + e.Exception.Message);
 426:              }
 427:   
 428:              Debug.WriteLine("============================================");
 429:              Debug.WriteLine(" ");
 430:          }
 431:   
 432:          ////////////////////////////////////////////////////////////////////////////
 433:   
 434:          /// <summary>
 435:          ///
 436:          /// </summary>
 437:          private void Telnet_ConnectedChangedEx(object sender, System.EventArgs e)
 438:          {
 439:              // always raised when Connection is established or closed (Connected property changes)
 440:   
 441:              //UpdateToolStripStatusLabel();
 442:          }
 443:   
 444:          ////////////////////////////////////////////////////////////////////////////
 445:   
 446:          /// <summary>
 447:          ///
 448:          /// </summary>
 449:          public Ia.Cl.Model.Result Update(string rowData, ref Ia.Ngn.Cl.Model.Client.Huawei.Ems ems)
 450:          {
 451:              return Ia.Ngn.Cl.Model.Business.Huawei.Ems.UpdateDatabaseWithEmsCommandOutput(rowData, ref ems);
 452:          }
 453:   
 454:          ////////////////////////////////////////////////////////////////////////////
 455:          ////////////////////////////////////////////////////////////////////////////
 456:      }
 457:   
 458:      ////////////////////////////////////////////////////////////////////////////
 459:      ////////////////////////////////////////////////////////////////////////////
 460:  }