Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / mediatorApp / src / actions / mediatorServerActions.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 import { Action } from '../../../../framework/src/flux/action';
19 import { Dispatch } from '../../../../framework/src/flux/store';
20
21 import { MediatorServerVersionInfo, MediatorConfig, MediatorConfigResponse, MediatorServerDevice } from '../models/mediatorServer';
22 import mediatorService from '../services/mediatorService';
23 import { AddSnackbarNotification } from '../../../../framework/src/actions/snackbarActions';
24 import { NavigateToApplication } from '../../../../framework/src/actions/navigationActions';
25 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
26
27 /** Represents the base action. */
28 export class BaseAction extends Action { }
29
30 export class SetMediatorServerBusy extends BaseAction {
31   constructor(public isBusy: boolean) {
32     super();
33   }
34 }
35
36 export class SetMediatorServerInfo extends BaseAction {
37   /**
38    * Initializes a new instance of this class.
39    */
40   constructor(public id: string | null, public name: string | null, public url: string | null) {
41     super();
42
43   }
44 }
45
46 export class SetMediatorServerVersion extends BaseAction {
47   /**
48    * Initializes a new instance of this class.
49    */
50   constructor(public versionInfo: MediatorServerVersionInfo | null) {
51     super();
52
53   }
54 }
55
56 export class SetAllMediatorServerConfigurations extends BaseAction {
57   /**
58    * Initializes a new instance of this class.
59    */
60   constructor(public allConfigurations: MediatorConfigResponse[] | null) {
61     super();
62
63   }
64 }
65
66 export class SetMediatorServerSupportedDevices extends BaseAction {
67   /**
68    * Initializes a new instance of this class.
69    */
70   constructor(public devices: MediatorServerDevice[] | null) {
71     super();
72
73   }
74 }
75
76 export class SetMediatorServerReachable extends BaseAction {
77   constructor(public isReachable: boolean) {
78     super();
79   }
80 }
81
82 export const initializeMediatorServerAsyncActionCreator = (serverId: string) => (dispatch: Dispatch) => {
83   dispatch(new SetMediatorServerBusy(true));
84   mediatorService.getMediatorServerById(serverId).then(mediatorServer => {
85     if (!mediatorServer) {
86       dispatch(new SetMediatorServerBusy(false));
87       dispatch(new AddSnackbarNotification({ message: `Error loading mediator server [${serverId}]`, options: { variant: 'error' } }));
88       dispatch(new NavigateToApplication("mediator"));
89       return;
90     }
91
92     dispatch(new SetMediatorServerInfo(mediatorServer.id, mediatorServer.name, mediatorServer.url));
93
94     Promise.all([
95       mediatorService.getMediatorServerAllConfigs(mediatorServer.id),
96       mediatorService.getMediatorServerSupportedDevices(mediatorServer.id),
97       mediatorService.getMediatorServerVersion(mediatorServer.id)
98     ]).then(([configurations, supportedDevices, versionInfo]) => {
99       if (configurations === null && supportedDevices === null && versionInfo === null) {
100         dispatch(new SetMediatorServerReachable(false));
101       } else {
102         dispatch(new SetMediatorServerReachable(true));
103       }
104       dispatch(new SetAllMediatorServerConfigurations(configurations));
105       dispatch(new SetMediatorServerSupportedDevices(supportedDevices));
106       dispatch(new SetMediatorServerVersion(versionInfo));
107       dispatch(new SetMediatorServerBusy(false));
108     }).catch(error => {
109       dispatch(new SetMediatorServerReachable(false));
110       dispatch(new SetMediatorServerBusy(false));
111     });
112   });
113 };
114