summaryrefslogtreecommitdiffstats
path: root/django/engagementmanager/management/commands/initial_populate_db.py
blob: a0ca177d3dd43d39a89b64be89f8d92c4a701d77 (plain)
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
#  
# ============LICENSE_START========================================== 
# org.onap.vvp/engagementmgr
# ===================================================================
# Copyright © 2017 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the “License”);
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
""" intial_populate_db
Will create initial content to use this system.

This command is creating users, templates, companies, deployment targets, sites and much more.
Will run at the state of a clean system so it won't cause data collisions.

WARNING: Do not run while there is data at the system.
"""
import inspect
import random
from django.conf import settings
from django.core.management.base import BaseCommand
from django.utils import timezone
from engagementmanager.models import Role, Vendor, IceUserProfile, DeploymentTarget, \
    DeploymentTargetSite, Checklist, ChecklistDecision, ChecklistLineItem, \
    ChecklistTemplate, ChecklistSection, ECOMPRelease, Engagement, \
    CustomUser
from engagementmanager.models import VF
from engagementmanager.serializers import ThinVFModelSerializer
from engagementmanager.utils.constants import Roles, Constants
from engagementmanager.utils.validator import logEncoding
from engagementmanager.views_helper import createUserTemplate
from validationmanager.models import ValidationTest
from uuid import uuid4
from engagementmanager.service.logging_service import LoggingServiceFactory

logger = LoggingServiceFactory.get_logger()


class Command(BaseCommand):

    def handle(self, *args, **options):
        if (settings.ENVIRONMENT == "local" or settings.ENVIRONMENT == "development"):
            execute_bootstrap_actions()


companies = [
    'Affirmed Networks',
    'Amdocs',
    'Brocade',
    'Ciena',
    'Cisco',
    'Ericsson',
    'Fujitsu',
    'Juniper',
    'Metaswitch',
    'Nokia'
]

companies_not_public = [
    'Adtran',
    'Velocloud',
    'Spirent',
    'Mitel',
]

admin_dummy_users = [['admin bogus user', Constants.service_provider_admin_mail, '+1-23-456-78901']]

admin_ro_dummy_users = [
    ['ro admin bogus user', Constants.service_provider_admin_ro_mail, '+1-23-456-78901']]

dummy_users = [
    ['Bugs Bunny', 'bb@' + Constants.service_provider_mail_domain[0], '+1-404-986-9624'],
    ['CI Standard 1', 'ci_standard_1@' + Constants.service_provider_mail_domain[0], '+1-404-986-9624'],
]

el_dummy_users = [
    ['Donald Duck', 'dd1122@' + Constants.service_provider_mail_domain[0], '+1-404-986-9624'],
    ['Homer Simpson', 'hs0007@' + Constants.service_provider_mail_domain[0], '+1-425-281-3547']
]

checklist_templates = [
    {
        'name': 'Heat Templates',
        'category': 'heat',
        'version': 1,
        'sections': [
            {
                'name': 'Basic Heat Format and Syntax',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Filenames',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Valid YAML and HEAT',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]
            },
            {
                'name': 'Parameter And Resource Specification',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Parameters',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Resources',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Unique Names for Resources',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Outputs',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]

            },
            {
                'name': 'Naming Conventions',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Name, Flavor, and Image Assignments',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Availability Zones',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Required Metadata',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Optional Metadata',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Volumes',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Keys and Keypairs',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Networks',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Subnet',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Fixed IPs',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Allowed Address Pairs',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Ports',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]

            },
            {
                'name': 'General Guidelines',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'HEAT Files Support (get_file)',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'HTTP-based references',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Anti-Affinity and Affinity Rules',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Resource Data Synchronization',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]

            }
        ]
    },
    {
        'name': 'Image Validation',
        'category': 'glance',
        'version': 1,
        'sections': [
            {
                'name': 'Prerequisites',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Image Source',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Vendor Provided Image',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]
            },
            {
                'name': 'Image Scan',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Clam AV Scan',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                ]
            },
        ]
    },
    {
        'name': 'AIC Instantiation',
        'category': 'instantiation',
        'version': 1,
        'sections': [
            {
                'name': 'Prerequisites',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Validated Heat Template(s)',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Validated Glance Image(s)',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]
            },
            {
                'name': 'Manual Instantiation in AIC',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Create the HEAT Stack',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Delete the HEAT Stack',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]
            }
        ]
    },
    {
        'name': 'ASDC Onboarding',
        'category': 'asdc',
        'version': 1,
        'sections': [
            {
                'name': 'Prerequisites',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Validated Heat Template(s)',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Validated Glance Image(s)',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                    {
                        'name': 'Successful Manual Instantiation',
                        'weight': 1,
                        'description': 'Description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    }
                ]
            },
            {
                'name': 'Onboard The VNF To The Target ASDC Environment',
                'weight': 1,
                'description': 'section description',
                'validation_instructions': 'validation instructions',
                'line_items': [
                    {
                        'name': 'Create the VNF',
                        'weight': 1,
                        'description': 'description',
                        'validation_instructions': 'Here are some useful tips for how to validate this item in the most awesome way:<br><br><ul><li>Here is my awesome tip 1</li><li>Here is my awesome tip 2</li><li>Here is my awesome tip 3</li></ul>',
                        'line_type': 'manual',
                    },
                ]
            },
        ]
    }
]

