RIFT-15502: Create Virtual Link does not work after the first VL create
[osm/UI.git] / skyquake / plugins / launchpad / src / virtual_links / nsVirtualLinkCreateStore.js
1 /*
2 *
3 * Copyright 2016 RIFT.IO Inc
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 import NSVirtualLinkCreateActions from './nsVirtualLinkCreateActions.js';
19 import NSVirtualLinkCreateSource from './nsVirtualLinkCreateSource.js';
20 import Alt from '../alt';
21 import _ from 'lodash';
22
23 class NSVirtualLinkCreateStore {
24 constructor() {
25
26 this.vld = null;
27 this.typeOptions = [{
28 label: 'ELAN',
29 value: 'ELAN'
30 }];
31 this.overlayTypeOptions = [{
32 label: 'LOCAL',
33 value: 'LOCAL'
34 }, {
35 label: 'FLAT',
36 value: 'FLAT'
37 }, {
38 label: 'VLAN',
39 value: 'VLAN'
40 }, {
41 label: 'VXLAN',
42 value: 'VXLAN'
43 }, {
44 label: 'GRE',
45 value: 'GRE'
46 }];
47
48 /* TODO: Move this to be populated from props */
49 this.vnfdConnectionPointRefs = [{
50 label: 'ping_vnfd/cp0',
51 value: 'ping_vnfd/cp0'
52 }, {
53 label: 'pong_vnfd/cp0',
54 value: 'pong_vnfd/cp0'
55 }];
56
57 /* end TODO */
58
59 this.vldInitParamsTypes = [
60 'vim-network-name',
61 'ip-profile-ref',
62 'unknown'
63 ];
64
65 this.currentVLDInitParamsType = 'unknown';
66 this.registerAsync(NSVirtualLinkCreateSource);
67 this.bindAction(NSVirtualLinkCreateActions.EDIT_VIRTUAL_LINK_ERROR, this.editVirtualLinkError);
68 this.bindAction(NSVirtualLinkCreateActions.DELETE_VIRTUAL_LINK_ERROR, this.deleteVirtualLinkError);
69 this.bindAction(NSVirtualLinkCreateActions.CREATE_VIRTUAL_LINK_SUCCESS, this.createVirtualLinkSuccess);
70 this.bindAction(NSVirtualLinkCreateActions.EDIT_VIRTUAL_LINK_SUCCESS, this.editVirtualLinkSuccess);
71 this.bindAction(NSVirtualLinkCreateActions.DELETE_VIRTUAL_LINK_SUCCESS, this.deleteVirtualLinkSuccess);
72 this.exportPublicMethods({
73 persistVirtualLink: this.persistVirtualLink,
74 updateFirstLevelKey: this.updateFirstLevelKey,
75 updateSecondLevelKey: this.updateSecondLevelKey,
76 updateVLDInitParamsType: this.updateVLDInitParamsType,
77 updateVLDInitParamsValue: this.updateVLDInitParamsValue,
78 saveNSRId: this.saveNSRId,
79 saveVld: this.saveVld,
80 addConnectionPointRef: this.addConnectionPointRef,
81 removeConnectionPointRef: this.removeConnectionPointRef,
82 updateFirstLevelListKeyChange: this.updateFirstLevelListKeyChange,
83 saveMemberVnfIndexRefs: this.saveMemberVnfIndexRefs,
84 saveVnfdIdRefs: this.saveVnfdIdRefs,
85 saveIpProfileNames: this.saveIpProfileNames,
86 removeVirtualLink: this.removeVirtualLink,
87 saveMode: this.saveMode,
88 saveOnSuccess: this.saveOnSuccess
89 });
90 }
91
92 resetState = () => {
93 delete this.vld;
94 let vld = {};
95
96 this.setState({
97
98 });
99 }
100
101 saveOnSuccess = (onSuccess) => {
102 this.setState({
103 onSuccess: onSuccess
104 })
105 }
106
107 saveMode = (mode) => {
108 this.setState({
109 mode: mode
110 })
111 }
112
113 saveVnfdIdRefs = (vnfdIdRefs) => {
114 this.setState({
115 vnfdIdRefs: vnfdIdRefs
116 });
117 }
118
119 saveMemberVnfIndexRefs = (memberVnfIndexRefs) => {
120 this.setState({
121 memberVnfIndexRefs: memberVnfIndexRefs
122 });
123 }
124
125 saveIpProfileNames = (ipProfileNames) => {
126 this.setState({
127 ipProfileNames: ipProfileNames
128 });
129 }
130
131 saveNSRId = (nsrId) => {
132 this.setState({
133 nsrId: nsrId
134 })
135 }
136
137 saveVld = (vld) => {
138 this.setState({
139 vld:vld
140 })
141 }
142
143 updateFirstLevelKey = (key, e) => {
144 let vld = _.cloneDeep(this.vld);
145 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
146 vld[key] = value;
147 this.setState({
148 vld: vld
149 });
150 }
151
152 updateSecondLevelKey = (firstLevelKey, secondLevelKey, e) => {
153 let vld = _.cloneDeep(this.vld);
154 if (!vld[firstLevelKey]) {
155 vld[firstLevelKey] = {};
156 }
157 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
158 vld[firstLevelKey][secondLevelKey] = value;
159 this.setState({
160 vld: vld
161 });
162 }
163
164 updateVLDInitParamsType = (value) => {
165 let vld = this.vld;
166
167 // Reset init param types
168 this.vldInitParamsTypes.map((vldInitParamType) => {
169 vld[vldInitParamType] && delete vld[vldInitParamType];
170 });
171
172 this.setState({
173 currentVLDInitParamsType: value,
174 vld: vld
175 })
176 }
177
178 updateVLDInitParamsValue = (currentVLDInitParamsType, e) => {
179 let vld = _.cloneDeep(this.vld);
180 this.vldInitParamsTypes.map((vldInitParamType) => {
181 if (currentVLDInitParamsType == vldInitParamType) {
182 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
183 vld[currentVLDInitParamsType] = value;
184 } else {
185 vld[vldInitParamType] && delete vld[vldInitParamType];
186 }
187 });
188
189 this.setState({
190 vld: vld
191 })
192 }
193
194 updateFirstLevelListKeyChange = (listName, index, keyName, e) => {
195 let vld = _.cloneDeep(this.vld);
196
197
198 !vld[listName] && (vld[listName] = []);
199 !vld[listName][index] && (vld[listName][index] = {});
200 vld[listName][index][keyName] = e.target.value;
201
202 this.setState({
203 vld: vld
204 })
205 }
206
207 addConnectionPointRef = () => {
208 let vld = {};
209 if (this.vld) {
210 vld = _.cloneDeep(this.vld);
211 if (!vld['vnfd-connection-point-ref']) {
212 vld['vnfd-connection-point-ref'] = [];
213 }
214 vld['vnfd-connection-point-ref'].push({
215 'member-vnf-index-ref': '',
216 'vnfd-id-ref': '',
217 'vnfd-connection-point-ref': ''
218 });
219
220 this.setState({
221 vld: vld
222 });
223 }
224 }
225
226 removeConnectionPointRef = (vnfdConnectionPointRefIndex) => {
227 let vld = _.cloneDeep(this.vld);
228 vld['vnfd-connection-point-ref'].splice(vnfdConnectionPointRefIndex, 1);
229 this.setState({
230 vld: vld
231 });
232 }
233
234 createVirtualLinkError(data) {
235 this.alt.actions.global.showError.defer('Something went wrong while trying to create the virtual link. Check the error logs for more information');
236 }
237
238 editVirtualLinkError(data) {
239 this.alt.actions.global.showError.defer('Something went wrong while trying to save the virtual link. Check the error logs for more information');
240 }
241
242 deleteVirtualLinkError(data) {
243 this.alt.actions.global.showError.defer('Something went wrong while trying to delete the virtual link. Check the error logs for more information');
244 this.setState({
245 deleteState: 'error'
246 })
247 }
248
249 createVirtualLinkSuccess(data) {
250 this.onSuccess();
251 }
252 editVirtualLinkSuccess(data) {
253 this.onSuccess();
254 }
255 deleteVirtualLinkSuccess(data) {
256 this.onSuccess();
257 }
258
259 cleanupPayload = (mode, vld) => {
260 // Do necessary cleanup here
261 let cleanVld = _.pickBy(vld, _.identity);
262 return cleanVld;
263 }
264
265 setLoadingState = (state = false) => {
266 this.setState({
267 isLoading: state
268 })
269 }
270
271 setLoaded = () => {
272 this.setLoadingState(true);
273 }
274
275 persistVirtualLink = (mode) => {
276 let self = this;
277
278 let payload = this.cleanupPayload(mode, this.vld);
279
280 if (mode == 'creating') {
281 this.getInstance().createVirtualLink(this.nsrId, payload);
282 } else {
283 this.getInstance().editVirtualLink(this.nsrId, this.vld.id, payload);
284 }
285 }
286
287 removeVirtualLink = (nsrId, vldId) => {
288 this.getInstance().deleteVirtualLink(nsrId, vldId);
289 }
290
291 }
292
293 export default NSVirtualLinkCreateStore;