blob: 8f9e0015b4b42aa2915849203daa4216b5ef25bc [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17package model
18
19import (
20 "encoding/hex"
21 "encoding/json"
22 "github.com/golang/protobuf/proto"
23 "github.com/google/uuid"
24 "github.com/opencord/voltha-go/common/log"
25 "reflect"
26 "sync"
27)
28
29// Root is used to provide an abstraction to the base root structure
30type Root interface {
31 Node
32
33 ExecuteCallbacks()
34 AddCallback(callback CallbackFunction, args ...interface{})
35 AddNotificationCallback(callback CallbackFunction, args ...interface{})
36}
37
38// root points to the top of the data model tree or sub-tree identified by a proxy
39type root struct {
40 *node
41
42 Callbacks []CallbackTuple
43 NotificationCallbacks []CallbackTuple
44
45 DirtyNodes map[string][]*node
46 KvStore *Backend
47 Loading bool
48 RevisionClass interface{}
49
50 mutex sync.RWMutex
51}
52
53// NewRoot creates an new instance of a root object
54func NewRoot(initialData interface{}, kvStore *Backend) *root {
55 root := &root{}
56
57 root.KvStore = kvStore
58 root.DirtyNodes = make(map[string][]*node)
59 root.Loading = false
60
61 // If there is no storage in place just revert to
62 // a non persistent mechanism
63 if kvStore != nil {
64 root.RevisionClass = reflect.TypeOf(PersistedRevision{})
65 } else {
66 root.RevisionClass = reflect.TypeOf(NonPersistedRevision{})
67 }
68
69 root.Callbacks = []CallbackTuple{}
70 root.NotificationCallbacks = []CallbackTuple{}
71
72 root.node = NewNode(root, initialData, false, "")
73
74 return root
75}
76
77// MakeTxBranch creates a new transaction branch
78func (r *root) MakeTxBranch() string {
79 txidBin, _ := uuid.New().MarshalBinary()
80 txid := hex.EncodeToString(txidBin)[:12]
81
82 r.DirtyNodes[txid] = []*node{r.node}
83 r.node.MakeBranch(txid)
84
85 return txid
86}
87
88// DeleteTxBranch removes a transaction branch
89func (r *root) DeleteTxBranch(txid string) {
90 for _, dirtyNode := range r.DirtyNodes[txid] {
91 dirtyNode.DeleteBranch(txid)
92 }
93 delete(r.DirtyNodes, txid)
94 delete(r.node.Branches, txid)
95}
96
97// FoldTxBranch will merge the contents of a transaction branch with the root object
98func (r *root) FoldTxBranch(txid string) {
99 // Start by doing a dry run of the merge
100 // If that fails, it bails out and the branch is deleted
101 if _, err := r.node.MergeBranch(txid, true); err != nil {
102 // Merge operation fails
103 r.DeleteTxBranch(txid)
104 } else {
105 r.node.MergeBranch(txid, false)
106 r.ExecuteCallbacks()
107 r.DeleteTxBranch(txid)
108 }
109}
110
111// ExecuteCallbacks will invoke all the callbacks linked to root object
112func (r *root) ExecuteCallbacks() {
113 r.mutex.Lock()
114 log.Debugf("ExecuteCallbacks has the ROOT lock : %+v", r)
115 defer r.mutex.Unlock()
116 defer log.Debugf("ExecuteCallbacks released the ROOT lock : %+v", r)
117 for len(r.Callbacks) > 0 {
118 callback := r.Callbacks[0]
119 r.Callbacks = r.Callbacks[1:]
120 go callback.Execute(nil)
121 }
122 for len(r.NotificationCallbacks) > 0 {
123 callback := r.NotificationCallbacks[0]
124 r.NotificationCallbacks = r.NotificationCallbacks[1:]
125 go callback.Execute(nil)
126 }
127}
128
129func (r *root) hasCallbacks() bool {
130 return len(r.Callbacks) == 0
131}
132
133// getCallbacks returns the available callbacks
134func (r *root) GetCallbacks() []CallbackTuple {
135 r.mutex.Lock()
136 log.Debugf("getCallbacks has the ROOT lock : %+v", r)
137 defer r.mutex.Unlock()
138 defer log.Debugf("getCallbacks released the ROOT lock : %+v", r)
139 return r.Callbacks
140}
141
142// getCallbacks returns the available notification callbacks
143func (r *root) GetNotificationCallbacks() []CallbackTuple {
144 r.mutex.Lock()
145 log.Debugf("GetNotificationCallbacks has the ROOT lock : %+v", r)
146 defer r.mutex.Unlock()
147 defer log.Debugf("GetNotificationCallbacks released the ROOT lock : %+v", r)
148 return r.NotificationCallbacks
149}
150
151// AddCallback inserts a new callback with its arguments
152func (r *root) AddCallback(callback CallbackFunction, args ...interface{}) {
153 r.mutex.Lock()
154 log.Debugf("AddCallback has the ROOT lock : %+v", r)
155 defer r.mutex.Unlock()
156 defer log.Debugf("AddCallback released the ROOT lock : %+v", r)
157 r.Callbacks = append(r.Callbacks, CallbackTuple{callback, args})
158}
159
160// AddNotificationCallback inserts a new notification callback with its arguments
161func (r *root) AddNotificationCallback(callback CallbackFunction, args ...interface{}) {
162 r.mutex.Lock()
163 log.Debugf("AddNotificationCallback has the ROOT lock : %+v", r)
164 defer r.mutex.Unlock()
165 defer log.Debugf("AddNotificationCallback released the ROOT lock : %+v", r)
166 r.NotificationCallbacks = append(r.NotificationCallbacks, CallbackTuple{callback, args})
167}
168
169func (r *root) syncParent(childRev Revision, txid string) {
170 data := proto.Clone(r.Proxy.ParentNode.Latest().GetData().(proto.Message))
171
172 for fieldName, _ := range ChildrenFields(data) {
173 childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0)
174 if reflect.TypeOf(childRev.GetData()) == reflect.TypeOf(childDataHolder.Interface()) {
175 childDataHolder = reflect.ValueOf(childRev.GetData())
176 reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder)
177 }
178 }
179
180 r.Proxy.ParentNode.Latest().SetConfig(NewDataRevision(r.Proxy.ParentNode.Root, data))
181 r.Proxy.ParentNode.Latest(txid).Finalize(false)
182}
183
184
185// Update modifies the content of an object at a given path with the provided data
186func (r *root) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
187 var result Revision
188
189 if makeBranch != nil {
190 // TODO: raise error
191 }
192
193 if r.hasCallbacks() {
194 // TODO: raise error
195 }
196
197 if txid != "" {
198 trackDirty := func(node *node) *Branch {
199 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
200 return node.MakeBranch(txid)
201 }
202 result = r.node.Update(path, data, strict, txid, trackDirty)
203 } else {
204 result = r.node.Update(path, data, strict, "", nil)
205 }
206
207 if result != nil {
208 if r.Proxy.FullPath != r.Proxy.Path {
209 r.syncParent(result, txid)
210 } else {
211 result.Finalize(false)
212 }
213 }
214
215 r.node.GetRoot().ExecuteCallbacks()
216
217 return result
218}
219
220// Add creates a new object at the given path with the provided data
221func (r *root) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
222 var result Revision
223
224 if makeBranch != nil {
225 // TODO: raise error
226 }
227
228 if r.hasCallbacks() {
229 // TODO: raise error
230 }
231
232 if txid != "" {
233 trackDirty := func(node *node) *Branch {
234 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
235 return node.MakeBranch(txid)
236 }
237 result = r.node.Add(path, data, txid, trackDirty)
238 } else {
239 result = r.node.Add(path, data, "", nil)
240 }
241
242 if result != nil {
243 result.Finalize(true)
244 r.node.GetRoot().ExecuteCallbacks()
245 }
246 return result
247}
248
249// Remove discards an object at a given path
250func (r *root) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
251 var result Revision
252
253 if makeBranch != nil {
254 // TODO: raise error
255 }
256
257 if r.hasCallbacks() {
258 // TODO: raise error
259 }
260
261 if txid != "" {
262 trackDirty := func(node *node) *Branch {
263 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
264 return node.MakeBranch(txid)
265 }
266 result = r.node.Remove(path, txid, trackDirty)
267 } else {
268 result = r.node.Remove(path, "", nil)
269 }
270
271 r.node.GetRoot().ExecuteCallbacks()
272
273 return result
274}
275
276// MakeLatest updates a branch with the latest node revision
277func (r *root) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
278 r.makeLatest(branch, revision, changeAnnouncement)
279}
280
281func (r *root) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
282 if r.RevisionClass.(reflect.Type) == reflect.TypeOf(PersistedRevision{}) {
283 return NewPersistedRevision(branch, data, children)
284 }
285
286 return NewNonPersistedRevision(r, branch, data, children)
287}
288
289func (r *root) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
290 r.node.makeLatest(branch, revision, changeAnnouncement)
291
292 if r.KvStore != nil && branch.Txid == "" {
293 tags := make(map[string]string)
294 for k, v := range r.node.Tags {
295 tags[k] = v.GetHash()
296 }
297 data := &rootData{
298 Latest: branch.GetLatest().GetHash(),
299 Tags: tags,
300 }
301 if blob, err := json.Marshal(data); err != nil {
302 // TODO report error
303 } else {
304 log.Debugf("Changing root to : %s", string(blob))
305 if err := r.KvStore.Put("root", blob); err != nil {
306 log.Errorf("failed to properly put value in kvstore - err: %s", err.Error())
307 }
308 }
309 }
310}
311
312type rootData struct {
313 Latest string `json:latest`
314 Tags map[string]string `json:tags`
315}