Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / connectApp / src / components / infoNetworkElementDialog.tsx
1 /**
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import * as React from 'react';
19
20 import Button from '@mui/material/Button';
21 import Dialog from '@mui/material/Dialog';
22 import DialogActions from '@mui/material/DialogActions';
23 import DialogTitle from '@mui/material/DialogTitle';
24
25 import { ColumnType, MaterialTable, MaterialTableCtorType } from '../../../../framework/src/components/material-table';
26 import { connect, Connect } from '../../../../framework/src/flux/connect';
27
28 import { NetworkElementConnection } from '../models/networkElementConnection';
29 import { AvailableCapabilities } from '../models/yangCapabilitiesType';
30
31 export enum InfoNetworkElementDialogMode {
32   None = 'none',
33   InfoNetworkElement = 'infoNetworkElement',
34 }
35
36 const mapDispatch = () => ({
37 });
38
39 const InfoElementTable = MaterialTable as MaterialTableCtorType<AvailableCapabilities>;
40
41 type DialogSettings = {
42   dialogTitle: string;
43   dialogDescription: string;
44   cancelButtonText: string;
45 };
46
47 const settings: { [key: string]: DialogSettings } = {
48   [InfoNetworkElementDialogMode.None]: {
49     dialogTitle: '',
50     dialogDescription: '',
51     cancelButtonText: '',
52   },
53   [InfoNetworkElementDialogMode.InfoNetworkElement]: {
54     dialogTitle: 'YANG Capabilities of the Node',
55     dialogDescription: '',
56     cancelButtonText: 'OK',
57   },
58 };
59
60 type InfoNetworkElementDialogComponentProps = Connect<undefined, typeof mapDispatch> & {
61   mode: InfoNetworkElementDialogMode;
62   initialNetworkElement: NetworkElementConnection;
63   onClose: () => void;
64 };
65
66 type InfoNetworkElementDialogComponentState = NetworkElementConnection;
67
68 class InfoNetworkElementDialogComponent extends React.Component<InfoNetworkElementDialogComponentProps, InfoNetworkElementDialogComponentState> {
69   constructor(props: InfoNetworkElementDialogComponentProps) {
70     super(props);
71
72     this.state = {
73       nodeId: this.props.initialNetworkElement.nodeId,
74       isRequired: false,
75       host: this.props.initialNetworkElement.host,
76       port: this.props.initialNetworkElement.port,
77     };
78   }
79
80   render(): JSX.Element {
81     const setting = settings[this.props.mode];
82     const availableCapabilities = this.props.state.connect.elementInfo.elementInfo['netconf-node-topology:available-capabilities']['available-capability'];
83     let yangFeatures = this.props.state.connect.elementFeatureInfo.elementFeatureInfo;
84     let yangCapabilities: AvailableCapabilities[] = [];
85
86     availableCapabilities.forEach(value => {
87       const capabilty = value.capability;
88       const indexRevision = capabilty.indexOf('revision=');
89       const indexModule = capabilty.indexOf(')', indexRevision);
90       if (indexRevision > 0 && indexModule > 0) {
91         let moduleName = capabilty.substring(indexModule + 1);
92         let ModuleFeaturesList;
93         for (let index = 0; index < yangFeatures.length; index++) {
94           if (yangFeatures[index].name == moduleName) {
95             ModuleFeaturesList = yangFeatures[index].feature ? yangFeatures[index].feature : null;
96             break;
97           }
98         }
99         const featuresListCommaSeparated = ModuleFeaturesList ? ModuleFeaturesList.toString() : '';
100         let featuresList = featuresListCommaSeparated.replace(',', ', ');
101
102         yangCapabilities.push({
103           module: moduleName,
104           revision: capabilty.substring(indexRevision + 9, indexRevision + 19),
105           features: featuresList,
106         });
107       }
108     });
109
110     yangCapabilities = yangCapabilities.sort((a, b) => a.module === b.module ? 0 : a.module > b.module ? 1 : -1);
111
112     return (
113       <>
114         <Dialog open={this.props.mode !== InfoNetworkElementDialogMode.None}  >
115           <DialogTitle id="form-dialog-title">{`${setting.dialogTitle}: "${this.state.nodeId}"`}</DialogTitle>
116           <InfoElementTable stickyHeader isPopup tableId="info-element-table" asynchronus columns={[
117             { property: 'module', title: 'YANG Capability', type: ColumnType.text, width: 900 },
118             {
119               property: 'revision', title: 'Revision', type: ColumnType.custom, customControl: ({ rowData }) => {
120                 return (
121                   <div>
122                     <a href={`/yang-schema/${rowData.module}/${rowData.revision}`} target="_blank"  > {rowData.revision} </a>
123                   </div>
124                 );
125               },
126             },
127             { property: 'features', title: 'Features', type: ColumnType.text, width: 500 },
128           ]} idProperty="id" rows={yangCapabilities}  >
129           </InfoElementTable>
130           <DialogActions>
131             <Button aria-label="ok-button" onClick={(event) => {
132               this.onCancel();
133               event.preventDefault();
134               event.stopPropagation();
135             }} color="secondary"> {setting.cancelButtonText} </Button>
136           </DialogActions>
137         </Dialog>
138       </>
139     );
140   }
141
142   private onCancel = () => {
143     this.props.onClose();
144   };
145
146   static getDerivedStateFromProps(props: InfoNetworkElementDialogComponentProps, state: InfoNetworkElementDialogComponentState & { initialNetworkElement: NetworkElementConnection }): InfoNetworkElementDialogComponentState & { initialNetworkElement: NetworkElementConnection } {
147     let returnState = state;
148     if (props.initialNetworkElement !== state.initialNetworkElement) {
149       returnState = {
150         ...state,
151         ...props.initialNetworkElement,
152         initialNetworkElement: props.initialNetworkElement,
153       };
154     }
155     return returnState;
156   }
157 }
158
159 export const InfoNetworkElementDialog = connect(undefined, mapDispatch)(InfoNetworkElementDialogComponent);
160 export default InfoNetworkElementDialog;