-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathProviderManager.cs
1016 lines (809 loc) · 49.5 KB
/
ProviderManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NextGenSoftware.OASIS.API.Core.Enums;
using NextGenSoftware.OASIS.API.Core.Helpers;
using NextGenSoftware.OASIS.API.Core.Interfaces;
using NextGenSoftware.OASIS.API.DNA;
using NextGenSoftware.OASIS.Common;
using NextGenSoftware.Utilities;
namespace NextGenSoftware.OASIS.API.Core.Managers
{
//TODO: Need to upgrade all methods to return OASISResult wrapper ASAP! :)
public class ProviderManager : OASISManager
{
private static ProviderManager _instance = null;
private List<IOASISProvider> _registeredProviders = new List<IOASISProvider>();
private List<EnumValue<ProviderType>> _registeredProviderTypes = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providerAutoFailOverList { get; set; } = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providerAutoFailOverListForAvatarLogin { get; set; } = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providerAutoFailOverListForCheckIfEmailAlreadyInUse { get; set; } = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providerAutoFailOverListForCheckIfUsernameAlreadyInUse { get; set; } = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providersThatAreAutoReplicating { get; set; } = new List<EnumValue<ProviderType>>();
private List<EnumValue<ProviderType>> _providerAutoLoadBalanceList { get; set; } = new List<EnumValue<ProviderType>>();
private bool _setProviderGlobally = false;
public EnumValue<ProviderType> CurrentStorageProviderType { get; private set; } = new EnumValue<ProviderType>(ProviderType.Default);
public EnumValue<ProviderCategory> CurrentStorageProviderCategory { get; private set; } = new EnumValue<ProviderCategory>(ProviderCategory.None);
public OASISProviderBootType OASISProviderBootType { get; set; } = OASISProviderBootType.Hot;
public bool IsAutoReplicationEnabled { get; set; } = true;
public bool IsAutoLoadBalanceEnabled { get; set; } = true;
public bool IsAutoFailOverEnabled { get; set; } = true;
//public bool IsAutoFailOverEnabledForAvatarLogin { get; set; } = true;
//public bool IsAutoFailOverEnabledForCheckIfEmailAlreadyInUse { get; set; } = true;
//public bool IsAutoFailOverEnabledForCheckIfUsernameAlreadyInUse { get; set; } = true;
//public string CurrentStorageProviderName
//{
// get
// {
// return Enum.GetName(CurrentStorageProviderType);
// }
//}
// public string[] DefaultProviderTypes { get; set; }
public IOASISStorageProvider DefaultGlobalStorageProvider { get; set; }
public IOASISStorageProvider CurrentStorageProvider { get; private set; } //TODO: Need to work this out because in future there can be more than one provider active at a time.
public bool OverrideProviderType { get; set; } = false;
//public delegate void StorageProviderError(object sender, AvatarManagerErrorEventArgs e);
public static ProviderManager Instance
{
get
{
if (_instance == null)
_instance = new ProviderManager(null);
return _instance;
}
}
//TODO: In future more than one storage provider can be active at a time where each call can specify which provider to use.
public ProviderManager(IOASISStorageProvider OASISStorageProvider, OASISDNA OASISDNA = null) : base(OASISStorageProvider, OASISDNA)
{
}
//TODO: In future the registered providers will be dynamically loaded from MEF by watching a hot folder for compiled provider dlls (and other ways in future...)
public bool RegisterProvider(IOASISProvider provider)
{
if (!_registeredProviders.Any(x => x.ProviderType == provider.ProviderType))
{
_registeredProviders.Add(provider);
_registeredProviderTypes.Add(provider.ProviderType);
return true;
}
return false;
}
public bool RegisterProviders(List<IOASISProvider> providers)
{
bool returnValue = false;
foreach (IOASISProvider provider in providers)
returnValue = RegisterProvider(provider);
return returnValue;
}
public bool UnRegisterProvider(IOASISProvider provider)
{
DeActivateProvider(provider);
_registeredProviders.Remove(provider);
_registeredProviderTypes.Remove(provider.ProviderType);
return true;
}
public bool UnRegisterProvider(ProviderType providerType)
{
foreach (IOASISProvider provider in _registeredProviders)
{
if (provider.ProviderType.Value == providerType)
{
UnRegisterProvider(provider);
break;
}
}
return true;
}
public bool UnRegisterProviders(List<ProviderType> providerTypes)
{
foreach (ProviderType providerType in providerTypes)
UnRegisterProvider(providerType);
return true;
}
public bool UnRegisterProviders(List<IOASISProvider> providers)
{
foreach (IOASISProvider provider in providers)
_registeredProviders.Remove(provider);
return true;
}
public ProviderCategory GetProviderCategory(ProviderType providerType)
{
foreach (IOASISProvider provider in _registeredProviders)
{
if (provider.ProviderType.Value == providerType)
return provider.ProviderCategory.Value;
}
return ProviderCategory.None;
}
public List<IOASISProvider> GetAllRegisteredProviders()
{
return _registeredProviders;
}
public List<EnumValue<ProviderType>> GetAllRegisteredProviderTypes()
{
return _registeredProviderTypes;
}
public List<IOASISProvider> GetProvidersOfCategory(ProviderCategory category)
{
return _registeredProviders.Where(x => x.ProviderCategory.Value == category).ToList();
}
public List<ProviderType> GetProviderTypesOfCategory(ProviderCategory category)
{
return GetProviderTypes(GetProvidersOfCategory(category));
}
public List<IOASISStorageProvider> GetStorageProviders()
{
List<IOASISStorageProvider> storageProviders = new List<IOASISStorageProvider>();
foreach (IOASISProvider provider in _registeredProviders.Where(x => x.ProviderCategory.Value == ProviderCategory.Storage || x.ProviderCategory.Value == ProviderCategory.StorageAndNetwork || x.ProviderCategory.Value == ProviderCategory.StorageLocal || x.ProviderCategory.Value == ProviderCategory.StorageLocalAndNetwork).ToList())
storageProviders.Add((IOASISStorageProvider)provider);
return storageProviders;
}
public List<ProviderType> GetStorageProviderTypes()
{
//return GetProviderTypes(GetStorageProviders().Select(x => x.ProviderType);
return GetStorageProviders().Select(x => x.ProviderType.Value).ToList();
}
public List<IOASISNETProvider> GetNetworkProviders()
{
List<IOASISNETProvider> networkProviders = new List<IOASISNETProvider>();
foreach (IOASISProvider provider in _registeredProviders.Where(x => x.ProviderCategory.Value == ProviderCategory.Network || x.ProviderCategory.Value == ProviderCategory.StorageAndNetwork || x.ProviderCategory.Value == ProviderCategory.StorageLocalAndNetwork).ToList())
networkProviders.Add((IOASISNETProvider)provider);
return networkProviders;
}
public List<ProviderType> GetNetworkProviderTypes()
{
return GetNetworkProviders().Select(x => x.ProviderType.Value).ToList();
//List<ProviderType> providerTypes = new List<ProviderType>();
//foreach (IOASISProvider provider in GetNetworkProviders())
// providerTypes.Add(provider.ProviderType);
//return providerTypes;
}
public List<ProviderType> GetProviderTypes(List<IOASISProvider> providers)
{
List<ProviderType> providerTypes = new List<ProviderType>();
foreach (IOASISProvider provider in providers)
providerTypes.Add(provider.ProviderType.Value);
return providerTypes;
}
public List<IOASISRenderer> GetRendererProviders()
{
List<IOASISRenderer> rendererProviders = new List<IOASISRenderer>();
foreach (IOASISProvider provider in _registeredProviders.Where(x => x.ProviderCategory.Value == ProviderCategory.Renderer).ToList())
rendererProviders.Add((IOASISRenderer)provider);
return rendererProviders;
}
public IOASISProvider GetProvider(ProviderType type)
{
return _registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type);
}
public IOASISStorageProvider GetStorageProvider(ProviderType type)
{
return (IOASISStorageProvider)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type);
//return (IOASISStorageProvider)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type && x.ProviderCategory.Value == ProviderCategory.Storage);
}
public IOASISNETProvider GetNetworkProvider(ProviderType type)
{
return (IOASISNETProvider)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type);
//return (IOASISNETProvider)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type && x.ProviderCategory.Value == ProviderCategory.Network);
}
public IOASISRenderer GetRendererProvider(ProviderType type)
{
return (IOASISRenderer)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type);
//return (IOASISRenderer)_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type && x.ProviderCategory.Value == ProviderCategory.Renderer);
}
public bool IsProviderRegistered(IOASISProvider provider)
{
return _registeredProviders.Any(x => x.ProviderName == provider.ProviderName);
}
public bool IsProviderRegistered(ProviderType providerType)
{
return _registeredProviders.Any(x => x.ProviderType.Value == providerType);
}
//public IOASISSuperStar SetAndActivateCurrentSuperStarProvider(ProviderType providerType)
//{
// SetAndActivateCurrentStorageProvider(providerType);
// }
// Called from Managers.
public OASISResult<IOASISStorageProvider> SetAndActivateCurrentStorageProvider(ProviderType providerType)
{
OASISResult<IOASISStorageProvider> result = new OASISResult<IOASISStorageProvider>();
if (providerType == ProviderType.Default)
result = SetAndActivateCurrentStorageProvider();
else
result = SetAndActivateCurrentStorageProvider(providerType, false);
if (result.IsError)
result.Message = string.Concat("ERROR: The ", Enum.GetName(providerType), " provider may not be registered. Please register it before calling this method. Reason: ", result.Message);
return result;
}
public async Task<OASISResult<IOASISStorageProvider>> SetAndActivateCurrentStorageProviderAsync(ProviderType providerType)
{
OASISResult<IOASISStorageProvider> result = new OASISResult<IOASISStorageProvider>();
if (providerType == ProviderType.Default)
result = await SetAndActivateCurrentStorageProviderAsync();
else
result = await SetAndActivateCurrentStorageProviderAsync(providerType, false);
if (result.IsError)
result.Message = string.Concat("ERROR: The ", Enum.GetName(providerType), " provider may not be registered. Please register it before calling this method. Reason: ", result.Message);
return result;
}
//TODO: Called internally (make private ?)
public async Task<OASISResult<IOASISStorageProvider>> SetAndActivateCurrentStorageProviderAsync()
{
// If a global provider has been set and the REST API call has not overiden the provider (OverrideProviderType) then set to global provider.
if (DefaultGlobalStorageProvider != null && DefaultGlobalStorageProvider != CurrentStorageProvider && !OverrideProviderType)
return await SetAndActivateCurrentStorageProviderAsync(DefaultGlobalStorageProvider);
// Otherwise set to default provider (configured in appSettings.json) if the provider has not been overiden in the REST call.
//else if (!OverrideProviderType && DefaultProviderTypes != null && CurrentStorageProviderType.Value != (ProviderType)Enum.Parse(typeof(ProviderType), DefaultProviderTypes[0]))
else if (!OverrideProviderType && _providerAutoFailOverList.Count > 0 && CurrentStorageProviderType.Value != _providerAutoFailOverList[0].Value) // TODO: Come back to this, not sure we should be setting the first entry every time? Needs thinking and testing through! ;-)
return await SetAndActivateCurrentStorageProviderAsync(ProviderType.Default, false);
if (!_setProviderGlobally)
OverrideProviderType = false;
return new OASISResult<IOASISStorageProvider>(CurrentStorageProvider);
}
//TODO: Called internally (make private ?)
public OASISResult<IOASISStorageProvider> SetAndActivateCurrentStorageProvider()
{
// If a global provider has been set and the REST API call has not overiden the provider (OverrideProviderType) then set to global provider.
if (DefaultGlobalStorageProvider != null && DefaultGlobalStorageProvider != CurrentStorageProvider && !OverrideProviderType)
return SetAndActivateCurrentStorageProvider(DefaultGlobalStorageProvider);
// Otherwise set to default provider (configured in appSettings.json) if the provider has not been overiden in the REST call.
//else if (!OverrideProviderType && DefaultProviderTypes != null && CurrentStorageProviderType.Value != (ProviderType)Enum.Parse(typeof(ProviderType), DefaultProviderTypes[0]))
else if (!OverrideProviderType && _providerAutoFailOverList.Count > 0 && CurrentStorageProviderType.Value != _providerAutoFailOverList[0].Value) // TODO: Come back to this, not sure we should be setting the first entry every time? Needs thinking and testing through! ;-)
return SetAndActivateCurrentStorageProvider(ProviderType.Default, false);
if (!_setProviderGlobally)
OverrideProviderType = false;
return new OASISResult<IOASISStorageProvider>(CurrentStorageProvider);
}
// Called from ONode.WebAPI.OASISProviderManager.
public OASISResult<IOASISStorageProvider> SetAndActivateCurrentStorageProvider(IOASISProvider OASISProvider)
{
if (OASISProvider != CurrentStorageProvider)
{
if (OASISProvider != null)
{
if (!IsProviderRegistered(OASISProvider))
RegisterProvider(OASISProvider);
return SetAndActivateCurrentStorageProvider(OASISProvider.ProviderType.Value);
}
}
return new OASISResult<IOASISStorageProvider>(CurrentStorageProvider);
}
public async Task<OASISResult<IOASISStorageProvider>> SetAndActivateCurrentStorageProviderAsync(IOASISProvider OASISProvider)
{
if (OASISProvider != CurrentStorageProvider)
{
if (OASISProvider != null)
{
if (!IsProviderRegistered(OASISProvider))
RegisterProvider(OASISProvider);
return await SetAndActivateCurrentStorageProviderAsync(OASISProvider.ProviderType.Value);
}
}
return new OASISResult<IOASISStorageProvider>(CurrentStorageProvider);
}
// Called from ONode.WebAPI.OASISProviderManager.
//TODO: In future more than one StorageProvider will be active at a time so we need to work out how to handle this...
public OASISResult<IOASISStorageProvider> SetAndActivateCurrentStorageProvider(ProviderType providerType, bool setGlobally = false)
{
OASISResult<IOASISStorageProvider> result = new OASISResult<IOASISStorageProvider>();
_setProviderGlobally = setGlobally;
// This is automatically handled in the Managers (AvatarManager, HolonManager, etc) whenever a provider throws an exception, it will try the next provider in the list... :)
if (providerType == ProviderType.Default && !OverrideProviderType && _providerAutoFailOverList.Count > 0)
providerType = _providerAutoFailOverList[0].Value;
if (providerType != CurrentStorageProviderType.Value)
{
IOASISProvider provider = _registeredProviders.FirstOrDefault(x => x.ProviderType.Value == providerType);
if (provider == null)
{
OASISErrorHandling.HandleError(ref result, string.Concat(Enum.GetName(typeof(ProviderType), providerType), " ProviderType is not registered. Please call RegisterProvider() method to register the provider before calling this method."));
return result;
}
if (provider != null && (provider.ProviderCategory.Value == ProviderCategory.Storage
|| provider.ProviderCategory.Value == ProviderCategory.StorageAndNetwork
|| provider.ProviderCategory.Value == ProviderCategory.StorageLocal
|| provider.ProviderCategory.Value == ProviderCategory.StorageLocalAndNetwork))
{
if (CurrentStorageProvider != null)
{
OASISResult<bool> deactivateProviderResult = DeActivateProvider(CurrentStorageProvider);
//TODO: Think its not an error as long as it can activate a provider below?
if ((deactivateProviderResult != null && (deactivateProviderResult.IsError || !deactivateProviderResult.Result)) || deactivateProviderResult == null)
OASISErrorHandling.HandleWarning(ref result, deactivateProviderResult != null ? $"Error Occured In ProviderManager.SetAndActivateCurrentStorageProvider Calling DeActivateProvider For Provider {CurrentStorageProviderType.Name}. Reason: {deactivateProviderResult.Message}" : "Unknown error (deactivateProviderResult was null!)");
else
LoggingManager.Log($"{CurrentStorageProviderType.Name} Provider DeActivated Successfully.", Logging.LogType.Info);
}
CurrentStorageProviderCategory = provider.ProviderCategory;
CurrentStorageProviderType.Value = providerType;
CurrentStorageProvider = (IOASISStorageProvider)provider;
OASISResult<bool> activateProviderResult = ActivateProvider(CurrentStorageProvider);
if ((activateProviderResult != null && (activateProviderResult.IsError || !activateProviderResult.Result)) || activateProviderResult == null)
OASISErrorHandling.HandleError(ref result, activateProviderResult != null ? $"Error Occured In ProviderManager.SetAndActivateCurrentStorageProvider Calling ActivateProvider For Provider {CurrentStorageProviderType.Name}. Reason: {activateProviderResult.Message}" : "Unknown error (activateProviderResult was null!)");
else
LoggingManager.Log($"{CurrentStorageProviderType.Name} Provider Activated Successfully.", Logging.LogType.Info);
if (setGlobally)
DefaultGlobalStorageProvider = CurrentStorageProvider;
}
}
result.Result = CurrentStorageProvider;
return result;
}
public async Task<OASISResult<IOASISStorageProvider>> SetAndActivateCurrentStorageProviderAsync(ProviderType providerType, bool setGlobally = false)
{
OASISResult<IOASISStorageProvider> result = new OASISResult<IOASISStorageProvider>();
_setProviderGlobally = setGlobally;
// This is automatically handled in the Managers (AvatarManager, HolonManager, etc) whenever a provider throws an exception, it will try the next provider in the list... :)
if (providerType == ProviderType.Default && !OverrideProviderType && _providerAutoFailOverList.Count > 0)
providerType = _providerAutoFailOverList[0].Value;
if (providerType != CurrentStorageProviderType.Value)
{
IOASISProvider provider = _registeredProviders.FirstOrDefault(x => x.ProviderType.Value == providerType);
if (provider == null)
{
OASISErrorHandling.HandleError(ref result, string.Concat(Enum.GetName(typeof(ProviderType), providerType), " ProviderType is not registered. Please call RegisterProvider() method to register the provider before calling this method."));
return result;
}
if (provider != null && (provider.ProviderCategory.Value == ProviderCategory.Storage
|| provider.ProviderCategory.Value == ProviderCategory.StorageAndNetwork
|| provider.ProviderCategory.Value == ProviderCategory.StorageLocal
|| provider.ProviderCategory.Value == ProviderCategory.StorageLocalAndNetwork))
{
if (CurrentStorageProvider != null)
{
OASISResult<bool> deactivateProviderResult = await DeActivateProviderAsync(CurrentStorageProvider);
//TODO: Think its not an error as long as it can activate a provider below?
if ((deactivateProviderResult != null && (deactivateProviderResult.IsError || !deactivateProviderResult.Result)) || deactivateProviderResult == null)
OASISErrorHandling.HandleWarning(ref result, deactivateProviderResult != null ? $"Error Occured In ProviderManager.SetAndActivateCurrentStorageProviderAsync Calling DeActivateProviderAsync For Provider {CurrentStorageProviderType.Name}. Reason: {deactivateProviderResult.Message}" : "Unknown error (deactivateProviderResult was null!)");
else
LoggingManager.Log($"{CurrentStorageProviderType.Name} Provider DeActivated Successfully (Async).", Logging.LogType.Info);
}
CurrentStorageProviderCategory = provider.ProviderCategory;
CurrentStorageProviderType.Value = providerType;
CurrentStorageProvider = (IOASISStorageProvider)provider;
OASISResult<bool> activateProviderResult = await ActivateProviderAsync(CurrentStorageProvider);
if ((activateProviderResult != null && (activateProviderResult.IsError || !activateProviderResult.Result)) || activateProviderResult == null)
OASISErrorHandling.HandleError(ref result, activateProviderResult != null ? $"Error Occured In ProviderManager.SetAndActivateCurrentStorageProviderAsync Calling ActivateProviderAsync For Provider {CurrentStorageProviderType.Name}. Reason: {activateProviderResult.Message}" : "Unknown error (activateProviderResult was null!)");
else
LoggingManager.Log($"{CurrentStorageProviderType.Name} Provider Activated Successfully (Async).", Logging.LogType.Info);
if (setGlobally)
DefaultGlobalStorageProvider = CurrentStorageProvider;
}
}
result.Result = CurrentStorageProvider;
return result;
}
public async Task<OASISResult<bool>> ActivateProviderAsync(ProviderType type)
{
return await ActivateProviderAsync(_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type));
}
public async Task<OASISResult<bool>> ActivateProviderAsync(IOASISProvider provider)
{
OASISResult<bool> result = new OASISResult<bool>();
string errorMessage = $"Error Occured Activating Provider {provider.ProviderType.Name} In ProviderManager ActivateProviderAsync. Reason: ";
if (provider != null)
{
try
{
LoggingManager.Log($"Attempting To Activate {provider.ProviderType.Name} Provider (Async)...", Logging.LogType.Info, true);
var task = provider.ActivateProviderAsync();
if (await Task.WhenAny(task, Task.Delay(OASISDNA.OASIS.StorageProviders.ActivateProviderTimeOutSeconds * 1000)) == task)
result = task.Result;
else
OASISErrorHandling.HandleError(ref result, string.Concat(errorMessage, $"Timeout Occured! ActivateProviderTimeOutSeconds In OASISDNA.json Is Set To {OASISDNA.OASIS.StorageProviders.ActivateProviderTimeOutSeconds}. Try Increasing The Value And Try Again..."));
}
catch (Exception ex)
{
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Unknown Error Occured: {ex}");
}
}
else
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Provider passed in is null!");
return result;
}
public OASISResult<bool> ActivateProvider(ProviderType type)
{
return ActivateProvider(_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type));
}
public OASISResult<bool> ActivateProvider(IOASISProvider provider)
{
OASISResult<bool> result = new OASISResult<bool>();
string errorMessage = $"Error Occured Activating Provider {provider.ProviderType.Name} In ProviderManager ActivateProvider. Reason: ";
if (provider != null)
{
try
{
LoggingManager.Log($"Attempting To Activate {provider.ProviderType.Name} Provider...", Logging.LogType.Info, true);
result = Task.Run(() => provider.ActivateProvider()).WaitAsync(TimeSpan.FromSeconds(OASISDNA.OASIS.StorageProviders.ActivateProviderTimeOutSeconds)).Result;
}
catch (TimeoutException)
{
OASISErrorHandling.HandleError(ref result, string.Concat(errorMessage, $"Timeout Occured! ActivateProviderTimeOutSeconds In OASISDNA.json Is Set To {OASISDNA.OASIS.StorageProviders.ActivateProviderTimeOutSeconds}. Try Increasing The Value And Try Again..."));
}
catch (Exception ex)
{
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Unknown Error Occured: {ex}");
}
}
else
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Provider passed in is null!");
return result;
}
public OASISResult<bool> DeActivateProvider(ProviderType type)
{
return DeActivateProvider(_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type));
}
public OASISResult<bool> DeActivateProvider(IOASISProvider provider)
{
OASISResult<bool> result = new OASISResult<bool>();
string errorMessage = $"Error Occured Deactivating Provider {provider.ProviderType.Name} In ProviderManager DeActivateProvider. Reason: ";
if (provider != null)
{
try
{
LoggingManager.Log($"Attempting To Deactivate {provider.ProviderType.Name} Provider...", Logging.LogType.Info, true);
result = Task.Run(() => provider.DeActivateProvider()).WaitAsync(TimeSpan.FromSeconds(OASISDNA.OASIS.StorageProviders.DectivateProviderTimeOutSeconds)).Result;
}
catch (TimeoutException)
{
OASISErrorHandling.HandleError(ref result, string.Concat(errorMessage, $"Timeout Occured! DectivateProviderTimeOutSeconds In OASISDNA.json Is Set To {OASISDNA.OASIS.StorageProviders.DectivateProviderTimeOutSeconds}. Try Increasing The Value And Try Again..."));
}
catch (Exception ex)
{
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Unknown Error Occured: {ex}");
}
}
else
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Provider passed in is null!");
return result;
}
public async Task<OASISResult<bool>> DeActivateProviderAsync(ProviderType type)
{
return await DeActivateProviderAsync(_registeredProviders.FirstOrDefault(x => x.ProviderType.Value == type));
}
public async Task<OASISResult<bool>> DeActivateProviderAsync(IOASISProvider provider)
{
OASISResult<bool> result = new OASISResult<bool>();
string errorMessage = $"Error Occured Deactivating Provider {provider.ProviderType.Name} In ProviderManager DeActivateProviderAsync. Reason: ";
if (provider != null)
{
try
{
LoggingManager.Log($"Attempting To Deactivate {provider.ProviderType.Name} Provider (Async)...", Logging.LogType.Info, true);
var task = provider.DeActivateProviderAsync();
if (await Task.WhenAny(task, Task.Delay(OASISDNA.OASIS.StorageProviders.DectivateProviderTimeOutSeconds * 1000)) == task)
result = task.Result;
else
OASISErrorHandling.HandleError(ref result, string.Concat(errorMessage, $"Timeout Occured! DectivateProviderTimeOutSeconds In OASISDNA.json Is Set To {OASISDNA.OASIS.StorageProviders.DectivateProviderTimeOutSeconds}. Try Increasing The Value And Try Again..."));
}
catch (Exception ex)
{
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Unknown Error Occured: {ex}");
}
}
else
OASISErrorHandling.HandleError(ref result, $"{errorMessage}Provider passed in is null!");
return result;
}
public bool SetAutoReplicationForProviders(bool autoReplicate, IEnumerable<ProviderType> providers)
{
return SetProviderList(autoReplicate, providers, _providersThatAreAutoReplicating);
}
public OASISResult<bool> SetAutoReplicationForProviders(bool autoReplicate, string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoReplicate", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
result.Result = SetAutoReplicationForProviders(autoReplicate, listResult.Result);
return result;
}
public OASISResult<bool> SetAndReplaceAutoReplicationListForProviders(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoReplicate", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
_providersThatAreAutoReplicating.Clear();
foreach (ProviderType providerType in listResult.Result)
_providersThatAreAutoReplicating.Add(new EnumValue<ProviderType>(providerType));
return result;
}
public OASISResult<bool> SetAndReplaceAutoReplicationListForProviders(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providersThatAreAutoReplicating = providerList.ToList();
return new OASISResult<bool>(true);
}
public bool SetAutoReplicateForAllProviders(bool autoReplicate)
{
return SetAutoReplicationForProviders(autoReplicate, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public bool SetAutoFailOverForProviders(bool addToFailOverList, IEnumerable<ProviderType> providers)
{
return SetProviderList(addToFailOverList, providers, _providerAutoFailOverList);
}
public bool SetAutoFailOverForProvidersForAvatarLogin(bool addToFailOverList, IEnumerable<ProviderType> providers)
{
return SetProviderList(addToFailOverList, providers, _providerAutoFailOverListForAvatarLogin);
}
public bool SetAutoFailOverForProvidersForCheckIfEmailAlreadyInUse(bool addToFailOverList, IEnumerable<ProviderType> providers)
{
return SetProviderList(addToFailOverList, providers, _providerAutoFailOverListForCheckIfEmailAlreadyInUse);
}
public bool SetAutoFailOverForProvidersForCheckIfUsernameAlreadyInUse(bool addToFailOverList, IEnumerable<ProviderType> providers)
{
return SetProviderList(addToFailOverList, providers, _providerAutoFailOverListForCheckIfUsernameAlreadyInUse);
}
public OASISResult<bool> SetAutoFailOverForProviders(bool addToFailOverList, string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoFailOver", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
result.Result = SetAutoFailOverForProviders(addToFailOverList, listResult.Result);
return result;
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProviders(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoFailOver", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
_providerAutoFailOverList.Clear();
foreach (ProviderType providerType in listResult.Result)
_providerAutoFailOverList.Add(new EnumValue<ProviderType>(providerType));
return result;
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForAvatarLogin(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoFailOverForAvatarLogin", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
_providerAutoFailOverListForAvatarLogin.Clear();
foreach (ProviderType providerType in listResult.Result)
_providerAutoFailOverListForAvatarLogin.Add(new EnumValue<ProviderType>(providerType));
return result;
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForCheckIfEmailAlreadyInUse(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoFailOverForCheckIfEmailAlreadyInUse", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
_providerAutoFailOverListForCheckIfEmailAlreadyInUse.Clear();
foreach (ProviderType providerType in listResult.Result)
_providerAutoFailOverListForCheckIfEmailAlreadyInUse.Add(new EnumValue<ProviderType>(providerType));
return result;
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForCheckIfUsernameAlreadyInUse(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoFailOverForCheckIfUsernameAlreadyInUse", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
_providerAutoFailOverListForCheckIfUsernameAlreadyInUse.Clear();
foreach (ProviderType providerType in listResult.Result)
_providerAutoFailOverListForCheckIfUsernameAlreadyInUse.Add(new EnumValue<ProviderType>(providerType));
return result;
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProviders(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providerAutoFailOverList = providerList.ToList();
return new OASISResult<bool>(true);
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForAvatarLogin(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providerAutoFailOverListForAvatarLogin = providerList.ToList();
return new OASISResult<bool>(true);
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForCheckIfEmailAlreadyInUse(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providerAutoFailOverListForCheckIfEmailAlreadyInUse = providerList.ToList();
return new OASISResult<bool>(true);
}
public OASISResult<bool> SetAndReplaceAutoFailOverListForProvidersForCheckIfUsernameAlreadyInUse(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providerAutoFailOverListForCheckIfUsernameAlreadyInUse = providerList.ToList();
return new OASISResult<bool>(true);
}
public OASISResult<T> ValidateProviderList<T>(string listName, string providerList)
{
string[] providers = providerList.Split(',');
object providerTypeObject = null;
foreach (string provider in providers)
{
if (!Enum.TryParse(typeof(ProviderType), provider.Trim(), out providerTypeObject))
return new OASISResult<T>() { Message = $"The ProviderType {provider.Trim()} passed in for the {listName} list is invalid. It must be one of the following types: {EnumHelper.GetEnumValues(typeof(ProviderType), EnumHelperListType.ItemsSeperatedByComma)}.", IsError = true };
}
return new OASISResult<T>();
}
public OASISResult<IEnumerable<ProviderType>> GetProvidersFromList(string listName, string providerList)
{
OASISResult<IEnumerable<ProviderType>> result = new OASISResult<IEnumerable<ProviderType>>();
List<ProviderType> providerTypes = new List<ProviderType>();
string[] providers = providerList.Split(",");
object providerTypeObject = null;
List<string> invalidProviderTypes = new List<string>();
foreach (string provider in providers)
{
if (Enum.TryParse(typeof(ProviderType), provider.Trim(), out providerTypeObject))
providerTypes.Add((ProviderType)providerTypeObject);
else
{
invalidProviderTypes.Add(provider.Trim());
//OASISErrorHandling.HandleWarning(ref result, $"{provider.Trim()} listName} list is invalid.");
OASISErrorHandling.HandleWarning(ref result, $"Error in GetProvidersFromList method in ProviderManager, the provider {provider.Trim()} specified in the {listName} list is invalid.");
}
}
if (result.WarningCount > 0)
result.Message = $"Error in GetProvidersFromList method in ProviderManager. {result.WarningCount} provider type(s) passed in for the {listName} list are invalid:\n\n{OASISResultHelper.BuildInnerMessageError(invalidProviderTypes, ", ", true)}.\n\nThey must be one of the following values: {EnumHelper.GetEnumValues(typeof(ProviderType))}";
//result.Message = $"Error in GetProvidersFromList method in ProviderManager. {result.WarningCount} provider type(s) passed in for the {listName} are invalid:\n\n{OASISResultHelper.BuildInnerMessageError(result.InnerMessages)}.\n\nThey must be one of the following values: {EnumHelper.GetEnumValues(typeof(ProviderType))}";
result.Result = providerTypes;
return result;
}
public OASISResult<IEnumerable<EnumValue<ProviderType>>> GetProvidersFromListAsEnumList(string listName, string providerList)
{
OASISResult<IEnumerable<EnumValue<ProviderType>>> result = new OASISResult<IEnumerable<EnumValue<ProviderType>>>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList(listName, providerList);
if (!listResult.IsError && listResult.Result != null)
result.Result = EnumHelper.ConvertToEnumValueList(listResult.Result);
else
OASISErrorHandling.HandleError(ref result, $"Error occured in GetProvidersFromListAsEnumList method in ProviderManager. Reason: {listResult.Message}", listResult.DetailedMessage);
return result;
}
public bool SetAutoFailOverForAllProviders(bool addToFailOverList)
{
return SetAutoFailOverForProviders(addToFailOverList, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public bool SetAutoFailOverForAllProvidersForAvatarLogin(bool addToFailOverList)
{
return SetAutoFailOverForProvidersForAvatarLogin(addToFailOverList, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public bool SetAutoFailOverForAllProvidersForCheckIfEmailAlreadyInUse(bool addToFailOverList)
{
return SetAutoFailOverForProvidersForCheckIfEmailAlreadyInUse(addToFailOverList, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public bool SetAutoFailOverForProvidersForCheckIfUsernameAlreadyInUse(bool addToFailOverList)
{
return SetAutoFailOverForProvidersForCheckIfUsernameAlreadyInUse(addToFailOverList, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public bool SetAutoLoadBalanceForProviders(bool addToLoadBalanceList, IEnumerable<ProviderType> providers)
{
return SetProviderList(addToLoadBalanceList, providers, _providerAutoLoadBalanceList);
}
public OASISResult<bool> SetAutoLoadBalanceForProviders(bool addToLoadBalanceList, string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoLoadBalance", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
result.Result = SetAutoLoadBalanceForProviders(addToLoadBalanceList, listResult.Result);
return result;
}
public OASISResult<bool> SetAndReplaceAutoLoadBalanceListForProviders(string providerList)
{
OASISResult<bool> result = new OASISResult<bool>();
OASISResult<IEnumerable<ProviderType>> listResult = GetProvidersFromList("AutoLoadBalance", providerList);
result.InnerMessages.AddRange(listResult.InnerMessages);
result.IsWarning = listResult.IsWarning;
result.WarningCount += listResult.WarningCount;
if (!listResult.IsError && listResult.Result != null)
{
_providerAutoLoadBalanceList.Clear();
foreach (ProviderType providerType in listResult.Result)
_providerAutoLoadBalanceList.Add(new EnumValue<ProviderType>(providerType));
}
else
OASISErrorHandling.HandleError(ref result, $"Error occured in SetAndReplaceAutoLoadBalanceListForProviders method in ProviderManager. Reason: {listResult.Result}");
return result;
}
public OASISResult<bool> SetAndReplaceAutoLoadBalanceListForProviders(IEnumerable<EnumValue<ProviderType>> providerList)
{
_providerAutoLoadBalanceList = providerList.ToList();
return new OASISResult<bool>(true);
}
public bool SetAutoLoadBalanceForAllProviders(bool addToLoadBalanceList)
{
return SetAutoLoadBalanceForProviders(addToLoadBalanceList, _registeredProviderTypes.Select(x => x.Value).ToList());
}
public List<EnumValue<ProviderType>> GetProviderAutoLoadBalanceList()
{
return _providerAutoLoadBalanceList;
}
public List<EnumValue<ProviderType>> GetProviderAutoFailOverList()
{
return _providerAutoFailOverList;
}
public List<EnumValue<ProviderType>> GetProviderAutoFailOverListForAvatarLogin()
{
return _providerAutoFailOverListForAvatarLogin;
}
public List<EnumValue<ProviderType>> GetProviderAutoFailOverListForCheckIfEmailAlreadyInUse()
{
return _providerAutoFailOverListForCheckIfEmailAlreadyInUse;
}
public List<EnumValue<ProviderType>> GetProviderAutoFailOverListForCheckIfUsernameAlreadyInUse()
{
return _providerAutoFailOverListForCheckIfUsernameAlreadyInUse;
}
public string GetProviderAutoFailOverListAsString()
{
return GetProviderListAsString(GetProviderAutoFailOverList());
}
public string GetProviderAutoFailOverListForAvatarLoginAsString()
{
return GetProviderListAsString(GetProviderAutoFailOverListForAvatarLogin());
}
public string GetProviderAutoFailOverListForCheckIfEmailAlreadyInUseAsString()
{
return GetProviderListAsString(GetProviderAutoFailOverListForCheckIfEmailAlreadyInUse());
}
public string GetProviderAutoFailOverListForCheckIfUsernameAlreadyInUseAsString()
{
return GetProviderListAsString(GetProviderAutoFailOverListForCheckIfUsernameAlreadyInUse());
}
public string GetProvidersThatAreAutoReplicatingAsString()
{
return GetProviderListAsString(GetProvidersThatAreAutoReplicating());
}
public string GetProviderAutoLoadBalanceListAsString()
{
return GetProviderListAsString(GetProviderAutoLoadBalanceList());
}
public string GetProviderListAsString(List<ProviderType> providerList)
{
return GetProviderListAsString(EnumHelper.ConvertToEnumValueList(providerList).ToList());
}
public string GetProviderListAsString(List<EnumValue<ProviderType>> providerList)
{
string list = "";
for (int i = 0; i < providerList.Count(); i++)
{
list = string.Concat(list, providerList[i].Name);
if (i < providerList.Count - 2)
list = string.Concat(list, ", ");
else if (i == providerList.Count() - 2)
list = string.Concat(list, " & ");
}
return list;
}
//public List<EnumValue<ProviderType>> GetProvidersThatAreAutoReplicating()
//{
// return _providersThatAreAutoReplicating;
//}
public List<EnumValue<ProviderType>> GetProvidersThatAreAutoReplicating()
{
//TODO: Handle OASISResult properly and make all methods return OASISResult ASAP!
//string providerListCache = GetProviderListAsString(_providersThatAreAutoReplicating);
//return GetProvidersFromListAsEnumList("AutoReplicate", providerListCache).Result.ToList();
return _providersThatAreAutoReplicating;
}
private bool SetProviderList(bool add, IEnumerable<ProviderType> providers, List<EnumValue<ProviderType>> listToAddTo)
{
foreach (ProviderType providerType in providers)
{
if (add && !listToAddTo.Any(x => x.Value == providerType))
listToAddTo.Add(new EnumValue<ProviderType>(providerType));
else if (!add)