2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import * as React from 'react';
19 import { Theme, createStyles, WithStyles, withStyles, Typography, FormControlLabel, Checkbox } from '@material-ui/core';
21 import Button from '@material-ui/core/Button';
22 import TextField from '@material-ui/core/TextField';
23 import Select from '@material-ui/core/Select';
24 import Dialog from '@material-ui/core/Dialog';
25 import DialogActions from '@material-ui/core/DialogActions';
26 import DialogContent from '@material-ui/core/DialogContent';
27 import DialogContentText from '@material-ui/core/DialogContentText';
28 import DialogTitle from '@material-ui/core/DialogTitle';
30 import Tabs from '@material-ui/core/Tabs';
31 import Tab from '@material-ui/core/Tab';
33 import Fab from '@material-ui/core/Fab';
34 import AddIcon from '@material-ui/icons/Add';
35 import DeleteIcon from '@material-ui/icons/Delete';
36 import IconButton from '@material-ui/core/IconButton';
38 import { addMediatorConfigAsyncActionCreator, updateMediatorConfigAsyncActionCreator, removeMediatorConfigAsyncActionCreator } from '../actions/mediatorConfigActions';
39 import { MediatorConfig, ODLConfig } from '../models/mediatorServer';
40 import FormControl from '@material-ui/core/FormControl';
41 import InputLabel from '@material-ui/core/InputLabel';
42 import MenuItem from '@material-ui/core/MenuItem';
44 import { Panel } from '../../../../framework/src/components/material-ui/panel';
46 import { IDispatcher, connect, Connect } from '../../../../framework/src/flux/connect';
47 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
50 const styles = (theme: Theme) => createStyles({
53 flexDirection: 'column',
58 bottom: theme.spacing.unit,
59 right: theme.spacing.unit,
68 justifyContent: "center",
75 marginRight: theme.spacing.unit,
82 const TabContainer: React.SFC = ({ children }) => {
84 <div style={{ width: "430px", height: "530px", position: "relative", display: 'flex', flexDirection: 'column' }}>
90 export enum EditMediatorConfigDialogMode {
92 AddMediatorConfig = "addMediatorConfig",
93 EditMediatorConfig = "editMediatorConfig",
94 RemoveMediatorConfig = "removeMediatorConfig",
97 const mapProps = (state: IApplicationStoreState) => ({
98 supportedDevices: state.mediator.mediatorServerState.supportedDevices
101 const mapDispatch = (dispatcher: IDispatcher) => ({
102 addMediatorConfig: (config: MediatorConfig) => {
103 dispatcher.dispatch(addMediatorConfigAsyncActionCreator(config));
105 updateMediatorConfig: (config: MediatorConfig) => {
106 dispatcher.dispatch(updateMediatorConfigAsyncActionCreator(config));
108 removeMediatorConfig: (config: MediatorConfig) => {
109 dispatcher.dispatch(removeMediatorConfigAsyncActionCreator(config));
113 type DialogSettings = {
115 dialogDescription: string;
116 applyButtonText: string;
117 cancelButtonText: string;
119 readonlyName: boolean;
122 const settings: { [key: string]: DialogSettings } = {
123 [EditMediatorConfigDialogMode.None]: {
125 dialogDescription: "",
127 cancelButtonText: "",
131 [EditMediatorConfigDialogMode.AddMediatorConfig]: {
132 dialogTitle: "Add Mediator Configuration",
133 dialogDescription: "",
134 applyButtonText: "Add",
135 cancelButtonText: "Cancel",
139 [EditMediatorConfigDialogMode.EditMediatorConfig]: {
140 dialogTitle: "Edit Mediator Configuration",
141 dialogDescription: "",
142 applyButtonText: "Update",
143 cancelButtonText: "Cancel",
147 [EditMediatorConfigDialogMode.RemoveMediatorConfig]: {
148 dialogTitle: "Remove Mediator Configuration",
149 dialogDescription: "",
150 applyButtonText: "Remove",
151 cancelButtonText: "Cancel",
157 type EditMediatorConfigDialogComponentProps = WithStyles<typeof styles> & Connect<typeof mapProps, typeof mapDispatch> & {
158 mode: EditMediatorConfigDialogMode;
159 mediatorConfig: MediatorConfig;
163 type EditMediatorConfigDialogComponentState = MediatorConfig & { activeTab: number; activeOdlConfig: string };
165 class EditMediatorConfigDialogComponent extends React.Component<EditMediatorConfigDialogComponentProps, EditMediatorConfigDialogComponentState> {
166 constructor (props: EditMediatorConfigDialogComponentProps) {
170 ...this.props.mediatorConfig,
176 private odlConfigValueChangeHandlerCreator = <THtmlElement extends HTMLElement, TKey extends keyof ODLConfig>(index: number, property: TKey, mapValue: (event: React.ChangeEvent<THtmlElement>) => any) => (event: React.ChangeEvent<THtmlElement>) => {
177 event.stopPropagation();
178 event.preventDefault();
181 ...this.state.ODLConfig.slice(0, index),
182 { ...this.state.ODLConfig[index], [property]: mapValue(event) },
183 ...this.state.ODLConfig.slice(index + 1)
188 render(): JSX.Element {
189 const setting = settings[this.props.mode];
190 const { classes } = this.props;
192 <Dialog open={this.props.mode !== EditMediatorConfigDialogMode.None}>
193 <DialogTitle id="form-dialog-title">{setting.dialogTitle}</DialogTitle>
196 {setting.dialogDescription}
198 <Tabs value={this.state.activeTab} indicatorColor="primary" textColor="primary" onChange={(event, value) => this.setState({ activeTab: value })} >
199 <Tab label="Config" />
200 <Tab label="ODL AutoConnect" />
202 {this.state.activeTab === 0 ? <TabContainer >
203 <TextField disabled={setting.readonly || setting.readonlyName} spellCheck={false} autoFocus margin="dense" id="name" label="Name" type="text" fullWidth value={this.state.Name} onChange={(event) => { this.setState({ Name: event.target.value }); }} />
204 <FormControl fullWidth disabled={setting.readonly}>
205 <InputLabel htmlFor="deviceType">Device</InputLabel>
206 <Select value={this.state.DeviceType} onChange={(event) => {
207 const device = this.props.supportedDevices.find(device => device.id === +event.target.value);
210 DeviceType: device.id,
211 NeXMLFile: device.xml
219 }} inputProps={{ name: 'deviceType', id: 'deviceType' }} fullWidth >
220 <MenuItem value={-1}><em>None</em></MenuItem>
221 {this.props.supportedDevices.map(device => (<MenuItem key={device.id} value={device.id} >{`${device.vendor} - ${device.device} (${device.version || '0.0.0'}) `}</MenuItem>))}
224 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ipAddress" label="Device IP" type="text" fullWidth value={this.state.DeviceIp} onChange={(event) => { this.setState({ DeviceIp: event.target.value }); }} />
225 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="devicePort" label="Device SNMP Port" type="number" fullWidth value={this.state.DevicePort || ""} onChange={(event) => { this.setState({ DevicePort: +event.target.value }); }} />
226 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="trapsPort" label="TrapsPort" type="number" fullWidth value={this.state.TrapPort || ""} onChange={(event) => { this.setState({ TrapPort: +event.target.value }); }} />
227 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncUser" label="Netconf User" type="text" fullWidth value={this.state.NcUsername} onChange={(event) => { this.setState({ NcUsername: event.target.value }); }} />
228 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncPassword" label="Netconf Password" type="password" fullWidth value={this.state.NcPassword} onChange={(event) => { this.setState({ NcPassword: event.target.value }); }} />
229 <TextField disabled={setting.readonly} spellCheck={false} autoFocus margin="dense" id="ncPort" label="Netconf Port" type="number" fullWidth value={this.state.NcPort || ""} onChange={(event) => { this.setState({ NcPort: +event.target.value }); }} />
230 </TabContainer> : null}
231 {this.state.activeTab === 1 ? <TabContainer >
232 {this.state.ODLConfig && this.state.ODLConfig.length > 0
233 ? this.state.ODLConfig.map((cfg, ind) => {
234 const panelId = `panel-${ind}`;
235 const deleteButton = (<IconButton onClick={() => {
238 ...this.state.ODLConfig.slice(0, ind),
239 ...this.state.ODLConfig.slice(ind + 1)
242 }} ><DeleteIcon /></IconButton>)
244 <Panel title={cfg.Server && `${cfg.User ? `${cfg.User}@` : ''}${cfg.Protocol}://${cfg.Server}:${cfg.Port}` || "new odl config"} key={panelId} panelId={panelId} activePanel={this.state.activeOdlConfig} customActionButtons={[deleteButton]}
245 onToggle={(id) => this.setState({ activeOdlConfig: (this.state.activeOdlConfig === id) ? "" : (id || "") })} >
246 <div className={classes.alignInOneLine}>
247 <FormControl className={classes.left} margin={"dense"} >
248 <InputLabel htmlFor={`protocol-${ind}`}>Protocoll</InputLabel>
249 <Select value={cfg.Protocol} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Protocol", e => (e.target.value))} inputProps={{ name: `protocol-${ind}`, id: `protocol-${ind}` }} fullWidth >
250 <MenuItem value={"http"}>http</MenuItem>
251 <MenuItem value={"https"}>https</MenuItem>
254 <TextField className={classes.left} spellCheck={false} margin="dense" id="hostname" label="Hostname" type="text" value={cfg.Server} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Server", e => e.target.value)} />
255 <TextField className={classes.right} style={{ maxWidth: "65px" }} spellCheck={false} margin="dense" id="port" label="Port" type="number" value={cfg.Port || ""} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Port", e => +e.target.value)} />
257 <div className={classes.alignInOneLine}>
258 <TextField className={classes.left} spellCheck={false} margin="dense" id="username" label="Username" type="text" value={cfg.User} onChange={this.odlConfigValueChangeHandlerCreator(ind, "User", e => e.target.value)} />
259 <TextField className={classes.right} spellCheck={false} margin="dense" id="password" label="Password" type="password" value={cfg.Password} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Password", e => e.target.value)} />
261 <div className={classes.alignInOneLine}>
262 <FormControlLabel className={classes.right} control={<Checkbox checked={cfg.Trustall} onChange={this.odlConfigValueChangeHandlerCreator(ind, "Trustall", e => e.target.checked)} />} label="Trustall" />
267 : <div className={classes.center} >
268 <Typography component={"div"} className={classes.title} color="textSecondary" gutterBottom>Please add an ODL auto connect configuration.</Typography>
271 <Fab className={classes.fab} color="primary" aria-label="Add" onClick={() => this.setState({
272 ODLConfig: [...this.state.ODLConfig, { Server: '', Port: 8181, Protocol: 'https', User: 'admin', Password: 'admin', Trustall: false }]
273 })} > <AddIcon /> </Fab>
275 </TabContainer> : null}
279 <Button onClick={(event) => {
280 this.onApply(Object.keys(this.state).reduce<MediatorConfig & { [kex: string]: any }>((acc, key) => {
281 // do not copy activeTab and activeOdlConfig
282 if (key !== "activeTab" && key !== "activeOdlConfig" && key !== "_initialMediatorConfig") acc[key] = (this.state as any)[key];
284 }, {} as MediatorConfig));
285 event.preventDefault();
286 event.stopPropagation();
287 }} > {setting.applyButtonText} </Button>
288 <Button onClick={(event) => {
290 event.preventDefault();
291 event.stopPropagation();
292 }} color="secondary"> {setting.cancelButtonText} </Button>
298 private onApply = (config: MediatorConfig) => {
299 this.props.onClose && this.props.onClose();
300 switch (this.props.mode) {
301 case EditMediatorConfigDialogMode.AddMediatorConfig:
302 config && this.props.addMediatorConfig(config);
304 case EditMediatorConfigDialogMode.EditMediatorConfig:
305 config && this.props.updateMediatorConfig(config);
307 case EditMediatorConfigDialogMode.RemoveMediatorConfig:
308 config && this.props.removeMediatorConfig(config);
313 private onCancel = () => {
314 this.props.onClose && this.props.onClose();
317 static getDerivedStateFromProps(props: EditMediatorConfigDialogComponentProps, state: EditMediatorConfigDialogComponentState & { _initialMediatorConfig: MediatorConfig }): EditMediatorConfigDialogComponentState & { _initialMediatorConfig: MediatorConfig } {
318 if (props.mediatorConfig !== state._initialMediatorConfig) {
321 ...props.mediatorConfig,
322 _initialMediatorConfig: props.mediatorConfig,
329 export const EditMediatorConfigDialog = withStyles(styles)(connect(mapProps, mapDispatch)(EditMediatorConfigDialogComponent));
330 export default EditMediatorConfigDialog;