1: using Microsoft.EntityFrameworkCore;
2: using System;
3: using System.Collections;
4: using System.Collections.Generic;
5: using System.Linq;
6: using System.Text;
7:
8: namespace Ia.Ngn.Cl.Model.Data.Nokia
9: {
10: ////////////////////////////////////////////////////////////////////////////
11:
12: /// <summary publish="true">
13: /// ONT support class for Optical Fiber Network (OFN) Nokia data model.
14: /// </summary>
15: ///
16: /// <remarks>
17: /// Copyright © 2006-2022 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
18: ///
19: /// 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
20: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
21: ///
22: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
23: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
24: ///
25: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
26: ///
27: /// Copyright notice: This notice may not be removed or altered from any source distribution.
28: /// </remarks>
29: public class Ont
30: {
31: /// <summary/>
32: public Ont() { }
33:
34: ////////////////////////////////////////////////////////////////////////////
35:
36: /// <summary>
37: ///
38: /// </summary>
39: public static bool Create(Ia.Ngn.Cl.Model.Ont ont, out string result)
40: {
41: bool b;
42:
43: b = false;
44: result = string.Empty;
45:
46: using (var db = new Ia.Ngn.Cl.Model.Ngn())
47: {
48: ont.Created = ont.Updated = DateTime.UtcNow.AddHours(3);
49:
50: db.Onts.Add(ont);
51: db.SaveChanges();
52:
53: b = true;
54: }
55:
56: return b;
57: }
58:
59: ////////////////////////////////////////////////////////////////////////////
60:
61: /// <summary>
62: ///
63: /// </summary>
64: public static Ia.Ngn.Cl.Model.Ont Read(string id)
65: {
66: Ia.Ngn.Cl.Model.Ont ont;
67:
68: using (var db = new Ia.Ngn.Cl.Model.Ngn())
69: {
70: ont = (from o in db.Onts
71: where o.Id == id
72: select o).Include(o => o.OntServiceVoips).AsNoTracking().SingleOrDefault();
73: }
74:
75: return ont;
76: }
77:
78: ////////////////////////////////////////////////////////////////////////////
79:
80: /// <summary>
81: ///
82: /// </summary>
83: public static Ia.Ngn.Cl.Model.Ont ReadIncludeAccess(string id)
84: {
85: Ia.Ngn.Cl.Model.Ont item;
86:
87: using (var db = new Ia.Ngn.Cl.Model.Ngn())
88: {
89: item = (from o in db.Onts where o.Id == id select o).Include(u => u.Access).SingleOrDefault();
90: }
91:
92: return item;
93: }
94:
95: ////////////////////////////////////////////////////////////////////////////
96:
97: /// <summary>
98: ///
99: /// </summary>
100: public static List<Ia.Ngn.Cl.Model.Ont> List()
101: {
102: List<Ia.Ngn.Cl.Model.Ont> ontList;
103:
104: using (var db = new Ia.Ngn.Cl.Model.Ngn())
105: {
106: ontList = (from o in db.Onts select o).ToList();
107: }
108:
109: return ontList;
110: }
111:
112: ////////////////////////////////////////////////////////////////////////////
113:
114: /// <summary>
115: ///
116: /// </summary>
117: public static Dictionary<string, string> IdToAccessIdDictionary
118: {
119: get
120: {
121: Dictionary<string, string> dictionary, nullDictionary;
122:
123: using (var db = new Ia.Ngn.Cl.Model.Ngn())
124: {
125: dictionary = (from o in db.Onts where o.Access != null select new { o.Id, o.Access }).ToDictionary(u => u.Id, u => u.Access.Id);
126:
127: nullDictionary = (from o in db.Onts where o.Access == null select o.Id).ToDictionary(u => u, null);
128: }
129:
130: return dictionary.Union(nullDictionary).ToDictionary(u => u.Key, u => u.Value);
131: }
132: }
133:
134:
135: ////////////////////////////////////////////////////////////////////////////
136:
137: /// <summary>
138: ///
139: /// </summary>
140: public static Dictionary<string, string> AccessIdToIdDictionary
141: {
142: get
143: {
144: Dictionary<string, string> dictionary, idToAccessIdDictionary;
145:
146: using (var db = new Ia.Ngn.Cl.Model.Ngn())
147: {
148: idToAccessIdDictionary = IdToAccessIdDictionary;
149:
150: dictionary = new Dictionary<string, string>(idToAccessIdDictionary.Count);
151:
152: foreach (KeyValuePair<string, string> kvp in idToAccessIdDictionary)
153: {
154: if (!dictionary.ContainsKey(kvp.Value)) dictionary[kvp.Value] = kvp.Key;
155: }
156: }
157:
158: return dictionary;
159: }
160: }
161:
162: ////////////////////////////////////////////////////////////////////////////
163:
164: /// <summary>
165: ///
166: /// </summary>
167: public static Dictionary<string, string> IdToDescription1ForNonNullAccessDictionary
168: {
169: get
170: {
171: Dictionary<string, string> dictionary;
172:
173: using (var db = new Ia.Ngn.Cl.Model.Ngn())
174: {
175: dictionary = (from s in db.Onts
176: where s.Access != null
177: select new
178: {
179: s.Id,
180: s.Description1
181: }).AsNoTracking().ToDictionary(u => u.Id, u => u.Description1);
182: }
183:
184: return dictionary;
185: }
186: }
187:
188: ////////////////////////////////////////////////////////////////////////////
189:
190: /// <summary>
191: ///
192: /// </summary>
193: public static Dictionary<string, string> IdToDescription1ForNullAccessDictionary
194: {
195: get
196: {
197: Dictionary<string, string> dictionary;
198:
199: using (var db = new Ia.Ngn.Cl.Model.Ngn())
200: {
201: dictionary = (from s in db.Onts
202: where s.Access == null
203: select new
204: {
205: s.Id,
206: s.Description1
207: }).AsNoTracking().ToDictionary(u => u.Id, u => u.Description1);
208: }
209:
210: return dictionary;
211: }
212: }
213:
214: ////////////////////////////////////////////////////////////////////////////
215:
216: /// <summary>
217: ///
218: /// </summary>
219: public static Dictionary<int, int> DistributionOfFamilyTypeIdInOntWhereSerialIsAssignedAndAccessIsNotNullDictionary
220: {
221: get
222: {
223: Dictionary<int, int> dictionary;
224:
225: using (var db = new Ia.Ngn.Cl.Model.Ngn())
226: {
227: var v = (from s in db.Onts where s.Serial != null && s.Serial != "ALCL00000000" && s.Access != null group s by s.FamilyTypeId into g select new { FamilyTypeId = g.Key, Count = g.Count() });
228: dictionary = v.ToDictionary(u => u.FamilyTypeId, u => u.Count);
229: }
230:
231: return dictionary;
232: }
233: }
234:
235: ////////////////////////////////////////////////////////////////////////////
236:
237: /// <summary>
238: ///
239: /// </summary>
240: public static Hashtable IdWithNullAccessHashtable
241: {
242: get
243: {
244: List<string> list;
245: Hashtable ht;
246:
247: using (var db = new Ia.Ngn.Cl.Model.Ngn())
248: {
249: list = (from o in db.Onts where o.Access == null select o.Id).ToList();
250:
251: if (list.Count > 0)
252: {
253: ht = new Hashtable(list.Count);
254:
255: foreach (string s in list) ht[s] = null;
256: }
257: else ht = null;
258: }
259:
260: return ht;
261: }
262: }
263:
264: ////////////////////////////////////////////////////////////////////////////
265:
266: /// <summary>
267: ///
268: /// </summary>
269: public static Dictionary<string, string> SerialToIdListDictionary
270: {
271: get
272: {
273: Dictionary<string, string> dictionary;
274:
275: dictionary = IdToSerialDictionary.Reverse();
276:
277: return dictionary;
278: }
279: }
280:
281: ////////////////////////////////////////////////////////////////////////////
282:
283: /// <summary>
284: ///
285: /// </summary>
286: public static Dictionary<string, string> IdToSerialDictionary
287: {
288: get
289: {
290: Dictionary<string, string> dictionary;
291:
292: using (var db = new Ia.Ngn.Cl.Model.Ngn())
293: {
294: dictionary = (from s in db.Onts
295: select new
296: {
297: s.Id,
298: s.Serial
299: }).AsNoTracking().ToDictionary(u => u.Id, u => u.Serial);
300: }
301:
302: return dictionary.ToDictionary(u => u.Key, u => u.Value);
303: }
304: }
305:
306: ////////////////////////////////////////////////////////////////////////////
307:
308: /// <summary>
309: ///
310: /// </summary>
311: public static List<string> IdList()
312: {
313: List<string> list;
314:
315: using (var db = new Ia.Ngn.Cl.Model.Ngn())
316: {
317: list = (from o in db.Onts select o.Id).ToList();
318: }
319:
320: return list;
321: }
322:
323: ////////////////////////////////////////////////////////////////////////////
324:
325: /// <summary>
326: ///
327: /// </summary>
328: public static List<string> IdList(int oltId)
329: {
330: List<string> list;
331:
332: using (var db = new Ia.Ngn.Cl.Model.Ngn())
333: {
334: list = (from o in db.Onts where o.Access.Olt == oltId select o.Id).ToList();
335: }
336:
337: return list;
338: }
339:
340: ////////////////////////////////////////////////////////////////////////////
341:
342: /// <summary>
343: ///
344: /// </summary>
345: public static bool Update(Ia.Ngn.Cl.Model.Ont ont, out string result)
346: {
347: bool b;
348:
349: b = false;
350: result = string.Empty;
351:
352: using (var db = new Ia.Ngn.Cl.Model.Ngn())
353: {
354: ont = (from o in db.Onts where o.Id == ont.Id select o).SingleOrDefault();
355:
356: ont.Updated = DateTime.UtcNow.AddHours(3);
357:
358: db.Onts.Attach(ont);
359:
360: db.Entry(ont).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
361: db.SaveChanges();
362:
363: b = true;
364: }
365:
366: return b;
367: }
368:
369: ////////////////////////////////////////////////////////////////////////////
370:
371: /// <summary>
372: ///
373: /// </summary>
374: public static bool NullifyAccessIdByAccessId(string accessId, out string result)
375: {
376: bool b;
377: int numberOfRecordsWhereAccessIsNullified;
378: Ia.Ngn.Cl.Model.Ont ont;
379:
380: b = false;
381: numberOfRecordsWhereAccessIsNullified = 0;
382:
383: using (var db = new Ia.Ngn.Cl.Model.Ngn())
384: {
385: // --update Onts set Access_Id = null where Access_Id = '1040101010040004'
386: //var query = (from o in db.Onts where o.Access.Id == accessId select o).ToList();
387:
388: //foreach (var q in query)
389: //{
390: //ont = (from o in db.Onts where o.Id == q.Id select o).SingleOrDefault();
391: ont = (from o in db.Onts where o.Access.Id == accessId select o).Include(u => u.Access).FirstOrDefault(); //.SingleOrDefault();
392:
393: if (ont != null)
394: {
395: ont.Access = null;
396: //ont.UserId = userId;
397: ont.Updated = DateTime.UtcNow.AddHours(3);
398:
399: db.Onts.Attach(ont);
400: db.Entry(ont).Property(u => u.Updated).IsModified = true;
401:
402: db.SaveChanges();
403:
404: numberOfRecordsWhereAccessIsNullified++;
405: }
406: //}
407:
408: b = true;
409: }
410:
411: result = "Number of records where access is nullified: " + numberOfRecordsWhereAccessIsNullified;
412:
413: return b;
414: }
415:
416: ////////////////////////////////////////////////////////////////////////////
417:
418: /// <summary>
419: ///
420: /// </summary>
421: public static bool Delete(string id, out string result)
422: {
423: bool b;
424:
425: b = false;
426: result = string.Empty;
427:
428: using (var db = new Ia.Ngn.Cl.Model.Ngn())
429: {
430: var v = (from o in db.Onts where o.Id == id select o).FirstOrDefault();
431:
432: db.Onts.Remove(v);
433: db.SaveChanges();
434:
435: b = true;
436: }
437:
438: return b;
439: }
440:
441: ////////////////////////////////////////////////////////////////////////////
442: ////////////////////////////////////////////////////////////////////////////
443:
444: /// <summary>
445: ///
446: /// </summary>
447: public static string FamilyTypeStringFromId(int familyTypeId)
448: {
449: string s;
450: Ia.Ngn.Cl.Model.Business.Nokia.Ont.FamilyType familyType;
451:
452: familyType = (Ia.Ngn.Cl.Model.Business.Nokia.Ont.FamilyType)familyTypeId;
453:
454: s = familyType.ToString().ToUpper();
455:
456: return s;
457: }
458:
459: ////////////////////////////////////////////////////////////////////////////
460:
461: /// <summary>
462: ///
463: /// </summary>
464: public static List<Ia.Ngn.Cl.Model.Ont> List(string serial)
465: {
466: List<Ia.Ngn.Cl.Model.Ont> list;
467:
468: using (var db = new Ia.Ngn.Cl.Model.Ngn())
469: {
470: list = (from o in db.Onts where o.Serial == serial select o).ToList();
471: }
472:
473: return list;
474: }
475:
476: ////////////////////////////////////////////////////////////////////////////
477:
478: /// <summary>
479: ///
480: /// </summary>
481: public static List<Ia.Ngn.Cl.Model.Ont> List(int oltId)
482: {
483: List<Ia.Ngn.Cl.Model.Ont> list;
484:
485: using (var db = new Ia.Ngn.Cl.Model.Ngn())
486: {
487: list = (from o in db.Onts where o.Access.Olt == oltId select o).ToList();
488: }
489:
490: return list;
491: }
492:
493: ////////////////////////////////////////////////////////////////////////////
494:
495: /// <summary>
496: ///
497: /// </summary>
498: public static List<Ia.Ngn.Cl.Model.Ont> ReadListIncludeOntServiceVoipsAndAccess(int oltId)
499: {
500: List<Ia.Ngn.Cl.Model.Ont> list;
501:
502: using (var db = new Ia.Ngn.Cl.Model.Ngn())
503: {
504: list = (from o in db.Onts where o.Access.Olt == oltId select o).Include(u => u.Access).Include(u => u.OntServiceVoips).ToList();
505: }
506:
507: return list;
508: }
509:
510: ////////////////////////////////////////////////////////////////////////////
511:
512: /// <summary>
513: ///
514: /// </summary>
515: public static List<Ia.Ngn.Cl.Model.Ont> ListIncludeAccess()
516: {
517: List<Ia.Ngn.Cl.Model.Ont> ontList;
518:
519: using (var db = new Ia.Ngn.Cl.Model.Ngn())
520: {
521: ontList = (from o in db.Onts select o).Include(u => u.Access).AsNoTracking().ToList();
522: }
523:
524: return ontList;
525: }
526:
527: ////////////////////////////////////////////////////////////////////////////
528:
529: /// <summary>
530: ///
531: /// </summary>
532: public static List<Ia.Ngn.Cl.Model.Ont> ListIncludeAccessAndOntOntPots()
533: {
534: List<Ia.Ngn.Cl.Model.Ont> ontList;
535:
536: using (var db = new Ia.Ngn.Cl.Model.Ngn())
537: {
538: ontList = (from o in db.Onts select o).Include(u => u.Access).Include(v => v.OntOntPotses).ToList();
539:
540: /*
541: ontList = (from o in db.Onts
542: join a in db.Accesses on o.Access equals a
543: join oop in db.OntOntPotses on o equals oop.Ont select o).ToList();
544: */
545: }
546:
547: return ontList;
548: }
549:
550: ////////////////////////////////////////////////////////////////////////////
551:
552: /// <summary>
553: ///
554: /// </summary>
555: public static List<Ia.Ngn.Cl.Model.Ont> ListIncludeAccessAndOntOntPots(int oltId)
556: {
557: List<Ia.Ngn.Cl.Model.Ont> ontList;
558:
559: using (var db = new Ia.Ngn.Cl.Model.Ngn())
560: {
561: ontList = (from o in db.Onts where o.Access.Olt == oltId select o).Include(u => u.Access).Include(v => v.OntOntPotses).ToList();
562: }
563:
564: return ontList;
565: }
566:
567: ////////////////////////////////////////////////////////////////////////////
568:
569: /// <summary>
570: ///
571: /// </summary>
572: public static List<Ia.Ngn.Cl.Model.Ont> ListIncludeAccess(int oltId)
573: {
574: List<Ia.Ngn.Cl.Model.Ont> ontList;
575:
576: using (var db = new Ia.Ngn.Cl.Model.Ngn())
577: {
578: ontList = (from o in db.Onts where o.Access.Olt == oltId select o).Include(u => u.Access).ToList();
579: }
580:
581: return ontList;
582: }
583:
584: ////////////////////////////////////////////////////////////////////////////
585:
586: /// <summary>
587: ///
588: /// </summary>
589: public static Dictionary<string, int> IdToFamilyTypeIdOfNonNullAccessDictionary()
590: {
591: var dictionary = new Dictionary<string, int>();
592:
593: using (var db = new Ia.Ngn.Cl.Model.Ngn())
594: {
595: dictionary = (from o in db.Onts
596: where o.Access != null
597: select new { o.Id, o.FamilyTypeId }).ToDictionary(n => n.Id, n => n.FamilyTypeId);
598: }
599:
600: return dictionary;
601: }
602:
603: ////////////////////////////////////////////////////////////////////////////
604:
605: /// <summary>
606: ///
607: /// </summary>
608: public static List<Ia.Ngn.Cl.Model.Ont> NonNullAccessList()
609: {
610: List<Ia.Ngn.Cl.Model.Ont> ontList;
611:
612: using (var db = new Ia.Ngn.Cl.Model.Ngn())
613: {
614: ontList = (from o in db.Onts
615: where o.Access != null
616: select o).Include(u => u.Access).ToList();
617: }
618:
619: return ontList;
620: }
621:
622: ////////////////////////////////////////////////////////////////////////////
623:
624: /// <summary>
625: ///
626: /// </summary>
627: public static List<Ia.Ngn.Cl.Model.Ont> NonNullAccessList(int oltId)
628: {
629: List<Ia.Ngn.Cl.Model.Ont> ontList;
630:
631: using (var db = new Ia.Ngn.Cl.Model.Ngn())
632: {
633: ontList = (from o in db.Onts
634: where o.Access != null && o.Access.Olt == oltId
635: select o).Include(u => u.Access).ToList();
636: }
637:
638: return ontList;
639: }
640:
641: ////////////////////////////////////////////////////////////////////////////
642:
643: /// <summary>
644: ///
645: /// </summary>
646: public static List<string> ReadNetworkDesignDocumentAccessNameListWithOntEquipmentIdNotNullAndAccessIsNullIncludeOntServiceVoips
647: {
648: get
649: {
650: Dictionary<string, string> di;
651: List<string> ontNameList;
652: List<Ia.Ngn.Cl.Model.Ont> ontList;
653:
654: using (var db = new Ia.Ngn.Cl.Model.Ngn())
655: {
656: ontList = (from o in db.Onts where o.EquipmentId != null && o.Access == null select o).ToList();
657:
658: ontNameList = new List<string>(ontList.Count);
659:
660: di = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntAccessIdToOntAccessNameDictionary;
661:
662: foreach (var ont in ontList)
663: {
664: if (di.ContainsKey(ont.Id)) ontNameList.Add(di[ont.Id]);
665: }
666: }
667:
668: return ontNameList;
669: }
670: }
671:
672: ////////////////////////////////////////////////////////////////////////////
673:
674: /// <summary>
675: ///
676: /// </summary>
677: public static List<string> ServiceWhereOntIsOfflineList()
678: {
679: string service;
680: List<string> list, serviceList;
681:
682: // Nokia ONT is not SIP therefore I can't read the service anywhere from the GPON
683: /*
684: using (var db = new Ia.Ngn.Cl.Model.Ngn())
685: {
686: list = (from o in db.Onts where o.Id == id select o).Include(u => u.Access).SingleOrDefault();
687:
688: list = (from oop in db.OntOntPots
689: join o in db.Onts on oop.Ont.Id equals o.Id
690: where o.StateId == (int)Ia.Ngn.Cl.Model.Business.Nokia.Ams.BellcoreState.IsNr
691: select oop.).AsNoTracking().ToList();
692: }
693:
694: if (list != null && list.Count > 0)
695: {
696: serviceList = new List<string>(list.Count);
697:
698: foreach (var s in list)
699: {
700: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(s);
701:
702: serviceList.Add(service);
703: }
704: }
705: else
706: {
707: serviceList = new List<string>();
708: }
709: */
710:
711: serviceList = new List<string>();
712:
713: return serviceList;
714: }
715:
716: ////////////////////////////////////////////////////////////////////////////
717: ////////////////////////////////////////////////////////////////////////////
718:
719: /// <summary>
720: ///
721: /// </summary>
722: public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Ont ont)
723: {
724: StringBuilder sb;
725:
726: sb = new StringBuilder();
727:
728: //sb.AppendLine("Vendor: " + Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Vendor.Nokia.Name);
729: sb.AppendLine("State: " + ont.State);
730: sb.AppendLine("FamilyType: " + Ia.Ngn.Cl.Model.Data.Nokia.Ont.FamilyTypeStringFromId(ont.FamilyTypeId));
731: sb.AppendLine("Serial: " + ont.Serial);
732: sb.AppendLine("EquipmentId: " + ont.EquipmentId);
733: sb.AppendLine("ActiveSoftware: " + ont.ActiveSoftware);
734: //sb.AppendLine("PassiveSoftware: " + ont.PassiveSoftware);
735: sb.AppendLine("PlannedSoftware: " + ont.PlannedSoftware);
736: //sb.AppendLine("BatteryBackupAvailable: " + ont.BatteryBackupAvailable);
737: sb.AppendLine("Description1: " + ont.Description1);
738: sb.AppendLine("Description2: " + ont.Description2);
739:
740: return sb.ToString();
741: }
742:
743: ////////////////////////////////////////////////////////////////////////////
744: ////////////////////////////////////////////////////////////////////////////
745: }
746:
747: ////////////////////////////////////////////////////////////////////////////
748: ////////////////////////////////////////////////////////////////////////////
749: }