aboutsummaryrefslogtreecommitdiffstats
path: root/src/orchestrator/pkg/rtcontext/rtcontext.go
blob: 5c5bee13704b7b1ffaa2fca1130f6e8b791115a8 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 * Copyright 2020 Intel Corporation, 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 rtcontext

import (
	"fmt"
	"math/rand"
	"time"
	"strings"
	"github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/contextdb"
	pkgerrors "github.com/pkg/errors"
)

const maxrand = 0x7fffffffffffffff
const prefix string = "/context/"

type RunTimeContext struct {
	cid interface{}
}

type Rtcontext interface {
	RtcCreate() (interface{}, error)
	RtcGet() (interface{}, error)
	RtcAddLevel(handle interface{}, level string, value string) (interface{}, error)
	RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error)
	RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error)
	RtcDeletePair(handle interface{}) (error)
	RtcDeletePrefix(handle interface{}) (error)
	RtcGetHandles(handle interface{}) ([]interface{}, error)
	RtcGetValue(handle interface{}, value interface{}) (error)
}

//Create context by assiging a new id
func (rtc *RunTimeContext) RtcCreate() (interface{}, error) {

	ra := rand.New(rand.NewSource(time.Now().UnixNano()))
	rn := ra.Int63n(maxrand)
	id := fmt.Sprintf("%v", rn)
	cid := (prefix + id + "/")
	rtc.cid = interface{}(cid)

	err := contextdb.Db.Put(cid, id)
	if err != nil {
		return nil, pkgerrors.Errorf("Error creating run time context: %s", err.Error())
	}

	return rtc.cid, nil
}

//Get the root handle
func (rtc *RunTimeContext) RtcGet() (interface{}, error) {
	str := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, prefix) {
		return nil, pkgerrors.Errorf("Not a valid run time context")
	}

	var value string
	err := contextdb.Db.Get(str, &value)
	if err != nil {
		return nil, pkgerrors.Errorf("Error getting run time context metadata: %s", err.Error())
	}
	if !strings.Contains(str, value) {
		return nil, pkgerrors.Errorf("Error matching run time context metadata")
	}

	return rtc.cid, nil
}

//Add a new level at a given handle and return the new handle
func (rtc *RunTimeContext) RtcAddLevel(handle interface{}, level string, value string) (interface{}, error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return nil, pkgerrors.Errorf("Not a valid run time context handle")
	}

	if level == "" {
		return nil, pkgerrors.Errorf("Not a valid run time context level")
	}
	if value == "" {
		return nil, pkgerrors.Errorf("Not a valid run time context level value")
	}

	key := str + level + "/" + value + "/"
	err := contextdb.Db.Put(key, value)
	if err != nil {
		return nil, pkgerrors.Errorf("Error adding run time context level: %s", err.Error())
	}

	return (interface{})(key), nil
}

// Add a resource under the given level and return new handle
func (rtc *RunTimeContext) RtcAddResource(handle interface{}, resname string, value interface{}) (interface{}, error) {

	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return nil, pkgerrors.Errorf("Not a valid run time context handle")
	}
	if resname == "" {
		return nil, pkgerrors.Errorf("Not a valid run time context resource name")
	}
	if value == nil {
		return nil, pkgerrors.Errorf("Not a valid run time context resource value")
	}

	k := str + "resource" + "/" + resname + "/"
	err := contextdb.Db.Put(k, value)
	if err != nil {
		return nil, pkgerrors.Errorf("Error adding run time context resource: %s", err.Error())
	}
	return (interface{})(k), nil
}

// Add instruction at a given level and type, return the new handle
func (rtc *RunTimeContext) RtcAddInstruction(handle interface{}, level string, insttype string, value interface{}) (interface{}, error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return nil, pkgerrors.Errorf("Not a valid run time context handle")
	}

	if level == "" {
		return nil, pkgerrors.Errorf("Not a valid run time context level")
	}
	if insttype  == "" {
		return nil, pkgerrors.Errorf("Not a valid run time context instruction type")
	}
	if value == nil {
		return nil, pkgerrors.Errorf("Not a valid run time context instruction value")
	}

	k := str + level + "/" + "instruction" + "/" + insttype +"/"
	err := contextdb.Db.Put(k, fmt.Sprintf("%v", value))
	if  err != nil  {
		return nil, pkgerrors.Errorf("Error adding run time context instruction: %s", err.Error())
	}

	return (interface{})(k), nil
}

//Delete the key value pair using given handle
func (rtc *RunTimeContext) RtcDeletePair(handle interface{}) (error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return pkgerrors.Errorf("Not a valid run time context handle")
	}

	err := contextdb.Db.Delete(str)
	if err != nil {
		return pkgerrors.Errorf("Error deleting run time context pair: %s", err.Error())
	}

	return nil
}

// Delete all handles underneath the given handle
func (rtc *RunTimeContext) RtcDeletePrefix(handle interface{}) (error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return pkgerrors.Errorf("Not a valid run time context handle")
	}

	err := contextdb.Db.DeleteAll(str)
	if err != nil {
		return pkgerrors.Errorf("Error deleting run time context with prefix: %s", err.Error())
	}

	return nil
}

// Return the list of handles under the given handle
func (rtc *RunTimeContext) RtcGetHandles(handle interface{}) ([]interface{}, error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return nil, pkgerrors.Errorf("Not a valid run time context handle")
	}

	s, err := contextdb.Db.GetAllKeys(str)
	if err != nil {
		return nil, pkgerrors.Errorf("Error getting run time context handles: %s", err.Error())
	}
	r := make([]interface{}, len(s))
	for i, v := range s {
		r[i] = v
	}
	return r, nil
}

// Get the value for a given handle
func (rtc *RunTimeContext) RtcGetValue(handle interface{}, value interface{}) (error) {
	str := fmt.Sprintf("%v", handle)
	sid := fmt.Sprintf("%v", rtc.cid)
	if !strings.HasPrefix(str, sid) {
		return pkgerrors.Errorf("Not a valid run time context handle")
	}

	err := contextdb.Db.Get(str, value)
	if err != nil {
		return pkgerrors.Errorf("Error getting run time context value: %s", err.Error())
	}

	return nil
}