aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/connectApp/src/components/editNetworkElementDialog.tsx
blob: 061303976ad38be25939571dffcf34b9b5409a88 (plain)
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
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */
import * as React from 'react';

import Button from '@material-ui/core/Button';
import TextField from '@material-ui/core/TextField';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import DialogContentText from '@material-ui/core/DialogContentText';
import DialogTitle from '@material-ui/core/DialogTitle';
import { FormControl, InputLabel, Select, MenuItem, Typography, Radio, RadioGroup, Options, FormLabel, FormControlLabel } from '@material-ui/core';
import { loadAllTlsKeyListAsync } from '../actions/tlsKeyActions';
import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';

import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';

import {
  editNetworkElementAsyncActionCreator,
  addNewNetworkElementAsyncActionCreator,
  removeNetworkElementAsyncActionCreator
} from '../actions/networkElementsActions';

import { unmountNetworkElementAsyncActionCreator, mountNetworkElementAsyncActionCreator } from '../actions/mountedNetworkElementsActions';
import { NetworkElementConnection, UpdateNetworkElement, propertyOf } from '../models/networkElementConnection';
import { removeWebUriAction } from '../actions/commonNetworkElementsActions';

export enum EditNetworkElementDialogMode {
  None = "none",
  EditNetworkElement = "editNetworkElement",
  RemoveNetworkElement = "removeNetworkElement",
  AddNewNetworkElement = "addNewNetworkElement",
  MountNetworkElement = "mountNetworkElement",
  UnmountNetworkElement = "unmountNetworkElement",
}



const mapDispatch = (dispatcher: IDispatcher) => ({
  addNewNetworkElement: async (element: NetworkElementConnection) => {
    await dispatcher.dispatch(addNewNetworkElementAsyncActionCreator(element));
    await dispatcher.dispatch(mountNetworkElementAsyncActionCreator(element));
  },
  mountNetworkElement: (element: NetworkElementConnection) => dispatcher.dispatch(mountNetworkElementAsyncActionCreator(element)),
  unmountNetworkElement: (element: NetworkElementConnection) => {
    dispatcher.dispatch(unmountNetworkElementAsyncActionCreator(element && element.nodeId));
  },
  editNetworkElement: async (element: UpdateNetworkElement, mountElement: NetworkElementConnection) => {

    const values = Object.keys(element);
    console.log("edit element");
    console.log(values);

    //make sure properties are there in case they get renamed
    const idProperty = propertyOf<UpdateNetworkElement>("id");
    const isRequiredProperty = propertyOf<UpdateNetworkElement>("isRequired");


    if (values.length === 2 && values.includes(idProperty as string) && values.includes(isRequiredProperty as string)) {
      // do not mount network element, if only isRequired is changed
      await dispatcher.dispatch(editNetworkElementAsyncActionCreator(element));

    } else if (!(values.length === 1 && values.includes(idProperty as string))) { //do not edit or mount element, if only id was saved into object (no changes made!)
      await dispatcher.dispatch(editNetworkElementAsyncActionCreator(element));
      await dispatcher.dispatch(mountNetworkElementAsyncActionCreator(mountElement));
    }
  },
  removeNetworkElement: async (element: UpdateNetworkElement) => {
    await dispatcher.dispatch(removeNetworkElementAsyncActionCreator(element));
    dispatcher.dispatch(removeWebUriAction(element.id));
  },
  getAvailableTlsKeys: async () => await dispatcher.dispatch(loadAllTlsKeyListAsync()),
});

type DialogSettings = {
  dialogTitle: string,
  dialogDescription: string,
  applyButtonText: string,
  cancelButtonText: string,
  enableMountIdEditor: boolean,
  enableUsernameEditor: boolean,
  enableExtendedEditor: boolean,
}

