Newer
Older
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
/*
* Copyright (c) 2020 InterDigital Communications, Inc
*
* 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 metricstore
import (
"errors"
log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
redis "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-redis"
)
const redisTable = 0
const keyRoot = "data:global:sandbox-store:"
// DB Fields
const fieldSandboxName = "sbox-name"
const fieldScenarioName = "scenario-name"
type Sandbox struct {
Name string
ScenarioName string
}
type SandboxStore struct {
rc *redis.Connector
}
// NewSandboxStore - Creates and initialize a Sandbox Store instance
func NewSandboxStore(redisAddr string) (ss *SandboxStore, err error) {
// Create new Sandbox Store instance
ss = new(SandboxStore)
// Connect to Redis DB
ss.rc, err = redis.NewConnector(redisAddr, redisTable)
if err != nil {
log.Error("Failed connection to Sandbox Store Redis DB. Error: ", err)
return nil, err
}
log.Info("Connected to Sandbox Store Redis DB")
log.Info("Created Sandbox Store")
return ss, nil
}
// Set - Create or update entry in DB
func (ss *SandboxStore) Set(sbox *Sandbox) error {
// Validate sandbox
if sbox == nil {
return errors.New("nil sandbox")
}
if sbox.Name == "" {
return errors.New("Invalid sandbox name")
}
// Prepare data
fields := make(map[string]interface{})
fields[fieldSandboxName] = sbox.Name
fields[fieldScenarioName] = sbox.ScenarioName
// Update entry in DB
key := keyRoot + sbox.Name
err := ss.rc.SetEntry(key, fields)
if err != nil {
log.Error("Failed to set entry with error: ", err.Error())
return err
}
return nil
}
// Get - Return sandbox with provided name
func (ss *SandboxStore) Get(sboxName string) (*Sandbox, error) {
key := keyRoot + sboxName
// Make sure entry exists
if !ss.rc.EntryExists(key) {
err := errors.New("Entry not found")
log.Error(err.Error())
return nil, err
}
// Find entry
fields, err := ss.rc.GetEntry(key)
if err != nil {
log.Error("Failed to get entry with error: ", err.Error())
return nil, err
}
// Prepare sandbox
sbox := new(Sandbox)
sbox.Name = fields[fieldSandboxName]
sbox.ScenarioName = fields[fieldScenarioName]
return sbox, nil
}
// GetAll - Return all sandboxes
func (ss *SandboxStore) GetAll() (map[string]*Sandbox, error) {
sboxMap := make(map[string]*Sandbox)
keyMatchStr := keyRoot + "*"
// Get all sandbox entry details
err := ss.rc.ForEachEntry(keyMatchStr, getSandbox, &sboxMap)
if err != nil {
log.Error("Failed to get all entries with error: ", err.Error())
return nil, err
}
return sboxMap, nil
}
// Del - Remove sandbox with provided name
func (ss *SandboxStore) Del(sboxName string) {
key := keyRoot + sboxName
err := ss.rc.DelEntry(key)
if err != nil {
log.Error("Failed to delete entry for ", sboxName, " with err: ", err.Error())
}
}
// Flush - Remove all sandbox store entries
func (ss *SandboxStore) Flush() {
ss.rc.DBFlush(keyRoot)
}
func getSandbox(key string, fields map[string]string, userData interface{}) error {
sboxMap := *(userData.(*map[string]*Sandbox))
// Prepare sandbox
sbox := new(Sandbox)
sbox.Name = fields[fieldSandboxName]
sbox.ScenarioName = fields[fieldScenarioName]
// Add sandbox to
sboxMap[sbox.Name] = sbox
return nil
}