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
|
/*
Copyright 2022 The KubeEdge Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package messagelayer
import (
"errors"
"fmt"
"strings"
"k8s.io/klog/v2"
"github.com/kubeedge/beehive/pkg/core/model"
"github.com/kubeedge/kubeedge/common/constants"
pkgutil "github.com/kubeedge/kubeedge/pkg/util"
)
const (
ResourceNode = "node"
ResourceNodeIDIndex = 1
ResourceNamespaceIndex = 2
ResourceResourceTypeIndex = 3
ResourceResourceNameIndex = 4
ResourceDeviceIndex = 2
ResourceDeviceNamespaceIndex = 3
ResourceDevice = "device"
ResourceTypeTwinEdgeUpdated = "twin/edge_updated"
ResourceTypeMembershipDetail = "membership/detail"
)
// BuildResource return a string as "beehive/pkg/core/model".Message.Router.Resource
func BuildResource(nodeID, namespace, resourceType, resourceID string) (resource string, err error) {
if namespace == "" || resourceType == "" || nodeID == "" {
err = fmt.Errorf("required parameter are not set (node id, namespace or resource type)")
return
}
resource = fmt.Sprintf("%s%s%s%s%s%s%s", ResourceNode, constants.ResourceSep, nodeID, constants.ResourceSep, namespace, constants.ResourceSep, resourceType)
if resourceID != "" {
resource += fmt.Sprintf("%s%s", constants.ResourceSep, resourceID)
}
return
}
// getElementByIndex returns a string from "beehive/pkg/core/model".Message.Router.Resource by index
func getElementByIndex(msg model.Message, index int) string {
sli := strings.Split(msg.GetResource(), constants.ResourceSep)
if len(sli) <= index {
return ""
}
return sli[index]
}
// GetNodeID from "beehive/pkg/core/model".Message.Router.Resource
func GetNodeID(msg model.Message) (string, error) {
res := getElementByIndex(msg, ResourceNodeIDIndex)
if res == "" {
return "", fmt.Errorf("node id not found")
}
klog.V(4).Infof("The node id %s, %d", res, ResourceNodeIDIndex)
return res, nil
}
// GetNamespace from "beehive/pkg/core/model".Model.Router.Resource
func GetNamespace(msg model.Message) (string, error) {
res := getElementByIndex(msg, ResourceNamespaceIndex)
if res == "" {
return "", fmt.Errorf("namespace not found")
}
klog.V(4).Infof("The namespace %s, %d", res, ResourceNamespaceIndex)
return res, nil
}
// GetResourceType from "beehive/pkg/core/model".Model.Router.Resource
func GetResourceType(msg model.Message) (string, error) {
res := getElementByIndex(msg, ResourceResourceTypeIndex)
if res == "" {
return "", fmt.Errorf("resource type not found")
}
klog.V(4).Infof("The resource type is %s, %d", res, ResourceResourceTypeIndex)
return res, nil
}
// GetResourceName from "beehive/pkg/core/model".Model.Router.Resource
func GetResourceName(msg model.Message) (string, error) {
res := getElementByIndex(msg, ResourceResourceNameIndex)
if res == "" {
return "", fmt.Errorf("resource name not found")
}
klog.V(4).Infof("The resource name is %s, %d", res, ResourceResourceNameIndex)
return res, nil
}
// BuildResourceForRouter return a string as "beehive/pkg/core/model".Message.Router.Resource
func BuildResourceForRouter(resourceType, resourceID string) (string, error) {
if resourceID == "" || resourceType == "" {
return "", fmt.Errorf("required parameter are not set (resourceID or resource type)")
}
return pkgutil.ConcatStrings(resourceType, constants.ResourceSep, resourceID), nil
}
// BuildResourceForDevice return a string as "beehive/pkg/core/model".Message.Router.Resource
func BuildResourceForDevice(nodeID, resourceType, resourceID string) (resource string, err error) {
if nodeID == "" || resourceType == "" {
err = fmt.Errorf("required parameter are not set (node id, namespace or resource type)")
return
}
resource = fmt.Sprintf("%s%s%s%s%s", ResourceNode, constants.ResourceSep, nodeID, constants.ResourceSep, resourceType)
if resourceID != "" {
resource += fmt.Sprintf("%s%s", constants.ResourceSep, resourceID)
}
return
}
// GetDeviceID returns the ID of the device,resource's format:$hw/events/device/{namespace}/{deviceName}
func GetDeviceID(resource string) (string, error) {
res := strings.Split(resource, "/")
if len(res) >= ResourceDeviceNamespaceIndex+2 && res[ResourceDeviceIndex] == ResourceDevice {
return res[ResourceDeviceNamespaceIndex] + "/" + res[ResourceDeviceNamespaceIndex+1], nil
}
return "", errors.New("failed to get device id")
}
// GetResourceTypeForDevice returns the resourceType of message received from edge
func GetResourceTypeForDevice(resource string) (string, error) {
if strings.Contains(resource, ResourceTypeTwinEdgeUpdated) {
return ResourceTypeTwinEdgeUpdated, nil
} else if strings.Contains(resource, ResourceTypeMembershipDetail) {
return ResourceTypeMembershipDetail, nil
}
return "", fmt.Errorf("unknown resource, found: %s", resource)
}
|