Add data-provider
[ccsdk/features.git] / sdnr / wt / odlux / apps / mediatorApp / src / handlers / mediatorServerHandler.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 { XmlFileInfo, MediatorConfig, BusySymbol, MediatorConfigResponse, MediatorServerDevice } from "../models/mediatorServer";
19 import { IActionHandler } from "../../../../framework/src/flux/action";
20 import { SetMediatorServerVersion, SetMediatorServerInfo, SetAllMediatorServerConfigurations, SetMediatorServerBusy, SetMediatorServerSupportedDevices } from "../actions/mediatorServerActions";
21 import { SetMediatorBusyByName, UpdateMediatorConfig, AddMediatorConfig, RemoveMediatorConfig } from "../actions/mediatorConfigActions";
22
23 export type MediatorServerState = {
24   busy: boolean;
25   name: string | null;
26   url: string | null;
27   id: string | null;
28   serverVersion: string | null;
29   mediatorVersion: string | null;
30   nexmls: XmlFileInfo[];
31   configurations: MediatorConfigResponse[];
32   supportedDevices: MediatorServerDevice[];
33 }
34
35 const mediatorServerInit: MediatorServerState = {
36   busy: false,
37   name: null,
38   url: null,
39   id: null,
40   serverVersion: null,
41   mediatorVersion: null,
42   nexmls: [],
43   configurations: [],
44   supportedDevices: []
45 }
46
47 export const mediatorServerHandler: IActionHandler<MediatorServerState> = (state = mediatorServerInit, action) => {
48   if (action instanceof SetMediatorServerBusy) {
49     state = {
50       ...state,
51       busy: action.isBusy
52     };
53   } else if (action instanceof SetMediatorServerInfo) {
54     state = {
55       ...state,
56       name: action.name,
57       url: action.url,
58       id: action.id,
59     };
60   } else if (action instanceof SetMediatorServerVersion) {
61     state = {
62       ...state,
63       serverVersion: action.versionInfo && action.versionInfo.server,
64       mediatorVersion: action.versionInfo && action.versionInfo.mediator,
65       nexmls: action.versionInfo && [...action.versionInfo.nexmls] || [],
66     };
67   } else if (action instanceof SetAllMediatorServerConfigurations) {
68     state = {
69       ...state,
70       configurations: action.allConfigurations && action.allConfigurations.map(config => ({ ...config, busy: false })) || [],
71     };
72   } else if (action instanceof SetMediatorServerSupportedDevices) {
73     state = {
74       ...state,
75       supportedDevices: action.devices || [],
76     };
77   } else if (action instanceof SetMediatorBusyByName) {
78     const index = state.configurations.findIndex(config => config.Name === action.name);
79     if (index > -1) state = {
80       ...state,
81       configurations: [
82         ...state.configurations.slice(0, index),
83         { ...state.configurations[index], [BusySymbol]: action.isBusy },
84         ...state.configurations.slice(index + 1)
85       ]
86     };
87   } else if (action instanceof AddMediatorConfig) {
88     state = {
89       ...state,
90       configurations: [
91         ...state.configurations,
92         action.mediatorConfig
93       ]
94     };
95   } else if (action instanceof UpdateMediatorConfig) {
96     const index = state.configurations.findIndex(config => config.Name === action.name);
97     if (index > -1) state = {
98       ...state,
99       configurations: [
100         ...state.configurations.slice(0, index),
101         { ...action.mediatorConfig, [BusySymbol]: state.configurations[index][BusySymbol] },
102         ...state.configurations.slice(index + 1)
103       ]
104     };
105   } else if (action instanceof RemoveMediatorConfig) {
106     const index = state.configurations.findIndex(config => config.Name === action.name);
107     if (index > -1) state = {
108       ...state,
109       configurations: [
110         ...state.configurations.slice(0, index),
111         ...state.configurations.slice(index + 1)
112       ]
113     };
114   }
115   return state;
116