-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathgql.go
221 lines (205 loc) · 6.19 KB
/
gql.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
package iface
import (
"errors"
"reflect"
"github.com/graphql-go/graphql"
"github.com/usnistgov/ndn-dpdk/core/gqlserver"
"github.com/usnistgov/ndn-dpdk/core/jsonhelper"
"github.com/usnistgov/ndn-dpdk/core/nnduration"
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
"github.com/usnistgov/ndn-dpdk/dpdk/ealthread"
)
var (
// GqlCreateFaceAllowed indicates whether face creation via GraphQL is allowed.
GqlCreateFaceAllowed bool
errGqlCreateFaceDisallowed = errors.New("createFace is disallowed; is NDN-DPDK forwarder activated?")
)
// GraphQL types.
var (
GqlPktQueueInput *graphql.InputObject
GqlFaceType *gqlserver.NodeType[Face]
GqlRxCountersType *graphql.Object
GqlTxCountersType *graphql.Object
GqlCountersType *graphql.Object
GqlRxGroupInterface *gqlserver.Interface
)
func init() {
GqlPktQueueInput = graphql.NewInputObject(graphql.InputObjectConfig{
Name: "FacePktQueueInput",
Description: "Packet queue configuration.",
Fields: gqlserver.BindInputFields[PktQueueConfig](gqlserver.FieldTypes{
reflect.TypeFor[nnduration.Nanoseconds](): nnduration.GqlNanoseconds,
}),
})
GqlFaceType = gqlserver.NewNodeType(graphql.ObjectConfig{
Name: "Face",
Fields: graphql.Fields{
"nid": &graphql.Field{
Type: gqlserver.NonNullInt,
Description: "Numeric face identifier.",
Resolve: func(p graphql.ResolveParams) (any, error) {
face := p.Source.(Face)
return int(face.ID()), nil
},
},
"locator": &graphql.Field{
Type: gqlserver.NonNullJSON,
Description: "Endpoint addresses.",
Resolve: func(p graphql.ResolveParams) (any, error) {
face := p.Source.(Face)
locw := LocatorWrapper{
Locator: face.Locator(),
}
return locw, nil
},
},
"numaSocket": eal.GqlWithNumaSocket,
"isDown": &graphql.Field{
Type: gqlserver.NonNullBoolean,
Description: "Whether the face is down.",
Resolve: func(p graphql.ResolveParams) (any, error) {
face := p.Source.(Face)
return IsDown(face.ID()), nil
},
},
"txLoop": &graphql.Field{
Type: ealthread.GqlWorkerType.Object,
Description: "TxLoop serving this face.",
Resolve: func(p graphql.ResolveParams) (any, error) {
txLoopLock.Lock()
defer txLoopLock.Unlock()
face := p.Source.(Face)
txl := mapFaceTxl[face.ID()]
if txl == nil {
return nil, nil
}
lc := txl.LCore()
return gqlserver.Optional(lc), nil
},
},
},
}, gqlserver.NodeConfig[Face]{
RetrieveInt: func(id int) Face {
return Get(ID(id))
},
Delete: func(source Face) error {
return source.Close()
},
})
gqlserver.AddQuery(&graphql.Field{
Name: "faces",
Description: "List of faces.",
Type: gqlserver.NewListNonNullBoth(GqlFaceType.Object),
Resolve: func(p graphql.ResolveParams) (any, error) {
return List(), nil
},
})
gqlserver.AddMutation(&graphql.Field{
Name: "createFace",
Description: "Create a face.",
Args: graphql.FieldConfigArgument{
"locator": &graphql.ArgumentConfig{
Description: "JSON object that satisfies the schema given in 'locator.schema.json'.",
Type: gqlserver.NonNullJSON,
},
},
Type: graphql.NewNonNull(GqlFaceType.Object),
Resolve: func(p graphql.ResolveParams) (any, error) {
if !GqlCreateFaceAllowed {
return nil, errGqlCreateFaceDisallowed
}
var locw LocatorWrapper
if e := jsonhelper.Roundtrip(p.Args["locator"], &locw, jsonhelper.DisallowUnknownFields); e != nil {
return nil, e
}
return locw.Locator.CreateFace()
},
})
GqlRxCountersType = graphql.NewObject(graphql.ObjectConfig{
Name: "FaceRxCounters",
Fields: gqlserver.BindFields[RxCounters](nil),
})
GqlTxCountersType = graphql.NewObject(graphql.ObjectConfig{
Name: "FaceTxCounters",
Fields: gqlserver.BindFields[TxCounters](nil),
})
GqlCountersType = graphql.NewObject(graphql.ObjectConfig{
Name: "FaceCounters",
Fields: gqlserver.BindFields[Counters](gqlserver.FieldTypes{
reflect.TypeFor[RxCounters](): GqlRxCountersType,
reflect.TypeFor[TxCounters](): GqlTxCountersType,
}),
})
gqlserver.AddCounters(&gqlserver.CountersConfig{
Description: "Face counters.",
Parent: GqlFaceType.Object,
Name: "counters",
Subscription: "faceCounters",
FindArgs: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Description: "Face ID.",
Type: gqlserver.NonNullID,
},
},
Find: func(p graphql.ResolveParams) (root any, enders []any, e error) {
face := GqlFaceType.Retrieve(p.Args["id"].(string))
return face, nil, nil
},
Type: GqlCountersType,
Read: func(p graphql.ResolveParams) (any, error) {
face := p.Source.(Face)
return face.Counters(), nil
},
})
GqlRxGroupInterface = gqlserver.NewInterface(graphql.InterfaceConfig{
Name: "RxGroup",
Fields: graphql.Fields{
"rxLoop": ealthread.GqlWithWorker(func(p graphql.ResolveParams) ealthread.Thread {
rxLoopLock.Lock()
defer rxLoopLock.Unlock()
rxg := p.Source.(RxGroup)
return mapRxgRxl[rxg]
}),
"faces": &graphql.Field{
Type: gqlserver.NewListNonNullBoth(GqlFaceType.Object),
Resolve: func(p graphql.ResolveParams) (any, error) {
rxg := p.Source.(RxGroup)
return rxg.Faces(), nil
},
},
},
})
ealthread.GqlWorkerType.Object.AddFieldConfig("txLoopFaces", &graphql.Field{
Description: "Faces on TX thread.",
Type: gqlserver.NewListNonNullElem(GqlFaceType.Object),
Resolve: func(p graphql.ResolveParams) (any, error) {
txLoopLock.Lock()
defer txLoopLock.Unlock()
lc := p.Source.(eal.LCore)
var txl TxLoop
txLoopThreads.Each(func(t TxLoop) {
if t.LCore() == lc {
txl = t
}
})
if txl == nil {
return nil, nil
}
list := []Face{}
for id, t := range mapFaceTxl {
if t == txl {
list = append(list, Get(id))
}
}
return list, nil
},
})
}
// RegisterGqlRxGroupType registers an implementation of GraphQL RxGroup interface.
func RegisterGqlRxGroupType[T RxGroup](oc graphql.ObjectConfig) (object *graphql.Object) {
GqlRxGroupInterface.AppendTo(&oc)
oc.Fields = GqlRxGroupInterface.CopyFieldsTo(oc.Fields)
object = graphql.NewObject(oc)
gqlserver.ImplementsInterface[T](object, GqlRxGroupInterface)
return object
}