blob: 15ea04e49d5bbc91fb0c1288f33f8d8e4bcdf5e0 [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()}}
688 childRev.SetupWatch(childRev.GetName())
689
690 n.makeLatest(branch, updatedRev, changes)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500691
692 return childRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400693 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500694 log.Errorf("cannot add to non-keyed container")
695
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400696 } else if field.Key != "" {
697 partition := strings.SplitN(path, "/", 2)
698 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400699 if len(partition) < 2 {
700 path = ""
701 } else {
702 path = partition[1]
703 }
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400704 keyValue := field.KeyFromStr(key)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500705
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500706 children = make([]Revision, len(rev.GetChildren(name)))
707 copy(children, rev.GetChildren(name))
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500708
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400709 idx, childRev := n.findRevByKey(children, field.Key, keyValue)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500710
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400711 if childRev == nil {
712 return branch.GetLatest()
713 }
714
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400715 childNode := childRev.GetNode()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400716 newChildRev := childNode.Add(path, data, txid, makeBranch)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500717
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500718 // Prefix the hash with the data type (e.g. devices, logical_devices, adapters)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400719 newChildRev.SetName(name + "/" + keyValue.(string))
720
721 branch.LatestLock.Lock()
722 defer branch.LatestLock.Unlock()
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500723
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400724 if idx >= 0 {
725 children[idx] = newChildRev
726 } else {
727 children = append(children, newChildRev)
728 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500729
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400730 updatedRev := rev.UpdateChildren(name, children, branch)
731 n.makeLatest(branch, updatedRev, nil)
732
733 updatedRev.ChildDrop(name, childRev.GetHash())
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500734
735 return newChildRev
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400736 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400737 log.Errorf("cannot add to non-keyed container")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400738 }
739 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -0400740 log.Errorf("cannot add to non-container field")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400741 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500742
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400743 return nil
744}
745
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500746// Remove eliminates a node at the specified path
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400747func (n *node) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarie802aca42019-05-21 12:19:28 -0400748 n.mutex.Lock()
749 defer n.mutex.Unlock()
750
Stephane Barbarie11b88e72019-02-07 12:28:29 -0500751 log.Debugw("node-remove-request", log.Fields{"path": path, "txid": txid, "makeBranch": makeBranch})
Stephane Barbarieaa467942019-02-06 14:09:44 -0500752
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400753 for strings.HasPrefix(path, "/") {
754 path = path[1:]
755 }
756 if path == "" {
757 // TODO raise error
Stephane Barbarie126101e2018-10-11 16:18:48 -0400758 log.Errorf("cannot remove for non-container mode")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400759 }
760 var branch *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400761 if txid == "" {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500762 branch = n.GetBranch(NONE)
763 } else if branch = n.GetBranch(txid); branch == nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400764 branch = makeBranch(n)
765 }
766
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500767 rev := branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400768
769 partition := strings.SplitN(path, "/", 2)
770 name := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400771 if len(partition) < 2 {
772 path = ""
773 } else {
774 path = partition[1]
775 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400776
777 field := ChildrenFields(n.Type)[name]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400778 var children []Revision
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400779 postAnnouncement := []ChangeTuple{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400780
781 if field.IsContainer {
782 if path == "" {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500783 log.Errorw("cannot-remove-without-key", log.Fields{"name": name, "key": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400784 } else if field.Key != "" {
785 partition := strings.SplitN(path, "/", 2)
786 key := partition[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400787 if len(partition) < 2 {
788 path = ""
789 } else {
790 path = partition[1]
791 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500792
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400793 keyValue := field.KeyFromStr(key)
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500794 children = make([]Revision, len(rev.GetChildren(name)))
795 copy(children, rev.GetChildren(name))
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500796
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400797 if path != "" {
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400798 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
799 childNode := childRev.GetNode()
800 if childNode.Proxy == nil {
801 childNode.Proxy = n.Proxy
802 }
803 newChildRev := childNode.Remove(path, txid, makeBranch)
804
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400805 branch.LatestLock.Lock()
806 defer branch.LatestLock.Unlock()
807
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400808 if idx >= 0 {
809 children[idx] = newChildRev
810 } else {
811 children = append(children, newChildRev)
812 }
813
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400814 rev.SetChildren(name, children)
815 branch.GetLatest().Drop(txid, false)
816 n.makeLatest(branch, rev, nil)
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500817 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400818 return branch.GetLatest()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400819 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500820
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400821 if idx, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil && idx >= 0 {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500822 if n.GetProxy() != nil {
823 data := childRev.GetData()
824 n.GetProxy().InvokeCallbacks(PRE_REMOVE, false, data)
825 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, data, nil})
826 } else {
827 postAnnouncement = append(postAnnouncement, ChangeTuple{POST_REMOVE, childRev.GetData(), nil})
828 }
829
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400830 childRev.StorageDrop(txid, true)
Stephane Barbarie7512fc82019-05-07 12:25:46 -0400831 GetRevCache().Cache.Delete(childRev.GetName())
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400832
833 branch.LatestLock.Lock()
834 defer branch.LatestLock.Unlock()
835
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500836 children = append(children[:idx], children[idx+1:]...)
837 rev.SetChildren(name, children)
838
839 branch.GetLatest().Drop(txid, false)
840 n.makeLatest(branch, rev, postAnnouncement)
841
842 return rev
843 } else {
844 log.Errorw("failed-to-find-revision", log.Fields{"name": name, "key": keyValue.(string)})
845 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400846 }
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500847 log.Errorw("cannot-add-to-non-keyed-container", log.Fields{"name": name, "path": path, "fieldKey": field.Key})
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500848
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400849 } else {
Stephane Barbarieb0c79892019-02-13 11:29:59 -0500850 log.Errorw("cannot-add-to-non-container-field", log.Fields{"name": name, "path": path})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400851 }
852
853 return nil
854}
855
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400856// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Branching ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
857
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500858// MakeBranchFunction is a type for function references intented to create a branch
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400859type MakeBranchFunction func(*node) *Branch
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400860
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500861// MakeBranch creates a new branch for the provided transaction id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400862func (n *node) MakeBranch(txid string) *Branch {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500863 branchPoint := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400864 branch := NewBranch(n, txid, branchPoint, true)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500865 n.SetBranch(txid, branch)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400866 return branch
867}
868
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500869// DeleteBranch removes a branch with the specified id
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400870func (n *node) DeleteBranch(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400871 delete(n.Branches, txid)
872}
873
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400874func (n *node) mergeChild(txid string, dryRun bool) func(Revision) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400875 f := func(rev Revision) Revision {
876 childBranch := rev.GetBranch()
877
878 if childBranch.Txid == txid {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400879 rev, _ = childBranch.Node.MergeBranch(txid, dryRun)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400880 }
881
882 return rev
883 }
884 return f
885}
886
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500887// MergeBranch will integrate the contents of a transaction branch within the latest branch of a given node
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400888func (n *node) MergeBranch(txid string, dryRun bool) (Revision, error) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500889 srcBranch := n.GetBranch(txid)
890 dstBranch := n.GetBranch(NONE)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400891
892 forkRev := srcBranch.Origin
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500893 srcRev := srcBranch.GetLatest()
894 dstRev := dstBranch.GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400895
896 rev, changes := Merge3Way(forkRev, srcRev, dstRev, n.mergeChild(txid, dryRun), dryRun)
897
898 if !dryRun {
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500899 if rev != nil {
Stephane Barbarief7fc1782019-03-28 22:33:41 -0400900 rev.SetName(dstRev.GetName())
Stephane Barbarie1039ec42019-02-04 10:43:16 -0500901 n.makeLatest(dstBranch, rev, changes)
902 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500903 n.DeleteBranch(txid)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400904 }
905
Stephane Barbariee16186c2018-09-11 10:46:34 -0400906 // TODO: return proper error when one occurs
907 return rev, nil
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400908}
909
910// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Diff utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
911
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400912//func (n *node) diff(hash1, hash2, txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400913// branch := n.Branches[txid]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500914// rev1 := branch.GetHash(hash1)
915// rev2 := branch.GetHash(hash2)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400916//
917// if rev1.GetHash() == rev2.GetHash() {
918// // empty patch
919// } else {
920// // translate data to json and generate patch
921// patch, err := jsonpatch.MakePatch(rev1.GetData(), rev2.GetData())
922// patch.
923// }
924//}
925
926// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tag utility ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
927
928// TODO: is tag mgmt used in the python implementation? Need to validate
929
930// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Internals ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
931
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400932func (n *node) hasChildren(data interface{}) bool {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400933 for fieldName, field := range ChildrenFields(n.Type) {
934 _, fieldValue := GetAttributeValue(data, fieldName, 0)
935
936 if (field.IsContainer && fieldValue.Len() > 0) || !fieldValue.IsNil() {
937 log.Error("cannot update external children")
938 return true
939 }
940 }
941
942 return false
943}
944
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400945// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node Proxy ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400946
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500947// CreateProxy returns a reference to a sub-tree of the data model
948func (n *node) CreateProxy(path string, exclusive bool) *Proxy {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500949 return n.createProxy(path, path, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400950}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500951
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500952func (n *node) createProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400953 for strings.HasPrefix(path, "/") {
954 path = path[1:]
955 }
956 if path == "" {
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500957 return n.makeProxy(path, fullPath, parentNode, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400958 }
959
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500960 rev := n.GetBranch(NONE).GetLatest()
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400961 partition := strings.SplitN(path, "/", 2)
962 name := partition[0]
Stephane Barbarie126101e2018-10-11 16:18:48 -0400963 if len(partition) < 2 {
964 path = ""
965 } else {
966 path = partition[1]
967 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400968
969 field := ChildrenFields(n.Type)[name]
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500970 if field.IsContainer {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400971 if path == "" {
khenaidoo21d51152019-02-01 13:48:37 -0500972 //log.Error("cannot proxy a container field")
Stephane Barbarieaa467942019-02-06 14:09:44 -0500973 newNode := n.MakeNode(reflect.New(field.ClassType.Elem()).Interface(), "")
974 return newNode.makeProxy(path, fullPath, parentNode, exclusive)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400975 } else if field.Key != "" {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400976 partition := strings.SplitN(path, "/", 2)
977 key := partition[0]
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400978 if len(partition) < 2 {
979 path = ""
980 } else {
981 path = partition[1]
982 }
983 keyValue := field.KeyFromStr(key)
Stephane Barbarie126101e2018-10-11 16:18:48 -0400984 var children []Revision
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500985 children = make([]Revision, len(rev.GetChildren(name)))
986 copy(children, rev.GetChildren(name))
Stephane Barbarie933b09b2019-01-09 11:12:09 -0500987 if _, childRev := n.findRevByKey(children, field.Key, keyValue); childRev != nil {
988 childNode := childRev.GetNode()
989 return childNode.createProxy(path, fullPath, n, exclusive)
990 }
Stephane Barbarie126101e2018-10-11 16:18:48 -0400991 } else {
992 log.Error("cannot index into container with no keys")
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400993 }
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400994 } else {
Stephane Barbarie3cb01222019-01-16 17:15:56 -0500995 childRev := rev.GetChildren(name)[0]
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400996 childNode := childRev.GetNode()
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500997 return childNode.createProxy(path, fullPath, n, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400998 }
999
Stephane Barbarie933b09b2019-01-09 11:12:09 -05001000 log.Warnf("Cannot create proxy - latest rev:%s, all revs:%+v", rev.GetHash(), n.GetBranch(NONE).Revisions)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001001 return nil
1002}
1003
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001004func (n *node) makeProxy(path string, fullPath string, parentNode *node, exclusive bool) *Proxy {
Stephane Barbarie126101e2018-10-11 16:18:48 -04001005 r := &root{
1006 node: n,
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001007 Callbacks: n.Root.GetCallbacks(),
1008 NotificationCallbacks: n.Root.GetNotificationCallbacks(),
Stephane Barbarie126101e2018-10-11 16:18:48 -04001009 DirtyNodes: n.Root.DirtyNodes,
1010 KvStore: n.Root.KvStore,
1011 Loading: n.Root.Loading,
1012 RevisionClass: n.Root.RevisionClass,
1013 }
1014
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001015 if n.Proxy == nil {
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001016 n.Proxy = NewProxy(r, n, parentNode, path, fullPath, exclusive)
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001017 } else {
1018 if n.Proxy.Exclusive {
1019 log.Error("node is already owned exclusively")
1020 }
1021 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001022
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001023 return n.Proxy
1024}
1025
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001026func (n *node) makeEventBus() *EventBus {
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001027 if n.EventBus == nil {
1028 n.EventBus = NewEventBus()
1029 }
1030 return n.EventBus
1031}
1032
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001033func (n *node) SetProxy(proxy *Proxy) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001034 n.Proxy = proxy
1035}
1036
1037func (n *node) GetProxy() *Proxy {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001038 return n.Proxy
1039}
1040
1041func (n *node) GetBranch(key string) *Branch {
Stephane Barbarie1ab43272018-12-08 21:42:13 -05001042 if n.Branches != nil {
1043 if branch, exists := n.Branches[key]; exists {
1044 return branch
1045 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001046 }
1047 return nil
1048}
1049
1050func (n *node) SetBranch(key string, branch *Branch) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001051 n.Branches[key] = branch
1052}
1053
1054func (n *node) GetRoot() *root {
Stephane Barbariedc5022d2018-11-19 15:21:44 -05001055 return n.Root
Stephane Barbarie06c4a742018-10-01 11:09:32 -04001056}