2018-04-06 11:02:18 +00:00
|
|
|
import {
|
|
|
|
Actions,
|
|
|
|
ActionType,
|
|
|
|
} from './discover.action';
|
|
|
|
|
|
|
|
import {
|
|
|
|
State,
|
|
|
|
initialState,
|
|
|
|
} from './discover.state';
|
|
|
|
|
|
|
|
import {
|
|
|
|
Zone,
|
|
|
|
Host,
|
|
|
|
Port,
|
|
|
|
Service,
|
|
|
|
} from '../../model';
|
|
|
|
|
|
|
|
// import * as _ 'lodash';
|
|
|
|
|
|
|
|
export function reducer(state = initialState, action: Actions): State {
|
|
|
|
switch (action.type) {
|
2018-04-19 15:15:20 +00:00
|
|
|
case ActionType.DiscoveryStart: {
|
2018-04-23 12:31:16 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isStart: true
|
|
|
|
};
|
2018-04-19 15:15:20 +00:00
|
|
|
}
|
|
|
|
case ActionType.DiscoveryStop: {
|
2018-04-23 12:31:16 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isEnd: true
|
|
|
|
};
|
2018-04-19 15:15:20 +00:00
|
|
|
}
|
2018-04-06 11:02:18 +00:00
|
|
|
case ActionType.DiscoveredZone: {
|
|
|
|
const zone: Zone = <Zone>action.payload;
|
|
|
|
const zones: Map<string, Zone> = null === state.zones ? new Map() : state.zones;
|
|
|
|
|
|
|
|
zones.set(zone.network, zone);
|
|
|
|
|
|
|
|
const newZones: Map<string, Zone> = new Map();
|
|
|
|
|
|
|
|
zones.forEach(function(value, key) {
|
|
|
|
newZones.set(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
zones : newZones,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case ActionType.DiscoveredHost: {
|
|
|
|
const host: Host = <Host>action.payload;
|
|
|
|
let zone = null;
|
|
|
|
|
|
|
|
let zones: Map<string, Zone> = state.zones;
|
|
|
|
|
|
|
|
if (zones === undefined || zones === null) {
|
|
|
|
zones = new Map();
|
|
|
|
zone = host.zone;
|
|
|
|
// zones.set(zone.network, zone);
|
|
|
|
} else {
|
|
|
|
zone = zones.get(host.zone.network);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (undefined === zone) {
|
|
|
|
// console.error(`Discovery.discoveredHost: Zone[${host.zone.network}] is not exist`);
|
|
|
|
zone = new Map();
|
|
|
|
}
|
|
|
|
if (null === zone.hosts || undefined === zone.hosts) {
|
|
|
|
zone.hosts = new Map();
|
|
|
|
zone.hosts.set(host.ip, host);
|
|
|
|
} else {
|
|
|
|
if (zone.hosts.has(host.ip) === false) {
|
|
|
|
zone.hosts.set(host.ip, host);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zones.set(zone.network, zone);
|
|
|
|
|
|
|
|
const newZones: Map<string, Zone> = new Map();
|
|
|
|
|
|
|
|
zones.forEach(function(value, key) {
|
|
|
|
newZones.set(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
zones: newZones
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
case ActionType.DiscoveredPort: {
|
|
|
|
const port: Port = <Port>action.payload;
|
|
|
|
let zone = state.zones.get(port.host.zone.network);
|
|
|
|
|
|
|
|
let zones: Map<string, Zone> = state.zones;
|
|
|
|
|
|
|
|
if (zones === undefined || zones === null) {
|
|
|
|
zones = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zone === undefined || zone === null) {
|
|
|
|
zone = port.host.zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (undefined === zone) {
|
|
|
|
// console.error(`Discovery.DiscoveredPort: Zone[${port.host.zone.network}] is not exist`);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (null === zone.hosts || undefined === zone.hosts.get(port.host.ip)) {
|
|
|
|
// console.error(`Discovery.DiscoveredPort: Host[${port.host.ip}] is not exist`);
|
|
|
|
// return state;
|
|
|
|
// }
|
|
|
|
|
|
|
|
if (zone.hosts === undefined || zone.hosts === null) {
|
|
|
|
zone.hosts = new Map();
|
|
|
|
}
|
|
|
|
let host: Host = null;
|
|
|
|
host = zone.hosts.get(port.host.ip);
|
|
|
|
|
|
|
|
if (host === undefined || host === null) {
|
|
|
|
host = port.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === host.ports || undefined === host.ports) {
|
|
|
|
host.ports = new Map();
|
|
|
|
host.ports.set(port.portNumber, port);
|
|
|
|
} else {
|
|
|
|
if (host.ports.has(port.portNumber) === false) {
|
|
|
|
host.ports.set(port.portNumber, port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
zone.hosts.set(host.ip, host);
|
|
|
|
zones.set(zone.network, zone);
|
|
|
|
|
|
|
|
const newZones: Map<string, Zone> = new Map();
|
|
|
|
|
|
|
|
zones.forEach(function(value, key) {
|
|
|
|
newZones.set(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
zones: newZones
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
case ActionType.DiscoveredService: {
|
|
|
|
const service: Service = <Service>action.payload;
|
|
|
|
let zone = state.zones.get(service.port.host.zone.network);
|
|
|
|
|
|
|
|
let zones: Map<string, Zone> = state.zones;
|
|
|
|
|
|
|
|
if (zones === undefined || zones === null) {
|
|
|
|
zones = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zone === undefined || zone === null) {
|
|
|
|
zone = service.port.host.zone;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zone.hosts === undefined || zone.hosts === null) {
|
|
|
|
zone.hosts = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (undefined === zone) {
|
|
|
|
// console.error(`Discovery.DiscoveredService: Zone[${service.port.host.zone.network}] is not exist`);
|
|
|
|
// }
|
|
|
|
// if (null === zone.hosts || undefined === zone.hosts.get(service.port.host.ip)) {
|
|
|
|
// console.error(`Discovery.DiscoveredPort: Host[${service.port.host.ip}] is not exist`);
|
|
|
|
// }
|
2018-04-23 12:31:16 +00:00
|
|
|
let host: Host = null;
|
|
|
|
host = zone.hosts.get(service.port.host.ip);
|
2018-04-06 11:02:18 +00:00
|
|
|
|
2018-04-23 12:31:16 +00:00
|
|
|
if (host === undefined || host === null) {
|
|
|
|
zone.hosts.set(service.port.host.ip, service.port.host);
|
|
|
|
host = service.port.host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (undefined === host.ports || null === host.ports) {
|
2018-04-06 11:02:18 +00:00
|
|
|
host.ports = new Map();
|
|
|
|
host.ports.set(service.port.portNumber, service.port);
|
|
|
|
}
|
|
|
|
|
|
|
|
const port: Port = host.ports.get(service.port.portNumber);
|
|
|
|
|
2018-04-23 12:31:16 +00:00
|
|
|
if ( undefined === port.services || null === port.services) {
|
2018-04-06 11:02:18 +00:00
|
|
|
port.services = new Map();
|
|
|
|
}
|
|
|
|
port.services.set(service.serviceName, service);
|
|
|
|
|
|
|
|
host.ports.set(service.port.portNumber, port);
|
|
|
|
zone.hosts.set(host.ip, host);
|
|
|
|
zones.set(zone.network, zone);
|
|
|
|
|
|
|
|
const newZones: Map<string, Zone> = new Map();
|
|
|
|
|
|
|
|
zones.forEach(function(value, key) {
|
|
|
|
newZones.set(key, value);
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
zones: newZones
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
default: {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// function checkZone(state, pZone: Zone) : {
|
|
|
|
|
|
|
|
|
|
|
|
// }
|