const settings: { [key: string]: DialogSettings } = {
  [EditNetworkElementDialogMode.None]: {
    dialogTitle: "",
    dialogDescription: "",
    applyButtonText: "",
    cancelButtonText: "",
    enableMountIdEditor: false,
    enableUsernameEditor: false,
    enableExtendedEditor: false,
  },

  [EditNetworkElementDialogMode.AddNewNetworkElement]: {
    dialogTitle: "Add new network element",
    dialogDescription: "Add this new network element:",
    applyButtonText: "Add network element",
    cancelButtonText: "Cancel",
    enableMountIdEditor: true,
    enableUsernameEditor: true,
    enableExtendedEditor: true,
  },
  [EditNetworkElementDialogMode.MountNetworkElement]: {
    dialogTitle: "Mount network element",
    dialogDescription: "mount this network element:",
    applyButtonText: "mount network element",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableUsernameEditor: false,
    enableExtendedEditor: false,
  },
  [EditNetworkElementDialogMode.UnmountNetworkElement]: {
    dialogTitle: "Unmount network element",
    dialogDescription: "unmount this network element:",
    applyButtonText: "Unmount network element",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableUsernameEditor: false,
    enableExtendedEditor: false,
  },
  [EditNetworkElementDialogMode.EditNetworkElement]: {
    dialogTitle: "Modify the network elements",
    dialogDescription: "Modify this network element",
    applyButtonText: "Modify",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableUsernameEditor: true,
    enableExtendedEditor: false,
  },
  [EditNetworkElementDialogMode.RemoveNetworkElement]: {
    dialogTitle: "Remove network element",
    dialogDescription: "Do you really want to remove this network element:",
    applyButtonText: "Remove network element",
    cancelButtonText: "Cancel",
    enableMountIdEditor: false,
    enableUsernameEditor: false,
    enableExtendedEditor: false,
  }
}

type EditNetworkElementDialogComponentProps = Connect<undefined, typeof mapDispatch> & {
  mode: EditNetworkElementDialogMode;
  initialNetworkElement: NetworkElementConnection;
  onClose: () => void;
  radioChecked: string
};

type EditNetworkElementDialogComponentState = NetworkElementConnection & {
  isNameValid: boolean,
  isHostSet: boolean,
  isPasswordSelected: boolean,
  isTlsSelected: boolean,
  radioSelected: string,
  showPasswordTextField: boolean,
  showTlsDropdown: boolean
};

class EditNetworkElementDialogComponent extends React.Component<EditNetworkElementDialogComponentProps, EditNetworkElementDialogComponentState> {
  constructor(props: EditNetworkElementDialogComponentProps) {
    super(props);
    this.handleRadioChange = this.handleRadioChange.bind(this);
    this.state = {
      nodeId: this.props.initialNetworkElement.nodeId,
      isRequired: false,
      host: this.props.initialNetworkElement.host,
      port: this.props.initialNetworkElement.port,
      isNameValid: true,
      isHostSet: true,
      isPasswordSelected: true,
      isTlsSelected: false,
      radioSelected: '',
      showPasswordTextField: true,
      showTlsDropdown: false
    };
  }
  public handleRadioChange = (event: any) => {
    this.setState({
      radioSelected: event.target.value,
      showPasswordTextField: event.target.value === 'password',
      showTlsDropdown: event.target.value === 'tlsKey'
    });
  }

