blob: 207df0973129e504a6629f84a9a289ac247572a9 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
Stephane Barbariee16186c2018-09-11 10:46:34 -040019// TODO: proper error handling
20// TODO: proper logging
21
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040022import (
23 "fmt"
24 "github.com/golang/protobuf/proto"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040025 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040026 "reflect"
27 "strings"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050028 "sync"
Stephane Barbarie802aca42019-05-21 12:19:28 -040029 "time"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040030)
31
Stephane Barbariedc5022d2018-11-19 15:21:44 -050032// When a branch has no transaction id, everything gets stored in NONE
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040033const (
34 NONE string = "none"
Stephane Barbarie802aca42019-05-21 12:19:28 -040035
36 // period to determine when data requires a refresh (in seconds)
37 // TODO: make this configurable?
38 DATA_REFRESH_PERIOD int64 = 5000
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040039)
40
Stephane Barbariedc5022d2018-11-19 15:21:44 -050041// Node interface is an abstraction of the node data structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040042type Node interface {
43 MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple)
44
45 // CRUD functions
46 Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision
47 Get(path string, hash string, depth int, deep bool, txid string) interface{}
48 Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision
49 Remove(path string, txid string, makeBranch MakeBranchFunction) Revision
50
51 MakeBranch(txid string) *Branch
52 DeleteBranch(txid string)
53 MergeBranch(txid string, dryRun bool) (Revision, error)
54
55 MakeTxBranch() string
56 DeleteTxBranch(txid string)
57 FoldTxBranch(txid string)
58
Stephane Barbariedc5022d2018-11-19 15:21:44 -050059 CreateProxy(path string, exclusive bool) *Proxy
60 GetProxy() *Proxy
Stephane Barbarie06c4a742018-10-01 11:09:32 -040061}
62
63type node struct {
Stephane Barbariec92d1072019-06-07 16:21:49 -040064 mutex sync.RWMutex
65 Root *root
66 Type interface{}
67
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068 Branches map[string]*Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -040069 Tags map[string]Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040070 Proxy *Proxy
71 EventBus *EventBus
72 AutoPrune bool
73}
74
Stephane Barbariedc5022d2018-11-19 15:21:44 -050075// ChangeTuple holds details of modifications made to a revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -040076type ChangeTuple struct {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040077 Type CallbackType
78 PreviousData interface{}
79 LatestData interface{}
Stephane Barbarie694e2b92018-09-07 12:17:36 -040080}
81
Stephane Barbariedc5022d2018-11-19 15:21:44 -050082// NewNode creates a new instance of the node data structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040083func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node {
84 n := &node{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040085
Stephane Barbarie126101e2018-10-11 16:18:48 -040086 n.Root = root
Stephane Barbarieec0919b2018-09-05 14:14:29 -040087 n.Branches = make(map[string]*Branch)
88 n.Tags = make(map[string]Revision)
89 n.Proxy = nil
90 n.EventBus = nil
91 n.AutoPrune = autoPrune
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040092
93 if IsProtoMessage(initialData) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040094 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040095 dataCopy := proto.Clone(initialData.(proto.Message))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040096 n.initialize(dataCopy, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040097 } else if reflect.ValueOf(initialData).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050098 // FIXME: this block does not reflect the original implementation
99 // it should be checking if the provided initial_data is already a type!??!
100 // it should be checked before IsProtoMessage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400101 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400102 } else {
103 // not implemented error
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400104 log.Errorf("cannot process initial data - %+v", initialData)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400105 }
106
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400107 return n
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400108}
109
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500110// MakeNode creates a new node in the tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400111func (n *node) MakeNode(data interface{}, txid string) *node {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400112 return NewNode(n.Root, data, true, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400113}
114
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500115// MakeRevision create a new revision of the node in the tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400116func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500117 return n.GetRoot().MakeRevision(branch, data, children)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400118}
119
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500120// makeLatest will mark the revision of a node as being the latest
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400121func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400122 // Keep a reference to the current revision
123 var previous string
124 if branch.GetLatest() != nil {
125 previous = branch.GetLatest().GetHash()
126 }
127
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500128 branch.AddRevision(revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400129
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400130 // If anything is new, then set the revision as the latest
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500131 if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400132 if revision.GetName() != "" {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400133 log.Debugw("saving-latest-data", log.Fields{"hash": revision.GetHash(), "data": revision.GetData()})
134 // Tag a timestamp to that revision
135 revision.SetLastUpdate()
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400136 GetRevCache().Cache.Store(revision.GetName(), revision)
137 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500138 branch.SetLatest(revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400139 }
140
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400141 // Delete the previous revision if anything has changed
142 if previous != "" && previous != branch.GetLatest().GetHash() {
143 branch.DeleteRevision(previous)
144 }
145
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400146 if changeAnnouncement != nil && branch.Txid == "" {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500147 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400148 for _, change := range changeAnnouncement {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400149 log.Debugw("adding-callback",
150 log.Fields{
151 "callbacks": n.Proxy.getCallbacks(change.Type),
152 "type": change.Type,
153 "previousData": change.PreviousData,
154 "latestData": change.LatestData,
155 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500156 n.Root.AddCallback(
157 n.Proxy.InvokeCallbacks,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400158 change.Type,
159 true,
160 change.PreviousData,
161 change.LatestData)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400162 }
163 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400164 }
165}
166
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500167// Latest returns the latest revision of node with or without the transaction id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400168func (n *node) Latest(txid ...string) Revision {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400169 var branch *Branch
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400170
171 if len(txid) > 0 && txid[0] != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500172 if branch = n.GetBranch(txid[0]); branch != nil {
173 return branch.GetLatest()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400174 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500175 } else if branch = n.GetBranch(NONE); branch != nil {
176 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400177 }
178 return nil
179}
180
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500181// initialize prepares the content of a node along with its possible ramifications
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400182func (n *node) initialize(data interface{}, txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500183 children := make(map[string][]Revision)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400184 for fieldName, field := range ChildrenFields(n.Type) {
185 _, fieldValue := GetAttributeValue(data, fieldName, 0)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400186
187 if fieldValue.IsValid() {
188 if field.IsContainer {
189 if field.Key != "" {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400190 for i := 0; i < fieldValue.Len(); i++ {
191 v := fieldValue.Index(i)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400192
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500193 if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil {
194 children[fieldName] = append(children[fieldName], rev)
195 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400196
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -0500197 // TODO: The following logic was ported from v1.0. Need to verify if it is required
198 //var keysSeen []string
199 //_, key := GetAttributeValue(v.Interface(), field.Key, 0)
200 //for _, k := range keysSeen {
201 // if k == key.String() {
202 // //log.Errorf("duplicate key - %s", k)
203 // }
204 //}
205 //keysSeen = append(keysSeen, key.String())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400206 }
207
208 } else {
209 for i := 0; i < fieldValue.Len(); i++ {
210 v := fieldValue.Index(i)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500211 if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil {
212 children[fieldName] = append(children[fieldName], newNodeRev)
213 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400214 }
215 }
216 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500217 if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil {
218 children[fieldName] = append(children[fieldName], newNodeRev)
219 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400220 }
221 } else {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400222 log.Errorf("field is invalid - %+v", fieldValue)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400223 }
224 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500225
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400226 branch := NewBranch(n, "", nil, n.AutoPrune)
227 rev := n.MakeRevision(branch, data, children)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400228 n.makeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400229
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400230 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500231 n.SetBranch(NONE, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400232 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500233 n.SetBranch(txid, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400234 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400235}
236
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500237// findRevByKey retrieves a specific revision from a node tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400238func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) {
239 for i, rev := range revs {
240 dataValue := reflect.ValueOf(rev.GetData())
241 dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0)
242
243 fieldValue := dataValue.Elem().FieldByName(dataStruct.Name)
244
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400245 a := fmt.Sprintf("%s", fieldValue.Interface())
246 b := fmt.Sprintf("%s", value)
247 if a == b {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248 return i, revs[i]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400249 }
250 }
251
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400252 return -1, nil
253}
254
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500255// Get retrieves the data from a node tree that resides at the specified path
Stephane Barbarieaa467942019-02-06 14:09:44 -0500256func (n *node) List(path string, hash string, depth int, deep bool, txid string) interface{} {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400257 n.mutex.Lock()
258 defer n.mutex.Unlock()
259
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500260 log.Debugw("node-list-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500261 if deep {
262 depth = -1
263 }
264
265 for strings.HasPrefix(path, "/") {
266 path = path[1:]
267 }
268
269 var branch *Branch
270 var rev Revision
271
272 if branch = n.GetBranch(txid); txid == "" || branch == nil {
273 branch = n.GetBranch(NONE)
274 }
275
276 if hash != "" {
277 rev = branch.GetRevision(hash)
278 } else {
279 rev = branch.GetLatest()
280 }
281
282 var result interface{}
283 var prList []interface{}
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400284 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500285 for _, revEntry := range pr {
286 prList = append(prList, revEntry.GetData())
287 }
288 result = prList
289 }
290
291 return result
292}
293
294// Get retrieves the data from a node tree that resides at the specified path
Stephane Barbarie260a5632019-02-26 16:12:49 -0500295func (n *node) Get(path string, hash string, depth int, reconcile bool, txid string) interface{} {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400296 n.mutex.Lock()
297 defer n.mutex.Unlock()
298
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400299 log.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid})
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400300
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400301 for strings.HasPrefix(path, "/") {
302 path = path[1:]
303 }
304
305 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400306 var rev Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400307
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500308 if branch = n.GetBranch(txid); txid == "" || branch == nil {
309 branch = n.GetBranch(NONE)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400310 }
311
312 if hash != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500313 rev = branch.GetRevision(hash)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400314 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500315 rev = branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400316 }
317
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500318 var result interface{}
Stephane Barbarie260a5632019-02-26 16:12:49 -0500319
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400320 // If there is no request to reconcile, try to get it from memory
Stephane Barbarie260a5632019-02-26 16:12:49 -0500321 if !reconcile {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400322 // Try to find an entry matching the path value from one of these sources
323 // 1. Start with the cache which stores revisions by watch names
324 // 2. Then look in the revision tree, especially if it's a sub-path such as /devices/1234/flows
Stephane Barbarie802aca42019-05-21 12:19:28 -0400325 // 3. Move on to the KV store if that path cannot be found or if the entry has expired
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400326 if entry, exists := GetRevCache().Cache.Load(path); exists && entry.(Revision) != nil {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400327 entryAge := time.Now().Sub(entry.(Revision).GetLastUpdate()).Nanoseconds() / int64(time.Millisecond)
328 if entryAge < DATA_REFRESH_PERIOD {
Stephane Barbariec92d1072019-06-07 16:21:49 -0400329 log.Debugw("using-cache-entry", log.Fields{
330 "path": path,
331 "hash": hash,
332 "age": entryAge,
333 })
Stephane Barbarie802aca42019-05-21 12:19:28 -0400334 return proto.Clone(entry.(Revision).GetData().(proto.Message))
335 } else {
336 log.Debugw("cache-entry-expired", log.Fields{"path": path, "hash": hash, "age": entryAge})
337 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400338 } else if result = n.getPath(rev.GetBranch().GetLatest(), path, depth); result != nil && reflect.ValueOf(result).IsValid() && !reflect.ValueOf(result).IsNil() {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400339 log.Debugw("using-rev-tree-entry", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid})
Stephane Barbarie260a5632019-02-26 16:12:49 -0500340 return result
Stephane Barbarie802aca42019-05-21 12:19:28 -0400341 } else {
342 log.Debugw("not-using-cache-entry", log.Fields{
343 "path": path,
344 "hash": hash, "depth": depth,
345 "reconcile": reconcile,
346 "txid": txid,
347 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500348 }
Stephane Barbarie802aca42019-05-21 12:19:28 -0400349 } else {
350 log.Debugw("reconcile-requested", log.Fields{
351 "path": path,
352 "hash": hash,
353 "reconcile": reconcile,
354 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500355 }
356
Stephane Barbarie802aca42019-05-21 12:19:28 -0400357 // If we got to this point, we are either trying to reconcile with the db
Stephane Barbarie260a5632019-02-26 16:12:49 -0500358 // or we simply failed at getting information from memory
359 if n.Root.KvStore != nil {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400360 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil && len(pr) > 0 {
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500361 // Did we receive a single or multiple revisions?
362 if len(pr) > 1 {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400363 var revs []interface{}
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500364 for _, revEntry := range pr {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400365 revs = append(revs, revEntry.GetData())
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500366 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400367 result = revs
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500368 } else {
369 result = pr[0].GetData()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500370 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500371 }
372 }
373
374 return result
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400375}
376
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400377//getPath traverses the specified path and retrieves the data associated to it
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400378func (n *node) getPath(rev Revision, path string, depth int) interface{} {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400379 if path == "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400380 return n.getData(rev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400381 }
382
383 partition := strings.SplitN(path, "/", 2)
384 name := partition[0]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400385
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400386 if len(partition) < 2 {
387 path = ""
388 } else {
389 path = partition[1]
390 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400391
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400392 names := ChildrenFields(n.Type)
393 field := names[name]
394
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500395 if field != nil && field.IsContainer {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500396 children := make([]Revision, len(rev.GetChildren(name)))
397 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500398
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400399 if field.Key != "" {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400400 if path != "" {
401 partition = strings.SplitN(path, "/", 2)
402 key := partition[0]
403 path = ""
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400404 keyValue := field.KeyFromStr(key)
405 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400406 return nil
407 } else {
408 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400409 return childNode.getPath(childRev, path, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400410 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400411 } else {
412 var response []interface{}
413 for _, childRev := range children {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400414 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400415 value := childNode.getData(childRev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400416 response = append(response, value)
417 }
418 return response
419 }
420 } else {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400421 var response []interface{}
422 if path != "" {
423 // TODO: raise error
424 return response
425 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500426 for _, childRev := range children {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400427 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400428 value := childNode.getData(childRev, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400429 response = append(response, value)
430 }
431 return response
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400432 }
433 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500434
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500435 childRev := rev.GetChildren(name)[0]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500436 childNode := childRev.GetNode()
437 return childNode.getPath(childRev, path, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400438}
439
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500440// getData retrieves the data from a node revision
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400441func (n *node) getData(rev Revision, depth int) interface{} {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500442 msg := rev.GetBranch().GetLatest().Get(depth)
Stephane Barbariea188d942018-10-16 16:43:04 -0400443 var modifiedMsg interface{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400444
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500445 if n.GetProxy() != nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500446 log.Debugw("invoking-get-callbacks", log.Fields{"data": msg})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500447 if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400448 msg = modifiedMsg
449 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400450
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400451 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500452
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400453 return msg
454}
455
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500456// Update changes the content of a node at the specified path with the provided data
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400457func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400458 n.mutex.Lock()
459 defer n.mutex.Unlock()
460
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500461 log.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500462
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400463 for strings.HasPrefix(path, "/") {
464 path = path[1:]
465 }
466
467 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400468 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500469 branch = n.GetBranch(NONE)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500470 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400471 branch = makeBranch(n)
472 }
473
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500474 if branch.GetLatest() != nil {
475 log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data)
476 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400477 if path == "" {
478 return n.doUpdate(branch, data, strict)
479 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400480
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500481 rev := branch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400482
483 partition := strings.SplitN(path, "/", 2)
484 name := partition[0]
485
486 if len(partition) < 2 {
487 path = ""
488 } else {
489 path = partition[1]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400490 }
491
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400492 field := ChildrenFields(n.Type)[name]
493 var children []Revision
494
Stephane Barbarieaa467942019-02-06 14:09:44 -0500495 if field == nil {
496 return n.doUpdate(branch, data, strict)
497 }
498
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400499 if field.IsContainer {
500 if path == "" {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400501 log.Errorf("cannot update a list")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400502 } else if field.Key != "" {
503 partition := strings.SplitN(path, "/", 2)
504 key := partition[0]
505 if len(partition) < 2 {
506 path = ""
507 } else {
508 path = partition[1]
509 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400510 keyValue := field.KeyFromStr(key)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500511
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500512 children = make([]Revision, len(rev.GetChildren(name)))
513 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500514
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400515 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400516
517 if childRev == nil {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400518 log.Debugw("child-revision-is-nil", log.Fields{"key": keyValue})
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400519 return branch.GetLatest()
520 }
521
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400522 childNode := childRev.GetNode()
Stephane Barbariea188d942018-10-16 16:43:04 -0400523
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500524 // Save proxy in child node to ensure callbacks are called later on
Stephane Barbaried62ac4e2019-02-05 14:08:38 -0500525 // only assign in cases of non sub-folder proxies, i.e. "/"
526 if childNode.Proxy == nil && n.Proxy != nil && n.Proxy.getFullPath() == "" {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500527 childNode.Proxy = n.Proxy
528 }
529
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400530 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400531
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400532 if newChildRev.GetHash() == childRev.GetHash() {
533 if newChildRev != childRev {
534 log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev)
535 newChildRev.ClearHash()
536 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400537 log.Debugw("child-revisions-have-matching-hash", log.Fields{"hash": childRev.GetHash(), "key": keyValue})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500538 return branch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400539 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400540
541 _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500542
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400543 _newKeyType := fmt.Sprintf("%s", newKey)
544 _keyValueType := fmt.Sprintf("%s", keyValue)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500545
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400546 if _newKeyType != _keyValueType {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400547 log.Errorf("cannot change key field")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400548 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500549
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500550 // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400551 newChildRev.SetName(name + "/" + _keyValueType)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400552
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400553 branch.LatestLock.Lock()
554 defer branch.LatestLock.Unlock()
555
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400556 if idx >= 0 {
557 children[idx] = newChildRev
558 } else {
559 children = append(children, newChildRev)
560 }
561
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500562 updatedRev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500563
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500564 n.makeLatest(branch, updatedRev, nil)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400565 updatedRev.ChildDrop(name, childRev.GetHash())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500566
Stephane Barbariea188d942018-10-16 16:43:04 -0400567 return newChildRev
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500568
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400569 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400570 log.Errorf("cannot index into container with no keys")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400571 }
572 } else {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500573 childRev := rev.GetChildren(name)[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400574 childNode := childRev.GetNode()
575 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400576
577 branch.LatestLock.Lock()
578 defer branch.LatestLock.Unlock()
579
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500580 updatedRev := rev.UpdateChildren(name, []Revision{newChildRev}, branch)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500581 n.makeLatest(branch, updatedRev, nil)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500582
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400583 updatedRev.ChildDrop(name, childRev.GetHash())
584
Stephane Barbariea188d942018-10-16 16:43:04 -0400585 return newChildRev
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400586 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500587
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400588 return nil
589}
590
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400591func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400592 log.Debugw("comparing-types", log.Fields{"expected": reflect.ValueOf(n.Type).Type(), "actual": reflect.TypeOf(data)})
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400593
594 if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() {
595 // TODO raise error
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400596 log.Errorw("types-do-not-match: %+v", log.Fields{"actual": reflect.TypeOf(data), "expected": n.Type})
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400597 return nil
598 }
599
600 // TODO: validate that this actually works
601 //if n.hasChildren(data) {
602 // return nil
603 //}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400604
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500605 if n.GetProxy() != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400606 log.Debug("invoking proxy PRE_UPDATE Callbacks")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500607 n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400608 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500609
610 if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400611 if strict {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400612 // TODO: checkAccessViolations(data, Branch.GetLatest.data)
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400613 log.Debugf("checking access violations")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400614 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500615
616 rev := branch.GetLatest().UpdateData(data, branch)
617 changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500618 n.makeLatest(branch, rev, changes)
619
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400620 return rev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400621 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500622 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400623}
624
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500625// Add inserts a new node at the specified path with the provided data
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400626func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400627 n.mutex.Lock()
628 defer n.mutex.Unlock()
629
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500630 log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500631
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400632 for strings.HasPrefix(path, "/") {
633 path = path[1:]
634 }
635 if path == "" {
636 // TODO raise error
Stephane Barbarie126101e2018-10-11 16:18:48 -0400637 log.Errorf("cannot add for non-container mode")
Stephane Barbarieaa467942019-02-06 14:09:44 -0500638 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400639 }
640
641 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400642 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500643 branch = n.GetBranch(NONE)
644 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400645 branch = makeBranch(n)
646 }
647
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500648 rev := branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400649
650 partition := strings.SplitN(path, "/", 2)
651 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400652
653 if len(partition) < 2 {
654 path = ""
655 } else {
656 path = partition[1]
657 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400658
659 field := ChildrenFields(n.Type)[name]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500660
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400661 var children []Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400662
663 if field.IsContainer {
664 if path == "" {
665 if field.Key != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500666 if n.GetProxy() != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400667 log.Debug("invoking proxy PRE_ADD Callbacks")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500668 n.GetProxy().InvokeCallbacks(PRE_ADD, false, data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400669 }
670
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500671 children = make([]Revision, len(rev.GetChildren(name)))
672 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500673
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400674 _, key := GetAttributeValue(data, field.Key, 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500675
Stephane Barbarie126101e2018-10-11 16:18:48 -0400676 if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400677 // TODO raise error
Stephane Barbarie260a5632019-02-26 16:12:49 -0500678 log.Warnw("duplicate-key-found", log.Fields{"key": key.String()})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500679 return exists
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400680 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500681 childRev := n.MakeNode(data, "").Latest()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500682
683 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400684 childRev.SetName(name + "/" + key.String())
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500685
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400686 branch.LatestLock.Lock()
687 defer branch.LatestLock.Unlock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500688
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500689 children = append(children, childRev)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400690
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400691 updatedRev := rev.UpdateChildren(name, children, branch)
692 changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}}
693 childRev.SetupWatch(childRev.GetName())
694
695 n.makeLatest(branch, updatedRev, changes)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500696
697 return childRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400698 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500699 log.Errorf("cannot add to non-keyed container")
700
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400701 } else if field.Key != "" {
702 partition := strings.SplitN(path, "/", 2)
703 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400704 if len(partition) < 2 {
705 path = ""
706 } else {
707 path = partition[1]
708 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400709 keyValue := field.KeyFromStr(key)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500710
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500711 children = make([]Revision, len(rev.GetChildren(name)))
712 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500713
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400714 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500715
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400716 if childRev == nil {
717 return branch.GetLatest()
718 }
719
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400720 childNode := childRev.GetNode()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400721 newChildRev := childNode.Add(path, data, txid, makeBranch)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500722
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500723 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400724 newChildRev.SetName(name + "/" + keyValue.(string))
725
726 branch.LatestLock.Lock()
727 defer branch.LatestLock.Unlock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500728
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400729 if idx >= 0 {
730 children[idx] = newChildRev
731 } else {
732 children = append(children, newChildRev)
733 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500734
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400735 updatedRev := rev.UpdateChildren(name, children, branch)
736 n.makeLatest(branch, updatedRev, nil)
737
738 updatedRev.ChildDrop(name, childRev.GetHash())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500739
740 return newChildRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400741 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400742 log.Errorf("cannot add to non-keyed container")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400743 }
744 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400745 log.Errorf("cannot add to non-container field")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400746 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500747
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400748 return nil
749}
750
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500751// Remove eliminates a node at the specified path
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400752func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400753 n.mutex.Lock()
754 defer n.mutex.Unlock()
755
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500756 log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500757
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400758 for strings.HasPrefix(path, "/") {
759 path = path[1:]
760 }
761 if path == "" {
762 // TODO raise error
Stephane Barbarie126101e2018-10-11 16:18:48 -0400763 log.Errorf("cannot remove for non-container mode")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400764 }
765 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400766 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500767 branch = n.GetBranch(NONE)
768 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400769 branch = makeBranch(n)
770 }
771
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500772 rev := branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400773
774 partition := strings.SplitN(path, "/", 2)
775 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400776 if len(partition) < 2 {
777 path = ""
778 } else {
779 path = partition[1]
780 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400781
782 field := ChildrenFields(n.Type)[name]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400783 var children []Revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400784 postAnnouncement := []ChangeTuple{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400785
786 if field.IsContainer {
787 if path == "" {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500788 log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400789 } else if field.Key != "" {
790 partition := strings.SplitN(path, "/", 2)
791 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400792 if len(partition) < 2 {
793 path = ""
794 } else {
795 path = partition[1]
796 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500797
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400798 keyValue := field.KeyFromStr(key)
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500799 children = make([]Revision, len(rev.GetChildren(name)))
800 copy(children, rev.GetChildren(name))
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500801
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400802 if path != "" {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400803 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
804 childNode := childRev.GetNode()
805 if childNode.Proxy == nil {
806 childNode.Proxy = n.Proxy
807 }
808 newChildRev := childNode.Remove(path, txid, makeBranch)
809
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400810 branch.LatestLock.Lock()
811 defer branch.LatestLock.Unlock()
812
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400813 if idx >= 0 {
814 children[idx] = newChildRev
815 } else {
816 children = append(children, newChildRev)
817 }
818
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400819 rev.SetChildren(name, children)
820 branch.GetLatest().Drop(txid, false)
821 n.makeLatest(branch, rev, nil)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500822 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400823 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400824 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500825
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400826 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil && idx >= 0 {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500827 if n.GetProxy() != nil {
828 data := childRev.GetData()
829 n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
830 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
831 } else {
832 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
833 }
834
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400835 childRev.StorageDrop(txid, true)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400836 GetRevCache().Cache.Delete(childRev.GetName())
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400837
838 branch.LatestLock.Lock()
839 defer branch.LatestLock.Unlock()
840
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500841 children = append(children[:idx], children[idx+1:]...)
842 rev.SetChildren(name, children)
843
844 branch.GetLatest().Drop(txid, false)
845 n.makeLatest(branch, rev, postAnnouncement)
846
847 return rev
848 } else {
849 log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)})
850 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400851 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500852 log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500853
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400854 } else {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500855 log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400856 }
857
858 return nil
859}
860
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400861// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
862
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500863// MakeBranchFunction is a type for function references intented to create a branch
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400864type MakeBranchFunction func(*node) *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400865
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500866// MakeBranch creates a new branch for the provided transaction id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400867func (n *node) MakeBranch(txid string) *Branch {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500868 branchPoint := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400869 branch := NewBranch(n, txid, branchPoint, true)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500870 n.SetBranch(txid, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400871 return branch
872}
873
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500874// DeleteBranch removes a branch with the specified id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400875func (n *node) DeleteBranch(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400876 delete(n.Branches, txid)
877}
878
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400879func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400880 f := func(rev Revision) Revision {
881 childBranch := rev.GetBranch()
882
883 if childBranch.Txid == txid {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400884 rev, _ = childBranch.Node.MergeBranch(txid, dryRun)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400885 }
886
887 return rev
888 }
889 return f
890}
891
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500892// MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400893func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500894 srcBranch := n.GetBranch(txid)
895 dstBranch := n.GetBranch(NONE)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400896
897 forkRev := srcBranch.Origin
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500898 srcRev := srcBranch.GetLatest()
899 dstRev := dstBranch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400900
901 rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
902
903 if !dryRun {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500904 if rev != nil {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400905 rev.SetName(dstRev.GetName())
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500906 n.makeLatest(dstBranch, rev, changes)
907 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500908 n.DeleteBranch(txid)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400909 }
910
Stephane Barbariee16186c2018-09-11 10:46:34 -0400911 // TODO: return proper error when one occurs
912 return rev, nil
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400913}
914
915// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
916
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400917//func (n *node) diff(hash1, hash2, txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400918// branch := n.Branches[txid]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500919// rev1 := branch.GetHash(hash1)
920// rev2 := branch.GetHash(hash2)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400921//
922// if rev1.GetHash() == rev2.GetHash() {
923// // empty patch
924// } else {
925// // translate data to json and generate patch
926// patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData())
927// patch.
928// }
929//}
930
931// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
932
933// TODO: is tag mgmt used in the python implementation? Need to validate
934
935// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
936
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400937func (n *node) hasChildren(data interface{}) bool {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400938 for fieldName, field := range ChildrenFields(n.Type) {
939 _, fieldValue := GetAttributeValue(data, fieldName, 0)
940
941 if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() {
942 log.Error("cannot update external children")
943 return true
944 }
945 }
946
947 return false
948}
949
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400950// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400951
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500952// CreateProxy returns a reference to a sub-tree of the data model
953func (n *node) CreateProxy(path string, exclusive bool) *Proxy {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500954 return n.createProxy(path, path, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400955}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500956
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500957func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbariec92d1072019-06-07 16:21:49 -0400958 log.Debugw("node-create-proxy", log.Fields{
959 "node-type": reflect.ValueOf(n.Type).Type(),
960 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
961 "path": path,
962 "fullPath": fullPath,
963 })
964
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400965 for strings.HasPrefix(path, "/") {
966 path = path[1:]
967 }
968 if path == "" {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500969 return n.makeProxy(path, fullPath, parentNode, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400970 }
971
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500972 rev := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400973 partition := strings.SplitN(path, "/", 2)
974 name := partition[0]
Stephane Barbariec92d1072019-06-07 16:21:49 -0400975 var nodeType interface{}
976 // Node type is chosen depending on if we have reached the end of path or not
Stephane Barbarie126101e2018-10-11 16:18:48 -0400977 if len(partition) < 2 {
978 path = ""
Stephane Barbariec92d1072019-06-07 16:21:49 -0400979 nodeType = n.Type
Stephane Barbarie126101e2018-10-11 16:18:48 -0400980 } else {
981 path = partition[1]
Stephane Barbariec92d1072019-06-07 16:21:49 -0400982 nodeType = parentNode.Type
Stephane Barbarie126101e2018-10-11 16:18:48 -0400983 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400984
Stephane Barbariec92d1072019-06-07 16:21:49 -0400985 field := ChildrenFields(nodeType)[name]
986
987 if field != nil {
988 if field.IsContainer {
989 log.Debugw("container-field", log.Fields{
990 "node-type": reflect.ValueOf(n.Type).Type(),
991 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
992 "path": path,
993 "name": name,
994 })
995 if path == "" {
996 log.Debugw("folder-proxy", log.Fields{
997 "node-type": reflect.ValueOf(n.Type).Type(),
998 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
999 "fullPath": fullPath,
1000 "name": name,
1001 })
1002 newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "")
1003 return newNode.makeProxy(path, fullPath, parentNode, exclusive)
1004 } else if field.Key != "" {
1005 log.Debugw("key-proxy", log.Fields{
1006 "node-type": reflect.ValueOf(n.Type).Type(),
1007 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1008 "fullPath": fullPath,
1009 "name": name,
1010 })
1011 partition := strings.SplitN(path, "/", 2)
1012 key := partition[0]
1013 if len(partition) < 2 {
1014 path = ""
1015 } else {
1016 path = partition[1]
1017 }
1018 keyValue := field.KeyFromStr(key)
1019 var children []Revision
1020 children = make([]Revision, len(rev.GetChildren(name)))
1021 copy(children, rev.GetChildren(name))
1022
1023 // Try to find a matching revision in memory
1024 // If not found try the db
1025 var childRev Revision
1026 if _, childRev = n.findRevByKey(children, field.Key, keyValue); childRev != nil {
1027 log.Debugw("found-revision-matching-key-in-memory", log.Fields{
1028 "node-type": reflect.ValueOf(n.Type).Type(),
1029 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1030 "fullPath": fullPath,
1031 "name": name,
1032 })
1033 } else if revs := n.GetBranch(NONE).GetLatest().LoadFromPersistence(fullPath, "", nil); revs != nil && len(revs) > 0 {
1034 log.Debugw("found-revision-matching-key-in-db", log.Fields{
1035 "node-type": reflect.ValueOf(n.Type).Type(),
1036 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1037 "fullPath": fullPath,
1038 "name": name,
1039 })
1040 childRev = revs[0]
1041 } else {
1042 log.Debugw("no-revision-matching-key", log.Fields{
1043 "node-type": reflect.ValueOf(n.Type).Type(),
1044 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1045 "fullPath": fullPath,
1046 "name": name,
1047 })
1048 }
1049 if childRev != nil {
1050 childNode := childRev.GetNode()
1051 return childNode.createProxy(path, fullPath, n, exclusive)
1052 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001053 } else {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001054 log.Errorw("cannot-access-index-of-empty-container", log.Fields{
1055 "node-type": reflect.ValueOf(n.Type).Type(),
1056 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1057 "path": path,
1058 "name": name,
1059 })
Stephane Barbarie933b09b2019-01-09 11:12:09 -05001060 }
Stephane Barbarie126101e2018-10-11 16:18:48 -04001061 } else {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001062 log.Debugw("non-container-field", log.Fields{
1063 "node-type": reflect.ValueOf(n.Type).Type(),
1064 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1065 "path": path,
1066 "name": name,
1067 })
1068 childRev := rev.GetChildren(name)[0]
1069 childNode := childRev.GetNode()
1070 return childNode.createProxy(path, fullPath, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001071 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001072 } else {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001073 log.Debugw("field-object-is-nil", log.Fields{
1074 "node-type": reflect.ValueOf(n.Type).Type(),
1075 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1076 "fullPath": fullPath,
1077 "name": name,
1078 })
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001079 }
1080
Stephane Barbariec92d1072019-06-07 16:21:49 -04001081 log.Warnw("cannot-create-proxy", log.Fields{
1082 "node-type": reflect.ValueOf(n.Type).Type(),
1083 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1084 "path": path,
1085 "fullPath": fullPath,
1086 "latest-rev": rev.GetHash(),
1087 })
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001088 return nil
1089}
1090
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001091func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001092 log.Debugw("node-make-proxy", log.Fields{
1093 "node-type": reflect.ValueOf(n.Type).Type(),
1094 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1095 "path": path,
1096 "fullPath": fullPath,
1097 })
1098
Stephane Barbarie126101e2018-10-11 16:18:48 -04001099 r := &root{
1100 node: n,
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001101 Callbacks: n.Root.GetCallbacks(),
1102 NotificationCallbacks: n.Root.GetNotificationCallbacks(),
Stephane Barbarie126101e2018-10-11 16:18:48 -04001103 DirtyNodes: n.Root.DirtyNodes,
1104 KvStore: n.Root.KvStore,
1105 Loading: n.Root.Loading,
1106 RevisionClass: n.Root.RevisionClass,
1107 }
1108
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001109 if n.Proxy == nil {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001110 log.Debugw("constructing-new-proxy", log.Fields{
1111 "node-type": reflect.ValueOf(n.Type).Type(),
1112 "parent-node-type": reflect.ValueOf(parentNode.Type).Type(),
1113 "path": path,
1114 "fullPath": fullPath,
1115 })
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001116 n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001117 } else {
Stephane Barbariec92d1072019-06-07 16:21:49 -04001118 log.Debugw("node-has-existing-proxy", log.Fields{
1119 "node-type": reflect.ValueOf(n.Proxy.Node.Type).Type(),
1120 "parent-node-type": reflect.ValueOf(n.Proxy.ParentNode.Type).Type(),
1121 "path": n.Proxy.Path,
1122 "fullPath": n.Proxy.FullPath,
1123 })
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001124 if n.Proxy.Exclusive {
1125 log.Error("node is already owned exclusively")
1126 }
1127 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001128
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001129 return n.Proxy
1130}
1131
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001132func (n *node) makeEventBus() *EventBus {
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001133 if n.EventBus == nil {
1134 n.EventBus = NewEventBus()
1135 }
1136 return n.EventBus
1137}
1138
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001139func (n *node) SetProxy(proxy *Proxy) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001140 n.Proxy = proxy
1141}
1142
1143func (n *node) GetProxy() *Proxy {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001144 return n.Proxy
1145}
1146
1147func (n *node) GetBranch(key string) *Branch {
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001148 if n.Branches != nil {
1149 if branch, exists := n.Branches[key]; exists {
1150 return branch
1151 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001152 }
1153 return nil
1154}
1155
1156func (n *node) SetBranch(key string, branch *Branch) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001157 n.Branches[key] = branch
1158}
1159
1160func (n *node) GetRoot() *root {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001161 return n.Root
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001162}