/
ints_view.go
549 lines (506 loc) · 15.1 KB
/
ints_view.go
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
package integrationdiagram
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/anz-bank/sysl/pkg/cmdutils"
"github.com/anz-bank/sysl/pkg/sysl"
"github.com/anz-bank/sysl/pkg/syslutil"
)
const ArrowColorNone = "none"
const PumlHeader = `''''''''''''''''''''''''''''''''''''''''''
'' ''
'' AUTOGENERATED CODE -- DO NOT EDIT! ''
'' ''
''''''''''''''''''''''''''''''''''''''''''
`
const ComponentStart = `hide stereotype
scale max 16384 height
skinparam component {
BackgroundColor FloralWhite
BorderColor Black
ArrowColor Crimson
`
const StateStart = `left to right direction
scale max 16384 height
hide empty description
skinparam state {
BackgroundColor FloralWhite
BorderColor Black
ArrowColor Crimson
`
type IntsParam struct {
Apps []string
DrawableApps map[string]struct{}
Integrations []AppDependency
App *sysl.Application
Endpt *sysl.Endpoint
}
type Args struct {
Title string
Project string
Clustered bool
Epa bool
}
type IntsDiagramVisitor struct {
Mod *sysl.Module
StringBuilder *strings.Builder
DrawableApps map[string]struct{}
Symbols map[string]*cmdutils.Var
TopSymbols map[string]*_topVar
Project string
}
type _topVar struct {
TopLabel string
TopAlias string
}
type AppPair struct {
Self string
Target string
}
type ViewParams struct {
RestrictBy string
EndptAttrs map[string]*sysl.Attribute
HighLightColor string
ArrowColor string
IndirectArrowColor string
DiagramTitle string
}
func MakeIntsDiagramVisitor(
mod *sysl.Module, stringBuilder *strings.Builder,
drawableApps map[string]struct{}, project string,
) *IntsDiagramVisitor {
return &IntsDiagramVisitor{
Mod: mod,
StringBuilder: stringBuilder,
DrawableApps: drawableApps,
Symbols: map[string]*cmdutils.Var{},
TopSymbols: map[string]*_topVar{},
Project: project,
}
}
func (v *IntsDiagramVisitor) VarManagerForComponent(appName string, nameMap map[string]string) string {
if key, ok := nameMap[appName]; ok {
appName = key
}
if s, ok := v.Symbols[appName]; ok {
return s.Alias
}
i := len(v.Symbols)
alias := fmt.Sprintf("_%d", i)
fp := cmdutils.MakeFormatParser(v.Mod.Apps[v.Project].GetAttrs()["appfmt"].GetS())
attrs := cmdutils.GetApplicationAttrs(v.Mod, appName)
controls := cmdutils.GetSortedISOCtrlStr(attrs)
label := fp.LabelApp(appName, controls, attrs)
s := &cmdutils.Var{
Label: label,
Alias: alias,
}
v.Symbols[appName] = s
if _, ok := v.DrawableApps[appName]; ok {
fmt.Fprintf(v.StringBuilder, "[%s] as %s <<highlight>>\n", label, alias)
} else {
fmt.Fprintf(v.StringBuilder, "[%s] as %s\n", label, alias)
}
return s.Alias
}
func (v *IntsDiagramVisitor) VarManagerForTopState(appName string) string {
var alias, label string
if ts, ok := v.TopSymbols[appName]; ok {
return ts.TopAlias
}
i := len(v.TopSymbols)
alias = fmt.Sprintf("_%d", i)
fp := cmdutils.MakeFormatParser(v.Mod.Apps[v.Project].GetAttrs()["appfmt"].GetS())
attrs := cmdutils.GetApplicationAttrs(v.Mod, appName)
controls := cmdutils.GetSortedISOCtrlStr(attrs)
label = fp.LabelApp(appName, controls, attrs)
ts := &_topVar{
TopLabel: label,
TopAlias: alias,
}
v.TopSymbols[appName] = ts
if _, ok := v.DrawableApps[appName]; ok {
fmt.Fprintf(v.StringBuilder, "state \"%s\" as X%s <<highlight>> {\n", label, alias)
} else {
fmt.Fprintf(v.StringBuilder, "state \"%s\" as X%s {\n", label, alias)
}
return ts.TopAlias
}
func (v *IntsDiagramVisitor) VarManagerForEPA(name string) string {
var appName, alias, label string
attrs := map[string]string{}
appName = strings.Split(name, " : ")[0]
epName := strings.Split(name, " : ")[1]
if s, ok := v.Symbols[name]; ok {
return s.Alias
}
i := len(v.Symbols)
alias = fmt.Sprintf("_%d", i)
if v.Mod.Apps[appName].Endpoints[epName] != nil {
for k, v := range v.Mod.Apps[appName].Endpoints[epName].Attrs {
attrs["@"+k] = v.GetS()
}
}
attrs["appname"] = epName
fp := cmdutils.MakeFormatParser(v.Mod.Apps[v.Project].GetAttrs()["appfmt"].GetS())
label = fp.Parse(attrs)
s := &cmdutils.Var{
Label: label,
Alias: alias,
}
v.Symbols[name] = s
if _, ok := v.DrawableApps[appName]; ok {
fmt.Fprintf(v.StringBuilder, " state \"%s\" as %s <<highlight>>\n", label, alias)
} else {
fmt.Fprintf(v.StringBuilder, " state \"%s\" as %s\n", label, alias)
}
return s.Alias
}
func (v *IntsDiagramVisitor) BuildClusterForEPAView(deps []AppDependency, restrictBy string) {
clusters := map[string][]string{}
for _, dep := range deps {
appA := dep.Self.Name
appB := dep.Target.Name
epA := dep.Self.Endpoint
epB := dep.Target.Endpoint
_, okA := v.Mod.Apps[appA].Endpoints[epA].Attrs[restrictBy]
_, okB := v.Mod.Apps[appB].Endpoints[epB].Attrs[restrictBy]
if _, ok := v.Mod.Apps[appA].Attrs[restrictBy]; !ok && restrictBy != "" {
if _, ok := v.Mod.Apps[appB].Attrs[restrictBy]; !ok {
continue
}
}
if !okA && restrictBy != "" && !okB {
continue
}
clusters[appA] = append(clusters[appA], epA)
if appA != appB && !v.Mod.Apps[appA].Endpoints[epA].IsPubsub {
clusters[appA] = append(clusters[appA], epB+" client")
}
clusters[appB] = append(clusters[appB], epB)
}
keys := []string{}
for k := range clusters {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v.VarManagerForTopState(k)
strSet := syslutil.MakeStrSet(clusters[k]...)
for _, m := range strSet.ToSortedSlice() {
v.VarManagerForEPA(k + " : " + m)
}
v.StringBuilder.WriteString("}\n")
}
}
func (v *IntsDiagramVisitor) BuildClusterForIntsView(apps []string) map[string]string {
nameMap := map[string]string{}
clusters := map[string][]string{}
for _, v := range apps {
cluster := strings.Split(v, " :: ")
if len(cluster) > 1 {
clusters[cluster[0]] = append(clusters[cluster[0]], v)
}
}
for k, v := range clusters {
if len(v) <= 1 {
delete(clusters, k)
}
for _, s := range v {
nameMap[s] = strings.Split(s, " :: ")[1]
}
}
for k, apps := range clusters {
fmt.Fprintf(v.StringBuilder, "package \"%s\" {\n", k)
for _, n := range apps {
v.VarManagerForComponent(n, nameMap)
}
v.StringBuilder.WriteString("}\n")
}
return nameMap
}
func (v *IntsDiagramVisitor) GenerateEPAView(viewParams ViewParams, params *IntsParam) string {
v.StringBuilder.WriteString("@startuml\n")
if viewParams.DiagramTitle != "" {
fmt.Fprintf(v.StringBuilder, "title %s\n", viewParams.DiagramTitle)
}
v.StringBuilder.WriteString(StateStart)
if viewParams.HighLightColor != "" {
fmt.Fprintf(v.StringBuilder, " BackgroundColor<<highlight>> %s\n", viewParams.HighLightColor)
}
if viewParams.ArrowColor != "" {
fmt.Fprintf(v.StringBuilder, " ArrowColor %s\n", viewParams.ArrowColor)
}
if viewParams.IndirectArrowColor != "" && viewParams.IndirectArrowColor != ArrowColorNone {
fmt.Fprintf(v.StringBuilder, " ArrowColor<<indirect>> %s\n", viewParams.IndirectArrowColor)
}
v.StringBuilder.WriteString("}\n")
v.BuildClusterForEPAView(params.Integrations, viewParams.RestrictBy)
var processed []string
for _, dep := range params.Integrations {
appA := dep.Self.Name
appB := dep.Target.Name
epA := dep.Self.Endpoint
epB := dep.Target.Endpoint
_, restrictByAppA := v.Mod.Apps[appA].Attrs[viewParams.RestrictBy]
_, restrictByAppB := v.Mod.Apps[appB].Attrs[viewParams.RestrictBy]
_, restrictByEpA := v.Mod.Apps[appA].Endpoints[epA].Attrs[viewParams.RestrictBy]
_, restrictByEpB := v.Mod.Apps[appB].Endpoints[epB].Attrs[viewParams.RestrictBy]
if viewParams.RestrictBy != "" && !(restrictByAppA || restrictByAppB) {
continue
}
if viewParams.RestrictBy != "" && !(restrictByEpA || restrictByEpB) {
continue
}
matchApp := appB
matchEp := epB
label := ""
needsInt := appA != matchApp
pubSubSrcPtrns := syslutil.MakeStrSetFromAttr("patterns", v.Mod.Apps[appA].Endpoints[epA].Attrs)
attrs := map[string]string{}
for k, v := range dep.Statement.GetAttrs() {
attrs["@"+k] = v.GetS()
}
var ptrns string
var targetPatterns syslutil.StrSet
var srcPtrns syslutil.StrSet
if v.Mod.Apps[matchApp].Endpoints[matchEp].Attrs["patterns"] != nil {
targetPatterns = syslutil.MakeStrSetFromAttr("patterns", v.Mod.Apps[matchApp].Endpoints[matchEp].Attrs)
} else {
targetPatterns = syslutil.MakeStrSetFromAttr("patterns", v.Mod.Apps[matchApp].Attrs)
}
if dep.Statement.GetAttrs()["patterns"] != nil {
srcPtrns = syslutil.MakeStrSetFromAttr("patterns", dep.Statement.GetAttrs())
} else {
srcPtrns = pubSubSrcPtrns
}
ptrns = strings.Join(srcPtrns.ToSlice(), ", ") + " → " + strings.Join(targetPatterns.ToSlice(), ", ")
attrs["patterns"] = ptrns
if needsInt {
attrs["needs_int"] = strconv.FormatBool(needsInt)
}
fp := cmdutils.MakeFormatParser(params.App.Attrs["epfmt"].GetS())
label = fp.Parse(attrs)
flow := strings.Join([]string{appA, epB, appB, epB}, ".")
isPubSub := v.Mod.Apps[appA].Endpoints[epA].GetIsPubsub()
epBClient := epB + " client"
if appA != appB {
if label != "" {
label = " : " + label
}
if isPubSub {
fmt.Fprintf(
v.StringBuilder,
"%s -%s> %s%s\n", v.VarManagerForEPA(appA+" : "+epA),
"[#blue]",
v.VarManagerForEPA(appB+" : "+epB),
label,
)
} else {
color := ""
if viewParams.IndirectArrowColor == "" {
color = "[#silver]-"
} else {
color = "[#" + viewParams.IndirectArrowColor + "]-"
}
fmt.Fprintf(
v.StringBuilder,
"%s -%s> %s\n",
v.VarManagerForEPA(appA+" : "+epA),
color,
v.VarManagerForEPA(appA+" : "+epBClient),
)
if !StringInSlice(flow, processed) {
fmt.Fprintf(
v.StringBuilder,
"%s -%s> %s%s\n",
v.VarManagerForEPA(appA+" : "+epBClient),
"[#black]",
v.VarManagerForEPA(appB+" : "+epB),
label,
)
processed = append(processed, flow)
}
}
} else {
color := ""
if viewParams.IndirectArrowColor == "" {
color = "[#silver]-"
} else {
color = "[#" + viewParams.IndirectArrowColor + "]-"
}
fmt.Fprintf(
v.StringBuilder,
"%s -%s> %s%s\n",
v.VarManagerForEPA(appA+" : "+epA),
color,
v.VarManagerForEPA(appB+" : "+epB),
label,
)
}
}
v.StringBuilder.WriteString("@enduml")
return v.StringBuilder.String()
}
func (v *IntsDiagramVisitor) DrawIntsView(viewParams ViewParams, params *IntsParam, nameMap map[string]string) {
callsDrawn := map[AppPair]struct{}{}
if viewParams.EndptAttrs["view"].GetS() == "system" {
v.DrawSystemView(viewParams, params, nameMap)
} else {
for _, dep := range params.Integrations {
appA := dep.Self.Name
appB := dep.Target.Name
if appA == appB {
continue
}
appPair := AppPair{
Self: appA,
Target: appB,
}
var direct []string
if _, ok := params.DrawableApps[appA]; ok {
direct = append(direct, appA)
}
if _, ok := params.DrawableApps[appB]; ok {
direct = append(direct, appB)
}
if _, ok := callsDrawn[appPair]; !ok {
if len(direct) > 0 || direct != nil || viewParams.IndirectArrowColor != ArrowColorNone {
indirect := ""
if len(direct) == 0 {
indirect = " <<indirect>>"
}
fmt.Fprintf(
v.StringBuilder,
"%s --> %s%s\n",
v.VarManagerForComponent(appA, nameMap),
v.VarManagerForComponent(appB, nameMap),
indirect,
)
callsDrawn[appPair] = struct{}{}
}
}
}
for _, app := range params.Apps {
for _, mixin := range v.Mod.Apps[app].GetMixin2() {
mixinName := strings.Join(mixin.Name.Part, " :: ")
fmt.Fprintf(
v.StringBuilder,
"%s <|.. %s\n",
v.VarManagerForComponent(mixinName, nameMap),
v.VarManagerForComponent(app, nameMap),
)
}
}
}
}
func (v *IntsDiagramVisitor) DrawSystemView(viewParams ViewParams, params *IntsParam, nameMap map[string]string) {
callsDrawn := map[AppPair]struct{}{}
for _, dep := range params.Integrations {
appA := dep.Self.Name
appB := dep.Target.Name
if appA == appB {
continue
}
appPair := AppPair{
Self: appA,
Target: appB,
}
var direct []string
if _, ok := params.DrawableApps[appA]; ok {
direct = append(direct, appA)
}
if _, ok := params.DrawableApps[appB]; ok {
direct = append(direct, appB)
}
appA = strings.Split(appA, " :: ")[0]
appB = strings.Split(appB, " :: ")[0]
if _, ok := callsDrawn[appPair]; !ok {
if len(direct) > 0 || direct != nil || viewParams.IndirectArrowColor != ArrowColorNone {
indirect := ""
if len(direct) == 0 {
indirect = " <<indirect>>"
}
fmt.Fprintf(
v.StringBuilder,
"%s --> %s%s\n",
v.VarManagerForComponent(appA, nameMap),
v.VarManagerForComponent(appB, nameMap),
indirect,
)
callsDrawn[appPair] = struct{}{}
}
}
}
}
func (v *IntsDiagramVisitor) GenerateIntsView(args *Args, viewParams ViewParams, params *IntsParam) string {
v.StringBuilder.WriteString("@startuml\n")
if viewParams.DiagramTitle != "" {
fmt.Fprintf(v.StringBuilder, "title %s\n", viewParams.DiagramTitle)
}
v.StringBuilder.WriteString(ComponentStart)
if viewParams.HighLightColor != "" {
fmt.Fprintf(v.StringBuilder, " BackgroundColor<<highlight>> %s\n", viewParams.HighLightColor)
}
if viewParams.ArrowColor != "" {
fmt.Fprintf(v.StringBuilder, " ArrowColor %s\n", viewParams.ArrowColor)
}
if viewParams.IndirectArrowColor != "" && viewParams.IndirectArrowColor != ArrowColorNone {
fmt.Fprintf(v.StringBuilder, " ArrowColor<<indirect>> %s\n", viewParams.IndirectArrowColor)
}
v.StringBuilder.WriteString("}\n")
nameMap := map[string]string{}
if args.Clustered || viewParams.EndptAttrs["view"].GetS() == "clustered" {
nameMap = v.BuildClusterForIntsView(params.Apps)
}
v.DrawIntsView(viewParams, params, nameMap)
v.StringBuilder.WriteString("@enduml")
return v.StringBuilder.String()
}
func GenerateView(args *Args, params *IntsParam, mod *sysl.Module) string {
var stringBuilder strings.Builder
var titleParser *cmdutils.FormatParser
v := MakeIntsDiagramVisitor(mod, &stringBuilder, params.DrawableApps, args.Project)
restrictBy := ""
if params.Endpt.Attrs["restrict_by"] != nil {
restrictBy = params.Endpt.Attrs["restrict_by"].GetS()
}
appAttrs := params.App.Attrs
endptAttrs := params.Endpt.Attrs
highLightColor := appAttrs["highlight_color"].GetS()
arrowColor := appAttrs["arrow_color"].GetS()
indirectArrowColor := appAttrs["indirect_arrow_color"].GetS()
attrs := map[string]string{
"epname": params.Endpt.Name,
"eplongname": params.Endpt.LongName,
}
title := args.Title
if appAttrs["title"].GetS() != "" {
title = appAttrs["title"].GetS()
}
titleParser = cmdutils.MakeFormatParser(title)
diagramTitle := titleParser.Parse(attrs)
viewParams := &ViewParams{
RestrictBy: restrictBy,
EndptAttrs: endptAttrs,
HighLightColor: highLightColor,
ArrowColor: arrowColor,
IndirectArrowColor: indirectArrowColor,
DiagramTitle: diagramTitle,
}
v.StringBuilder.WriteString(PumlHeader)
if args.Epa || endptAttrs["view"].GetS() == "epa" {
return v.GenerateEPAView(*viewParams, params)
}
return v.GenerateIntsView(args, *viewParams, params)
}
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}