  render(): JSX.Element {
    const setting = settings[this.props.mode];
    let { showPasswordTextField, showTlsDropdown, radioSelected } = this.state;
    radioSelected = this.state.radioSelected.length > 0 ? this.state.radioSelected : this.props.radioChecked;

    if (radioSelected === 'password') {
      radioSelected = 'password';
      showPasswordTextField = true;
      showTlsDropdown = false;
    } else if (radioSelected === 'tlsKey') {
      radioSelected = 'tlsKey';
      showPasswordTextField = false;
      showTlsDropdown = true;
    }

    let tlsKeysList = this.props.state.connect.availableTlsKeys ? this.props.state.connect.availableTlsKeys.tlsKeysList ? this.props.state.connect.availableTlsKeys.tlsKeysList : [] : []

    return (
      <Dialog open={this.props.mode !== EditNetworkElementDialogMode.None}>
        <DialogTitle id="form-dialog-title" aria-label={`${setting.dialogTitle.replace(/ /g, "-").toLowerCase()}-dialog`}>{setting.dialogTitle}</DialogTitle>
        <DialogContent>
          <DialogContentText>
            {setting.dialogDescription}
          </DialogContentText>
          <TextField disabled={!setting.enableMountIdEditor} spellCheck={false} autoFocus margin="dense" id="name" label="Name" aria-label="name" type="text" fullWidth value={this.state.nodeId} onChange={(event) => { this.setState({ nodeId: event.target.value }); }} />
          {!this.state.isNameValid && <Typography variant="body1" color="error">Name cannot be empty.</Typography>}
          <TextField disabled={!setting.enableMountIdEditor} spellCheck={false} margin="dense" id="ipaddress" label="IP address" aria-label="ip adress" type="text" fullWidth value={this.state.host} onChange={(event) => { this.setState({ host: event.target.value }); }} />
          {!this.state.isHostSet && <Typography variant="body1" color="error">IP Adress cannot be empty.</Typography>}

          <TextField disabled={!setting.enableMountIdEditor} spellCheck={false} margin="dense" id="netconfport" label="NetConf port" aria-label="netconf port" type="number" fullWidth value={this.state.port.toString()} onChange={(event) => { this.setState({ port: +event.target.value }); }} />
          {setting.enableUsernameEditor && <TextField disabled={!setting.enableUsernameEditor} spellCheck={false} margin="dense" id="username" label="Username" aria-label="username" type="text" fullWidth value={this.state.username} onChange={(event) => { this.setState({ username: event.target.value }); }} /> || null}

          {setting.enableUsernameEditor &&
            <RadioGroup row aria-label="password-tls-key" name="password-tls-key" value={radioSelected}
              onChange={this.handleRadioChange} >
              <FormControlLabel aria-label="passwordSelection" value='password' control={<Radio />} label="Password" onChange={this.onRadioSelect} />
              <FormControlLabel aria-label="tlsKeySelection" value='tlsKey' control={<Radio />} label="TlsKey" onChange={this.onRadioSelect} />
            </RadioGroup> || null}

          {setting.enableUsernameEditor && showPasswordTextField &&
            <TextField disabled={!setting.enableUsernameEditor || !showPasswordTextField} spellCheck={false} margin="dense"
              id="password" aria-label="password" type="password" fullWidth value={this.state.password}
              onChange={(event) => { this.setState({ password: event.target.value }); }}
            /> || null}

          <FormControl fullWidth disabled={!setting.enableUsernameEditor}>
            {setting.enableUsernameEditor && showTlsDropdown &&
              <div>
                <InputLabel htmlFor="pass">--Select tls-key--</InputLabel>
                <Select disabled={!setting.enableUsernameEditor || !showTlsDropdown}
                  id="tlsKey" aria-label="tlsKey" value={this.state.tlsKey} fullWidth // displayEmpty
                  onChange={(event) => { this.setState({ tlsKey: event.target.value as any }); }}
                  inputProps={{ name: 'tlsKey', id: 'tlsKey' }}  >
                  <MenuItem value={""} disabled >--Select tls-key--</MenuItem>
                  {tlsKeysList.map(tlsKey =>
                    (<MenuItem value={tlsKey.key} key={tlsKey.key} aria-label={tlsKey.key} >{tlsKey.key}</MenuItem>))}
                </Select>
              </div>
            }
          </FormControl>

          <FormControl fullWidth disabled={!setting.enableUsernameEditor}>
            <InputLabel htmlFor="active">Required</InputLabel>
            <Select aria-label="required-selection" value={this.state.isRequired || false} onChange={(event) => {
              this.setState({ isRequired: event.target.value as any as boolean });
            }} inputProps={{ name: 'required', id: 'required' }} fullWidth >
              <MenuItem value={true as any as string} aria-label="true">True</MenuItem>
              <MenuItem value={false as any as string} aria-label="false">False</MenuItem>
            </Select>
          </FormControl>
        </DialogContent>
        <DialogActions>
          <Button aria-label="dialog-confirm-button" onClick={(event) => {

            if (this.areRequieredFieldsValid()) {
              this.onApply({
                isRequired: this.state.isRequired,
                id: this.state.nodeId,
                nodeId: this.state.nodeId,
                host: this.state.host,
                port: this.state.port,
                username: this.state.username,
                password: this.state.password,
                tlsKey: this.state.tlsKey
              });
            }
            event.preventDefault();
            event.stopPropagation();
          }} > {setting.applyButtonText} </Button>
          <Button aria-label="dialog-cancel-button" onClick={(event) => {
            this.onCancel();
            event.preventDefault();
            event.stopPropagation();
          }} color="secondary"> {setting.cancelButtonText} </Button>
        </DialogActions>
      </Dialog>
    )
  }

