forked from anz-bank/sysl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seqs.go
284 lines (251 loc) · 9.33 KB
/
seqs.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
package main
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
sysl "github.com/anz-bank/sysl/src/proto"
"github.com/anz-bank/sysl/sysl2/sysl/parse"
"github.com/anz-bank/sysl/sysl2/sysl/syslutil"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)
type sequenceDiagParam struct {
AppLabeler
EndpointLabeler
endpoints []string
title string
blackboxes map[string]*Upto
appName string
group string
}
func generateSequenceDiag(m *sysl.Module, p *sequenceDiagParam) (string, error) {
w := MakeSequenceDiagramWriter(true, "skinparam maxMessageSize 250")
v := MakeSequenceDiagramVisitor(p.AppLabeler, p.EndpointLabeler, w, m, p.appName, p.group)
e := MakeEndpointCollectionElement(p.title, p.endpoints, p.blackboxes)
if err := e.Accept(v); err != nil {
return "", err
}
const color = "#LightBlue"
for boxname, appset := range v.groupboxes {
fmt.Fprintf(w, "box \"%s\" %s\n", boxname, color)
for key := range appset {
fmt.Fprintf(w, "\tparticipant %s\n", v.UniqueVarForAppName(key))
}
fmt.Fprintf(w, "end box\n")
}
return w.String(), nil
}
func loadApp(root string, models string) (*sysl.Module, error) {
// Model we want to generate seqs for, the non-empty model
mod, err := parse.NewParser().Parse(models, root)
if err != nil {
return nil, fmt.Errorf(
"loadApp: unable to load module:\n\troot: %s\n\tmodel: %s\nerror: %s",
root, models, err.Error(),
)
}
return mod, nil
}
func constructFormatParser(former, latter string) *FormatParser {
fmtstr := former
if former == "" {
fmtstr = latter
}
return MakeFormatParser(escapeWordBoundary(fmtstr))
}
func escapeWordBoundary(src string) string {
result, err := json.Marshal(src)
syslutil.PanicOnError(err)
escapeStr := strings.Replace(string(result), `\u0008`, `\\b`, -1)
var val string
err = json.Unmarshal([]byte(escapeStr), &val)
syslutil.PanicOnError(err)
return val
}
func DoConstructSequenceDiagrams(cmdContextParam *CmdContextParamSeqgen) (map[string]string, error) {
var blackboxes [][]string
log.Debugf("root: %s\n", *cmdContextParam.root)
log.Debugf("endpoints: %v\n", cmdContextParam.endpointsFlag)
log.Debugf("app: %v\n", cmdContextParam.appsFlag)
log.Debugf("endpoint_format: %s\n", *cmdContextParam.endpointFormat)
log.Debugf("app_format: %s\n", *cmdContextParam.appFormat)
log.Debugf("title: %s\n", *cmdContextParam.title)
log.Debugf("modules: %s\n", *cmdContextParam.modulesFlag)
log.Debugf("output: %s\n", *cmdContextParam.output)
log.Debugf("loglevel: %s\n", *cmdContextParam.loglevel)
if *cmdContextParam.plantuml == "" {
plantuml := os.Getenv("SYSL_PLANTUML")
cmdContextParam.plantuml = &plantuml
if *cmdContextParam.plantuml == "" {
*cmdContextParam.plantuml = "http://localhost:8080/plantuml"
}
}
log.Debugf("plantuml: %s\n", *cmdContextParam.plantuml)
if cmdContextParam.blackboxes == nil {
blackboxes = ParseBlackBoxesFromArgument(*cmdContextParam.blackboxesFlag)
log.Debugf("blackbox: %s\n", *cmdContextParam.blackboxesFlag)
} else {
blackboxes = *cmdContextParam.blackboxes
}
if *cmdContextParam.isVerbose {
*cmdContextParam.loglevel = debug
}
// Default info
if level, has := syslutil.LogLevels[*cmdContextParam.loglevel]; has {
log.SetLevel(level)
}
result := make(map[string]string)
mod, err := loadApp(*cmdContextParam.root, *cmdContextParam.modulesFlag)
if err != nil {
return nil, err
}
if strings.Contains(*cmdContextParam.output, "%(epname)") {
if len(blackboxes) > 0 {
log.Warnf("Ignoring blackboxes passed from command line")
}
spout := MakeFormatParser(*cmdContextParam.output)
for _, appName := range *cmdContextParam.appsFlag {
app := mod.Apps[appName]
bbs := TransformBlackBoxes(app.GetAttrs()["blackboxes"].GetA().GetElt())
spseqtitle := constructFormatParser(app.GetAttrs()["seqtitle"].GetS(), *cmdContextParam.title)
spep := constructFormatParser(app.GetAttrs()["epfmt"].GetS(), *cmdContextParam.endpointFormat)
spapp := constructFormatParser(app.GetAttrs()["appfmt"].GetS(), *cmdContextParam.appFormat)
keys := []string{}
for k := range app.GetEndpoints() {
keys = append(keys, k)
}
sort.Strings(keys)
bbsAll := map[string]*Upto{}
TransformBlackboxesToUptos(bbsAll, bbs, BBApplication)
var sd *sequenceDiagParam
for _, k := range keys {
endpoint := app.GetEndpoints()[k]
epAttrs := endpoint.GetAttrs()
outputDir := spout.FmtOutput(appName, k, endpoint.GetLongName(), epAttrs)
bbs2 := TransformBlackBoxes(endpoint.GetAttrs()["blackboxes"].GetA().GetElt())
varrefs := MergeAttributes(app.GetAttrs(), endpoint.GetAttrs())
sdEndpoints := []string{}
for _, stmt := range endpoint.GetStmt() {
_, ok := stmt.Stmt.(*sysl.Statement_Call)
if ok {
parts := stmt.GetCall().GetTarget().GetPart()
ep := stmt.GetCall().GetEndpoint()
sdEndpoints = append(sdEndpoints, strings.Join(parts, " :: ")+" <- "+ep)
}
}
if len(sdEndpoints) == 0 {
log.Errorf("No call statements to build sequence diagram for endpoint %s", endpoint.Name)
return result, nil
}
groupAttr := epAttrs["groupby"].GetS()
if len(groupAttr) == 0 {
groupAttr = *cmdContextParam.group
} else if len(*cmdContextParam.group) > 0 {
log.Warnf("Ignoring groupby passed from command line")
}
TransformBlackboxesToUptos(bbsAll, bbs2, BBEndpointCollection)
sd = &sequenceDiagParam{
endpoints: sdEndpoints,
AppLabeler: spapp,
EndpointLabeler: spep,
title: spseqtitle.FmtSeq(endpoint.GetName(), endpoint.GetLongName(), varrefs),
blackboxes: bbsAll,
appName: fmt.Sprintf("'%s :: %s'", appName, endpoint.GetName()),
group: groupAttr,
}
out, err := generateSequenceDiag(mod, sd)
if err != nil {
return nil, err
}
for indx := range bbs2 {
delete(bbsAll, bbs2[indx][0])
}
result[outputDir] = out
}
for bbKey, bbVal := range bbsAll {
if bbVal.VisitCount == 0 && bbVal.ValueType == BBApplication {
log.Warnf("blackbox '%s' not hit in app '%s'\n", bbKey, appName)
}
}
}
} else {
if *cmdContextParam.endpointsFlag == nil {
return result, nil
}
spep := constructFormatParser("", *cmdContextParam.endpointFormat)
spapp := constructFormatParser("", *cmdContextParam.appFormat)
bbsAll := map[string]*Upto{}
TransformBlackboxesToUptos(bbsAll, blackboxes, BBCommandLine)
sd := &sequenceDiagParam{
endpoints: *cmdContextParam.endpointsFlag,
AppLabeler: spapp,
EndpointLabeler: spep,
title: *cmdContextParam.title,
blackboxes: bbsAll,
group: *cmdContextParam.group,
}
out, err := generateSequenceDiag(mod, sd)
if err != nil {
return nil, err
}
for bbKey, bbVal := range bbsAll {
if bbVal.VisitCount == 0 && bbVal.ValueType == BBCommandLine {
log.Warnf("blackbox '%s' passed on commandline not hit\n", bbKey)
}
}
result[*cmdContextParam.output] = out
}
return result, nil
}
func configureCmdlineForSeqgen(sysl *kingpin.Application) *CmdContextParamSeqgen {
defer func() {
if err := recover(); err != nil {
log.Errorln(err)
}
}()
sd := sysl.Command("sd", "Generate sequence diagram")
returnValues := &CmdContextParamSeqgen{}
returnValues.root = sd.Flag("root",
"sysl root directory for input model file (default: .)",
).Default(".").String()
returnValues.endpointFormat = sd.Flag("endpoint_format",
"Specify the format string for sequence diagram endpoints. May include "+
"%(epname), %(eplongname) and %(@foo) for attribute foo (default: %(epname))",
).Default("%(epname)").String()
returnValues.appFormat = sd.Flag("app_format",
"Specify the format string for sequence diagram participants. "+
"May include %%(appname) and %%(@foo) for attribute foo (default: %(appname))",
).Default("%(appname)").String()
returnValues.title = sd.Flag("title", "diagram title").Short('t').String()
returnValues.plantuml = sd.Flag("plantuml",
"base url of plantuml server (default: $SYSL_PLANTUML or "+
"http://localhost:8080/plantuml see "+
"http://plantuml.com/server.html#install for more info)",
).Short('p').String()
returnValues.output = sd.Flag("output",
"output file (default: %(epname).png)",
).Default("%(epname).png").Short('o').String()
returnValues.endpointsFlag = sd.Flag("endpoint",
"Include endpoint in sequence diagram",
).Short('s').Strings()
returnValues.appsFlag = sd.Flag("app",
"Include all endpoints for app in sequence diagram (currently "+
"only works with templated --output). Use SYSL_SD_FILTERS env (a "+
"comma-list of shell globs) to limit the diagrams generated",
).Short('a').Strings()
returnValues.blackboxesFlag = sd.Flag("blackbox",
"Input blackboxes in the format App <- Endpoint=Some description, "+
"repeat '-b App <- Endpoint=Some description' to set multiple blackboxes",
).Short('b').StringMap()
returnValues.group = sd.Flag("groupby", "Enter the groupby attribute (apps having "+
"the same attribute value are grouped together in one box").Short('g').String()
returnValues.loglevel = sd.Flag("log", "log level[debug,info,warn,off]").Default("info").String()
returnValues.isVerbose = sd.Flag("verbose", "show output").Short('v').Default("false").Bool()
returnValues.modulesFlag = sd.Arg("modules",
"input files without .sysl extension and with leading /, eg: "+
"/project_dir/my_models combine with --root if needed",
).String()
return returnValues
}