forked from srl-labs/vscode-containerlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagerSocketDataEnrichment.js
More file actions
184 lines (160 loc) · 7.07 KB
/
managerSocketDataEnrichment.js
File metadata and controls
184 lines (160 loc) · 7.07 KB
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
// ./src/topoViewer/webview-ui/html-static/js/managerSocketDataEnrichment.js
//
// +------------------------------------------------------------------+
// | Socket.io/vscode message Feed: "clab-tree-provider-data" |
// | (Extension Backend sends lab data via socket.io/vscode message) |
// +------------------------------------------------------------------+
// |
// v
// +-----------------------+
// | Lab Data |
// | (clabTreeProviderData)|
// +-----------------------+
// | |
// v v
// +-------------------------------+ +-------------------------------+
// | socketDataEncrichmentLink() | | socketDataEncrichmentNode() |
// | (Enriches Cytoscape Edges) | | (Enriches Cytoscape Nodes) |
// +-------------------------------+ +-------------------------------+
// | |
// | Updates edge data (MAC, MTU, | Updates node data (state, image, longname)
// | type for source/target) |
// v v
// +-----------------------+ +-----------------------+
// | Cytoscape Edges | | Cytoscape Nodes |
// +-----------------------+ +-----------------------+
'use strict';
/**
* Enriches Cytoscape edge elements with interface attributes (MAC, MTU, and type)
* from container interface data provided in labData.
*
* This function processes backend lab data to extract interface information from
* each container inside a lab whose name matches `globalLabName`. It builds a mapping
* from a composite key (`labName::nodeName::interfaceLabel`) to interface details.
*
* Then it iterates over all Cytoscape edges and updates each edge's source or target
* data if it matches the composite key's node name and endpoint.
*
* Fields added to Cytoscape edge:
* - sourceMac / targetMac
* - sourceMtu / targetMtu
* - sourceType / targetType
*
* @param {Object} labData - Raw lab data from the backend. Should follow the structure of
* the "clab-tree-provider-data" feed from the extension backend.
*/
function socketDataEncrichmentLink(labData) {
const linkMap = new Map();
console.log(`debug labData:`, labData);
console.log(`debug labData.name`, labData.name);
console.log(`debug globalLabName`, globalLabName);
// Build interface key mapping for the current lab
Object.values(labData).forEach(lab => {
if (lab.name !== globalLabName || !Array.isArray(lab.containers)) return;
lab.containers.forEach(container => {
if (typeof container.label !== 'string' || !Array.isArray(container.interfaces)) return;
const nodeName = container.label.split(lab.name)[1]?.replace(/^-/, '') || container.label;
container.interfaces.forEach(iface => {
const key = `${lab.name}::${nodeName}::${iface.label}`;
linkMap.set(key, { mac: iface.mac, mtu: iface.mtu, type: iface.type });
});
console.log(`Enriched link data for node: ${nodeName} with interfaces:`, container.interfaces);
console.log(`Enriched link map data:`, linkMap);
});
});
// Compute prefix safely
let assignedPrefixLabName;
switch (true) {
case typeof globalPrefixName === "string" && globalPrefixName.trim() === "undefined":
assignedPrefixLabName = `clab-${globalLabName}-`;
break;
case typeof globalPrefixName === "string" && globalPrefixName.trim() !== "":
assignedPrefixLabName = `${globalPrefixName.trim()}-${globalLabName}-`;
break;
default:
assignedPrefixLabName = null;
break;
}
// Enrich edges
linkMap.forEach((iface, key) => {
const [, nodeName, endpoint] = key.split('::');
cy.edges().forEach(edge => {
const data = edge.data();
// Safely build clabSourceLongName and clabTargetLongName
const clabSourceLongName = assignedPrefixLabName
? `${assignedPrefixLabName}${data.source}`
: data.source;
const clabTargetLongName = assignedPrefixLabName
? `${assignedPrefixLabName}${data.target}`
: data.target;
const updatedExtraData = {
...edge.data('extraData'),
clabSourceLongName,
clabTargetLongName
};
edge.data('extraData', updatedExtraData);
// Enrich with interface details if matched
if (data.source === nodeName && data.sourceEndpoint === endpoint) {
edge.data({ sourceMac: iface.mac, sourceMtu: iface.mtu, sourceType: iface.type });
}
if (data.target === nodeName && data.targetEndpoint === endpoint) {
edge.data({ targetMac: iface.mac, targetMtu: iface.mtu, targetType: iface.type });
}
console.log(`Edge data after enrichment:`, edge.data());
});
});
}
/**
* Enriches Cytoscape node elements with container attributes (state, image, longname, and management IPs)
* extracted from the lab data.
*
* This function processes backend lab data and targets the lab whose name matches `globalLabName`.
* It constructs a mapping between each container's longname and its corresponding metadata.
*
* Then it iterates over all Cytoscape nodes and, if the node's `extraData.shortname` matches
* a container `longname`, it updates the node's `extraData` with:
* - state: container's operational state (e.g. running, exited)
* - image: container image used (e.g. alpine, sros)
* - longname: container's long form name (e.g. clab-demo-r1)
* - mgmtIpv4Address: management IPv4 address
* - mgmtIpv6Address: management IPv6 address
*
* @param {Object} labData - Raw lab data from the backend. Expected to match
* the "clab-tree-provider-data" format.
*/
function socketDataEncrichmentNode(labData) {
const nodeMap = new Map();
// Build node mapping from container longname -> metadata
Object.values(labData).forEach(lab => {
if (lab.name !== globalLabName || !Array.isArray(lab.containers)) return;
lab.containers.forEach(container => {
if (typeof container.label !== 'string') return;
const nodeData = {
labname: lab.name,
state: container.state,
image: container.image,
longname: container.name,
mgmtIpv4Address: container.v4Address,
mgmtIpv6Address: container.v6Address,
};
nodeMap.set(container.name, nodeData);
});
});
// Enrich each Cytoscape node that matches by shortname === longname
nodeMap.forEach((nodeData, longname) => {
cy.nodes().forEach(node => {
const shortname = node.data()?.extraData?.shortname;
if (shortname === longname) {
const updatedExtraData = {
...node.data('extraData'),
state: nodeData.state,
image: nodeData.image,
longname,
mgmtIpv4Address: nodeData.mgmtIpv4Address,
mgmtIpv6Address: nodeData.mgmtIpv6Address,
};
node.data('extraData', updatedExtraData);
}
});
});
}