# Map all the validation tests to the right line items
#
#   Get the validation test mappings like this from a reference deployment:
#   from validationmanager.models import ValidationTest
#   vts = sorted(ValidationTest.objects.all(), key=lambda vt: vt.name)
#   for vt in vts:
#       for li in vt.line_items.all():
#           print("['" + vt.name + "', '" + li.name + "'],")
validation_tests = [
    ['test_all_nested_templates_provided', 'Filenames'],
    ['test_all_referenced_resources_exists', 'Resources'],
    ['test_allowed_address_pairs_format', 'Allowed Address Pairs'],
    ['test_allowed_address_pairs_format', 'Ports'],
    ['test_allowed_address_pairs_include_vm_type_network_role', 'Ports'],
    ['test_allowed_address_pairs_include_vm_type_network_role', 'Ports'],
    ['test_alphanumeric_resource_ids_only', 'Resources'],
    ['test_availability_zone_naming', 'Availability Zones'],
    ['test_availability_zone_naming_use_get_param', 'Availability Zones'],
    ['test_base_template_names', 'Filenames'],
    ['test_base_template_outputs_consumed', 'Outputs'],
    ['test_default_values', 'Parameters'],
    ['test_env_and_yaml_same_name', 'Filenames'],
    ['test_env_files_provided', 'Filenames'],
    ['test_environment_file_contains_required_sections', 'Valid YAML and HEAT'],
    ['test_environment_file_extension', 'Filenames'],
    ['test_environment_file_sections_have_the_right_format', 'Valid YAML and HEAT'],
    ['test_environment_structure', 'Valid YAML and HEAT'],
    ['test_fixed_ips_format', 'Fixed IPs'],
    ['test_fixed_ips_format', 'Ports'],
    ['test_fixed_ips_format_use_get_parm', 'Fixed IPs'],
    ['test_fixed_ips_format_use_get_parm', 'Ports'],
    ['test_fixed_ips_include_vm_type_network_role', 'Ports'],
    ['test_fixed_ips_include_vm_type_network_role', 'Ports'],
    ['test_get_file_only_reference_local_files', 'HEAT Files Support (get_file)'],
    ['test_heat_pairs_provided', 'Filenames'],
    ['test_heat_template_file_extension', 'Filenames'],
    ['test_heat_template_parameters_contain_required_fields', 'Valid YAML and HEAT'],
    ['test_heat_template_structure', 'Valid YAML and HEAT'],
    ['test_heat_template_structure_contains_required_sections', 'Valid YAML and HEAT'],
    ['test_heat_template_structure_sections_have_the_right_format', 'Valid YAML and HEAT'],
    ['test_heat_templates_provided', 'Filenames'],
    ['test_network_format', 'Networks'],
    ['test_network_format', 'Ports'],
    ['test_network_format_use_get_param_or_get_resource', 'Networks'],
    ['test_network_format_use_get_param_or_get_resource', 'Ports'],
    ['test_no_unused_parameters_between_env_and_templates', 'Parameters'],
    ['test_nova_servers_correct_parameter_types', 'Name, Flavor, and Image Assignments'],
    ['test_nova_servers_valid_resource_ids', 'Resources'],
    ['test_numeric_parameter', 'Parameters'],
    ['test_parameter_valid_keys', 'Valid YAML and HEAT'],
    ['test_parameter_valid_keys', 'Parameters'],
    ['test_parse_yaml', 'Valid YAML and HEAT'],
    ['test_port_resource_ids', 'Resources'],
    ['test_port_resource_ids', 'Ports'],
    ['test_referenced_and_defined_parameters_match', 'Parameters'],
    ['test_required_parameters_no_constraints', 'Required Metadata'],
    ['test_required_parameters_provided_in_env_file', 'Required Metadata'],
    ['test_required_parameters_provided_in_heat_template', 'Required Metadata'],
    ['test_required_parameters_provided_in_heat_template', 'Required Metadata'],
    ['test_servers_have_optional_metadata', 'Optional Metadata'],
    ['test_servers_have_required_metadata', 'Required Metadata'],
    ['test_servers_metadata_use_get_param', 'Required Metadata'],
    ['test_subnet_format', 'Subnet'],
    ['test_subnet_format', 'Ports'],
    ['test_subnet_format_use_get_param_or_get_resource', 'Subnet'],
    ['test_subnet_format_use_get_param_or_get_resource', 'Ports'],
    ['test_unique_name_resources', 'Unique Names for Resources'],
    ['test_unique_name_str_replace_use_req_params', 'Unique Names for Resources'],
    ['test_unique_name_str_replace_use_req_params_in_tmpl', 'Unique Names for Resources'],
    ['test_unique_resources_across_all_yaml_files', 'Resources'],
    ['test_unique_resources_across_yaml_file', 'Unique Names for Resources'],
    ['test_unique_resources_across_yaml_file', 'Resources'],
    ['test_valid_nesting', 'Filenames'],
    ['test_vm_type_assignments_on_nova_servers_only_use_get_param', 'Name, Flavor, and Image Assignments'],
    ['test_vm_type_consistent_on_nova_servers', 'Name, Flavor, and Image Assignments'],
    ['test_volume_format_outputs', 'Volumes'],
    ['test_volume_outputs_consumed', 'Volumes'],
    ['test_volume_resource_ids', 'Resources'],
    ['test_volume_resource_ids', 'Volumes'],
    ['test_volume_templates_contains_outputs', 'Volumes'],
    ['test_volume_templates_only_contains_cinder', 'Volumes'],
    ['test_volume_templates_outputs_match_resources', 'Volumes'],
]

