RIFT-14793: Creating new Virtual Link in Viewport includes double quotes in ip profil...
[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 });
89 }
90
91 resetState = () => {
92 delete this.vld;
93 let vld = {};
94
95 this.setState({
96
97 });
98 }
99
100 saveMode = (mode) => {
101 this.setState({
102 mode: mode
103 })
104 }
105
106 saveVnfdIdRefs = (vnfdIdRefs) => {
107 this.setState({
108 vnfdIdRefs: vnfdIdRefs
109 });
110 }
111
112 saveMemberVnfIndexRefs = (memberVnfIndexRefs) => {
113 this.setState({
114 memberVnfIndexRefs: memberVnfIndexRefs
115 });
116 }
117
118 saveIpProfileNames = (ipProfileNames) => {
119 this.setState({
120 ipProfileNames: ipProfileNames
121 });
122 }
123
124 saveNSRId = (nsrId) => {
125 this.setState({
126 nsrId: nsrId
127 })
128 }
129
130 saveVld = (vld) => {
131 this.setState({
132 vld:vld
133 })
134 }
135
136 updateFirstLevelKey = (key, e) => {
137 let vld = _.cloneDeep(this.vld);
138 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
139 vld[key] = value;
140 this.setState({
141 vld: vld
142 });
143 }
144
145 updateSecondLevelKey = (firstLevelKey, secondLevelKey, e) => {
146 let vld = _.cloneDeep(this.vld);
147 if (!vld[firstLevelKey]) {
148 vld[firstLevelKey] = {};
149 }
150 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
151 vld[firstLevelKey][secondLevelKey] = value;
152 this.setState({
153 vld: vld
154 });
155 }
156
157 updateVLDInitParamsType = (value) => {
158 let vld = this.vld;
159
160 // Reset init param types
161 this.vldInitParamsTypes.map((vldInitParamType) => {
162 vld[vldInitParamType] && delete vld[vldInitParamType];
163 });
164
165 this.setState({
166 currentVLDInitParamsType: value,
167 vld: vld
168 })
169 }
170
171 updateVLDInitParamsValue = (currentVLDInitParamsType, e) => {
172 let vld = _.cloneDeep(this.vld);
173 this.vldInitParamsTypes.map((vldInitParamType) => {
174 if (currentVLDInitParamsType == vldInitParamType) {
175 let value = e.target.nodeName == "SELECT" ? JSON.parse(e.target.value) : e.target.value;
176 vld[currentVLDInitParamsType] = value;
177 } else {
178 vld[vldInitParamType] && delete vld[vldInitParamType];
179 }
180 });
181
182 this.setState({
183 vld: vld
184 })
185 }
186
187 updateFirstLevelListKeyChange = (listName, index, keyName, e) => {
188 let vld = _.cloneDeep(this.vld);
189
190
191 !vld[listName] && (vld[listName] = []);
192 !vld[listName][index] && (vld[listName][index] = {});
193 vld[listName][index][keyName] = e.target.value;
194
195 this.setState({
196 vld: vld
197 })
198 }
199
200 addConnectionPointRef = () => {
201 let vld = {};
202 if (this.vld) {
203 vld = _.cloneDeep(this.vld);
204 if (!vld['vnfd-connection-point-ref']) {
205 vld['vnfd-connection-point-ref'] = [];
206 }
207 vld['vnfd-connection-point-ref'].push({
208 'member-vnf-index-ref': '',
209 'vnfd-id-ref': '',
210 'vnfd-connection-point-ref': ''
211 });
212
213 this.setState({
214 vld: vld
215 });
216 }
217 }
218
219 removeConnectionPointRef = (vnfdConnectionPointRefIndex) => {
220 let vld = _.cloneDeep(this.vld);
221 vld['vnfd-connection-point-ref'].splice(vnfdConnectionPointRefIndex, 1);
222 this.setState({
223 vld: vld
224 });
225 }
226
227 createVirtualLinkError(data) {
228 this.alt.actions.global.showError.defer('Something went wrong while trying to create the virtual link. Check the error logs for more information');
229 }
230
231 editVirtualLinkError(data) {
232 this.alt.actions.global.showError.defer('Something went wrong while trying to save the virtual link. Check the error logs for more information');
233 }
234
235 deleteVirtualLinkError(data) {
236 this.alt.actions.global.showError.defer('Something went wrong while trying to delete the virtual link. Check the error logs for more information');
237 this.setState({
238 deleteState: 'error'
239 })
240 }
241
242 createVirtualLinkSuccess(data) {
243 this.setState({
244 // vld: this.vld,
245 mode: 'editing',
246 isLoading: false
247 });
248 }
249 editVirtualLinkSuccess(data) {
250 this.setState({
251 mode: 'editing',
252 isLoading: false
253 });
254 }
255 deleteVirtualLinkSuccess(data) {
256 this.setState({
257 deleteState: 'success'
258 });
259 }
260
261 cleanupPayload = (mode, vld) => {
262 // Do necessary cleanup here
263 let cleanVld = _.pickBy(vld, _.identity);
264 return cleanVld;
265 }
266
267 setLoadingState = (state = false) => {
268 this.setState({
269 isLoading: state
270 })
271 }
272
273 setLoaded = () => {
274 this.setLoadingState(true);
275 }
276
277 persistVirtualLink = (mode) => {
278 let self = this;
279
280 let payload = this.cleanupPayload(mode, this.vld);
281
282 if (mode == 'creating') {
283 this.getInstance().createVirtualLink(this.nsrId, payload);
284 } else {
285 this.getInstance().editVirtualLink(this.nsrId, this.vld.id, payload);
286 }
287 }
288
289 removeVirtualLink = (nsrId, vldId) => {
290 this.getInstance().deleteVirtualLink(nsrId, vldId);
291 }
292
293 }
294
295 export default NSVirtualLinkCreateStore;