Merge "SDN-R ws notifications"
[ccsdk/features.git] / sdnr / wt / odlux / apps / mediatorApp / src / actions / mediatorConfigActions.ts
1
2 import { Action } from '../../../../framework/src/flux/action';
3 import { Dispatch } from '../../../../framework/src/flux/store';
4
5 import { AddSnackbarNotification } from '../../../../framework/src/actions/snackbarActions';
6 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
7
8 import mediatorService from '../services/mediatorService';
9 import { MediatorConfig, MediatorConfigResponse } from '../models/mediatorServer';
10
11 /** Represents the base action. */
12 export class BaseAction extends Action { }
13
14 export class SetMediatorBusyByName extends BaseAction {
15   constructor (public name: string, public isBusy: boolean) {
16     super();
17   }
18 }
19
20 export class AddMediatorConfig extends BaseAction {
21   constructor (public mediatorConfig: MediatorConfigResponse) {
22     super();
23   }
24 }
25
26 export class UpdateMediatorConfig extends BaseAction {
27   constructor (public name: string, public mediatorConfig: MediatorConfigResponse) {
28     super();
29   }
30 }
31
32 export class RemoveMediatorConfig extends BaseAction {
33   constructor (public name: string) {
34     super();
35   }
36 }
37
38
39 export const startMediatorByNameAsyncActionCreator = (name: string) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
40   dispatch(new SetMediatorBusyByName(name, true));
41   const { mediator: { mediatorServerState: { url } } } = getState();
42   if (url) {
43     mediatorService.startMediatorByName(url, name).then(msg => {
44       dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
45       // since there is no notification, a timeout will be need here
46       window.setTimeout(() => {
47         mediatorService.getMediatorServerConfigByName(url, name).then(config => {
48           if (config) {
49             dispatch(new UpdateMediatorConfig(name, config));
50           } else {
51             dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
52           }
53           dispatch(new SetMediatorBusyByName(name, false));
54         });
55       }, 2100);
56     });
57   } else {
58     dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
59     dispatch(new SetMediatorBusyByName(name, false));
60   }
61 };
62
63 export const stopMediatorByNameAsyncActionCreator = (name: string) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
64   dispatch(new SetMediatorBusyByName(name, true));
65   const { mediator: { mediatorServerState: { url } } } = getState();
66   if (url) {
67     mediatorService.stopMediatorByName(url, name).then(msg => {
68       dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
69       // since there is no notification, a timeout will be need here
70       window.setTimeout(() => {
71         mediatorService.getMediatorServerConfigByName(url, name).then(config => {
72           if (config) {
73             dispatch(new UpdateMediatorConfig(name, config));
74           } else {
75             dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
76           }
77           dispatch(new SetMediatorBusyByName(name, false));
78         });
79       }, 2100);
80     });
81   } else {
82     dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
83     dispatch(new SetMediatorBusyByName(name, false));
84   }
85 };
86
87 export const addMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
88   const { Name: name } = config;
89   const { mediator: { mediatorServerState: { url } } } = getState();
90   if (url) {
91     mediatorService.createMediatorConfig(url, config).then(msg => {
92       dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
93       // since there is no notification, a timeout will be need here
94       window.setTimeout(() => {
95         mediatorService.getMediatorServerConfigByName(url, name).then(config => {
96           if (config) {
97             dispatch(new AddMediatorConfig(config));
98           } else {
99             dispatch(new AddSnackbarNotification({ message: `Error: reading mediator config for ${name}.`, options: { variant: 'error' } }));
100           }
101         });
102       }, 2100);
103     });
104   } else {
105     dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
106   }
107 };
108
109 export const updateMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch) => {
110   // currently not supported be backend
111 };
112
113 export const removeMediatorConfigAsyncActionCreator = (config: MediatorConfig) => (dispatch: Dispatch, getState: () => IApplicationStoreState) => {
114   const { Name: name } = config;
115   const { mediator: { mediatorServerState: { url } } } = getState();
116   if (url) {
117     mediatorService.deleteMediatorConfigByName(url, name).then(msg => {
118       dispatch(new AddSnackbarNotification({ message: msg + ' ' + name, options: { variant: 'info' } }));
119       // since there is no notification, a timeout will be need here
120       window.setTimeout(() => {
121         mediatorService.getMediatorServerConfigByName(url, config.Name).then(config => {
122           if (!config) {
123             dispatch(new RemoveMediatorConfig(name));
124           } else {
125             dispatch(new AddSnackbarNotification({ message: `Error: deleting mediator config for ${name}.`, options: { variant: 'error' } }));
126           }
127         });
128       }, 2100);
129     });
130   } else {
131     dispatch(new AddSnackbarNotification({ message: `Error: currently no mediator server selected.`, options: { variant: 'error' } }));
132     dispatch(new SetMediatorBusyByName(name, false));
133   }
134 };
135
136
137