2 * Copyright 2020 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb"
22 pkgerrors "github.com/pkg/errors"
28 const maxrand = 0x7fffffffffffffff
29 const prefix string = "/context/"
31 type RunTimeContext struct {
35 type Rtcontext interface {
36 RtcInit() (interface{}, error)
37 RtcLoad(interface{}) (interface{}, error)
38 RtcCreate() (interface{}, error)
39 RtcGet() (interface{}, error)
40 RtcAddLevel(handle interface{}, level string, value string) (interface{}, error)
41 RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error)
42 RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error)
43 RtcDeletePair(handle interface{}) error
44 RtcDeletePrefix(handle interface{}) error
45 RtcGetHandles(handle interface{}) ([]interface{}, error)
46 RtcGetValue(handle interface{}, value interface{}) error
47 RtcUpdateValue(handle interface{}, value interface{}) error
50 //Intialize context by assiging a new id
51 func (rtc *RunTimeContext) RtcInit() (interface{}, error) {
53 return nil, pkgerrors.Errorf("Error, context already initialized")
55 ra := rand.New(rand.NewSource(time.Now().UnixNano()))
56 rn := ra.Int63n(maxrand)
57 id := fmt.Sprintf("%v", rn)
58 cid := (prefix + id + "/")
59 rtc.cid = interface{}(cid)
60 return interface{}(id), nil
64 //Load context using the given id
65 func (rtc *RunTimeContext) RtcLoad(id interface{}) (interface{}, error) {
66 str := fmt.Sprintf("%v", id)
68 return nil, pkgerrors.Errorf("Not a valid context id")
70 cid := (prefix + str + "/")
71 rtc.cid = interface{}(cid)
72 handle, err := rtc.RtcGet()
74 return nil, pkgerrors.Errorf("Error finding the context id: %s", err.Error())
79 //Create context using the id and prefix
80 func (rtc *RunTimeContext) RtcCreate() (interface{}, error) {
81 cid := fmt.Sprintf("%v", rtc.cid)
83 return nil, pkgerrors.Errorf("Error, context not intialized")
85 if !strings.HasPrefix(cid, prefix) {
86 return nil, pkgerrors.Errorf("Not a valid run time context prefix")
88 id := strings.SplitN(cid, "/", 4)[2]
89 err := contextdb.Db.Put(cid, id)
91 return nil, pkgerrors.Errorf("Error creating run time context: %s", err.Error())
98 func (rtc *RunTimeContext) RtcGet() (interface{}, error) {
99 str := fmt.Sprintf("%v", rtc.cid)
100 if !strings.HasPrefix(str, prefix) {
101 return nil, pkgerrors.Errorf("Not a valid run time context")
105 err := contextdb.Db.Get(str, &value)
107 return nil, pkgerrors.Errorf("Error getting run time context metadata: %s", err.Error())
109 if !strings.Contains(str, value) {
110 return nil, pkgerrors.Errorf("Error matching run time context metadata")
116 //Add a new level at a given handle and return the new handle
117 func (rtc *RunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) {
118 str := fmt.Sprintf("%v", handle)
119 sid := fmt.Sprintf("%v", rtc.cid)
120 if !strings.HasPrefix(str, sid) {
121 return nil, pkgerrors.Errorf("Not a valid run time context handle")
125 return nil, pkgerrors.Errorf("Not a valid run time context level")
128 return nil, pkgerrors.Errorf("Not a valid run time context level value")
131 key := str + level + "/" + value + "/"
132 err := contextdb.Db.Put(key, value)
134 return nil, pkgerrors.Errorf("Error adding run time context level: %s", err.Error())
137 return (interface{})(key), nil
140 // Add a resource under the given level and return new handle
141 func (rtc *RunTimeContext) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) {
143 str := fmt.Sprintf("%v", handle)
144 sid := fmt.Sprintf("%v", rtc.cid)
145 if !strings.HasPrefix(str, sid) {
146 return nil, pkgerrors.Errorf("Not a valid run time context handle")
149 return nil, pkgerrors.Errorf("Not a valid run time context resource name")
152 return nil, pkgerrors.Errorf("Not a valid run time context resource value")
155 k := str + "resource" + "/" + resname + "/"
156 err := contextdb.Db.Put(k, value)
158 return nil, pkgerrors.Errorf("Error adding run time context resource: %s", err.Error())
160 return (interface{})(k), nil
163 // Add instruction at a given level and type, return the new handle
164 func (rtc *RunTimeContext) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) {
165 str := fmt.Sprintf("%v", handle)
166 sid := fmt.Sprintf("%v", rtc.cid)
167 if !strings.HasPrefix(str, sid) {
168 return nil, pkgerrors.Errorf("Not a valid run time context handle")
172 return nil, pkgerrors.Errorf("Not a valid run time context level")
175 return nil, pkgerrors.Errorf("Not a valid run time context instruction type")
178 return nil, pkgerrors.Errorf("Not a valid run time context instruction value")
181 k := str + level + "/" + "instruction" + "/" + insttype + "/"
182 err := contextdb.Db.Put(k, fmt.Sprintf("%v", value))
184 return nil, pkgerrors.Errorf("Error adding run time context instruction: %s", err.Error())
187 return (interface{})(k), nil
190 //Delete the key value pair using given handle
191 func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) error {
192 str := fmt.Sprintf("%v", handle)
193 sid := fmt.Sprintf("%v", rtc.cid)
194 if !strings.HasPrefix(str, sid) {
195 return pkgerrors.Errorf("Not a valid run time context handle")
198 err := contextdb.Db.Delete(str)
200 return pkgerrors.Errorf("Error deleting run time context pair: %s", err.Error())
206 // Delete all handles underneath the given handle
207 func (rtc *RunTimeContext) RtcDeletePrefix(handle interface{}) error {
208 str := fmt.Sprintf("%v", handle)
209 sid := fmt.Sprintf("%v", rtc.cid)
210 if !strings.HasPrefix(str, sid) {
211 return pkgerrors.Errorf("Not a valid run time context handle")
214 err := contextdb.Db.DeleteAll(str)
216 return pkgerrors.Errorf("Error deleting run time context with prefix: %s", err.Error())
222 // Return the list of handles under the given handle
223 func (rtc *RunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, error) {
224 str := fmt.Sprintf("%v", handle)
225 sid := fmt.Sprintf("%v", rtc.cid)
226 if !strings.HasPrefix(str, sid) {
227 return nil, pkgerrors.Errorf("Not a valid run time context handle")
230 s, err := contextdb.Db.GetAllKeys(str)
232 return nil, pkgerrors.Errorf("Error getting run time context handles: %s", err.Error())
234 r := make([]interface{}, len(s))
235 for i, v := range s {
241 // Get the value for a given handle
242 func (rtc *RunTimeContext) RtcGetValue(handle interface{}, value interface{}) error {
243 str := fmt.Sprintf("%v", handle)
244 sid := fmt.Sprintf("%v", rtc.cid)
245 if !strings.HasPrefix(str, sid) {
246 return pkgerrors.Errorf("Not a valid run time context handle")
249 err := contextdb.Db.Get(str, value)
251 return pkgerrors.Errorf("Error getting run time context value: %s", err.Error())
257 // Update the value of a given handle
258 func (rtc *RunTimeContext) RtcUpdateValue(handle interface{}, value interface{}) error {
259 str := fmt.Sprintf("%v", handle)
260 sid := fmt.Sprintf("%v", rtc.cid)
261 if !strings.HasPrefix(str, sid) {
262 return pkgerrors.Errorf("Not a valid run time context handle")
264 err := contextdb.Db.Put(str, value)
266 return pkgerrors.Errorf("Error updating run time context value: %s", err.Error())