deployment_targets = [
    ['AIC', '2.5'],
    ['AIC', '3.0'],
    ['AIC', '3.5'],
    ['AIC', '4.0'],
    ['Other', 'No version number available']
]

ecomps = [
    ['Unknown']
]

deployment_targets_sites = [
    "Alpharetta (PDK2)",
    "Willows (RDM2)",
    "Willows (RDM3)",
    "Middletown (ICENJ)",
    "Lisle (DPA2)",
    "Lisle (DPA3)",
    "Palo Alto (PAO21)"
]


def execute_bootstrap_actions():
    create_roles()
    create_companies()
    create_deployment_targets()
    create_ecomp()
    create_deployment_targets_sites()

    # Non-default data:
    create_standard_users()
    create_el_users()
    create_admin_users()
    create_admin_ro_users()
    create_templates()
    create_validation_tests()


# TODO maybe enter this into migration scripts
def patch_migration_missing_fields():
    logger.info('Patching migration default fields')
    vf_ids = VF.objects.filter()
    if (vf_ids):
        vf_list = ThinVFModelSerializer(vf_ids, many=True).data
        for vf_data in vf_list:
                    # TODO remove into migration script
            if vf_data['ecomp_release'] is None:
                default_ecomp = ECOMPRelease.objects.get(name="Unknown")
                vfObj = VF.objects.get(uuid=vf_data['uuid'])
                vfObj.ecomp_release = default_ecomp
                vfObj.save()
            if vf_data['engagement']['reviewer'] is None:
                # @UndefinedVariable
                elRole = Role.objects.get(name=Roles.el.name)
                ids_list = []
                for user in vf_data['engagement']['engagement_team']:
                    ids_list.append(user['uuid'])
                # Fetch another random el Reviewer
                qs = IceUserProfile.objects.all().filter(role=elRole).filter(
                    uuid__in=ids_list)  # @UndefinedVariable
                if qs.count() > 0:
                    elUser = IceUserProfile.objects.get(uuid=qs[0].uuid)
                    engagementObj = Engagement.objects.get(
                        uuid=vf_data['engagement']['uuid'])
                    engagementObj.reviewer = elUser
                    engagementObj.save()

            # TODO remove into migration script
            if vf_data['engagement']['peer_reviewer'] is None:
                # @UndefinedVariable
                elRole = Role.objects.get(name=Roles.el.name)
                ids_list = []
                for user in vf_data['engagement']['engagement_team']:
                    ids_list.append(user['uuid'])
                # Fetch another random el to be a Peer Reviewer
                qs = IceUserProfile.objects.all().filter(role=elRole).exclude(
                    uuid__in=ids_list)  # @UndefinedVariable
                if qs.count() > 0:
                    randUser = qs[random.randint(0, qs.count() - 1)]
                    prUser = IceUserProfile.objects.get(uuid=randUser.uuid)
                    engagementObj = Engagement.objects.get(
                        uuid=vf_data['engagement']['uuid'])
                    engagementObj.peer_reviewer = prUser
                    engagementObj.save()


