1: using Microsoft.EntityFrameworkCore;
2: using System;
3: using System.Collections.Generic;
4: using System.Data;
5: using System.Data.Linq.SqlClient;
6: using System.Linq;
7: using System.Text;
8:
9: namespace Ia.Ngn.Cl.Model.Data.Huawei
10: {
11: ////////////////////////////////////////////////////////////////////////////
12:
13: /// <summary publish="true">
14: /// AGCF Users (ASBR) support class for Huawei's Optical Fiber Network (OFN) data model.
15: /// </summary>
16: ///
17: /// <remarks>
18: /// Copyright © 2019-2020 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated 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 class Asbr
31: {
32: /// <summary/>
33: public Asbr() { }
34:
35: ////////////////////////////////////////////////////////////////////////////
36:
37: /// <summary>
38: ///
39: /// </summary>
40: public static bool Update(Ia.Ngn.Cl.Model.Business.Huawei.SoftX.Response response, out Ia.Cl.Model.Result result)
41: {
42: bool isUpdated;
43: int readItemCount, existingItemCount, insertedItemCount, updatedItemCount, deletedItemCount;
44: string id, mgwId, eid, queryImpuSipDomain, queryCommand;
45: Ia.Ngn.Cl.Model.Huawei.Asbr asbr, newAsbr;
46: Ia.Ngn.Cl.Model.Huawei.Mgw mgw;
47:
48: isUpdated = false;
49: readItemCount = existingItemCount = insertedItemCount = updatedItemCount = deletedItemCount = 0;
50: result = new Ia.Cl.Model.Result();
51:
52: queryCommand = response.CommandString;
53:
54: // LST ASBR: PUI="sip:+96525449809@ims.moc.kw";
55: queryImpuSipDomain = Ia.Cl.Model.Default.Match(queryCommand, @"PUI=""(.+?)""");
56:
57: readItemCount = response.Count;
58:
59: if (response.QueryDictionary.ContainsKey("EID"))
60: {
61: eid = response.QueryDictionary["EID"];
62:
63: mgwId = Ia.Ngn.Cl.Model.Business.Huawei.Mgw.MgwId(response.NetworkElement, eid);
64:
65: using (var db = new Ia.Ngn.Cl.Model.Ngn())
66: {
67: mgw = (from m in db.Mgws where m.Id == mgwId select m).SingleOrDefault();
68:
69: if (mgw != null)
70: {
71: id = Ia.Ngn.Cl.Model.Business.Huawei.Asbr.AsbrId(response.NetworkElement, queryImpuSipDomain);
72:
73: asbr = (from a in db.Asbrs where /*a.Mgw.Id == mgwId &&*/ a.Id == id select a).SingleOrDefault();
74:
75: if (response.Count >= 1)
76: {
77: newAsbr = new Ia.Ngn.Cl.Model.Huawei.Asbr()
78: {
79: Id = id,
80: PUI = queryImpuSipDomain,
81: NetworkElement = response.NetworkElement,
82: PRI = response.QueryDictionary.ContainsKey("PRI") ? response.QueryDictionary["PRI"] : string.Empty,
83: REGTP = response.QueryDictionary.ContainsKey("REGTP") ? response.QueryDictionary["REGTP"] : string.Empty,
84: DID = response.QueryDictionary.ContainsKey("DID") ? response.QueryDictionary["DID"] : string.Empty,
85: DIDG = response.QueryDictionary.ContainsKey("DIDG") ? response.QueryDictionary["DIDG"] : string.Empty,
86: TET = response.QueryDictionary.ContainsKey("TET") ? response.QueryDictionary["TET"] : string.Empty,
87: MN = response.QueryDictionary.ContainsKey("MN") ? int.Parse(response.QueryDictionary["MN"]) : 0,
88: MGWID = response.QueryDictionary.ContainsKey("MGWID") ? Ia.Ngn.Cl.Model.Business.Huawei.SoftX.SpecialIntegerParameterHandling("MGWID", response.QueryDictionary["MGWID"]) : 0,
89: TEN = response.QueryDictionary.ContainsKey("TEN") ? response.QueryDictionary["TEN"] : string.Empty,
90: EDESC = response.QueryDictionary.ContainsKey("EDESC") ? response.QueryDictionary["EDESC"] : string.Empty,
91: EID = response.QueryDictionary.ContainsKey("EID") ? response.QueryDictionary["EID"] : string.Empty,
92: RGN = response.QueryDictionary.ContainsKey("RGN") ? response.QueryDictionary["RGN"] : string.Empty,
93: TID = response.QueryDictionary.ContainsKey("TID") ? response.QueryDictionary["TID"] : string.Empty,
94: LKS = response.QueryDictionary.ContainsKey("LKS") ? response.QueryDictionary["LKS"] : string.Empty,
95: IID = response.QueryDictionary.ContainsKey("IID") ? int.Parse(response.QueryDictionary["IID"]) : 0,
96: SGIAD = response.QueryDictionary.ContainsKey("SGIAD") ? int.Parse(response.QueryDictionary["SGIAD"]) : 0,
97: ISDN = response.QueryDictionary.ContainsKey("ISDN") ? response.QueryDictionary["ISDN"] : string.Empty,
98: ISA = response.QueryDictionary.ContainsKey("ISA") ? response.QueryDictionary["ISA"] : string.Empty,
99: TRUNKGP = response.QueryDictionary.ContainsKey("TRUNKGP") ? response.QueryDictionary["TRUNKGP"] : string.Empty,
100: V5IID = response.QueryDictionary.ContainsKey("V5IID") ? response.QueryDictionary["V5IID"] : string.Empty,
101: L3ADDR = response.QueryDictionary.ContainsKey("L3ADDR") ? int.Parse(response.QueryDictionary["L3ADDR"]) : 0,
102: B1NO = response.QueryDictionary.ContainsKey("B1NO") ? int.Parse(response.QueryDictionary["B1NO"]) : 0,
103: B2NO = response.QueryDictionary.ContainsKey("B2NO") ? int.Parse(response.QueryDictionary["B2NO"]) : 0,
104: ISDNDS = response.QueryDictionary.ContainsKey("ISDNDS") ? response.QueryDictionary["ISDNDS"] : string.Empty,
105: PLF = response.QueryDictionary.ContainsKey("PLF") ? bool.Parse(response.QueryDictionary["PLF"]) : false,
106: TS = response.QueryDictionary.ContainsKey("TS") ? Ia.Ngn.Cl.Model.Business.Huawei.SoftX.SpecialBooleanParameterHandling("TS", response.QueryDictionary["TS"]) : false,
107: DT = response.QueryDictionary.ContainsKey("DT") ? response.QueryDictionary["DT"] : string.Empty,
108: HNID = response.QueryDictionary.ContainsKey("HNID") ? response.QueryDictionary["HNID"] : string.Empty,
109: NETID = response.QueryDictionary.ContainsKey("NETID") ? response.QueryDictionary["NETID"] : string.Empty,
110: NETINFO = response.QueryDictionary.ContainsKey("NETINFO") ? response.QueryDictionary["NETINFO"] : string.Empty,
111: PHNCON = response.QueryDictionary.ContainsKey("PHNCON") ? response.QueryDictionary["PHNCON"] : string.Empty,
112: DIGMAP = response.QueryDictionary.ContainsKey("DIGMAP") ? response.QueryDictionary["DIGMAP"] : string.Empty,
113: GLOBDMAPIDX = response.QueryDictionary.ContainsKey("GLOBDMAPIDX") ? response.QueryDictionary["GLOBDMAPIDX"] : string.Empty,
114: PWD = response.QueryDictionary.ContainsKey("PWD") ? response.QueryDictionary["PWD"] : string.Empty,
115: SGN = response.QueryDictionary.ContainsKey("SGN") ? response.QueryDictionary["SGN"] : string.Empty,
116: SOCGN = response.QueryDictionary.ContainsKey("SOCGN") ? response.QueryDictionary["SOCGN"] : string.Empty,
117: EMGCN = response.QueryDictionary.ContainsKey("EMGCN") ? response.QueryDictionary["EMGCN"] : string.Empty,
118: DP = response.QueryDictionary.ContainsKey("DP") ? response.QueryDictionary["DP"] : string.Empty,
119: DR = response.QueryDictionary.ContainsKey("DR") ? response.QueryDictionary["DR"] : string.Empty,
120: CONF = response.QueryDictionary.ContainsKey("CONF") ? response.QueryDictionary["CONF"] : string.Empty,
121: CLIPMD = response.QueryDictionary.ContainsKey("CLIPMD") ? response.QueryDictionary["CLIPMD"] : string.Empty,
122: IFMIMN = response.QueryDictionary.ContainsKey("IFMIMN") ? int.Parse(response.QueryDictionary["IFMIMN"]) : 0,
123: CODEC = response.QueryDictionary.ContainsKey("CODEC") ? response.QueryDictionary["CODEC"] : string.Empty,
124: CGP = response.QueryDictionary.ContainsKey("CGP") ? response.QueryDictionary["CGP"] : string.Empty,
125: Mgw = (from m in db.Mgws where m.Id == mgwId select m).SingleOrDefault()
126: };
127:
128: if (asbr == null)
129: {
130: newAsbr.Created = newAsbr.Updated = DateTime.UtcNow.AddHours(3);
131:
132: db.Asbrs.Add(newAsbr);
133:
134: insertedItemCount++;
135: }
136: else
137: {
138: existingItemCount = 1;
139:
140: if (asbr.Update(newAsbr))
141: {
142: db.Asbrs.Attach(asbr);
143: db.Entry(asbr).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
144:
145: updatedItemCount++;
146: }
147: }
148: }
149: else
150: {
151: // below: remove since reading was empty
152:
153: if (asbr != null)
154: {
155: existingItemCount = 1;
156:
157: asbr = (from a in db.Asbrs where a.Id == id select a).SingleOrDefault();
158:
159: db.Asbrs.Remove(asbr);
160:
161: deletedItemCount++;
162: }
163: }
164:
165: db.SaveChanges();
166: }
167: else
168: {
169: result.AddWarning("Mgw is null for Asbr.Pui: " + queryImpuSipDomain);
170: }
171: }
172: }
173: else if (response.ReturnCode == Ia.Ngn.Cl.Model.Client.Huawei.SoftX.ResultCode.TheUserDoesNotExist)
174: {
175: // handle a non existing number with an unknown MGW
176:
177: using (var db = new Ia.Ngn.Cl.Model.Ngn())
178: {
179: id = Ia.Ngn.Cl.Model.Business.Huawei.Asbr.AsbrId(response.NetworkElement, queryImpuSipDomain);
180:
181: asbr = (from a in db.Asbrs where a.Id == id select a).SingleOrDefault();
182:
183: if (asbr != null)
184: {
185: existingItemCount = 1;
186:
187: asbr = (from a in db.Asbrs where a.Id == id select a).SingleOrDefault();
188:
189: db.Asbrs.Remove(asbr);
190:
191: deletedItemCount++;
192: }
193: else
194: {
195: //result.AddWarning("Asbr is null for Asbr.Pui: " + queryImpuSipDomain);
196: }
197:
198: db.SaveChanges();
199: }
200: }
201: else
202: {
203: result.AddWarning("Mgw EquipmentId (Eid) does not exist for Asbr.Pui: " + queryImpuSipDomain);
204: }
205:
206: if (insertedItemCount != 0 || updatedItemCount != 0 || deletedItemCount != 0) isUpdated = true;
207: else isUpdated = false;
208:
209: result.AddSuccess("(" + readItemCount + "/" + existingItemCount + "/" + insertedItemCount + "," + updatedItemCount + "," + deletedItemCount + ")");
210:
211: return isUpdated;
212: }
213:
214: ////////////////////////////////////////////////////////////////////////////
215:
216: /// <summary>
217: ///
218: /// </summary>
219: public static List<Ia.Ngn.Cl.Model.Huawei.Asbr> List()
220: {
221: List<Ia.Ngn.Cl.Model.Huawei.Asbr> list;
222:
223: using (var db = new Ia.Ngn.Cl.Model.Ngn())
224: {
225: list = (from s in db.Asbrs select s).ToList();
226: }
227:
228: return list;
229: }
230:
231: ////////////////////////////////////////////////////////////////////////////
232:
233: /// <summary>
234: ///
235: /// </summary>
236: public static List<string> PuiList()
237: {
238: List<string> list;
239:
240: using (var db = new Ia.Ngn.Cl.Model.Ngn())
241: {
242: list = (from a in db.Asbrs select a.PUI).ToList();
243: }
244:
245: return list;
246: }
247:
248: ////////////////////////////////////////////////////////////////////////////
249:
250: /// <summary>
251: ///
252: /// </summary>
253: public static List<string> ServiceList()
254: {
255: List<string> list, serviceList;
256:
257: using (var db = new Ia.Ngn.Cl.Model.Ngn())
258: {
259: list = (from a in db.Asbrs select a.PUI).Distinct().ToList();
260:
261: serviceList = new List<string>(list.Count);
262:
263: foreach (var pui in list)
264: {
265: serviceList.Add(Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(pui));
266: }
267: }
268:
269: return serviceList;
270: }
271:
272: ////////////////////////////////////////////////////////////////////////////
273:
274: /// <summary>
275: ///
276: /// </summary>
277: public static List<string> ServiceNotWithinSoftXDomainList()
278: {
279: var list = new List<string>();
280:
281: var softXDomainList = Ia.Ngn.Cl.Model.Data.Service.SoftXDomainList;
282:
283: var serviceList = Ia.Ngn.Cl.Model.Data.Huawei.Asbr.ServiceList();
284:
285: foreach (var s in serviceList)
286: {
287: if (!softXDomainList.Any(u => s.StartsWith(u.ToString()))) list.Add(s);
288: }
289:
290: list.Sort();
291:
292: return list;
293: }
294:
295: ////////////////////////////////////////////////////////////////////////////
296:
297: /// <summary>
298: ///
299: /// </summary>
300: public static List<Ia.Ngn.Cl.Model.Huawei.Asbr> List(List<string> serviceList)
301: {
302: List<Ia.Ngn.Cl.Model.Huawei.Asbr> list;
303:
304: using (var db = new Ia.Ngn.Cl.Model.Ngn())
305: {
306: list = (from s in db.Asbrs
307: //where serviceList.Contains(s.USRNUM)
308: select s).ToList();
309: }
310:
311: return list;
312: }
313:
314: ////////////////////////////////////////////////////////////////////////////
315:
316: /// <summary>
317: ///
318: /// </summary>
319: public static Dictionary<string, Ia.Ngn.Cl.Model.Business.Default.SwitchRoute> PstnServiceToSwitchRouteDictionary()
320: {
321: Dictionary<string, Ia.Ngn.Cl.Model.Business.Default.SwitchRoute> dictionary;
322:
323: dictionary = new Dictionary<string, Ia.Ngn.Cl.Model.Business.Default.SwitchRoute>();
324:
325: using (var db = new Ia.Ngn.Cl.Model.Ngn())
326: {
327: var list = (from s in db.Asbrs select new { s.Id /*.USRNUM, s.RNIDX2*/ }).ToList();
328:
329: foreach (var l in list)
330: {
331: //dictionary[l.USRNUM] = Ia.Ngn.Cl.Model.Business.Huawei.Asbr.RnidxRouteToSwitchRoute(l.RNIDX2);
332: }
333: }
334:
335: return dictionary;
336: }
337:
338: ////////////////////////////////////////////////////////////////////////////
339:
340: /// <summary>
341: ///
342: /// </summary>
343: public static List<Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated> ServiceOntWithinAllowedToBeProvisionedOrMigratedHuaweiSwitchNokiaAccessOltList()
344: {
345: int serviceType;
346: string service, serviceId;
347: List<int> oltIdList;
348: List<Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated> tempList, serviceOntList;
349:
350: serviceType = Ia.Ngn.Cl.Model.Business.Service.ServiceType.ImsService;
351:
352: //oltIdList = Ia.Ngn.Cl.Model.Data.Service.AllowedToBeProvisionedOltIdList.Intersect(Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiSwitchNokiaAccessOltIdList).ToList();
353: oltIdList = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.HuaweiRouterNokiaAccessOltIdList;
354:
355: var ontIpToOntForOltIdListDictionary = Ia.Ngn.Cl.Model.Data.NetworkDesignDocument.OntIpToOntForOltIdListDictionary(oltIdList);
356:
357: using (var db = new Ia.Ngn.Cl.Model.Ngn())
358: {
359: tempList = (from a in db.Asbrs
360: join m in db.Mgws on a.Mgw.Id equals m.Id
361: //where o.EmsOnt.Access != null && oltIdList.Contains(o.EmsOnt.Access.Olt) may use a special like ontIpRangeList?
362: select new Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated { Service = a.PUI, Ip = a.Mgw.RA1, CreatedDateTime = a.Created }).AsNoTracking().ToList();
363: }
364:
365: if (tempList != null && tempList.Count > 0)
366: {
367: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated>(tempList.Count);
368:
369: foreach (var so in tempList)
370: {
371: service = Ia.Ngn.Cl.Model.Business.NumberFormatConverter.Service(so.Service); // so.Service here is o.PUI
372:
373: serviceId = Ia.Ngn.Cl.Model.Business.Service.ServiceToServiceId(service, serviceType);
374:
375: var nddOnt = ontIpToOntForOltIdListDictionary.ContainsKey(so.Ip) ? ontIpToOntForOltIdListDictionary[so.Ip] : null;
376:
377: if (nddOnt != null)
378: {
379: serviceOntList.Add(new Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated
380: {
381: ServiceId = serviceId,
382: Service = service,
383: Ont = nddOnt,
384: AccessId = nddOnt.Access.Id,
385: CreatedDateTime = so.CreatedDateTime
386: });
387: }
388: }
389: }
390: else
391: {
392: serviceOntList = new List<Ia.Ngn.Cl.Model.Business.ServiceAccessIpOntCreated>();
393: }
394:
395: return serviceOntList;
396: }
397:
398: ////////////////////////////////////////////////////////////////////////////
399:
400: /// <summary>
401: ///
402: /// </summary>
403: public static int NextVacantTidForOnt(Ia.Ngn.Cl.Model.Business.NetworkDesignDocument.Ont nddOnt, Ia.Ngn.Cl.Model.Ont ont)
404: {
405: int tid;
406: List<int> list;
407:
408: var eid = Ia.Ngn.Cl.Model.Business.Huawei.SoftX.IpToEid(nddOnt.Ip);
409:
410: list = UsedTidListForEid(eid);
411:
412: if (list.Count == 0) tid = 1; // this means the box is empty, and we should create at 1
413: else
414: {
415: list = Ia.Cl.Model.Default.ExcludedNumberListFromNumberListWithinRange(list, Ia.Ngn.Cl.Model.Business.Nokia.Ont.PossibleNumberOfTdForOntFamilyType((Ia.Ngn.Cl.Model.Business.Nokia.Ont.FamilyType)ont.FamilyTypeId));
416:
417: if (list.Count > 0) tid = list[0];
418: else tid = Ia.Ngn.Cl.Model.Business.Default.PortUndefinedOrInvalidOrUnknown; // something went wrong
419: }
420:
421: return tid;
422: }
423:
424: ////////////////////////////////////////////////////////////////////////////
425:
426: /// <summary>
427: ///
428: /// </summary>
429: private static List<int> UsedTidListForEid(string eid)
430: {
431: List<int> list;
432:
433: using (var db = new Ia.Ngn.Cl.Model.Ngn())
434: {
435: var tidList = (from asb in db.Asbrs where asb.EID == eid orderby asb.TID ascending select asb.TID).ToList<string>();
436:
437: list = tidList.Select(int.Parse).ToList();
438: }
439:
440: return list;
441: }
442:
443: ////////////////////////////////////////////////////////////////////////////
444: ////////////////////////////////////////////////////////////////////////////
445:
446: /// <summary>
447: ///
448: /// </summary>
449: public static string ToSimpleTextString(Ia.Ngn.Cl.Model.Huawei.Asbr asbr)
450: {
451: StringBuilder sb;
452:
453: sb = new StringBuilder();
454:
455: sb.AppendLine("PUI: " + asbr.PUI);
456: sb.AppendLine("PRI: " + asbr.PRI);
457: sb.AppendLine("RGN: " + asbr.RGN);
458: sb.AppendLine("MN: " + asbr.MN);
459: sb.AppendLine("MGWID: " + asbr.MGWID);
460: sb.AppendLine("TEN: " + asbr.TEN);
461: sb.AppendLine("EDESC: " + asbr.EDESC);
462: sb.AppendLine("EID: " + asbr.EID);
463: sb.AppendLine("TID: " + asbr.TID);
464: //sb.AppendLine("ENDTID: " + asbr.ENDTID);
465: sb.AppendLine("V5IID: " + asbr.V5IID);
466: sb.AppendLine("LKS: " + asbr.LKS);
467: sb.AppendLine("IID: " + asbr.IID);
468: sb.AppendLine("SGIAD: " + asbr.SGIAD);
469: sb.AppendLine("TRUNKGP: " + asbr.TRUNKGP);
470: sb.AppendLine("L3ADDR: " + asbr.L3ADDR);
471: //sb.AppendLine("EL3ADDR: " + asbr.EL3ADDR);
472: //sb.AppendLine("STARTR: " + asbr.STARTR);
473: //sb.AppendLine("MRR: " + asbr.MRR);
474:
475: return sb.ToString();
476: }
477:
478: ////////////////////////////////////////////////////////////////////////////
479: ////////////////////////////////////////////////////////////////////////////
480: }
481:
482: ////////////////////////////////////////////////////////////////////////////
483: ////////////////////////////////////////////////////////////////////////////
484: }