Rift.IO OSM R1 Initial Submission
[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 vld[currentVLDInitParamsType] = e.target.value;
176 } else {
177 vld[vldInitParamType] && delete vld[vldInitParamType];
178 }
179 });
180
181 this.setState({
182 vld: vld
183 })
184 }
185
186 updateFirstLevelListKeyChange = (listName, index, keyName, e) => {
187 let vld = _.cloneDeep(this.vld);
188
189
190 !vld[listName] && (vld[listName] = []);
191 !vld[listName][index] && (vld[listName][index] = {});
192 vld[listName][index][keyName] = e.target.value;
193
194 this.setState({
195 vld: vld
196 })
197 }
198
199 addConnectionPointRef = () => {
200 let vld = {};
201 if (this.vld) {
202 vld = _.cloneDeep(this.vld);
203 if (!vld['vnfd-connection-point-ref']) {
204 vld['vnfd-connection-point-ref'] = [];
205 }
206 vld['vnfd-connection-point-ref'].push({
207 'member-vnf-index-ref': '',
208 'vnfd-id-ref': '',
209 'vnfd-connection-point-ref': ''
210 });
211
212 this.setState({
213 vld: vld
214 });
215 }
216 }
217
218 removeConnectionPointRef = (vnfdConnectionPointRefIndex) => {
219 let vld = _.cloneDeep(this.vld);
220 vld['vnfd-connection-point-ref'].splice(vnfdConnectionPointRefIndex, 1);
221 this.setState({
222 vld: vld
223 });
224 }
225
226 createVirtualLinkError(data) {
227 this.alt.actions.global.showError.defer('Something went wrong while trying to create the virtual link. Check the error logs for more information');
228 }
229
230 editVirtualLinkError(data) {
231 this.alt.actions.global.showError.defer('Something went wrong while trying to save the virtual link. Check the error logs for more information');
232 }
233
234 deleteVirtualLinkError(data) {
235 this.alt.actions.global.showError.defer('Something went wrong while trying to delete the virtual link. Check the error logs for more information');
236 this.setState({
237 deleteState: 'error'
238 })
239 }
240
241 createVirtualLinkSuccess(data) {
242 this.setState({
243 // vld: this.vld,
244 mode: 'editing',
245 isLoading: false
246 });
247 }
248 editVirtualLinkSuccess(data) {
249 this.setState({
250 mode: 'editing',
251 isLoading: false
252 });
253 }
254 deleteVirtualLinkSuccess(data) {
255 this.setState({
256 deleteState: 'success'
257 });
258 }
259
260 cleanupPayload = (mode, vld) => {
261 // Do necessary cleanup here
262 let cleanVld = _.pickBy(vld, _.identity);
263 return cleanVld;
264 }
265
266 setLoadingState = (state = false) => {
267 this.setState({
268 isLoading: state
269 })
270 }
271
272 setLoaded = () => {
273 this.setLoadingState(true);
274 }
275
276 persistVirtualLink = (mode) => {
277 let self = this;
278
279 let payload = this.cleanupPayload(mode, this.vld);
280
281 if (mode == 'creating') {
282 this.getInstance().createVirtualLink(this.nsrId, payload);
283 } else {
284 this.getInstance().editVirtualLink(this.nsrId, this.vld.id, payload);
285 }
286 }
287
288 removeVirtualLink = (nsrId, vldId) => {
289 this.getInstance().deleteVirtualLink(nsrId, vldId);
290 }
291
292 }
293
294 export default NSVirtualLinkCreateStore;