# TODO remove after check engine is connected
def populate_checklist_automation_value():
    logger.info('Populating existing checklist with decisions value')

    outerframes = inspect.getouterframes(inspect.currentframe())
    for outerframe in outerframes:
        if ('unittest' in str(outerframe)):
            logger.error(
                "Avoiding setting checklists in automation to be review since this is a test run: " + logEncoding(outerframe))
            return

    checklists = Checklist.objects.filter(state='automation')
    first = True
    for checklist in checklists:
        checklist = Checklist.objects.get(uuid=checklist.uuid)
        if (first):
            checklist.state = 'review'
        else:
            checklist.state = 'peer_review'
        checklist.save()
    #    first = False
        decisions = ChecklistDecision.objects.filter(checklist=checklist)
        first_decision = True
        for decision in decisions:
            decision = ChecklistDecision.objects.get(uuid=decision.uuid)
            line_item = ChecklistLineItem.objects.get(uuid=decision.line_item_id)
            if line_item.line_type == 'auto':
                rand_decision_value = bool(random.getrandbits(1))
                if rand_decision_value:
                    decision.review_value = 'approved'
                else:
                    decision.review_value = 'denied'
            decision.save()


def create_templates():
    logger.info('Creating Checklist templates')
    for template in checklist_templates:
        created_template, created = ChecklistTemplate.objects.get_or_create(name=template['name'],
                                                                            defaults={
            'category': template['category'],
            'version': template['version'],
            'create_time': timezone.now()
        })
        for section in template['sections']:
            created_section = ChecklistSection.objects.get_or_create(name=section['name'],
                                                                     template_id=created_template.uuid,
                                                                     defaults={
                'weight': section['weight'],
                'description': section['description'],
                'validation_instructions': section['validation_instructions']

            })
            created_section = ChecklistSection.objects.get(
                name=section['name'], template_id=created_template.uuid)
            for line_item in section['line_items']:
                created_line_item = ChecklistLineItem.objects.get_or_create(name=line_item['name'],
                                                                            section_id=created_section.uuid,
                                                                            template_id=created_template.uuid,
                                                                            defaults={
                    'weight': line_item['weight'],
                    'description': line_item['description'],
                    'validation_instructions': line_item['validation_instructions'],
                    'line_type': line_item['line_type'],
                    'section_id': created_section.uuid,
                })


def create_validation_tests():
    logger.info('Creating Validation Tests')
    template = ChecklistTemplate.objects.get(category='heat')
    for test_name, line_item_name in validation_tests:
        line_item = ChecklistLineItem.objects.get(name=line_item_name, template=template)
        if line_item:
            validation_test, status = ValidationTest.objects.get_or_create(name=test_name)
            validation_test.line_items.add(line_item)


