-
Notifications
You must be signed in to change notification settings - Fork 44
/
PortTupleModel.js
132 lines (123 loc) · 5.16 KB
/
PortTupleModel.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
/*
rt_tuples
* Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
*/
define([
'underscore',
'contrail-model',
'config/services/instances/ui/js/models/InterfaceTypesModel',
'config/services/instances/ui/js/svcInst.utils'
], function (_, ContrailModel, InterfaceTypesModel, SvcInstUtils) {
var gridElId = "#" + ctwl.SERVICE_INSTANCES_GRID_ID;
var svcInstUtils = new SvcInstUtils();
var InterfacesModel = ContrailModel.extend({
defaultConfig: {
portTupleName: "",
portTupleDisplayName: "",
portTupleData: null,
intfTypes: [],
disable: false
},
validateAttr: function (attributePath, validation, data) {
var model = data.model().attributes.model(),
attr = cowu.getAttributeFromPath(attributePath),
errors = model.get(cowc.KEY_MODEL_ERRORS),
attrErrorObj = {}, isValid;
isValid = model.isValid(attributePath, validation);
attrErrorObj[attr + cowc.ERROR_SUFFIX_ID] = (isValid == true) ? false : isValid;
errors.set(attrErrorObj);
},
formatModelConfig: function(modelConfig) {
var propModel;
var propModels = [];
var propCollectionModel;
var vmis =
getValueByJsonPath(modelConfig,
'portTupleData;virtual-machine-interfaces',
[]);
var vnVmis = modelConfig.parentIntfs;
var vmisCnt = vmis.length;
var vmiTypeToObjMap = {};
for (var i = 0; i < vmisCnt; i++) {
var intfType =
vmis[i]['virtual_machine_interface_properties']
['service_interface_type'];
vmiTypeToObjMap[intfType] = vmis[i];
}
var intfTypes = getValueByJsonPath(modelConfig, 'intfTypes', []);
var intfCnt = intfTypes.length;
for (var i = 0; i < intfCnt; i++) {
intfType = intfTypes[i];
var vmiObj = vmiTypeToObjMap[intfType];
if (null == vmiObj) {
continue;
}
var vnName = vnVmis[intfType];
var vmiList = [];
if (window.vnVmiMaps[vnName]) {
vmiList = window.vnVmiMaps[vnName];
}
var vmi = vmiObj['fq_name'].join(':') + "~~" + vmiObj['uuid'];
var propModel =
new InterfaceTypesModel({interfaceType: intfType,
interface: vmiObj['fq_name'].join(':') +
"~~" + vmiObj['uuid'],
vmiListData: vmiList,
disable: modelConfig['disable']});
propModels.push(propModel);
}
if (!vmisCnt) {
var vmi = null;
for (var i = 0; i < intfCnt; i++) {
var vnName = vnVmis[intfTypes[i]];
var vmiList = [];
if (window.vnVmiMaps[vnName]) {
vmiList = window.vnVmiMaps[vnName];
}
var propModel =
new InterfaceTypesModel({interfaceType: intfTypes[i],
interface: vmi,
vmiListData: vmiList,
disable: modelConfig['disable']});
propModels.push(propModel);
}
}
propCollectionModel = new Backbone.Collection(propModels);
modelConfig['portTupleInterfaces'] = propCollectionModel;
return modelConfig;
},
deletePortTuple: function() {
var portTupleCollection =this .model().collection;
var portTupleEntry = this.model();
portTupleCollection.remove(portTupleEntry);
},
validations: {
portTuplesValidation: {
'portTupleName': {
required: true
},
'interface': function(val, attr, fieldObj) {
console.log("Getting fieldObj as:", fieldObj);
var intfs = fieldObj['portTupleInterfaces'].toJSON();
var len = intfs.length;
var intfsList = [];
for (var i = 0; i < len; i++) {
var intf = intfs[i]['interface']();
if (null != intf) {
intfsList.push(intf);
} else {
/* NULL case will be handled by child model
* validator
*/
}
}
if (intfsList.length != (_.uniq(intfsList)).length) {
return 'Same interface assigned to multiple interface' +
' types';
}
}
}
}
});
return InterfacesModel;
});