  public renderTlsKeys = () => {
    try {
      this.props.getAvailableTlsKeys();
    } catch (err) {
      console.log(err);
    }
  }

  public componentDidMount() {
    this.renderTlsKeys();
  }

  public onRadioSelect = (e: any) => {
    if (e.target.value == 'password') {
      this.setState({ isPasswordSelected: true, isTlsSelected: false })
    } else if (e.target.value == 'tlsKey') {
      this.setState({ isPasswordSelected: false, isTlsSelected: true })
    }
  };

  private onApply = (element: NetworkElementConnection) => {
    this.props.onClose && this.props.onClose();
    let updateElement: UpdateNetworkElement = {
      id: this.state.nodeId
    }
    if (this.state.isPasswordSelected) {
      element.tlsKey = ''
    }
    else if (this.state.isTlsSelected) { //check here
      element.password = ''
    }

    switch (this.props.mode) {
      case EditNetworkElementDialogMode.AddNewNetworkElement:
        element && this.props.addNewNetworkElement(element);
        this.setState({
          radioSelected: ''
        });
        break;
      case EditNetworkElementDialogMode.MountNetworkElement:
        element && this.props.mountNetworkElement(element);
        break;
      case EditNetworkElementDialogMode.UnmountNetworkElement:
        element && this.props.unmountNetworkElement(element);
        break;
      case EditNetworkElementDialogMode.EditNetworkElement:
        if (this.props.initialNetworkElement.isRequired !== this.state.isRequired)
          updateElement.isRequired = this.state.isRequired;
        if (this.props.initialNetworkElement.username !== this.state.username)
          updateElement.username = this.state.username;
        if (this.props.initialNetworkElement.password !== this.state.password && this.state.isPasswordSelected) {
          updateElement.password = this.state.password;
          updateElement.tlsKey = '';
        }
        if (this.props.initialNetworkElement.tlsKey !== this.state.tlsKey && this.state.isTlsSelected) {
          updateElement.tlsKey = this.state.tlsKey;
          updateElement.password = '';
        }
        element && this.props.editNetworkElement(updateElement, element);
        this.setState({
          radioSelected: ''
        });
        break;
      case EditNetworkElementDialogMode.RemoveNetworkElement:
        element && this.props.removeNetworkElement(updateElement);
        break;
    }

    this.setState({ password: '', username: '', tlsKey: '' });
    this.resetRequieredFields();
  };

  private onCancel = () => {
    this.props.onClose && this.props.onClose();
    this.setState({ password: '', username: '', tlsKey: '', radioSelected: '' });
    this.resetRequieredFields();
  }

  private resetRequieredFields() {
    this.setState({ isNameValid: true, isHostSet: true });
  }

  private areRequieredFieldsValid() {
    let areFieldsValid = true;

    if (this.state.nodeId == undefined || this.state.nodeId.trim().length === 0) {
      this.setState({ isNameValid: false });
      areFieldsValid = false;
    } else {
      this.setState({ isNameValid: true });
    }

    if (this.state.host == undefined || this.state.host.trim().length === 0) {
      this.setState({ isHostSet: false });
      areFieldsValid = false;
    } else {
      this.setState({ isHostSet: true });
    }

    return areFieldsValid;
  }

  static getDerivedStateFromProps(props: EditNetworkElementDialogComponentProps, state: EditNetworkElementDialogComponentState & { _initialNetworkElement: NetworkElementConnection }): EditNetworkElementDialogComponentState & { _initialNetworkElement: NetworkElementConnection } {
    if (props.initialNetworkElement !== state._initialNetworkElement) {
      state = {
        ...state,
        ...props.initialNetworkElement,
        _initialNetworkElement: props.initialNetworkElement,
      };
    }
    return state;
  }
}

export const EditNetworkElementDialog = connect(undefined, mapDispatch)(EditNetworkElementDialogComponent);
export default EditNetworkElementDialog;