def create_roles():
    role_standard_user, created = Role.objects.get_or_create(
        name=Roles.standard_user.name)  # @UndefinedVariable
    Constants.role_standard_user = role_standard_user
    logger.info(
        'user role found or created : ' + logEncoding(role_standard_user))

    role_el, created = Role.objects.get_or_create(
        name=Roles.el.name)  # @UndefinedVariable
    Constants.role_el = role_el
    logger.info('user role found or created : ' + logEncoding(role_el))

    role_admin, created = Role.objects.get_or_create(
        name=Roles.admin.name)  # @UndefinedVariable
    Constants.role_admin = role_admin
    logger.info('user role found or created : ' + logEncoding(role_admin))

    role_admin_ro, created = Role.objects.get_or_create(
        name=Roles.admin_ro.name)  # @UndefinedVariable
    Constants.role_admin_ro = role_admin_ro
    logger.info('user role found or created : ' + logEncoding(role_admin_ro))


def create_companies():
    service_provider_company = None
    created = None
    try:
        service_provider_company, created = Vendor.objects.get_or_create(
            name=Constants.service_provider_company_name, public=True)
        Constants.service_provider_company = service_provider_company
        logger.info('The company was found or created : ' + str(service_provider_company))
    except Exception as e:
        logger.error("bootstrap_actions - create_companies error:")
        logger.error(e)
        logger.error('The company could not be found or created : ' + Constants.service_provider_company_name)

    for company in companies_not_public:
        try:
            company, created = Vendor.objects.get_or_create(
                name=company, public=False)  # @UndefinedVariable
            logger.info('The company was found or created : ' + str(company))
        except Exception as e:
            logger.error("bootstrap_actions - create_companies error:")
            logger.error(e)
            logger.error(
                'The company could not be found or created.' + str(company))

    for company in companies:
        try:
            company, created = Vendor.objects.get_or_create(
                name=company, public=True)  # @UndefinedVariable
            logger.info('The company was found or created : ' + str(company))
        except Exception as e:
            logger.error("bootstrap_actions - create_companies error:")
            logger.error(e)
            logger.error(
                'The company could not be found or created.' + str(company))

    try:
        company_other, created = Vendor.objects.get_or_create(
            name='Other', public=True, defaults={'public': True})
        logger.info('The company was found or created : ' + str(company_other))
    except Exception as e:
        logger.error("bootstrap_actions - create_deployment_targets error:")
        logger.error(e)
        logger.error('The company could not be found or created : Other')


"""expected: nothing , result: IceUserProfiles with el role creation"""


def create_standard_users():
    service_provider_company = Vendor.objects.get(name=Constants.service_provider_company_name)
    user_role = Role.objects.get(name="standard_user")
    user_list = dummy_users

    for user in user_list:
        try:
            user_object, created = CustomUser.objects.get_or_create(username=user[1], defaults={
                                                                    'is_active': True, 'email': user[1], 'activation_token': uuid4(), 'activation_token_create_time': timezone.now()})
            user_object.set_password('iceusers')
            user_object.save()
            data = createUserTemplate(
                service_provider_company, user[0], user_role, user[2], True, None, True, user_object)
            standard_user, profile_created = IceUserProfile.objects.update_or_create(
                email=user_object.email, defaults=data)
            logger.info(
                'The Standard user was found or created: ' + str(standard_user.full_name))
        except Exception as e:
            logger.error("bootstrap_actions - create_el_users error:")
            logger.error(e)
            logger.error('The EL User could not be found or created.')


def create_el_users():
    service_provider_company = Vendor.objects.get(name=Constants.service_provider_company_name)
    el_role = Role.objects.get(name="el")
    el_list = el_dummy_users

    for user in el_list:
        try:
            user_object, created = CustomUser.objects.get_or_create(username=user[1], defaults={
                                                                    'is_active': True, 'email': user[1], 'activation_token': uuid4(), 'activation_token_create_time': timezone.now()})
            user_object.set_password('iceusers')
            user_object.save()
            data = createUserTemplate(
                service_provider_company, user[0], el_role, user[2], True, None, True, user_object)
            el_user, profile_created = IceUserProfile.objects.update_or_create(
                email=user_object.email, defaults=data)
            logger.info(
                'The EL user was found or created: ' + str(el_user.full_name))
        except Exception as e:
            logger.error("bootstrap_actions - create_el_users error:")
            logger.error(e)
            logger.error('The EL User could not be found or created.')


