-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeep_cache.go
105 lines (87 loc) · 2.98 KB
/
nodeep_cache.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
/*
Copyright 2022 Nokia.
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 wirecontroller
import (
"context"
"fmt"
"log/slog"
"github.com/henderiw-nephio/wire-connector/pkg/wirer"
"github.com/henderiw-nephio/wire-connector/pkg/wirer/cache/resolve"
"github.com/henderiw-nephio/wire-connector/pkg/wirer/state"
"github.com/henderiw/logger/log"
"k8s.io/apimachinery/pkg/types"
)
type NodeEpCache interface {
Get(types.NamespacedName) (*NodeEndpoint, error)
Upsert(context.Context, types.NamespacedName, *NodeEndpoint)
Delete(context.Context, types.NamespacedName)
List() map[types.NamespacedName]*NodeEndpoint
Resolve(nsn types.NamespacedName, resolvedData *resolve.Data)
UnResolve(nsn types.NamespacedName)
HandleEvent(context.Context, types.NamespacedName, state.Event, *state.EventCtx) error
}
func NewEpCache(ctx context.Context, c wirer.Cache[*NodeEndpoint]) NodeEpCache {
return &nodeepCache{
c: c,
l: log.FromContext(ctx).WithGroup("ep-cache"),
}
}
type nodeepCache struct {
c wirer.Cache[*NodeEndpoint]
l *slog.Logger
}
// Get return the type
func (r *nodeepCache) Get(nsn types.NamespacedName) (*NodeEndpoint, error) {
return r.c.Get(nsn)
}
// Upsert creates or updates the entry in the cache
func (r *nodeepCache) Upsert(ctx context.Context, nsn types.NamespacedName, ep *NodeEndpoint) {
r.c.Upsert(ctx, nsn, ep)
}
// Delete deletes the entry in the cache
func (r *nodeepCache) Delete(ctx context.Context, nsn types.NamespacedName) {
r.c.Delete(ctx, nsn)
}
func (r *nodeepCache) List() map[types.NamespacedName]*NodeEndpoint {
return r.c.List()
}
func (r *nodeepCache) Resolve(nsn types.NamespacedName, resolvedData *resolve.Data) {
ep, err := r.c.Get(nsn)
if err == nil {
ep.NodeEpReq.Resolve(resolvedData)
r.c.Upsert(context.Background(), nsn, ep)
}
}
func (r *nodeepCache) UnResolve(nsn types.NamespacedName) {
ep, err := r.c.Get(nsn)
if err == nil {
ep.NodeEpReq.Unresolve()
r.c.Upsert(context.Background(), nsn, ep)
}
}
func (r *nodeepCache) HandleEvent(ctx context.Context, nsn types.NamespacedName, event state.Event, eventCtx *state.EventCtx) error {
ep, err := r.c.Get(nsn)
if err != nil {
return fmt.Errorf("cannot handleEvent, nsn not found %s", nsn.String())
}
log := log.FromContext(ctx).With("event", event, "nsn", nsn, "evenCtx", eventCtx, "state", ep.State.String())
log.Info("handleEvent")
ep.State.HandleEvent(ctx, event, eventCtx, ep)
// update the wire status
if ep.State.String() == "Deleted" {
r.c.Delete(ctx, nsn)
} else {
r.Upsert(ctx, nsn, ep)
}
return nil
}