-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
types.go
133 lines (118 loc) · 2.93 KB
/
types.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
package model
import (
_ "encoding/json" // Mapping value of json to struct member
"fmt"
"strings"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/cloud/pkg/common/messagelayer"
"github.com/kubeedge/kubeedge/cloud/pkg/common/modules"
)
// constants for resource types
const (
ResNode = "node"
ResMember = "membership"
ResTwin = "twin"
ResAuth = "auth_info"
ResDevice = "device"
)
// constants for resource operations
const (
OpGet = "get"
OpResult = "get_result"
OpList = "list"
OpDetail = "detail"
OpDelta = "delta"
OpDoc = "document"
OpUpdate = "updated"
OpInsert = "insert"
OpDelete = "deleted"
OpConnect = "connected"
OpDisConnect = "disconnected"
OpKeepalive = "keepalive"
)
// GpResource constants for message group
const (
GpResource = "resource"
)
// constants for message source
const (
SrcCloudHub = "cloudhub"
SrcManager = "edgemgr"
)
// constants for identifier information for edge hub
const (
ProjectID = "project_id"
NodeID = "node_id"
)
var cloudModuleArray = []string{
modules.CloudHubModuleName,
modules.CloudStreamModuleName,
modules.DeviceControllerModuleName,
modules.EdgeControllerModuleName,
modules.SyncControllerModuleName,
}
// HubInfo saves identifier information for edge hub
type HubInfo struct {
ProjectID string
NodeID string
}
// NewResource constructs a resource field using resource type and ID
func NewResource(resType, resID string, info *HubInfo) string {
var prefix string
if info != nil {
prefix = fmt.Sprintf("%s/%s/", model.ResourceTypeNode, info.NodeID)
}
if resID == "" {
return fmt.Sprintf("%s%s", prefix, resType)
}
return fmt.Sprintf("%s%s/%s", prefix, resType, resID)
}
// IsNodeStopped indicates if the node is stopped or running
func IsNodeStopped(msg *model.Message) bool {
resourceType, _ := messagelayer.GetResourceType(*msg)
if resourceType != model.ResourceTypeNode {
return false
}
if msg.Router.Operation == model.DeleteOperation {
return true
}
return false
}
// IsFromEdge judges if the event is sent from edge
func IsFromEdge(msg *model.Message) bool {
source := msg.Router.Source
for _, item := range cloudModuleArray {
if source == item {
return false
}
}
return true
}
// IsToEdge judges if the vent should be sent to edge
func IsToEdge(msg *model.Message) bool {
if msg.Router.Source != SrcManager {
return true
}
resource := msg.Router.Resource
if strings.HasPrefix(resource, ResNode) {
tokens := strings.Split(resource, "/")
if len(tokens) >= 3 {
resource = strings.Join(tokens[2:], "/")
}
}
// apply special check for edge manager
resOpMap := map[string][]string{
ResMember: {OpGet},
ResTwin: {OpDelta, OpDoc, OpGet},
ResAuth: {OpGet},
ResNode: {OpDelete},
}
for res, ops := range resOpMap {
for _, op := range ops {
if msg.Router.Operation == op && strings.Contains(resource, res) {
return false
}
}
}
return true
}