/
context.go
86 lines (69 loc) · 2.43 KB
/
context.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
package ngsi
//ContextRegistry is where Context Sources register the information that they can provide
type ContextRegistry interface {
GetContextSourcesForQuery(query Query) []ContextSource
GetContextSourcesForEntity(entityID string) []ContextSource
GetContextSourcesForEntityType(entityType string) []ContextSource
Register(source ContextSource)
}
//NewContextRegistry initializes and returns a new default context registry without
//any registered context sources
func NewContextRegistry() ContextRegistry {
return ®istry{}
}
type registry struct {
sources []ContextSource
}
func (r *registry) GetContextSourcesForEntity(entityID string) []ContextSource {
matchingSources := []ContextSource{}
// TODO: Fix potential race issue
for _, src := range r.sources {
if src.ProvidesEntitiesWithMatchingID(entityID) {
matchingSources = append(matchingSources, src)
}
}
return matchingSources
}
func (r *registry) GetContextSourcesForEntityType(entityType string) []ContextSource {
matchingSources := []ContextSource{}
// TODO: Fix potential race issue
for _, src := range r.sources {
if src.ProvidesType(entityType) {
matchingSources = append(matchingSources, src)
}
}
return matchingSources
}
func (r *registry) GetContextSourcesForQuery(query Query) []ContextSource {
matchingSources := []ContextSource{}
entityTypeNames := query.EntityTypes()
entityAttributeNames := query.EntityAttributes()
// TODO: Fix potential race issue
for _, src := range r.sources {
for _, typeName := range entityTypeNames {
if typeName == "" || src.ProvidesType(typeName) {
for _, attributeName := range entityAttributeNames {
if attributeName == "" || src.ProvidesAttribute(attributeName) {
matchingSources = append(matchingSources, src)
break
}
}
}
}
}
return matchingSources
}
func (r *registry) Register(source ContextSource) {
// TODO: Fix potential race issue
r.sources = append(r.sources, source)
}
//ContextSource provides query and subscription support for a set of entities
type ContextSource interface {
ProvidesAttribute(attributeName string) bool
ProvidesEntitiesWithMatchingID(entityID string) bool
ProvidesType(typeName string) bool
CreateEntity(typeName, entityID string, request Request) error
GetEntities(query Query, callback QueryEntitiesCallback) error
RetrieveEntity(entityID string, request Request) (Entity, error)
UpdateEntityAttributes(entityID string, request Request) error
}