def create_admin_users():
    service_provider_company = Vendor.objects.get(name=Constants.service_provider_company_name)
    admin_role = Role.objects.get(name=Roles.admin.name)  # @UndefinedVariable

    admin_list = admin_dummy_users

    for user in admin_list:
        try:
            user_object, created = CustomUser.objects.get_or_create(username=user[1], defaults={'is_active': True, 'email': user[
                                                                    1], 'password': "iceusers", 'activation_token': uuid4(), 'activation_token_create_time': timezone.now()})
            user_object.set_password('iceusers')
            user_object.save()
            data = createUserTemplate(
                service_provider_company, user[0], admin_role, user[2], True, None, True, user_object)
            admin_user, profile_created = IceUserProfile.objects.update_or_create(
                email=user_object.email, defaults=data)
            logger.info(
                'The admin user was found or created: ' + str(admin_user.full_name))
        except Exception as e:
            logger.error("bootstrap_actions - create_admin_users error:")
            logger.error(e)
            logger.error('The admin user could not be found or created.')


def create_admin_ro_users():
    service_provider_company = Vendor.objects.get(name=Constants.service_provider_company_name)
    admin_ro_role = Role.objects.get(
        name=Roles.admin_ro.name)  # @UndefinedVariable

    admin_ro_list = admin_ro_dummy_users

    for user in admin_ro_list:
        try:
            user_object, created = CustomUser.objects.get_or_create(username=user[1], defaults={'is_active': True, 'email': user[
                                                                    1], 'password': "iceusers", 'activation_token': uuid4(), 'activation_token_create_time': timezone.now()})
            user_object.set_password('iceusers')
            user_object.save()
            data = createUserTemplate(
                service_provider_company, user[0], admin_ro_role, user[2], True, None, True, user_object)
            admin_ro_user, profile_created = IceUserProfile.objects.update_or_create(
                email=user_object.email, defaults=data)
            logger.info(
                'The admin_ro user was found or created: ' + str(admin_ro_user.full_name))
        except Exception as e:
            logger.error("bootstrap_actions - create_admin_ro_users error:")
            logger.error(e)
            logger.error('The admin_ro user could not be found or created.')


"""expected: nothing , result: Deployment Target objects creation"""


def create_deployment_targets():
    for dt in deployment_targets:
        try:
            deployment_target, created = DeploymentTarget.objects.get_or_create(
                name=dt[0], version=dt[1], defaults={'version': dt[1]})
            logger.info(
                'Deployment Target found or created: ' + str(deployment_target))
        except Exception as e:
            logger.error(
                "bootstrap_actions - create_deployment_targets error:")
            logger.error(e)
            logger.error(
                'Deployment Target could not be found or created : ' + str(dt))


"""expected: nothing , result: ECOMP objects creation"""


def create_ecomp():
    for dt in ecomps:
        try:
            ecomp, created = ECOMPRelease.objects.get_or_create(
                name=dt[0])
            logger.info('ECOMP Release found or created: ' + str(ecomp))
        except Exception as e:
            logger.error("bootstrap_actions - create_ecomp error:")
            logger.error(e)
            logger.error(
                'ECOMP Release could not be found or created : ' + str(dt))


"""expected: nothing , result: creation of Deployment Target Sites objects"""


def create_deployment_targets_sites():
    for dt in deployment_targets_sites:
        try:
            deployment_target_site, created = DeploymentTargetSite.objects.get_or_create(
                name=dt)
            logger.info(
                'Deployment Target found or created: ' + str(deployment_target_site.name))
        except Exception as e:
            logger.error(
                "bootstrap_actions - create_deployment_targets_sites error:")
            logger.error(e)
            logger.error(
                'Deployment Target could not be found or created : ' + str(dt))