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