-
Notifications
You must be signed in to change notification settings - Fork 44
/
svcInstModel.js
1435 lines (1418 loc) · 63.8 KB
/
svcInstModel.js
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
/*
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
define([
'underscore',
'contrail-model',
'knockout',
'config/services/instances/ui/js/models/InterfacesModel',
'config/services/instances/ui/js/svcInst.utils',
'config/services/instances/ui/js/models/PortTupleModel',
'config/services/instances/ui/js/models/SvcHealthChkModel',
'config/services/instances/ui/js/models/IntfRtTableModel',
'config/services/instances/ui/js/models/RtPolicyModel',
'config/services/instances/ui/js/models/RtAggregateModel',
], function (_, ContrailModel, Knockout, InterfacesModel, SvcInstUtils,
PortTupleModel, SvcHealthChkModel, IntfRtTableModel, RtPolicyModel,
RtAggregateModel) {
var gridElId = "#" + ctwl.SERVICE_INSTANCES_GRID_ID;
var svcInstUtils = new SvcInstUtils();
var SvcInstModel = ContrailModel.extend({
defaultConfig: {
service_template: null,
display_name: null,
status: 'Inactive',
service_instance_properties: {
interface_list: [],
left_virtual_network: "",
management_virtual_network: "",
right_virtual_network: "",
ha_mode: "",
scale_out: {
max_instances: 1
},
},
service_template_refs: [],
fq_name: [],
uuid: null,
no_of_instances: 1,
availability_zone: 'ANY',
host_list: [],
host: null,
parent_uuid: null
},
validations: {
svcInstValidations: {
'no_of_instances': function(val, attr, data) {
var svcTmpl = data.service_template;
var svcTmpls = $(gridElId).data('svcInstTmplts');
var svcTmplFqn = getCookie('domain') + ":" +
svcTmpl.split(' - [')[0];
var svcTmplObj = svcTmpls[svcTmplFqn];
var svcScaling =
getValueByJsonPath(svcTmplObj,'service_template_properties;service_scaling',
false);
var tmplVer =
getValueByJsonPath(svcTmplObj,'service_template_properties;version',
1);
if ((2 == tmplVer) || (false == svcScaling)) {
return;
}
if ((null == val) || (isNaN(val))) {
return 'Instance count should be integer';
}
},
'display_name': function(val, attr, data) {
if ((null == data) || (null == data['display_name'])) {
return "Service Instance Name is required";
}
var dispName = data['display_name'].trim();
if (!dispName.length) {
return "Service Instance Name is required";
}
},
'service_health_check': function(val, attr, data) {
var svcTmpl = data.service_template;
var svcTmplObj =
svcInstUtils.getSvcTmplDetailsBySvcTmplStr(svcTmpl);
var tmplVer =
getValueByJsonPath(svcTmplObj,'service_template_properties;version',
1);
if (1 == tmplVer) {
return;
}
var svcHealthChecks = data['svcHealtchChecks'];
if (null == svcHealthChecks) {
return;
}
svcHealthChecks = svcHealthChecks.toJSON();
var len = svcHealthChecks.length;
var intfTypes = [];
var svcHealthChks = [];
for (var i = 0; i < len; i++) {
var intfType = svcHealthChecks[i]['interface_type']();
var svcHealthChkEntry =
svcHealthChecks[i]['service_health_check']();
if (null == intfType) {
return 'Interface type is required for service '+
'health check';
}
if (null == svcHealthChkEntry) {
return 'Select service health check entry';
}
intfTypes.push(intfType);
svcHealthChks.push(svcHealthChkEntry);
}
if (intfTypes.length > 1) {
if (intfTypes.length != (_.uniq(intfTypes)).length) {
return 'Same interface types selected for ' +
'multiple service health check';
}
if (svcHealthChks.length !=
(_.uniq(svcHealthChks)).length) {
return 'Same service health check entries ' +
'selected for multiple interface types';
}
}
},
'routing_policy': function(val, attr, data) {
var rtPolicys = data['rtPolicys'];
if (null == rtPolicys) {
return 'Select routing policy in order';
}
rtPolicys = rtPolicys.toJSON();
var len = rtPolicys.length;
var intfTypes = [];
var rtPolicyList = [];
for (var i = 0; i < len; i++) {
var intfType = rtPolicys[i]['interface_type']();
if (null == intfType) {
return 'Interface type is required for routing '+
'policy';
}
var entry =
rtPolicys[i]['routing_policy']();
if (null == entry) {
return 'Select routing policy in order';
}
intfTypes.push(intfType);
}
if (intfTypes.length > 1) {
if (intfTypes.length != (_.uniq(intfTypes)).length) {
return 'Same interface types selected for ' +
'multiple routing policy';
}
}
},
'virtualNetwork': function(val, attr, data) {
var svcTmpl = data.service_template;
var svcTmplObj =
svcInstUtils.getSvcTmplDetailsBySvcTmplStr(svcTmpl);
var svcTmplIntfs =
getValueByJsonPath(svcTmplObj,
'service_template_properties;interface_type',
[]);
var version =
getValueByJsonPath(svcTmplObj,
'service_template_properties;version',
1);
var svcMode =
getValueByJsonPath(svcTmplObj,
'service_template_properties;service_mode',
null);
var interfaceCollection = data['interfaces'];
if (null == interfaceCollection) {
return;
}
var models = interfaceCollection['models'];
var len = interfaceCollection.length;
var errStr = "Auto Configured network not allowed";
for (var i = 0; i < len; i++) {
var attr = models[i]['attributes'];
var vn = attr['virtualNetwork']();
if ('management' !=
svcTmplIntfs[i]['service_interface_type']) {
if (('in-network' == svcMode) ||
('in-network-nat' == svcMode) ||
('other' ==
svcTmplIntfs[i]['service_interface_type'])) {
if ("autoConfigured" == vn) {
return errStr;
}
}
}
if (true == svcTmplIntfs[i]["static_route_enable"]) {
if ("autoConfigured" == vn) {
return errStr;
}
}
}
},
'interface': function(val, attr, data) {
var portTuples =
svcInstUtils.getPortTuples(data['display_name'],
data['portTuples']);
if (null == portTuples) {
return;
}
var cnt = portTuples.length;
var vmiListPerPortTuple = [];
for (var i = 0; i < cnt; i++) {
vmiListPerPortTuple[i] = [];
var vmis = portTuples[i]['vmis'];
if (null == vmis) {
continue;
}
var vmisCnt = vmis.length;
for (var j = 0; j < vmisCnt; j++) {
vmiListPerPortTuple[i].push(vmis[j]['uuid']);
}
var uniqVmisList = _.uniq(vmiListPerPortTuple[i]);
if (uniqVmisList.length !=
vmiListPerPortTuple[i].length) {
var errorStr = 'Same interface assigned for ' +
'multiple interface types';
if ((null != portTuples[i]['to']) &&
(null != portTuples[i]['to'][3])) {
errorStr += ' for port tuple ' +
portTuples[i]['to'][3];
}
return errorStr;
}
}
var tmpVmiListPerPortTuple = _.flatten(vmiListPerPortTuple);
if (tmpVmiListPerPortTuple.length !=
(_.uniq(tmpVmiListPerPortTuple)).length) {
return 'One or more same interfaces assigned to ' +
'multiple port tuple';
}
},
'service_template': function(val, attr, data) {
if (null == val) {
return "Select Service Template";
}
var svcTmpl = getCookie('domain') + ":" +
val.split(' - [')[0];
var svcTmpls = $(gridElId).data('svcInstTmplts');
if (null == svcTmpls[svcTmpl]) {
return 'Service Template is not valid';
}
var tmpl = svcTmpls[svcTmpl];
var imgName =
getValueByJsonPath(tmpl,
'service_template_properties;image_name',
null);
var tmplVersion =
getValueByJsonPath(tmpl,
'service_template_properties;version',
1);
if (2 == tmplVersion) {
return;
}
var svcVirtType =
getValueByJsonPath(tmpl,
'service_template_properties;service_virtualization_type',
null);
if ('physical-device' == svcVirtType) {
return;
}
if (null == imgName) {
return 'Image name not found for this template';
}
var imgList = window.imageList;
var imgCnt = imgList.length;
for (var i = 0; i < imgCnt; i++) {
if (imgList[i]['name'] == imgName) {
break;
}
}
if (i == imgCnt) {
return 'Image not found as specified in template';
}
}
}
},
changePortTupleName: function() {
var portTuples = this.model().get('portTuples');
var svcInstName = this.model().get('display_name');
if ((null == svcInstName) || (!svcInstName.trim().length)) {
return;
}
var cnt = portTuples.length;
for (var i = 0; i < cnt; i++) {
var portTupleName = portTuples.models[i].get('portTupleName')();
var splitArr = portTupleName.split('-port-tuple');
if (2 == splitArr.length) {
splitArr[0] = svcInstName;
splitArr[2] = splitArr[1];
splitArr[1] = '-port-tuple';
} else if (splitArr.length > 2) {
tmpPortTupleArr = [];
tmpPortTupleArr[0] = svcInstName;
tmpPortTupleArr[1] = '-port-tuple';
tmpPortTupleArr[2] = splitArr[splitArr.length - 1];
splitArr = tmpPortTupleArr;
}
portTuples.models[i].attributes.portTupleName(splitArr.join(''));
}
},
formRoutingPolicyToDisplay: function(rtList, intfType) {
if (!rtList.length) {
return null;
}
var policyList = [];
rtList.sort(function(entry1, entry2) {
return entry1.seqId - entry2.seqId;
});
var entryObj = {};
var cnt = rtList.length;
for (var i = 0; i < cnt; i++) {
policyList.push(rtList[i]['rtPolicy']);
}
entryObj['routing_policy'] = policyList.join(',');
entryObj['interface_type'] = intfType;
var types = [{text: 'left', id: 'left'},
{text: 'right', id: 'right'}];
entryObj['rtPolicyInterfaceTypesData'] = types;
var newEntry = new RtPolicyModel(entryObj);
return newEntry;
},
getBackRefsByType: function(modelConfig, type) {
var svcInstProps = [];
var backRefs = null;
var modelType;
var modelKey;
var backRefsLen = 0;
if ('interface_route_table' == type) {
backRefs = modelConfig['interface_route_table_back_refs'];
modelType = IntfRtTableModel;
modelKey = 'intfRtTables';
}
if ('routing_policy' == type) {
backRefs = modelConfig['routing_policy_back_refs'];
modelType = RtPolicyModel;
modelKey = 'rtPolicys';
}
if ('service_health_check' == type) {
backRefs = modelConfig['service_health_check_back_refs'];
modelType = SvcHealthChkModel;
modelKey = 'svcHealtchChecks';
}
if ('route_aggregate' == type) {
backRefs = modelConfig['route_aggregate_back_refs'];
modelType = RtAggregateModel;
modelKey = 'rtAggregates';
}
if (null != backRefs) {
backRefsLen = backRefs.length;
}
var properties = [];
var intfTypes = [];
if ((null != modelConfig) &&
(null != modelConfig['service_template'])) {
var tmpl = modelConfig['service_template'];
var intfTypeStrStart = tmpl.indexOf('(');
var intfTypeStrEnd = tmpl.indexOf(')');
intfTypes =
tmpl.substr(intfTypeStrStart + 1,
intfTypeStrEnd - intfTypeStrStart - 1);
intfTypes = intfTypes.split(', ');
}
var intfTypesList = [];
var intfCnt = intfTypes.length;
for (var i = 0; i < intfCnt; i++) {
intfTypesList.push({text: intfTypes[i], id: intfTypes[i]});
}
var intfTypeToBackRefsMap = {};
var leftRtPolSeqList = [];
var rightRtPolSeqList = [];
var dataObjArr = [];
if ((null != backRefs) && (backRefsLen > 0)) {
for (var i = 0; i < backRefsLen; i++) {
var value = backRefs[i]['to'].join(':') +
'~~' + backRefs[i]['uuid'];
var intfType =
getValueByJsonPath(backRefs[i],
'attr;interface_type', null);
if ('routing_policy' == type) {
var leftRtPolSeq =
getValueByJsonPath(backRefs[i],
'attr;left_sequence', null);
var rightRtPolSeq =
getValueByJsonPath(backRefs[i],
'attr;right_sequence', null);
if ((null == leftRtPolSeq) && (null == rightRtPolSeq)) {
continue;
}
if (null != leftRtPolSeq) {
leftRtPolSeqList.push({seqId: Number(leftRtPolSeq),
rtPolicy: value});
}
if (null != rightRtPolSeq) {
rightRtPolSeqList.push({seqId:
Number(rightRtPolSeq),
rtPolicy: value});
}
} else {
if (null == intfType) {
continue;
}
}
if (null == intfTypeToBackRefsMap[intfType]) {
intfTypeToBackRefsMap[intfType] = [];
}
intfTypeToBackRefsMap[intfType].push(value);
if ('service_health_check' == type) {
var entryObj = {};
entryObj[type] = value;
entryObj['interface_type'] = intfType;
entryObj['interfaceTypesData'] = intfTypesList;
var newEntry = new modelType(entryObj);
properties.push(newEntry);
}
}
if (leftRtPolSeqList.length > 0) {
var newEntry =
this.formRoutingPolicyToDisplay(leftRtPolSeqList,
'left');
properties.push(newEntry);
}
if (rightRtPolSeqList.length > 0) {
var newEntry =
this.formRoutingPolicyToDisplay(rightRtPolSeqList,
'right');
properties.push(newEntry);
}
}
if (('routing_policy' != type) &&
('service_health_check' != type)) {
for (var key in intfTypeToBackRefsMap) {
var entryObj = {};
entryObj[type] = intfTypeToBackRefsMap[key].join(',');
entryObj['interface_type'] = key;
entryObj['interfaceTypesData'] = intfTypesList;
var newEntry = new modelType(entryObj);
properties.push(newEntry);
}
}
var collection = new Backbone.Collection(properties);
modelConfig[modelKey] = collection;
return modelConfig;
},
setVMIsByVN: function(modelConfig, vnName, intfType) {
var pTuples = modelConfig.get('portTuples');
var vmiList = [];
if (null == pTuples) {
return;
}
var portTuplesCnt = pTuples.length;
for (var i = 0; i < portTuplesCnt; i++) {
var portTupleIntfs =
pTuples.models[i].get('portTupleInterfaces')();
var portTupleIntfsCnt = portTupleIntfs.length;
for (var j = 0; j < portTupleIntfsCnt; j++) {
if (intfType == portTupleIntfs[j]['interfaceType']()()) {
if (window.vnVmiMaps[vnName]) {
vmiList = window.vnVmiMaps[vnName];
}
portTupleIntfs[j]['vmiListData']()(vmiList);
break;
}
}
}
return;
},
getVNListBySvcIntfType: function(svcTmpl, intf) {
var vnList = $.extend([], true, window.allVNList);
if (null == intf) {
return vnList;
}
var showAuto = true;
var svcMode =
getValueByJsonPath(svcTmpl,
'service_template_properties;service_mode',
null);
if ('management' != intf['service_interface_type']) {
if (('in-network' == svcMode) ||
('in-network-nat' == svcMode)) {
showAuto = false;
}
}
if (-1 != intf['service_interface_type'].indexOf('other')) {
showAuto = false;
}
if (true == intf['static_route_enable']) {
showAuto = false;
}
if ((false == showAuto) && (vnList.length > 0)) {
if ('autoConfigured' == vnList[0]['id']) {
vnList.splice(0, 1);
}
}
return vnList;
},
formatModelConfig: function(modelConfig) {
if (!window.svcTmplsFormatted.length) {
showInfoWindow("No Service Template found.",
"Add Service Template");
return null;
}
var self = this;
var intfTypes = [];
if ((null != modelConfig) &&
(null != modelConfig['svcTmplDetails']) &&
(null != modelConfig['svcTmplDetails'][0])) {
var svcTmplDetails = modelConfig['svcTmplDetails'][0];
var svcTmpl = {'service-template': svcTmplDetails};
svcTmpl = svcInstUtils.svcTemplateFormatter(svcTmpl);
modelConfig['service_template'] = svcTmpl;
svcTmpl = {'service-template': svcTmplDetails};
intfTypes = svcInstUtils.getSvcTmplIntfTypes(svcTmpl);
} else {
modelConfig['service_template'] =
window.svcTmplsFormatted[0]['id'];
var tmpl = modelConfig['service_template'];
var intfTypeStrStart = tmpl.indexOf('(');
var intfTypeStrEnd = tmpl.indexOf(')');
intfTypes =
tmpl.substr(intfTypeStrStart + 1,
intfTypeStrEnd - intfTypeStrStart - 1);
intfTypes = intfTypes.split(', ');
var svcTmplObjsByFqn = $(gridElId).data('svcInstTmplts');
if (null != tmpl) {
var tmplFqn = getCookie('domain') + ':' +
tmpl.split(' - [')[0];
svcTmpl = {'service-template': svcTmplObjsByFqn[tmplFqn]};
} else {
svcTmpl = null;
}
}
var maxInst =
getValueByJsonPath(modelConfig,
'service_instance_properties;scale_out;max_instances',
null);
if (null != maxInst) {
modelConfig['no_of_instances'] = maxInst;
}
var intfList =
getValueByJsonPath(modelConfig,
'service_instance_properties;interface_list',
[]);
var svcTmpls = $(gridElId).data('svcInstTmplts');
var tmpl = modelConfig['service_template'];
var svcTmplFqn = getCookie('domain') + ":" +
tmpl.split(' - [')[0];
var tmplVer =
getValueByJsonPath(svcTmpls[svcTmplFqn],
'service_template_properties;version',
1);
var interfaeTypes = [];
var intfsCnt = intfTypes.length;
if ((null != svcTmpls) && (null != svcTmpls[svcTmplFqn])) {
interfaeTypes =
getValueByJsonPath(svcTmpls[svcTmplFqn],
'service_template_properties;interface_type',
[]);
}
if (!intfList.length) {
var interfaeTypesCnt = interfaeTypes.length;
for (var i = 0; i < intfsCnt; i++) {
var intfType = intfTypes[i];
/*
var intfType =
intfTypes[i].replace(intfTypes[i][0],
intfTypes[i][0].toUpperCase());
*/
var vn = "";
if ((null != window.vnList) && (window.vnList.length > 0)) {
vn = svcInstUtils.getVNByTmplType(intfType, svcTmpl);
if (null == vn) {
vn = "";
} else {
vn = vn['id'];
}
}
intfList.push({virtual_network: vn});
}
}
var len = intfList.length;
for (i = 0; i < len; i++) {
if ("" == intfList[i]['virtual_network']) {
intfList[i]['virtual_network'] = 'autoConfigured';
}
}
var interfacesModels = [];
var interfacesCollectionModel;
var cnt = intfTypes.length;
for (var i = 0; i < cnt; i++) {
var intfType = intfTypes[i];
/*
intfType = intfType.replace(intfType[0], intfType[0].toUpperCase());
*/
var vnList = window.allVNList;
if (2 == tmplVer) {
vnList = window.vnList;
} else {
vnList = this.getVNListBySvcIntfType(svcTmpls[svcTmplFqn],
interfaeTypes[i]);
}
var interfacesModel =
new InterfacesModel({interfaceType: intfType,
virtualNetwork:
intfList[i]['virtual_network'],
interfaceIndex: i,
interfaceData: intfList[i],
allVNListData: vnList});
interfacesModels.push(interfacesModel);
interfacesModel.__kb.view_model.model().on('change:virtualNetwork',
function(model,
newValue) {
self.setVMIsByVN(self.model(),
newValue,
model.get('interfaceType'));
});
}
interfacesCollectionModel = new Backbone.Collection(interfacesModels);
modelConfig['interfaces'] = interfacesCollectionModel;
modelConfig.host_list = [{'text': 'ANY', 'id': 'ANY'}];
var portTupleList = modelConfig['port_tuples'];
var portTupleModels = [];
var portTuplesCnt = 0;
if (null != portTupleList) {
portTuplesCnt = portTupleList.length;
}
var intfs =
this.getInterfaceTypeVNMap(modelConfig['interfaces'].toJSON());
for (var i = 0; i < portTuplesCnt; i++) {
var portTupleDispName =
this.getPortTupleDisplayName(portTupleList[i]['to'][3],
portTupleList[i], intfTypes);
var portTupleModel =
new PortTupleModel({portTupleName:
portTupleList[i]['to'][3],
portTupleDisplayName: portTupleDispName,
portTupleData:
portTupleList[i],
intfTypes: intfTypes,
parentIntfs: intfs, disable: true});
portTupleModels.push(portTupleModel);
}
var portTuplesCollection = new Backbone.Collection(portTupleModels);
modelConfig['portTuples'] = portTuplesCollection;
this.getBackRefsByType(modelConfig, 'interface_route_table');
this.getBackRefsByType(modelConfig, 'service_health_check');
this.getBackRefsByType(modelConfig, 'routing_policy');
this.getBackRefsByType(modelConfig, 'route_aggregate');
return modelConfig;
},
getPortTupleDisplayName: function(portTupleName, portTupleData,
intfTypes) {
var vmis = getValueByJsonPath(portTupleData,
'virtual-machine-interfaces', []);
var dispName = "port-tuple";
var splitArr = portTupleName.split('-port-tuple');
if (splitArr.length > 0) {
var newArr = splitArr[1].split('-');
if (newArr.length > 0) {
dispName += newArr[0];
}
}
var vmisCnt = vmis.length;
var vmiTypeToObjMap = {};
for (var i = 0; i < vmisCnt; i++) {
var vmiType =
getValueByJsonPath(vmis[i],
'virtual_machine_interface_properties;service_interface_type',
null);
if (null == vmiType) {
/* Weired */
console.error('service interface type is null');
continue;
}
vmiTypeToObjMap[vmiType] = vmis[i];
}
var intfTypesCnt = intfTypes.length;
for (var i = 0; i < intfTypesCnt; i++) {
var vmiObj = vmiTypeToObjMap[intfTypes[i]];
if (null == vmiObj) {
continue;
}
if (0 == i) {
dispName += ' : ';
}
var vmiId = vmiObj['uuid'];
var instIps = window.vmiToInstIpsMap[vmiId];
if ((null != instIps) && (instIps.length > 0)) {
dispName += instIps[0];
}
if (i < vmisCnt - 1) {
dispName += ', ';
}
}
return dispName;
},
setPortTupleName: function(model, portTupleEntry) {
var intfTypes = portTupleEntry.intfTypes();
var ctIntfType = model.get('interfaceType');
var intfIdx = intfTypes.indexOf(ctIntfType);
if (-1 == intfIdx) {
return;
}
var portTupleDispName = portTupleEntry.portTupleDisplayName();
var portTupleSplit = portTupleDispName.split(' : ');
var ips = [];
if (null != portTupleSplit[1]) {
ips = portTupleSplit[1].split(', ');
}
var intf = model.get('interface');
var vmiId = intf.split('~~')[1];
var intfCnt = intfTypes.length;
var dispStr = portTupleSplit[0] + ' : ';
for (var i = 0; i < intfCnt; i++) {
if (i == intfIdx) {
/* Set the IP here */
var instIps = window.vmiToInstIpsMap[vmiId];
if ((null != instIps) && (instIps.length > 0)) {
dispStr += instIps[0];
}
} else {
if (null != ips[i]) {
dispStr += ips[i];
}
}
if (i < intfCnt - 1) {
dispStr += ', ';
}
}
portTupleEntry['portTupleDisplayName'](dispStr);
},
deleteModelCollectionData: function(model, type) {
var collection = model.attributes[type];
var len = collection.length;
for (var i = 0; i < len; i++) {
collection.remove(collection.models[i]);
len--;
i--;
}
},
formatModelConfigColl: function(intfTypes, isDisabled) {
if (true == isDisabled) {
return;
}
var self = this;
var cnt = intfTypes.length;
var svcTmpl = this.model().get('service_template');
var svcTmpls = $(gridElId).data('svcInstTmplts');
var svcTmplFqn = getCookie('domain') + ":" +
svcTmpl.split(' - [')[0];
var svcTmplObj = {'service-template': svcTmpls[svcTmplFqn]};
var tmplVer = getValueByJsonPath(svcTmpls[svcTmplFqn],
'service_template_properties;version',
1);
this.deleteModelCollectionData(this.model(), 'interfaces');
this.deleteModelCollectionData(this.model(), 'portTuples');
this.deleteModelCollectionData(this.model(), 'svcHealtchChecks');
this.deleteModelCollectionData(this.model(), 'intfRtTables');
this.deleteModelCollectionData(this.model(), 'rtPolicys');
this.deleteModelCollectionData(this.model(), 'rtAggregates');
var interfaces = this.model().attributes['interfaces'];
var interfaeTypes = [];
if ((null != svcTmpls) && (null != svcTmpls[svcTmplFqn])) {
interfaeTypes =
getValueByJsonPath(svcTmpls[svcTmplFqn],
'service_template_properties;interface_type',
[]);
}
for (var i = 0; i < cnt; i++) {
var intfType = intfTypes[i];
var vn = null;
var intfList =
getValueByJsonPath(this.model().attributes,
'service_instance_properties;interface_list',
[]);
var vnList = window.allVNList;
if (2 == tmplVer) {
vnList = window.vnList;
} else {
vnList = this.getVNListBySvcIntfType(svcTmpls[svcTmplFqn],
interfaeTypes[i]);
}
var newInterface =
new InterfacesModel({'interfaceType': intfType,
'virtualNetwork': vn,
'interfaceIndex': i,
interfaceData: intfList[i],
allVNListData: vnList});
newInterface.__kb.view_model.model().on('change:virtualNetwork',
function(model,
newValue) {
self.setVMIsByVN(self.model(),
newValue,
model.get('interfaceType'));
});
kbValidation.bind(this.editView,
{collection:
newInterface.model().attributes.staticRoutes});
interfaces.add([newInterface]);
}
return;
},
addPortTuple: function() {
var svcInstName = this.model().get('display_name');
var self = this;
if ((null == svcInstName) || (!svcInstName.trim().length)) {
var model = this.model();
var attr = cowu.getAttributeFromPath('display_name');
var errors = model.get(cowc.KEY_MODEL_ERRORS);
var attrErrorObj = {}
attrErrorObj[attr + cowc.ERROR_SUFFIX_ID] =
'Service Instance Name is required';
errors.set(attrErrorObj);
return;
}
/* Why this function gets called, even when the port_tuple stuff
in invisible
*/
var svcTmpl = this.model().get('service_template');
var svcTmpls = $(gridElId).data('svcInstTmplts');
var svcTmplFqn = getCookie('domain') + ":" +
svcTmpl.split(' - [')[0];
var tmplVer = getValueByJsonPath(svcTmpls[svcTmplFqn],
'service_template_properties;version',
1);
if (1 == tmplVer) {
return;
}
var portTupleCollection = this.model().get('portTuples');
var collLen = portTupleCollection.length;
var models = portTupleCollection['models'];
var portTupleIds = [];
var tmpPortTupleId = 0;
for (var i = 0; i < collLen; i++) {
var attr = models[i].attributes;
var ptName = attr.portTupleName();
var splitArr = ptName.split(svcInstName + '-port-tuple');
if (2 == splitArr.length) {
tmpPortTupleId = Number(splitArr[1].split('-')[0]);
portTupleIds.push(tmpPortTupleId);
}
}
if (portTupleIds.length > 0) {
portTupleIds.sort();
var portTupleIdsCnt = portTupleIds.length;
if (portTupleIds[portTupleIdsCnt - 1] == (portTupleIdsCnt - 1)) {
tmpPortTupleId = portTupleIdsCnt;
} else {
for (var i = 0; i < portTupleIdsCnt; i++) {
if (i != portTupleIds[i]) {
tmpPortTupleId = i;
break;
}
}
if (i == portTupleIdsCnt) {
/* We must not come here */
tmpPortTupleId = portTupleIdsCnt;
}
}
}
var newUUID = UUIDjs.create();
var portTupleName = svcInstName + '-port-tuple' +
tmpPortTupleId.toString() + '-' + newUUID['hex'];
var intfs =
this.getInterfaceTypeVNMap(this.model().get('interfaces').toJSON());
var intfTypes = this.getIntfTypes(true);
var portTupleDispName =
this.getPortTupleDisplayName(portTupleName, null, intfTypes);
var newPortTupleEntry =
new PortTupleModel({portTupleName: portTupleName,
portTupleDisplayName: portTupleDispName,
portTupleData: {},
intfTypes: intfTypes,
parentIntfs: intfs, disable: false});
kbValidation.bind(this.editView,
{collection:
newPortTupleEntry.model().attributes.portTupleInterfaces});
var portTupleIntfsColl =
newPortTupleEntry.model().attributes.portTupleInterfaces;
var portTupleIntfsCollLen = portTupleIntfsColl.length;
for (var i = 0; i < portTupleIntfsCollLen; i++) {
var model = portTupleIntfsColl.models[i]['attributes'].model();
self.newPortTupleEntry = newPortTupleEntry;
model.on('change:interface', function(model, newValue) {
self.setPortTupleName(model,
self.newPortTupleEntry);
});
}
portTupleCollection.add([newPortTupleEntry]);
},
getInterfaceTypeVNMap: function(intfData) {
var intfObjs = {};
if (null == intfData) {
return intfs;
}
var cnt = intfData.length;
for (var i = 0; i < cnt; i++) {
var intfType = intfData[i]['interfaceType']();
var vn = intfData[i]['virtualNetwork']();
intfObjs[intfType] = vn;
}
return intfObjs;
},
getIntfTypes: function(isRaw) {
var tmpl =
$('#service_template_dropdown').data('contrailDropdown').value();
var intfTypeStrStart = tmpl.indexOf('(');
var intfTypeStrEnd = tmpl.indexOf(')');
var itfTypes =
tmpl.substr(intfTypeStrStart + 1,
intfTypeStrEnd -
intfTypeStrStart - 1);
var intfTypes = itfTypes.split(', ');
if (isRaw) {
return intfTypes;
}
var cnt = intfTypes.length;
var types = [];
for (var i = 0; i < cnt; i++) {
types.push({text: intfTypes[i], id: intfTypes[i]});
}
return types;
},
addPropSvcHealthChk: function() {
var svcHealtchChecks = this.model().get('svcHealtchChecks');
var svcHealthChkEntry = null;
var types = this.getIntfTypes(false);
var newEntry =
new SvcHealthChkModel({service_health_check: null,
interface_type: null,
interfaceTypesData: types});
svcHealtchChecks.add([newEntry]);
},
addPropIntfRtTable: function() {
var intfRtTables = this.model().get('intfRtTables');
var types = this.getIntfTypes(false);
var newEntry =
new IntfRtTableModel({interface_route_table: null,
interface_type: null,
interfaceTypesData: types});
intfRtTables.add([newEntry]);
},
addPropRtPolicy: function() {
var rtPolicys = this.model().get('rtPolicys');
var types = [{text: 'left', id: 'left'},
{text: 'right', id: 'right'}];
var newEntry =
new RtPolicyModel({routing_policy: null,
interface_type: null,
rtPolicyInterfaceTypesData: types});
rtPolicys.add([newEntry]);
},
addPropRtAggregate: function() {
var rtAggregates = this.model().get('rtAggregates');
var rtAgg = "";
var types = this.getIntfTypes(false);
if (null != types) {
var typesCnt = types.length;
for (var i = 0; i < typesCnt; i++) {
if ('management' == types[i]['id']) {
/* Remove management */
types.splice(0, 1);
break;
}
}
}
var newEntry =
new RtAggregateModel({route_aggregate: null,
interface_type: null,
interfaceTypesData: types});
rtAggregates.add([newEntry]);
},
deleteSvcInstProperty: function(data, property) {
var collection = data.model().collection;
var entry = property.model();
collection.remove(entry);
},
getHostListByZone: function() {
var self = this;
return ko.computed(function () {
return window.hostListByZone;
}, this);
},
showHideStaticRTs: function(interfaceIndex) {
var idx = interfaceIndex();
var tmplStr = this.model().get('service_template');
if (null == tmplStr) {
return false;
}
tmplStr = tmplStr.trim();
tmplStr = contrail.getCookie('domain') + ":" + tmplStr.split(' - [')[0];
var tmplData = $(gridElId).data('svcInstTmplts');
if (null == tmplData[tmplStr]) {