-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
645 lines (603 loc) · 24 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
import _ = require("lodash");
import { ActiveReplication, CheckPointRecovery, Port, ComponentInfo, Connection, Data, ComponentInstanceExtended, ComponentInstance, Unit, Device, conn_protocol, UnitConnection, ResilienceInfo, DegradationInfo, SecConfig, ExtToolsConfig, NpmConfig, MigrationStatus, CheckPointRecoveryErrors, DebuggingConfiguration} from "./types";
export function checkParams(input: Record<string, unknown>, types: Record<string, "string" | "boolean" | "object" | "number" | "Array<object>">) {
const invalidParams = {}
for (const key of Object.keys(types)) {
const t = typeof input[key];
const tExpected = types[key];
if (t !== tExpected) {
invalidParams[key] = `'${input[key]}' is not of type '${tExpected}'.`;
}
}
return Object.keys(invalidParams).length > 0 ? invalidParams : undefined;
}
export function handleGenericInputErrors(types: Record<string, "string" | "boolean" | "object" | "number" | "Array<object>">, body: any, res: any) {
const invalidParams = checkParams(body, types)
if (invalidParams) {
res.status(400);
res.send({
"error": "faulty parameters",
"info": invalidParams
});
return true;
}
return false;
}
export function executionSitesFeasible(sites: Array<string>, data: Data): boolean {
for (const site of sites) {
if (!site) {
return false;
}
const unitNamesNames: string[] = Array.from(data.units, el => { return el.id });
if (!unitNamesNames.includes(site)) {
return false;
}
}
return true;
}
export function setExecutionSites(sites: Array<string>, name: string, data: Data): string[] | undefined {
let usedIndx = -1;
for (let i = 0; i < data.resilienceInfo.components.length; i++) {
if (data.resilienceInfo.components[i].id === name) {
usedIndx = i;
break;
}
}
if (usedIndx === -1) {
return undefined;
}
let component = data.resilienceInfo.components[usedIndx];
(<ActiveReplication>component.mechanisms).activeReplication.executionSites = sites;
data.resilienceInfo.components[usedIndx] = component;
return sites;
}
export function raiseInputError(msg: string, res: any) {
res.status(400);
res.send({
"error": "input error",
"info": msg,
});
}
export function getInstance(name: string, data: Data): ComponentInstance | undefined {
for (const l of Object.values(data.componentInstances)) {
const instance = _.find(l, (el) => el.name === name);
if (instance) {
return instance;
}
};
return undefined;
}
export function componentTypeUsed(type: string, data: Data): boolean {
for (const l of Object.keys(data.componentInstances)) {
if (l === type) {
return true;
}
};
return false;
}
export function componentHasActiveReplication(name: string, data: Data): boolean {
for(const comp of data.resilienceInfo.components) {
const activeReplicated = Object.getPrototypeOf(comp.mechanisms) === "ActiveReplication" ? true: false;
if(activeReplicated !== undefined && activeReplicated === true) {
if (comp.id === name) {
return true;
}
}
else if (comp.id === name) {
return true;
}
};
return false;
}
export function componentHasCheckpointRecovery(name: string, data: Data): boolean {
for(const comp of data.resilienceInfo.components) {
const checkpointRecovered = Object.getPrototypeOf(comp.mechanisms) === "CheckPointRecovery" ? true: false;
if(checkpointRecovered !== undefined && checkpointRecovered === true) {
if (comp.id === name) {
return true;
}
}
else if (comp.id === name) {
return true;
}
};
return false;
}
export function unitHasCheckpointRecovery(unitName: string, data: Data): CheckPointRecoveryErrors {
const feasibleUnits:Unit[] = data.units.filter(unit => unit.id === unitName);
let checkPointRecoveryErrors:CheckPointRecoveryErrors = {
unitNameUnique: true,
hasCheckPointing: false,
};
if (feasibleUnits.length !== 1) {
checkPointRecoveryErrors.unitNameUnique = false;
}
const unit:Unit = feasibleUnits[0];
unit.components.flatMap(comp => comp.instance).flatMap(inst => {
if(inst && componentHasCheckpointRecovery(inst?.name, data)) {
checkPointRecoveryErrors.hasCheckPointing = true;
}})
return checkPointRecoveryErrors;
}
export function strHasDirFormat(dir: string): boolean {
const re = /^\/|(\/[a-zA-Z0-9_-]+)+$/;
return dir.match(re) !== undefined;
}
export function addReplicatedComponent(faultModel:string, name: string, n: number, f: number, data: Data) {
const component = {
id: name,
mechanisms: {
activeReplication: {
faultModel: faultModel,
n: n,
f: f,
enabled: true,
executionSites: []
}
}
};
data.resilienceInfo.components.push(component);
}
export function setReplicationParameters(faultModel:string, name: string, n: number, f: number, data: Data) {
for (let i = 0; i < data.resilienceInfo.components.length; i++) {
const comp = data.resilienceInfo.components[i];
if (comp.id === name) {
const ar: ActiveReplication = <ActiveReplication>comp.mechanisms;
ar.activeReplication.faultModel = faultModel;
ar.activeReplication.n = n;
ar.activeReplication.f = f;
const resComp = {
id: name,
mechanisms: ar
}
data.resilienceInfo.components[i] = resComp;
}
}
}
export function setReplicationExecutionSite(type: string, executionSite: string, data: Data) {
for (let i = 0; i < data.resilienceInfo.components.length; i++) {
const comp = data.resilienceInfo.components[i];
if (comp.id === type) {
const ar: ActiveReplication = <ActiveReplication>comp.mechanisms;
(<ActiveReplication>data.resilienceInfo.components[i].mechanisms).activeReplication.executionSites.push(executionSite);
}
}
}
export function addCheckpointedComponent(type: string, data: Data) {
const component = {
id: type,
mechanisms: {
checkpointRecovery: {
recovery: {
enabled: true,
},
checkpoint: {
enabled: true,
},
}
}
};
data.resilienceInfo.components.push(component);
}
export function getUnitIndx(unit_name: string, data: Data): number | undefined {
for (let i = 0; i < data.units.length; i++) {
const unit = data.units[i];
if (unit.id == unit_name) {
return i;
}
}
return undefined;
}
export function getDeviceIndx(device_name: string, data: Data): number | undefined {
for (let i = 0; i < data.devices.length; i++) {
const device = data.devices[i];
if (device.deviceName === device_name) {
return i;
}
}
return undefined;
}
export function getComponentInfoOfInstance(name: string, data: Data): ComponentInfo | undefined {
for (const [type, l] of Object.entries(data.componentInstances)) {
const instance = _.find(l, (el) => el.name === name);
if (instance) {
return data.setupInfo.components[type];
}
};
return undefined;
}
export function arePortsCompatible(sourcePort: Port, targetPort: Port): boolean {
if(_.intersection(sourcePort.eventTypes, targetPort.eventTypes).length != sourcePort.eventTypes.length) {
console.log("source: " + sourcePort.eventTypes + ", target: " + targetPort.eventTypes);
return false;
}
return _.intersection(sourcePort.eventTypes, targetPort.eventTypes).length === sourcePort.eventTypes.length;
}
/**
* Compares two UnitConnections, but ignores mismatches in protocols.
* */
function isEqualProtoLess(conn1: UnitConnection, conn2: UnitConnection) {
return (conn1.sourceComponent === conn2.sourceComponent && conn1.sourcePort === conn2.sourcePort &&
conn1.targetComponent === conn2.targetComponent && conn1.targetPort === conn2.targetPort)
};
export function connectionExists(toChangeConnection: UnitConnection, data: Data): boolean {
for (const unit of data.units) {
for (const connection of unit.unitConnections) {
if (isEqualProtoLess(toChangeConnection, connection)) {
return true;
}
}
}
return false;
}
/**
* Changes the protocol of all UnitConnections in the given data to a given connection if source and target are the same.
*
* @param toChangeConnection the connection to match (includes the target protocol)
* @param data the data
* @returns true if successful
*/
export function changeProtocol(toChangeConnection: UnitConnection, data: Data): boolean {
// find out which units are involved in the connection
let relevantUnitNames: string[] = [];
for (const unit of data.units) {
for (const connection of unit.unitConnections) {
if (isEqualProtoLess(toChangeConnection, connection)) {
relevantUnitNames.push(unit.id);
}
}
}
// check if involved devices support the desired protocol
for (const dev of data.devices) {
for (const unitName of dev.units) {
if (relevantUnitNames.includes(unitName)) {
if (!dev.protocols.includes(conn_protocol[toChangeConnection.protocol])) {
return false;
}
}
}
}
// changes protocols of involved connections
for (let i = 0; i < data.units.length; i++) {
const unit: Unit = data.units[i];
if (relevantUnitNames.includes(unit.id)) {
for (let j = 0; j < unit.unitConnections.length; j++) {
const connection: UnitConnection = unit.unitConnections[j];
if (isEqualProtoLess(toChangeConnection, connection)) {
data.units[i].unitConnections[j].protocol = toChangeConnection.protocol;
}
}
}
}
return true;
}
function generateExtToolsConfig(extToolsConfig_:ExtToolsConfig) {
let extToolsConfig:ExtToolsConfig = {};
if(extToolsConfig_.honeyPots && extToolsConfig_.honeyPots.length > 0) {
extToolsConfig.honeyPots = extToolsConfig_.honeyPots;
}
if(extToolsConfig_.debugFrontendInfos && extToolsConfig_.debugFrontendInfos.portToOpenContainer && extToolsConfig_.debugFrontendInfos.portToOpenHost && extToolsConfig_.debugFrontendInfos.hostName) {
extToolsConfig.debugFrontendInfos = extToolsConfig_.debugFrontendInfos;
}
if(extToolsConfig_.useIds && extToolsConfig_.useIds === true) {
extToolsConfig.useIds = true;
} else {
extToolsConfig.useIds = false;
}
return extToolsConfig;
}
export function generateId(n) {
var s = "abcdefghijklmnopqrstuvwxyz0123456789";
var rndStr = Array(n).join().split(',').map(function () { return s.charAt(Math.floor(Math.random() * s.length)); }).join('');
return rndStr;
}
function checkRelevantConn(connection: Connection, unitConnections: UnitConnection[], relCompName: string | undefined) {
if (!relCompName) {
return false;
}
for (const unitConnection of unitConnections) {
if (connection.sourceComponent != relCompName &&
connection.targetComponent != relCompName) {
return false;
}
if (connection.sourceComponent === unitConnection.sourceComponent && connection.sourcePort === unitConnection.sourcePort &&
connection.targetComponent === unitConnection.targetComponent && connection.targetPort === unitConnection.targetPort) {
return false;
}
}
return true;
}
function generateOutputConnections(componentInstanceExtended: ComponentInstanceExtended, unitConnections: UnitConnection[], connections: Connection[]): UnitConnection[] {
let conns: UnitConnection[] = [];
let relevantUnitConns = unitConnections.filter(el => {
return el.componentOnUnit === componentInstanceExtended.instance?.name;
})
for (const relevantUnitConn of relevantUnitConns) {
const conn = {
sourceComponent: relevantUnitConn.sourceComponent,
sourcePort: relevantUnitConn.sourcePort,
targetComponent: relevantUnitConn.targetComponent,
targetPort: relevantUnitConn.targetPort,
protocol: relevantUnitConn.protocol
}
conns.push(conn);
}
// also need connections not leaving unit
for (const connection of connections) {
if (!checkRelevantConn(connection, conns, componentInstanceExtended.instance?.name)) {
continue;
}
const conn = {
sourceComponent: connection.sourceComponent,
sourcePort: connection.sourcePort,
targetComponent: connection.targetComponent,
targetPort: connection.targetPort,
protocol: conn_protocol.ON_UNIT
}
conns.push(conn);
}
return conns;
}
function generateOutputComponents(componentInstancesExtended: ComponentInstanceExtended[], unitConnections: UnitConnection[], connections: Connection[]) {
let comps: any[] = [];
for (const componentInstanceExtended of componentInstancesExtended) {
const comp = {
type: componentInstanceExtended.type?.name,
id: componentInstanceExtended.instance?.name,
ports: componentInstanceExtended.type?.ports,
connections: generateOutputConnections(componentInstanceExtended, unitConnections, connections),
startState: componentInstanceExtended.instance?.startState,
startStateGenerator: componentInstanceExtended.instance?.startStateGenerator,
startStateArgs: componentInstanceExtended.instance?.startStateArgs
}
comps.push(comp);
}
return comps;
}
function generateOutputUnits(unitNames: string[], data: Data) {
let units: any[] = [];
for (const unitName of unitNames) {
const indx = getUnitIndx(unitName, data);
if (indx === undefined) {
continue;
}
const unit = data.units[indx];
const unit_transformed = {
id: unit.id,
components: generateOutputComponents(unit.components, unit.unitConnections, data.connections),
shadowModeConfig: unit.shadowModeConfig,
pinnedNodePort: unit.pinnedNodePort,
}
units.push(unit_transformed);
}
return units;
}
// init -> does not check for duplicate, sets default: REST
export function initUnitConnections(device_indx: number, unit_indx: number, data: Data) {
const unitConsidered: Unit = data.units[unit_indx];
const deviceConsidered: Device = data.devices[device_indx];
for (let i = 0; i < data.units.length; i++) {
if (i === unit_indx) {
continue;
}
const unit = data.units[i];
for (const connection of data.connections) {
const sourceComp = connection.sourceComponent;
const targetComp = connection.targetComponent;
for (const compExtendedConsidered of unitConsidered.components) {
for (const compExtended of unit.components) {
if ((compExtendedConsidered.instance?.name === sourceComp && compExtended.instance?.name === targetComp) ||
(compExtendedConsidered.instance?.name === targetComp && compExtended.instance?.name === sourceComp)) {
const unitConnectionBase: UnitConnection = {
sourceComponent: sourceComp,
sourcePort: connection.sourcePort,
targetComponent: targetComp,
targetPort: connection.targetPort,
protocol: conn_protocol.REST
}
if (compExtendedConsidered.instance?.name === sourceComp) {
data.units[unit_indx].unitConnections.push({
componentOnUnit: sourceComp,
...unitConnectionBase
});
} else {
data.units[unit_indx].unitConnections.push({
componentOnUnit: targetComp,
...unitConnectionBase
});
}
// TODO: do not use REST as default but some of intersect proto of two devices of mapped units
// -> intersect ===0 -> if(...) return false;
}
}
}
}
}
return true;
}
function generateOutputElements(data: Data) {
let elems: any[] = [];
for (const dev of data.devices) {
if (dev.units.length === 0) {
continue;
}
const elem = {
device_name: dev.deviceName,
id: generateId(6),
architecture: dev.architecture,
protocols: dev.protocols,
location: dev.location,
units: generateOutputUnits(dev.units, data),
}
elems.push(elem);
}
return elems;
}
export function instanceNameUnique(name: string, data: Data): boolean {
let foundInstances: string[] = [];
for (const instances of Object.values(data.componentInstances)) {
for (const instance of instances) {
if (name === instance.name) {
foundInstances.push(name);
}
}
}
return foundInstances.length == 1 ? true : false;
}
export function transformOutput(data: Data) {//: ConfigurationOutput {
return {
elements: generateOutputElements(data),
resilienceLibrary: {
directoryPath: data.resilienceInfo.resilienceLibraryPath,
},
resilienceConfiguration: data.resilienceInfo.components.length > 0 ? {
components: data.resilienceInfo.components
} : {},
securityConfiguration: data.securityConfig ? data.securityConfig : {},
extToolsConfiguration: generateExtToolsConfig(data.extToolsConfig),
degradationConfiguration: data.degradationInfo,
npmConfiguration: data.npmConfig,
migrationStatus: data.migrationStatus,
debuggingConfiguration: data.debuggingConfiguration,
}
}
// todo: congiguration type bauen & Fehlerbehandlung falls einglesener state inkompatibel
export function transformInput(configuration:any):Data {
let data:Data;
let setupInfo:typeof data.setupInfo = {components: {}};
let devices:Device[] = [];
let connections:Connection[] = [];
let units:Unit[] = [];
let resilienceInfo: ResilienceInfo;
let securityConfig: SecConfig;
let extToolsConfig: ExtToolsConfig;
let npmConfig: NpmConfig;
let degradationInfo: DegradationInfo;
let migrationStatus:MigrationStatus;
let debuggingConfiguration:DebuggingConfiguration;
let components:typeof data.setupInfo.components = {};
let componentInstances: Record<string, ComponentInstance[]> = {};
let startStates:typeof data.setupInfo.startStates = {};
let startStateGenerators:typeof data.setupInfo.stateGenerators = new Map<string,object>();
configuration.elements.flatMap(el => el.units).flatMap(unit => unit.components).forEach(comp => {
components[comp.type] = {
name: comp.type,
ports: comp.ports,
tsType: "Component"
}
const compInstance:ComponentInstance = {
name: <string>comp.id,
startState: comp.startState,
startStateGenerator: comp.startStateGenerator,
startStateArgs: comp.startStateArgs,
}
const instances = componentInstances[comp.type];
if(!instances) {
componentInstances[comp.type] = [];
componentInstances[comp.type].push(compInstance);
} else {
instances.push(compInstance);
}
if(comp.startState) {
if(startStates) {
startStates[<string>comp.type + "StartState" + comp.startState ? "" : "Empty"] = {
state: comp.startState,
events: [],
tsType: "State",
}}
}
if(comp.startStateGenerator) {
if(startStateGenerators) {
startStateGenerators.set(<string>comp.startStateGenerator, comp.startStateArgs)
}
}
comp.connections.forEach(conn => {
if (!connections.find(item => {
if(conn.sourceComponent == item.sourceComponent && conn.sourcePort == item.sourcePort
&& conn.targetComponent == item.targetComponent && conn.targetPort == item.targetPort) {
return true;
}
})) {
connections.push(conn);
}
});
})
setupInfo.components = components;
setupInfo.startStates = startStates;
setupInfo.stateGenerators = startStateGenerators;
for (const elem of configuration.elements) {
const dev:Device = {
deviceName: elem.device_name,
architecture: elem.architecture,
protocols: elem.protocols,
location: elem.location,
units: elem.units.map(unit =>
unit.id
),
}
devices.push(dev);
}
units = configuration.elements.flatMap(el => el.units).map(unit => {
const unitConnections:UnitConnection[] = unit.components.flatMap( comp => comp.connections);
const unitComponentNames:string[] = unit.components.flatMap(component => component.id);
for(const conn of unitConnections) {
for(const name of unitComponentNames) {
if(name === conn.sourceComponent || name === conn.targetComponent) {
conn.componentOnUnit = name;
break;
}
}
}
let insts:typeof componentInstances = {};
unit.components.forEach(comp => {
componentInstances[comp.type].forEach(inst => {
if(comp.id === inst.name) {
if(!insts[comp.type]) {
insts[comp.type] = [];
}
insts[comp.type].push(inst);
}
})
});
return {
id: unit.id,
components: unit.components.map(comp => {
return {
type: {
name: comp.type,
ports: comp.ports,
tsType: "Component"
},
// todo: errror-handling
instance: insts[comp.type] ? insts[comp.type][0] ? insts[comp.type][0] : undefined: undefined,
}
}),
unitConnections: unitConnections,
shadowModeConfig: unit.shadowModeConfig,
}
});
resilienceInfo = {
resilienceLibraryPath: configuration.resilienceLibrary.directoryPath,
components: configuration.resilienceConfiguration.components,
}
securityConfig = configuration.securityConfiguration;
extToolsConfig = configuration.extToolsConfiguration;
degradationInfo = configuration.degradationConfiguration;
npmConfig = configuration.npmConfiguration;
migrationStatus = configuration.migrationStatus;
debuggingConfiguration = configuration.DebuggingConfiguration;
return {
setupInfo: setupInfo,
componentInstances: componentInstances,
connections: connections,
units: units,
devices: devices,
resilienceInfo: resilienceInfo,
securityConfig: securityConfig,
extToolsConfig: extToolsConfig,
npmConfig: npmConfig,
degradationInfo: degradationInfo,
migrationStatus: migrationStatus,
debuggingConfiguration: debuggingConfiguration,
}
}