Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / 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, SetMediatorServerReachable } 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   isReachable: boolean;
34 }
35
36 const mediatorServerInit: MediatorServerState = {
37   busy: false,
38   name: null,
39   url: null,
40   id: null,
41   serverVersion: null,
42   mediatorVersion: null,
43   nexmls: [],
44   configurations: [],
45   supportedDevices: [],
46   isReachable: true
47 }
48
49 export const mediatorServerHandler: IActionHandler<MediatorServerState> = (state = mediatorServerInit, action) => {
50   if (action instanceof SetMediatorServerBusy) {
51     state = {
52       ...state,
53       busy: action.isBusy
54     };
55   } else if (action instanceof SetMediatorServerInfo) {
56     state = {
57       ...state,
58       name: action.name,
59       url: action.url,
60       id: action.id,
61     };
62   } else if (action instanceof SetMediatorServerVersion) {
63     state = {
64       ...state,
65       serverVersion: action.versionInfo && action.versionInfo.server,
66       mediatorVersion: action.versionInfo && action.versionInfo.mediator,
67       nexmls: action.versionInfo && [...action.versionInfo.nexmls] || [],
68     };
69   } else if (action instanceof SetAllMediatorServerConfigurations) {
70     state = {
71       ...state,
72       configurations: action.allConfigurations && action.allConfigurations.map(config => ({ ...config, busy: false })) || [],
73     };
74   } else if (action instanceof SetMediatorServerSupportedDevices) {
75     state = {
76       ...state,
77       supportedDevices: action.devices || [],
78     };
79   } else if (action instanceof SetMediatorBusyByName) {
80     const index = state.configurations.findIndex(config => config.Name === action.name);
81     if (index > -1) state = {
82       ...state,
83       configurations: [
84         ...state.configurations.slice(0, index),
85         { ...state.configurations[index], [BusySymbol]: action.isBusy },
86         ...state.configurations.slice(index + 1)
87       ]
88     };
89   } else if (action instanceof AddMediatorConfig) {
90     state = {
91       ...state,
92       configurations: [
93         ...state.configurations,
94         action.mediatorConfig
95       ]
96     };
97   } else if (action instanceof UpdateMediatorConfig) {
98     const index = state.configurations.findIndex(config => config.Name === action.name);
99     if (index > -1) state = {
100       ...state,
101       configurations: [
102         ...state.configurations.slice(0, index),
103         { ...action.mediatorConfig, [BusySymbol]: state.configurations[index][BusySymbol] },
104         ...state.configurations.slice(index + 1)
105       ]
106     };
107   } else if (action instanceof RemoveMediatorConfig) {
108     const index = state.configurations.findIndex(config => config.Name === action.name);
109     if (index > -1) state = {
110       ...state,
111       configurations: [
112         ...state.configurations.slice(0, index),
113         ...state.configurations.slice(index + 1)
114       ]
115     };
116   } else if( action instanceof SetMediatorServerReachable){
117     state = {...state, isReachable: action.isReachable}
118   }
119   return state;
120