Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / faultApp / src / pluginFault.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 // app configuration and main entry point for the app
19
20 import React from 'react';
21 import { Redirect, Route, RouteComponentProps, Switch, withRouter } from 'react-router-dom';
22
23 import { connect, Connect, IDispatcher } from '../../../framework/src/flux/connect';
24 import applicationManager from '../../../framework/src/services/applicationManager';
25 import { IFormatedMessage, subscribe } from '../../../framework/src/services/notificationService';
26 import { IApplicationStoreState } from '../../../framework/src/store/applicationStore';
27
28 import { AddFaultNotificationAction } from './actions/notificationActions';
29 import { SetPanelAction } from './actions/panelChangeActions';
30 import { refreshFaultStatusAsyncAction, SetFaultStatusAction } from './actions/statusActions';
31 import DashboardHome from './components/dashboardHome';
32 import { FaultStatus } from './components/faultStatus';
33 import { createCurrentAlarmsActions, createCurrentAlarmsProperties, currentAlarmsReloadAction } from './handlers/currentAlarmsHandler';
34 import { faultAppRootHandler } from './handlers/faultAppRootHandler';
35 import { FaultAlarmNotificationWS } from './models/fault';
36 import { PanelId } from './models/panelId';
37 import { FaultApplication } from './views/faultApplication';
38
39 const appIcon = require('./assets/icons/faultAppIcon.svg');  // select app icon
40
41 let currentMountId: string | undefined = undefined;
42 let currentSeverity: string | undefined = undefined;
43 let refreshInterval: ReturnType<typeof window.setInterval> | null = null;
44
45 const mapProps = (state: IApplicationStoreState) => ({
46   currentAlarmsProperties: createCurrentAlarmsProperties(state),
47 });
48
49 const mapDispatch = (dispatcher: IDispatcher) => ({
50   currentAlarmsActions: createCurrentAlarmsActions(dispatcher.dispatch, true),
51   setCurrentPanel: (panelId: PanelId) => dispatcher.dispatch(new SetPanelAction(panelId)),
52 });
53
54 const FaultApplicationRouteAdapter = connect(mapProps, mapDispatch)((props: RouteComponentProps<{ mountId?: string }> & Connect<typeof mapProps, typeof mapDispatch>) => {
55   if (currentMountId !== props.match.params.mountId) {
56     // route parameter has changed
57     currentMountId = props.match.params.mountId || undefined;
58     // Hint: This timeout is need, since it is not recommended to change the state while rendering is in progress !
59     window.setTimeout(() => {
60       if (currentMountId) {
61         props.setCurrentPanel('CurrentAlarms');
62         props.currentAlarmsActions.onFilterChanged('nodeId', currentMountId);
63         if (!props.currentAlarmsProperties.showFilter) {
64           props.currentAlarmsActions.onToggleFilter(false);
65           props.currentAlarmsActions.onRefresh();
66         } else
67           props.currentAlarmsActions.onRefresh();
68       }
69     });
70   }
71   return (
72     <FaultApplication />
73   );
74 });
75
76 const FaultApplicationAlarmStatusRouteAdapter = connect(mapProps, mapDispatch)((props: RouteComponentProps<{ severity?: string }> & Connect<typeof mapProps, typeof mapDispatch>) => {
77   if (currentSeverity !== props.match.params.severity) {
78     currentSeverity = props.match.params.severity || undefined;
79     window.setTimeout(() => {
80       if (currentSeverity) {
81         props.setCurrentPanel('CurrentAlarms');
82         props.currentAlarmsActions.onFilterChanged('severity', currentSeverity);
83         if (!props.currentAlarmsProperties.showFilter) {
84           props.currentAlarmsActions.onToggleFilter(false);
85           props.currentAlarmsActions.onRefresh();
86         } else
87           props.currentAlarmsActions.onRefresh();
88       }
89     });
90   }
91   return (
92     <FaultApplication />
93   );
94 });
95
96 const App = withRouter((props: RouteComponentProps) => (
97   <Switch>
98     <Route path={`${props.match.path}/alarmStatus/:severity?`} component={FaultApplicationAlarmStatusRouteAdapter} />
99     <Route path={`${props.match.path}/:mountId?`} component={FaultApplicationRouteAdapter} />
100     <Redirect to={`${props.match.path}`} />
101   </Switch>
102 ));
103
104 export function register() {
105   const applicationApi = applicationManager.registerApplication({
106     name: 'fault',
107     icon: appIcon,
108     rootComponent: App,
109     rootActionHandler: faultAppRootHandler,
110     statusBarElement: FaultStatus,
111     dashbaordElement: DashboardHome,
112     menuEntry: 'Fault',
113   });
114
115   let counter = 0;
116   // subscribe to the websocket notifications
117   subscribe<FaultAlarmNotificationWS & IFormatedMessage>('problem-notification', (fault => {
118     const store = applicationApi && applicationApi.applicationStore;
119     if (fault && store) {
120
121       store.dispatch(new AddFaultNotificationAction({
122         id: String(counter++),
123         nodeName: fault['node-id'],
124         counter: +fault.data.counter,
125         objectId: fault.data['object-id-ref'],
126         problem: fault.data.problem,
127         severity: fault.data.severity || '',
128         timeStamp: fault.data['time-stamp'],
129       }));
130     }
131   }));
132
133   applicationApi.applicationStoreInitialized.then(store => {
134     store.dispatch(currentAlarmsReloadAction);
135   });
136
137   applicationApi.applicationStoreInitialized.then(store => {
138     store.dispatch(refreshFaultStatusAsyncAction);
139   });
140
141   applicationApi.logoutEvent.addHandler(()=>{
142
143     applicationApi.applicationStoreInitialized.then(store => {
144       store.dispatch(new SetFaultStatusAction(0, 0, 0, 0, false, 0, 0, 0, 0, 0, 0, 0, 0, false));
145       clearInterval(refreshInterval!);
146     });
147   });
148
149   function startRefreshInterval()  {
150     const refreshFaultStatus = window.setInterval(() => {
151       applicationApi.applicationStoreInitialized.then(store => {
152   
153         store.dispatch(refreshFaultStatusAsyncAction);
154       });
155     }, 15000);
156
157     return refreshFaultStatus;
158   }
159
160   applicationApi.loginEvent.addHandler(()=>{
161     if (refreshInterval) {
162       clearInterval(refreshInterval);
163     }
164     refreshInterval = startRefreshInterval() as any;
165   });
166
167   applicationApi.logoutEvent.addHandler(()=>{
168     refreshInterval && window.clearInterval(refreshInterval);
169     refreshInterval = null;
170   });
171 }