blob: 9c450afe376754983bb3b8ba472234500fba7ffc [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 Barbarie802aca42019-05-21 12:19:28 -040064 mutex sync.RWMutex
Stephane Barbarie126101e2018-10-11 16:18:48 -040065 Root *root
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040066 Type interface{}
67 Branches map[string]*Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -040068 Tags map[string]Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040069 Proxy *Proxy
70 EventBus *EventBus
71 AutoPrune bool
72}
73
Stephane Barbariedc5022d2018-11-19 15:21:44 -050074// ChangeTuple holds details of modifications made to a revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -040075type ChangeTuple struct {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040076 Type CallbackType
77 PreviousData interface{}
78 LatestData interface{}
Stephane Barbarie694e2b92018-09-07 12:17:36 -040079}
80
Stephane Barbariedc5022d2018-11-19 15:21:44 -050081// NewNode creates a new instance of the node data structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040082func NewNode(root *root, initialData interface{}, autoPrune bool, txid string) *node {
83 n := &node{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040084
Stephane Barbarie126101e2018-10-11 16:18:48 -040085 n.Root = root
Stephane Barbarieec0919b2018-09-05 14:14:29 -040086 n.Branches = make(map[string]*Branch)
87 n.Tags = make(map[string]Revision)
88 n.Proxy = nil
89 n.EventBus = nil
90 n.AutoPrune = autoPrune
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040091
92 if IsProtoMessage(initialData) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -040093 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040094 dataCopy := proto.Clone(initialData.(proto.Message))
Stephane Barbarieec0919b2018-09-05 14:14:29 -040095 n.initialize(dataCopy, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040096 } else if reflect.ValueOf(initialData).IsValid() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050097 // FIXME: this block does not reflect the original implementation
98 // it should be checking if the provided initial_data is already a type!??!
99 // it should be checked before IsProtoMessage
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400100 n.Type = reflect.ValueOf(initialData).Interface()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400101 } else {
102 // not implemented error
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400103 log.Errorf("cannot process initial data - %+v", initialData)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400104 }
105
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400106 return n
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400107}
108
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500109// MakeNode creates a new node in the tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400110func (n *node) MakeNode(data interface{}, txid string) *node {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400111 return NewNode(n.Root, data, true, txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400112}
113
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500114// MakeRevision create a new revision of the node in the tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400115func (n *node) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500116 return n.GetRoot().MakeRevision(branch, data, children)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400117}
118
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500119// makeLatest will mark the revision of a node as being the latest
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400120func (n *node) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400121 // Keep a reference to the current revision
122 var previous string
123 if branch.GetLatest() != nil {
124 previous = branch.GetLatest().GetHash()
125 }
126
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500127 branch.AddRevision(revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400128
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400129 // If anything is new, then set the revision as the latest
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500130 if branch.GetLatest() == nil || revision.GetHash() != branch.GetLatest().GetHash() {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400131 if revision.GetName() != "" {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400132 log.Debugw("saving-latest-data", log.Fields{"hash": revision.GetHash(), "data": revision.GetData()})
133 // Tag a timestamp to that revision
134 revision.SetLastUpdate()
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400135 GetRevCache().Cache.Store(revision.GetName(), revision)
136 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500137 branch.SetLatest(revision)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400138 }
139
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400140 // Delete the previous revision if anything has changed
141 if previous != "" && previous != branch.GetLatest().GetHash() {
142 branch.DeleteRevision(previous)
143 }
144
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400145 if changeAnnouncement != nil && branch.Txid == "" {
Stephane Barbarie260a5632019-02-26 16:12:49 -0500146 if n.Proxy != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400147 for _, change := range changeAnnouncement {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400148 log.Debugw("adding-callback",
149 log.Fields{
150 "callbacks": n.Proxy.getCallbacks(change.Type),
151 "type": change.Type,
152 "previousData": change.PreviousData,
153 "latestData": change.LatestData,
154 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500155 n.Root.AddCallback(
156 n.Proxy.InvokeCallbacks,
Stephane Barbarie126101e2018-10-11 16:18:48 -0400157 change.Type,
158 true,
159 change.PreviousData,
160 change.LatestData)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400161 }
162 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400163 }
164}
165
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500166// Latest returns the latest revision of node with or without the transaction id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400167func (n *node) Latest(txid ...string) Revision {
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400168 var branch *Branch
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400169
170 if len(txid) > 0 && txid[0] != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500171 if branch = n.GetBranch(txid[0]); branch != nil {
172 return branch.GetLatest()
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400173 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500174 } else if branch = n.GetBranch(NONE); branch != nil {
175 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400176 }
177 return nil
178}
179
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500180// initialize prepares the content of a node along with its possible ramifications
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400181func (n *node) initialize(data interface{}, txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500182 children := make(map[string][]Revision)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400183 for fieldName, field := range ChildrenFields(n.Type) {
184 _, fieldValue := GetAttributeValue(data, fieldName, 0)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400185
186 if fieldValue.IsValid() {
187 if field.IsContainer {
188 if field.Key != "" {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400189 for i := 0; i < fieldValue.Len(); i++ {
190 v := fieldValue.Index(i)
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400191
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500192 if rev := n.MakeNode(v.Interface(), txid).Latest(txid); rev != nil {
193 children[fieldName] = append(children[fieldName], rev)
194 }
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -0400195
Stephane Barbarie1e28f3e2019-02-08 15:45:20 -0500196 // TODO: The following logic was ported from v1.0. Need to verify if it is required
197 //var keysSeen []string
198 //_, key := GetAttributeValue(v.Interface(), field.Key, 0)
199 //for _, k := range keysSeen {
200 // if k == key.String() {
201 // //log.Errorf("duplicate key - %s", k)
202 // }
203 //}
204 //keysSeen = append(keysSeen, key.String())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400205 }
206
207 } else {
208 for i := 0; i < fieldValue.Len(); i++ {
209 v := fieldValue.Index(i)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210 if newNodeRev := n.MakeNode(v.Interface(), txid).Latest(); newNodeRev != nil {
211 children[fieldName] = append(children[fieldName], newNodeRev)
212 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400213 }
214 }
215 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500216 if newNodeRev := n.MakeNode(fieldValue.Interface(), txid).Latest(); newNodeRev != nil {
217 children[fieldName] = append(children[fieldName], newNodeRev)
218 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400219 }
220 } else {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400221 log.Errorf("field is invalid - %+v", fieldValue)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400222 }
223 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500224
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400225 branch := NewBranch(n, "", nil, n.AutoPrune)
226 rev := n.MakeRevision(branch, data, children)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400227 n.makeLatest(branch, rev, nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400228
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400229 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500230 n.SetBranch(NONE, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400231 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500232 n.SetBranch(txid, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400233 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400234}
235
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500236// findRevByKey retrieves a specific revision from a node tree
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400237func (n *node) findRevByKey(revs []Revision, keyName string, value interface{}) (int, Revision) {
238 for i, rev := range revs {
239 dataValue := reflect.ValueOf(rev.GetData())
240 dataStruct := GetAttributeStructure(rev.GetData(), keyName, 0)
241
242 fieldValue := dataValue.Elem().FieldByName(dataStruct.Name)
243
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400244 a := fmt.Sprintf("%s", fieldValue.Interface())
245 b := fmt.Sprintf("%s", value)
246 if a == b {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500247 return i, revs[i]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400248 }
249 }
250
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400251 return -1, nil
252}
253
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500254// Get retrieves the data from a node tree that resides at the specified path
Stephane Barbarieaa467942019-02-06 14:09:44 -0500255func (n *node) List(path string, hash string, depth int, deep bool, txid string) interface{} {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400256 n.mutex.Lock()
257 defer n.mutex.Unlock()
258
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500259 log.Debugw("node-list-request", log.Fields{"path": path, "hash": hash, "depth": depth, "deep": deep, "txid": txid})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500260 if deep {
261 depth = -1
262 }
263
264 for strings.HasPrefix(path, "/") {
265 path = path[1:]
266 }
267
268 var branch *Branch
269 var rev Revision
270
271 if branch = n.GetBranch(txid); txid == "" || branch == nil {
272 branch = n.GetBranch(NONE)
273 }
274
275 if hash != "" {
276 rev = branch.GetRevision(hash)
277 } else {
278 rev = branch.GetLatest()
279 }
280
281 var result interface{}
282 var prList []interface{}
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400283 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500284 for _, revEntry := range pr {
285 prList = append(prList, revEntry.GetData())
286 }
287 result = prList
288 }
289
290 return result
291}
292
293// Get retrieves the data from a node tree that resides at the specified path
Stephane Barbarie260a5632019-02-26 16:12:49 -0500294func (n *node) Get(path string, hash string, depth int, reconcile bool, txid string) interface{} {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400295 n.mutex.Lock()
296 defer n.mutex.Unlock()
297
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400298 log.Debugw("node-get-request", log.Fields{"path": path, "hash": hash, "depth": depth, "reconcile": reconcile, "txid": txid})
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400299
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400300 for strings.HasPrefix(path, "/") {
301 path = path[1:]
302 }
303
304 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400305 var rev Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400306
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500307 if branch = n.GetBranch(txid); txid == "" || branch == nil {
308 branch = n.GetBranch(NONE)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400309 }
310
311 if hash != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500312 rev = branch.GetRevision(hash)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400313 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500314 rev = branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400315 }
316
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500317 var result interface{}
Stephane Barbarie260a5632019-02-26 16:12:49 -0500318
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400319 // If there is no request to reconcile, try to get it from memory
Stephane Barbarie260a5632019-02-26 16:12:49 -0500320 if !reconcile {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400321 // Try to find an entry matching the path value from one of these sources
322 // 1. Start with the cache which stores revisions by watch names
323 // 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 -0400324 // 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 -0400325 if entry, exists := GetRevCache().Cache.Load(path); exists && entry.(Revision) != nil {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400326 entryAge := time.Now().Sub(entry.(Revision).GetLastUpdate()).Nanoseconds() / int64(time.Millisecond)
327 if entryAge < DATA_REFRESH_PERIOD {
328 log.Debugw("using-cache-entry", log.Fields{"path": path, "hash": hash, "age": entryAge})
329 return proto.Clone(entry.(Revision).GetData().(proto.Message))
330 } else {
331 log.Debugw("cache-entry-expired", log.Fields{"path": path, "hash": hash, "age": entryAge})
332 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400333 } 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 -0400334 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 -0500335 return result
Stephane Barbarie802aca42019-05-21 12:19:28 -0400336 } else {
337 log.Debugw("not-using-cache-entry", log.Fields{
338 "path": path,
339 "hash": hash, "depth": depth,
340 "reconcile": reconcile,
341 "txid": txid,
342 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500343 }
Stephane Barbarie802aca42019-05-21 12:19:28 -0400344 } else {
345 log.Debugw("reconcile-requested", log.Fields{
346 "path": path,
347 "hash": hash,
348 "reconcile": reconcile,
349 })
Stephane Barbarie260a5632019-02-26 16:12:49 -0500350 }
351
Stephane Barbarie802aca42019-05-21 12:19:28 -0400352 // If we got to this point, we are either trying to reconcile with the db
Stephane Barbarie260a5632019-02-26 16:12:49 -0500353 // or we simply failed at getting information from memory
354 if n.Root.KvStore != nil {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400355 if pr := rev.LoadFromPersistence(path, txid, nil); pr != nil && len(pr) > 0 {
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500356 // Did we receive a single or multiple revisions?
357 if len(pr) > 1 {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400358 var revs []interface{}
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500359 for _, revEntry := range pr {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400360 revs = append(revs, revEntry.GetData())
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500361 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400362 result = revs
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500363 } else {
364 result = pr[0].GetData()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500365 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500366 }
367 }
368
369 return result
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400370}
371
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400372//getPath traverses the specified path and retrieves the data associated to it
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400373func (n *node) getPath(rev Revision, path string, depth int) interface{} {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400374 if path == "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400375 return n.getData(rev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400376 }
377
378 partition := strings.SplitN(path, "/", 2)
379 name := partition[0]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400380
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400381 if len(partition) < 2 {
382 path = ""
383 } else {
384 path = partition[1]
385 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400386
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400387 names := ChildrenFields(n.Type)
388 field := names[name]
389
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500390 if field != nil && field.IsContainer {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500391 children := make([]Revision, len(rev.GetChildren(name)))
392 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500393
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400394 if field.Key != "" {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400395 if path != "" {
396 partition = strings.SplitN(path, "/", 2)
397 key := partition[0]
398 path = ""
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400399 keyValue := field.KeyFromStr(key)
400 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev == nil {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400401 return nil
402 } else {
403 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400404 return childNode.getPath(childRev, path, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400405 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400406 } else {
407 var response []interface{}
408 for _, childRev := range children {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400409 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400410 value := childNode.getData(childRev, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400411 response = append(response, value)
412 }
413 return response
414 }
415 } else {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400416 var response []interface{}
417 if path != "" {
418 // TODO: raise error
419 return response
420 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500421 for _, childRev := range children {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400422 childNode := childRev.GetNode()
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400423 value := childNode.getData(childRev, depth)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400424 response = append(response, value)
425 }
426 return response
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400427 }
428 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500429
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500430 childRev := rev.GetChildren(name)[0]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500431 childNode := childRev.GetNode()
432 return childNode.getPath(childRev, path, depth)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400433}
434
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500435// getData retrieves the data from a node revision
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400436func (n *node) getData(rev Revision, depth int) interface{} {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500437 msg := rev.GetBranch().GetLatest().Get(depth)
Stephane Barbariea188d942018-10-16 16:43:04 -0400438 var modifiedMsg interface{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400439
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500440 if n.GetProxy() != nil {
Stephane Barbarieaa467942019-02-06 14:09:44 -0500441 log.Debugw("invoking-get-callbacks", log.Fields{"data": msg})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500442 if modifiedMsg = n.GetProxy().InvokeCallbacks(GET, false, msg); modifiedMsg != nil {
Stephane Barbariea188d942018-10-16 16:43:04 -0400443 msg = modifiedMsg
444 }
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400445
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400446 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500447
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400448 return msg
449}
450
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500451// Update changes the content of a node at the specified path with the provided data
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400452func (n *node) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400453 n.mutex.Lock()
454 defer n.mutex.Unlock()
455
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500456 log.Debugw("node-update-request", log.Fields{"path": path, "strict": strict, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500457
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400458 for strings.HasPrefix(path, "/") {
459 path = path[1:]
460 }
461
462 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400463 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500464 branch = n.GetBranch(NONE)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500465 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400466 branch = makeBranch(n)
467 }
468
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500469 if branch.GetLatest() != nil {
470 log.Debugf("Branch data : %+v, Passed data: %+v", branch.GetLatest().GetData(), data)
471 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400472 if path == "" {
473 return n.doUpdate(branch, data, strict)
474 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400475
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500476 rev := branch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400477
478 partition := strings.SplitN(path, "/", 2)
479 name := partition[0]
480
481 if len(partition) < 2 {
482 path = ""
483 } else {
484 path = partition[1]
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400485 }
486
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400487 field := ChildrenFields(n.Type)[name]
488 var children []Revision
489
Stephane Barbarieaa467942019-02-06 14:09:44 -0500490 if field == nil {
491 return n.doUpdate(branch, data, strict)
492 }
493
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400494 if field.IsContainer {
495 if path == "" {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400496 log.Errorf("cannot update a list")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400497 } else if field.Key != "" {
498 partition := strings.SplitN(path, "/", 2)
499 key := partition[0]
500 if len(partition) < 2 {
501 path = ""
502 } else {
503 path = partition[1]
504 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400505 keyValue := field.KeyFromStr(key)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500506
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500507 children = make([]Revision, len(rev.GetChildren(name)))
508 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500509
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400510 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400511
512 if childRev == nil {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400513 log.Debugw("child-revision-is-nil", log.Fields{"key": keyValue})
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400514 return branch.GetLatest()
515 }
516
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400517 childNode := childRev.GetNode()
Stephane Barbariea188d942018-10-16 16:43:04 -0400518
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500519 // Save proxy in child node to ensure callbacks are called later on
Stephane Barbaried62ac4e2019-02-05 14:08:38 -0500520 // only assign in cases of non sub-folder proxies, i.e. "/"
521 if childNode.Proxy == nil && n.Proxy != nil && n.Proxy.getFullPath() == "" {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500522 childNode.Proxy = n.Proxy
523 }
524
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400525 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400526
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400527 if newChildRev.GetHash() == childRev.GetHash() {
528 if newChildRev != childRev {
529 log.Debug("clear-hash - %s %+v", newChildRev.GetHash(), newChildRev)
530 newChildRev.ClearHash()
531 }
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400532 log.Debugw("child-revisions-have-matching-hash", log.Fields{"hash": childRev.GetHash(), "key": keyValue})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500533 return branch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400534 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400535
536 _, newKey := GetAttributeValue(newChildRev.GetData(), field.Key, 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500537
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400538 _newKeyType := fmt.Sprintf("%s", newKey)
539 _keyValueType := fmt.Sprintf("%s", keyValue)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500540
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400541 if _newKeyType != _keyValueType {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400542 log.Errorf("cannot change key field")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400543 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500544
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500545 // Prefix the hash value with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400546 newChildRev.SetName(name + "/" + _keyValueType)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400547
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400548 branch.LatestLock.Lock()
549 defer branch.LatestLock.Unlock()
550
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400551 if idx >= 0 {
552 children[idx] = newChildRev
553 } else {
554 children = append(children, newChildRev)
555 }
556
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500557 updatedRev := rev.UpdateChildren(name, children, branch)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500558
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500559 n.makeLatest(branch, updatedRev, nil)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400560 updatedRev.ChildDrop(name, childRev.GetHash())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500561
Stephane Barbariea188d942018-10-16 16:43:04 -0400562 return newChildRev
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500563
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400564 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400565 log.Errorf("cannot index into container with no keys")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400566 }
567 } else {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500568 childRev := rev.GetChildren(name)[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400569 childNode := childRev.GetNode()
570 newChildRev := childNode.Update(path, data, strict, txid, makeBranch)
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400571
572 branch.LatestLock.Lock()
573 defer branch.LatestLock.Unlock()
574
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500575 updatedRev := rev.UpdateChildren(name, []Revision{newChildRev}, branch)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500576 n.makeLatest(branch, updatedRev, nil)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500577
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400578 updatedRev.ChildDrop(name, childRev.GetHash())
579
Stephane Barbariea188d942018-10-16 16:43:04 -0400580 return newChildRev
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400581 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500582
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400583 return nil
584}
585
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400586func (n *node) doUpdate(branch *Branch, data interface{}, strict bool) Revision {
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400587 log.Debugw("comparing-types", log.Fields{"expected": reflect.ValueOf(n.Type).Type(), "actual": reflect.TypeOf(data)})
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400588
589 if reflect.TypeOf(data) != reflect.ValueOf(n.Type).Type() {
590 // TODO raise error
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400591 log.Errorw("types-do-not-match: %+v", log.Fields{"actual": reflect.TypeOf(data), "expected": n.Type})
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400592 return nil
593 }
594
595 // TODO: validate that this actually works
596 //if n.hasChildren(data) {
597 // return nil
598 //}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400599
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500600 if n.GetProxy() != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400601 log.Debug("invoking proxy PRE_UPDATE Callbacks")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500602 n.GetProxy().InvokeCallbacks(PRE_UPDATE, false, branch.GetLatest(), data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400603 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500604
605 if branch.GetLatest().GetData().(proto.Message).String() != data.(proto.Message).String() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400606 if strict {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400607 // TODO: checkAccessViolations(data, Branch.GetLatest.data)
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -0400608 log.Debugf("checking access violations")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400609 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500610
611 rev := branch.GetLatest().UpdateData(data, branch)
612 changes := []ChangeTuple{{POST_UPDATE, branch.GetLatest().GetData(), rev.GetData()}}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500613 n.makeLatest(branch, rev, changes)
614
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400615 return rev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400616 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500617 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400618}
619
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500620// Add inserts a new node at the specified path with the provided data
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400621func (n *node) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400622 n.mutex.Lock()
623 defer n.mutex.Unlock()
624
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500625 log.Debugw("node-add-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500626
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400627 for strings.HasPrefix(path, "/") {
628 path = path[1:]
629 }
630 if path == "" {
631 // TODO raise error
Stephane Barbarie126101e2018-10-11 16:18:48 -0400632 log.Errorf("cannot add for non-container mode")
Stephane Barbarieaa467942019-02-06 14:09:44 -0500633 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400634 }
635
636 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400637 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500638 branch = n.GetBranch(NONE)
639 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400640 branch = makeBranch(n)
641 }
642
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500643 rev := branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400644
645 partition := strings.SplitN(path, "/", 2)
646 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400647
648 if len(partition) < 2 {
649 path = ""
650 } else {
651 path = partition[1]
652 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400653
654 field := ChildrenFields(n.Type)[name]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500655
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400656 var children []Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400657
658 if field.IsContainer {
659 if path == "" {
660 if field.Key != "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500661 if n.GetProxy() != nil {
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400662 log.Debug("invoking proxy PRE_ADD Callbacks")
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500663 n.GetProxy().InvokeCallbacks(PRE_ADD, false, data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400664 }
665
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500666 children = make([]Revision, len(rev.GetChildren(name)))
667 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500668
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400669 _, key := GetAttributeValue(data, field.Key, 0)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500670
Stephane Barbarie126101e2018-10-11 16:18:48 -0400671 if _, exists := n.findRevByKey(children, field.Key, key.String()); exists != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400672 // TODO raise error
Stephane Barbarie260a5632019-02-26 16:12:49 -0500673 log.Warnw("duplicate-key-found", log.Fields{"key": key.String()})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500674 return exists
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400675 }
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500676 childRev := n.MakeNode(data, "").Latest()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500677
678 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400679 childRev.SetName(name + "/" + key.String())
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500680
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400681 branch.LatestLock.Lock()
682 defer branch.LatestLock.Unlock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500683
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500684 children = append(children, childRev)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400685
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400686 updatedRev := rev.UpdateChildren(name, children, branch)
687 changes := []ChangeTuple{{POST_ADD, nil, childRev.GetData()}}
Stephane Barbarie802aca42019-05-21 12:19:28 -0400688 childRev.GetNode().SetProxy(n.GetProxy())
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400689 childRev.SetupWatch(childRev.GetName())
690
691 n.makeLatest(branch, updatedRev, changes)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500692
693 return childRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400694 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500695 log.Errorf("cannot add to non-keyed container")
696
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400697 } else if field.Key != "" {
698 partition := strings.SplitN(path, "/", 2)
699 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400700 if len(partition) < 2 {
701 path = ""
702 } else {
703 path = partition[1]
704 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400705 keyValue := field.KeyFromStr(key)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500706
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500707 children = make([]Revision, len(rev.GetChildren(name)))
708 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500709
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400710 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500711
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400712 if childRev == nil {
713 return branch.GetLatest()
714 }
715
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400716 childNode := childRev.GetNode()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400717 newChildRev := childNode.Add(path, data, txid, makeBranch)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500718
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500719 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400720 newChildRev.SetName(name + "/" + keyValue.(string))
721
722 branch.LatestLock.Lock()
723 defer branch.LatestLock.Unlock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500724
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400725 if idx >= 0 {
726 children[idx] = newChildRev
727 } else {
728 children = append(children, newChildRev)
729 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500730
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400731 updatedRev := rev.UpdateChildren(name, children, branch)
732 n.makeLatest(branch, updatedRev, nil)
733
734 updatedRev.ChildDrop(name, childRev.GetHash())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500735
736 return newChildRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400737 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400738 log.Errorf("cannot add to non-keyed container")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400739 }
740 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400741 log.Errorf("cannot add to non-container field")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400742 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500743
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400744 return nil
745}
746
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500747// Remove eliminates a node at the specified path
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400748func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400749 n.mutex.Lock()
750 defer n.mutex.Unlock()
751
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500752 log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500753
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400754 for strings.HasPrefix(path, "/") {
755 path = path[1:]
756 }
757 if path == "" {
758 // TODO raise error
Stephane Barbarie126101e2018-10-11 16:18:48 -0400759 log.Errorf("cannot remove for non-container mode")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400760 }
761 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400762 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500763 branch = n.GetBranch(NONE)
764 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400765 branch = makeBranch(n)
766 }
767
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500768 rev := branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400769
770 partition := strings.SplitN(path, "/", 2)
771 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400772 if len(partition) < 2 {
773 path = ""
774 } else {
775 path = partition[1]
776 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400777
778 field := ChildrenFields(n.Type)[name]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400779 var children []Revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400780 postAnnouncement := []ChangeTuple{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400781
782 if field.IsContainer {
783 if path == "" {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500784 log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400785 } else if field.Key != "" {
786 partition := strings.SplitN(path, "/", 2)
787 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400788 if len(partition) < 2 {
789 path = ""
790 } else {
791 path = partition[1]
792 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500793
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400794 keyValue := field.KeyFromStr(key)
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500795 children = make([]Revision, len(rev.GetChildren(name)))
796 copy(children, rev.GetChildren(name))
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500797
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400798 if path != "" {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400799 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
800 childNode := childRev.GetNode()
801 if childNode.Proxy == nil {
802 childNode.Proxy = n.Proxy
803 }
804 newChildRev := childNode.Remove(path, txid, makeBranch)
805
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400806 branch.LatestLock.Lock()
807 defer branch.LatestLock.Unlock()
808
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400809 if idx >= 0 {
810 children[idx] = newChildRev
811 } else {
812 children = append(children, newChildRev)
813 }
814
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400815 rev.SetChildren(name, children)
816 branch.GetLatest().Drop(txid, false)
817 n.makeLatest(branch, rev, nil)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500818 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400819 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400820 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500821
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400822 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil && idx >= 0 {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500823 if n.GetProxy() != nil {
824 data := childRev.GetData()
825 n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
826 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
827 } else {
828 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
829 }
830
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400831 childRev.StorageDrop(txid, true)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400832 GetRevCache().Cache.Delete(childRev.GetName())
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400833
834 branch.LatestLock.Lock()
835 defer branch.LatestLock.Unlock()
836
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500837 children = append(children[:idx], children[idx+1:]...)
838 rev.SetChildren(name, children)
839
840 branch.GetLatest().Drop(txid, false)
841 n.makeLatest(branch, rev, postAnnouncement)
842
843 return rev
844 } else {
845 log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)})
846 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400847 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500848 log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500849
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400850 } else {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500851 log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400852 }
853
854 return nil
855}
856
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400857// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
858
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500859// MakeBranchFunction is a type for function references intented to create a branch
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400860type MakeBranchFunction func(*node) *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400861
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500862// MakeBranch creates a new branch for the provided transaction id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400863func (n *node) MakeBranch(txid string) *Branch {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500864 branchPoint := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400865 branch := NewBranch(n, txid, branchPoint, true)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500866 n.SetBranch(txid, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400867 return branch
868}
869
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500870// DeleteBranch removes a branch with the specified id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400871func (n *node) DeleteBranch(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400872 delete(n.Branches, txid)
873}
874
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400875func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400876 f := func(rev Revision) Revision {
877 childBranch := rev.GetBranch()
878
879 if childBranch.Txid == txid {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400880 rev, _ = childBranch.Node.MergeBranch(txid, dryRun)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400881 }
882
883 return rev
884 }
885 return f
886}
887
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500888// MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400889func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500890 srcBranch := n.GetBranch(txid)
891 dstBranch := n.GetBranch(NONE)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400892
893 forkRev := srcBranch.Origin
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500894 srcRev := srcBranch.GetLatest()
895 dstRev := dstBranch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400896
897 rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
898
899 if !dryRun {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500900 if rev != nil {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400901 rev.SetName(dstRev.GetName())
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500902 n.makeLatest(dstBranch, rev, changes)
903 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500904 n.DeleteBranch(txid)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400905 }
906
Stephane Barbariee16186c2018-09-11 10:46:34 -0400907 // TODO: return proper error when one occurs
908 return rev, nil
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400909}
910
911// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
912
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400913//func (n *node) diff(hash1, hash2, txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400914// branch := n.Branches[txid]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500915// rev1 := branch.GetHash(hash1)
916// rev2 := branch.GetHash(hash2)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400917//
918// if rev1.GetHash() == rev2.GetHash() {
919// // empty patch
920// } else {
921// // translate data to json and generate patch
922// patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData())
923// patch.
924// }
925//}
926
927// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
928
929// TODO: is tag mgmt used in the python implementation? Need to validate
930
931// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
932
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400933func (n *node) hasChildren(data interface{}) bool {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400934 for fieldName, field := range ChildrenFields(n.Type) {
935 _, fieldValue := GetAttributeValue(data, fieldName, 0)
936
937 if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() {
938 log.Error("cannot update external children")
939 return true
940 }
941 }
942
943 return false
944}
945
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400946// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400947
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500948// CreateProxy returns a reference to a sub-tree of the data model
949func (n *node) CreateProxy(path string, exclusive bool) *Proxy {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500950 return n.createProxy(path, path, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400951}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500952
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500953func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400954 for strings.HasPrefix(path, "/") {
955 path = path[1:]
956 }
957 if path == "" {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500958 return n.makeProxy(path, fullPath, parentNode, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400959 }
960
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500961 rev := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400962 partition := strings.SplitN(path, "/", 2)
963 name := partition[0]
Stephane Barbarie126101e2018-10-11 16:18:48 -0400964 if len(partition) < 2 {
965 path = ""
966 } else {
967 path = partition[1]
968 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400969
970 field := ChildrenFields(n.Type)[name]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500971 if field.IsContainer {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400972 if path == "" {
khenaidoo21d51152019-02-01 13:48:37 -0500973 //log.Error("cannot proxy a container field")
Stephane Barbarieaa467942019-02-06 14:09:44 -0500974 newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "")
975 return newNode.makeProxy(path, fullPath, parentNode, exclusive)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400976 } else if field.Key != "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400977 partition := strings.SplitN(path, "/", 2)
978 key := partition[0]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400979 if len(partition) < 2 {
980 path = ""
981 } else {
982 path = partition[1]
983 }
984 keyValue := field.KeyFromStr(key)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400985 var children []Revision
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500986 children = make([]Revision, len(rev.GetChildren(name)))
987 copy(children, rev.GetChildren(name))
Stephane Barbarie933b09b2019-01-09 11:12:09 -0500988 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
989 childNode := childRev.GetNode()
990 return childNode.createProxy(path, fullPath, n, exclusive)
991 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400992 } else {
993 log.Error("cannot index into container with no keys")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400994 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400995 } else {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500996 childRev := rev.GetChildren(name)[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400997 childNode := childRev.GetNode()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500998 return childNode.createProxy(path, fullPath, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400999 }
1000
Stephane Barbarie933b09b2019-01-09 11:12:09 -05001001 log.Warnf("Cannot create proxy - latest rev:%s, all revs:%+v", rev.GetHash(), n.GetBranch(NONE).Revisions)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001002 return nil
1003}
1004
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001005func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbarie126101e2018-10-11 16:18:48 -04001006 r := &root{
1007 node: n,
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001008 Callbacks: n.Root.GetCallbacks(),
1009 NotificationCallbacks: n.Root.GetNotificationCallbacks(),
Stephane Barbarie126101e2018-10-11 16:18:48 -04001010 DirtyNodes: n.Root.DirtyNodes,
1011 KvStore: n.Root.KvStore,
1012 Loading: n.Root.Loading,
1013 RevisionClass: n.Root.RevisionClass,
1014 }
1015
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001016 if n.Proxy == nil {
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001017 n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001018 } else {
1019 if n.Proxy.Exclusive {
1020 log.Error("node is already owned exclusively")
1021 }
1022 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001023
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001024 return n.Proxy
1025}
1026
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001027func (n *node) makeEventBus() *EventBus {
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001028 if n.EventBus == nil {
1029 n.EventBus = NewEventBus()
1030 }
1031 return n.EventBus
1032}
1033
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001034func (n *node) SetProxy(proxy *Proxy) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001035 n.Proxy = proxy
1036}
1037
1038func (n *node) GetProxy() *Proxy {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001039 return n.Proxy
1040}
1041
1042func (n *node) GetBranch(key string) *Branch {
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001043 if n.Branches != nil {
1044 if branch, exists := n.Branches[key]; exists {
1045 return branch
1046 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001047 }
1048 return nil
1049}
1050
1051func (n *node) SetBranch(key string, branch *Branch) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001052 n.Branches[key] = branch
1053}
1054
1055func (n *node) GetRoot() *root {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001056 return n.Root
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001057}