khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame^] | 1 | /* |
| 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "fmt" |
| 20 | "github.com/golang/protobuf/proto" |
| 21 | "reflect" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | NONE string = "none" |
| 27 | ) |
| 28 | |
| 29 | type Node struct { |
| 30 | root *Root |
| 31 | Type interface{} |
| 32 | Branches map[string]*Branch |
| 33 | Tags map[string]*Revision |
| 34 | Proxy *Proxy |
| 35 | EventBus *EventBus |
| 36 | AutoPrune bool |
| 37 | } |
| 38 | |
| 39 | func NewNode(root *Root, initialData interface{}, autoPrune bool, txid string) *Node { |
| 40 | cn := &Node{} |
| 41 | |
| 42 | cn.root = root |
| 43 | cn.Branches = make(map[string]*Branch) |
| 44 | cn.Tags = make(map[string]*Revision) |
| 45 | cn.Proxy = nil |
| 46 | cn.EventBus = nil |
| 47 | cn.AutoPrune = autoPrune |
| 48 | |
| 49 | if IsProtoMessage(initialData) { |
| 50 | cn.Type = reflect.ValueOf(initialData).Interface() |
| 51 | dataCopy := proto.Clone(initialData.(proto.Message)) |
| 52 | cn.initialize(dataCopy, txid) |
| 53 | } else if reflect.ValueOf(initialData).IsValid() { |
| 54 | cn.Type = reflect.ValueOf(initialData).Interface() |
| 55 | } else { |
| 56 | // not implemented error |
| 57 | fmt.Errorf("cannot process initial data - %+v", initialData) |
| 58 | } |
| 59 | |
| 60 | return cn |
| 61 | } |
| 62 | |
| 63 | func (cn *Node) makeNode(data interface{}, txid string) *Node { |
| 64 | return NewNode(cn.root, data, true, txid) |
| 65 | } |
| 66 | |
| 67 | func (cn *Node) makeRevision(branch *Branch, data interface{}, children map[string][]*Revision) *Revision { |
| 68 | return cn.root.makeRevision(branch, data, children) |
| 69 | } |
| 70 | |
| 71 | func (cn *Node) makeLatest(branch *Branch, revision *Revision, changeAnnouncement map[string]interface{}) { |
| 72 | if _, ok := branch.revisions[revision.Hash]; !ok { |
| 73 | branch.revisions[revision.Hash] = revision |
| 74 | } |
| 75 | |
| 76 | if branch.Latest == nil || revision.Hash != branch.Latest.Hash { |
| 77 | branch.Latest = revision |
| 78 | } |
| 79 | |
| 80 | if changeAnnouncement != nil && branch.Txid == "" { |
| 81 | if cn.Proxy != nil { |
| 82 | for changeType, data := range changeAnnouncement { |
| 83 | // TODO: Invoke callback |
| 84 | fmt.Printf("invoking callback - changeType: %+v, data:%+v\n", changeType, data) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | for changeType, data := range changeAnnouncement { |
| 89 | // TODO: send notifications |
| 90 | fmt.Printf("sending notification - changeType: %+v, data:%+v\n", changeType, data) |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (cn *Node) Latest() *Revision { |
| 96 | if branch, exists := cn.Branches[NONE]; exists { |
| 97 | return branch.Latest |
| 98 | } |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | func (cn *Node) GetHash(hash string) *Revision { |
| 103 | return cn.Branches[NONE].revisions[hash] |
| 104 | } |
| 105 | |
| 106 | func (cn *Node) initialize(data interface{}, txid string) { |
| 107 | var children map[string][]*Revision |
| 108 | children = make(map[string][]*Revision) |
| 109 | for fieldName, field := range ChildrenFields(cn.Type) { |
| 110 | fieldValue := GetAttributeValue(data, fieldName, 0) |
| 111 | |
| 112 | if fieldValue.IsValid() { |
| 113 | if field.IsContainer { |
| 114 | if field.Key != "" { |
| 115 | var keysSeen []string |
| 116 | |
| 117 | for i := 0; i < fieldValue.Len(); i++ { |
| 118 | v := fieldValue.Index(i) |
| 119 | rev := cn.makeNode(v.Interface(), txid).Latest() |
| 120 | key := GetAttributeValue(v.Interface(), field.Key, 0) |
| 121 | for _, k := range keysSeen { |
| 122 | if k == key.String() { |
| 123 | fmt.Errorf("duplicate key - %s", k) |
| 124 | } |
| 125 | } |
| 126 | children[fieldName] = append(children[fieldName], rev) |
| 127 | keysSeen = append(keysSeen, key.String()) |
| 128 | } |
| 129 | |
| 130 | } else { |
| 131 | for i := 0; i < fieldValue.Len(); i++ { |
| 132 | v := fieldValue.Index(i) |
| 133 | children[fieldName] = append(children[fieldName], cn.makeNode(v.Interface(), txid).Latest()) |
| 134 | } |
| 135 | } |
| 136 | } else { |
| 137 | children[fieldName] = append(children[fieldName], cn.makeNode(fieldValue.Interface(), txid).Latest()) |
| 138 | } |
| 139 | } else { |
| 140 | fmt.Errorf("field is invalid - %+v", fieldValue) |
| 141 | } |
| 142 | } |
| 143 | // FIXME: ClearField??? No such method in go protos. Reset? |
| 144 | //data.ClearField(field_name) |
| 145 | branch := NewBranch(cn, "", nil, cn.AutoPrune) |
| 146 | rev := cn.makeRevision(branch, data, children) |
| 147 | cn.makeLatest(branch, rev, nil) |
| 148 | cn.Branches[txid] = branch |
| 149 | } |
| 150 | |
| 151 | func (cn *Node) makeTxBranch(txid string) *Branch { |
| 152 | branchPoint := cn.Branches[NONE].Latest |
| 153 | branch := NewBranch(cn, txid, branchPoint, true) |
| 154 | cn.Branches[txid] = branch |
| 155 | return branch |
| 156 | } |
| 157 | |
| 158 | func (cn *Node) deleteTxBranch(txid string) { |
| 159 | delete(cn.Branches, txid) |
| 160 | } |
| 161 | |
| 162 | type t_makeBranch func(*Node) *Branch |
| 163 | |
| 164 | // |
| 165 | // Get operation |
| 166 | // |
| 167 | func (cn *Node) Get(path string, hash string, depth int, deep bool, txid string) interface{} { |
| 168 | if deep { |
| 169 | depth = -1 |
| 170 | } |
| 171 | |
| 172 | for strings.HasPrefix(path, "/") { |
| 173 | path = path[1:] |
| 174 | } |
| 175 | |
| 176 | var branch *Branch |
| 177 | var rev *Revision |
| 178 | |
| 179 | // FIXME: should empty txid be cleaned up? |
| 180 | if branch = cn.Branches[txid]; txid == "" || branch == nil { |
| 181 | branch = cn.Branches[NONE] |
| 182 | } |
| 183 | |
| 184 | if hash != "" { |
| 185 | rev = branch.revisions[hash] |
| 186 | } else { |
| 187 | rev = branch.Latest |
| 188 | } |
| 189 | |
| 190 | return cn.get(rev, path, depth) |
| 191 | } |
| 192 | |
| 193 | func (cn *Node) findRevByKey(revs []*Revision, keyName string, value string) (int, *Revision) { |
| 194 | for i, rev := range revs { |
| 195 | dataValue := reflect.ValueOf(rev.Config.Data) |
| 196 | dataStruct := GetAttributeStructure(rev.Config.Data, keyName, 0) |
| 197 | |
| 198 | fieldValue := dataValue.Elem().FieldByName(dataStruct.Name) |
| 199 | |
| 200 | if fieldValue.Interface().(string) == value { |
| 201 | return i, rev |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | fmt.Errorf("key %s=%s not found", keyName, value) |
| 206 | |
| 207 | return -1, nil |
| 208 | } |
| 209 | |
| 210 | func (cn *Node) get(rev *Revision, path string, depth int) interface{} { |
| 211 | if path == "" { |
| 212 | return cn.doGet(rev, depth) |
| 213 | } |
| 214 | |
| 215 | partition := strings.SplitN(path, "/", 2) |
| 216 | name := partition[0] |
| 217 | path = partition[1] |
| 218 | |
| 219 | field := ChildrenFields(cn.Type)[name] |
| 220 | |
| 221 | if field.IsContainer { |
| 222 | if field.Key != "" { |
| 223 | children := rev.Children[name] |
| 224 | if path != "" { |
| 225 | partition = strings.SplitN(path, "/", 2) |
| 226 | key := partition[0] |
| 227 | path = "" |
| 228 | key = field.KeyFromStr(key).(string) |
| 229 | _, childRev := cn.findRevByKey(children, field.Key, key) |
| 230 | childNode := childRev.getNode() |
| 231 | return childNode.get(childRev, path, depth) |
| 232 | } else { |
| 233 | var response []interface{} |
| 234 | for _, childRev := range children { |
| 235 | childNode := childRev.getNode() |
| 236 | value := childNode.doGet(childRev, depth) |
| 237 | response = append(response, value) |
| 238 | } |
| 239 | return response |
| 240 | } |
| 241 | } else { |
| 242 | childRev := rev.Children[name][0] |
| 243 | childNode := childRev.getNode() |
| 244 | return childNode.get(childRev, path, depth) |
| 245 | } |
| 246 | } |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | func (cn *Node) doGet(rev *Revision, depth int) interface{} { |
| 251 | msg := rev.Get(depth) |
| 252 | |
| 253 | if cn.Proxy != nil { |
| 254 | // TODO: invoke GET callback |
| 255 | fmt.Println("invoking proxy GET Callbacks") |
| 256 | } |
| 257 | return msg |
| 258 | } |
| 259 | |
| 260 | // |
| 261 | // Update operation |
| 262 | // |
| 263 | func (n *Node) Update(path string, data interface{}, strict bool, txid string, makeBranch t_makeBranch) *Revision { |
| 264 | // FIXME: is this required ... a bit overkill to take out a "/" |
| 265 | for strings.HasPrefix(path, "/") { |
| 266 | path = path[1:] |
| 267 | } |
| 268 | |
| 269 | var branch *Branch |
| 270 | var ok bool |
| 271 | if branch, ok = n.Branches[txid]; !ok { |
| 272 | branch = makeBranch(n) |
| 273 | } |
| 274 | |
| 275 | if path == "" { |
| 276 | return n.doUpdate(branch, data, strict) |
| 277 | } |
| 278 | return &Revision{} |
| 279 | } |
| 280 | |
| 281 | func (n *Node) doUpdate(branch *Branch, data interface{}, strict bool) *Revision { |
| 282 | if reflect.TypeOf(data) != n.Type { |
| 283 | // TODO raise error |
| 284 | fmt.Errorf("data does not match type: %+v", n.Type) |
| 285 | } |
| 286 | |
| 287 | // TODO: noChildren? |
| 288 | |
| 289 | if n.Proxy != nil { |
| 290 | // TODO: n.proxy.InvokeCallbacks(CallbackType.PRE_UPDATE, data) |
| 291 | fmt.Println("invoking proxy PRE_UPDATE Callbacks") |
| 292 | } |
| 293 | if branch.Latest.getData() != data { |
| 294 | if strict { |
| 295 | // TODO: checkAccessViolations(data, branch.GetLatest.data) |
| 296 | fmt.Println("checking access violations") |
| 297 | } |
| 298 | rev := branch.Latest.UpdateData(data, branch) |
| 299 | n.makeLatest(branch, rev, nil) // TODO -> changeAnnouncement needs to be a tuple (CallbackType.POST_UPDATE, rev.data) |
| 300 | return rev |
| 301 | } else { |
| 302 | return branch.Latest |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // TODO: the python implementation has a method to check if the data has no children |
| 307 | //func (n *SomeNode) noChildren(data interface{}) bool { |
| 308 | // for fieldName, field := range ChildrenFields(n.Type) { |
| 309 | // fieldValue := GetAttributeValue(data, fieldName) |
| 310 | // |
| 311 | // if fieldValue.IsValid() { |
| 312 | // if field.IsContainer { |
| 313 | // if len(fieldValue) > 0 { |
| 314 | // |
| 315 | // } |
| 316 | // } else { |
| 317 | // |
| 318 | // } |
| 319 | // |
| 320 | // } |
| 321 | // } |
| 322 | //} |
| 323 | |
| 324 | // |
| 325 | // Add operation |
| 326 | // |
| 327 | func (n *Node) Add(path string, data interface{}, txid string, makeBranch t_makeBranch) *Revision { |
| 328 | for strings.HasPrefix(path, "/") { |
| 329 | path = path[1:] |
| 330 | } |
| 331 | if path == "" { |
| 332 | // TODO raise error |
| 333 | fmt.Errorf("cannot add for non-container mode\n") |
| 334 | } |
| 335 | |
| 336 | var branch *Branch |
| 337 | var ok bool |
| 338 | if branch, ok = n.Branches[txid]; !ok { |
| 339 | branch = makeBranch(n) |
| 340 | } |
| 341 | |
| 342 | rev := branch.Latest |
| 343 | |
| 344 | partition := strings.SplitN(path, "/", 2) |
| 345 | name := partition[0] |
| 346 | path = partition[1] |
| 347 | |
| 348 | field := ChildrenFields(n.Type)[name] |
| 349 | var children []*Revision |
| 350 | |
| 351 | if field.IsContainer { |
| 352 | if path == "" { |
| 353 | if field.Key != "" { |
| 354 | if n.Proxy != nil { |
| 355 | // TODO -> n.proxy.InvokeCallbacks(PRE_ADD, data) |
| 356 | fmt.Println("invoking proxy PRE_ADD Callbacks") |
| 357 | } |
| 358 | |
| 359 | copy(children, rev.Children[name]) |
| 360 | key := GetAttributeValue(data, field.Key, 0) |
| 361 | if _, rev := n.findRevByKey(children, field.Key, key.String()); rev != nil { |
| 362 | // TODO raise error |
| 363 | fmt.Errorf("duplicate key found: %s", key.String()) |
| 364 | } |
| 365 | |
| 366 | childRev := n.makeNode(data, "").Latest() |
| 367 | children = append(children, childRev) |
| 368 | rev := rev.UpdateChildren(name, children, branch) |
| 369 | n.makeLatest(branch, rev, nil) // TODO -> changeAnnouncement needs to be a tuple (CallbackType.POST_ADD, rev.data) |
| 370 | return rev |
| 371 | } else { |
| 372 | fmt.Errorf("cannot add to non-keyed container\n") |
| 373 | } |
| 374 | } else if field.Key != "" { |
| 375 | partition := strings.SplitN(path, "/", 2) |
| 376 | key := partition[0] |
| 377 | path = partition[1] |
| 378 | key = field.KeyFromStr(key).(string) |
| 379 | copy(children, rev.Children[name]) |
| 380 | idx, childRev := n.findRevByKey(children, field.Key, key) |
| 381 | childNode := childRev.getNode() |
| 382 | newChildRev := childNode.Add(path, data, txid, makeBranch) |
| 383 | children[idx] = newChildRev |
| 384 | rev := rev.UpdateChildren(name, children, branch) |
| 385 | n.makeLatest(branch, rev, nil) |
| 386 | return rev |
| 387 | } else { |
| 388 | fmt.Errorf("cannot add to non-keyed container\n") |
| 389 | } |
| 390 | } else { |
| 391 | fmt.Errorf("cannot add to non-container field\n") |
| 392 | } |
| 393 | return nil |
| 394 | } |
| 395 | |
| 396 | // |
| 397 | // Remove operation |
| 398 | // |
| 399 | func (n *Node) Remove(path string, txid string, makeBranch t_makeBranch) *Revision { |
| 400 | for strings.HasPrefix(path, "/") { |
| 401 | path = path[1:] |
| 402 | } |
| 403 | if path == "" { |
| 404 | // TODO raise error |
| 405 | fmt.Errorf("cannot remove for non-container mode\n") |
| 406 | } |
| 407 | var branch *Branch |
| 408 | var ok bool |
| 409 | if branch, ok = n.Branches[txid]; !ok { |
| 410 | branch = makeBranch(n) |
| 411 | } |
| 412 | |
| 413 | rev := branch.Latest |
| 414 | |
| 415 | partition := strings.SplitN(path, "/", 2) |
| 416 | name := partition[0] |
| 417 | path = partition[1] |
| 418 | |
| 419 | field := ChildrenFields(n.Type)[name] |
| 420 | var children []*Revision |
| 421 | post_anno := make(map[string]interface{}) |
| 422 | |
| 423 | if field.IsContainer { |
| 424 | if path == "" { |
| 425 | fmt.Errorf("cannot remove without a key\n") |
| 426 | } else if field.Key != "" { |
| 427 | partition := strings.SplitN(path, "/", 2) |
| 428 | key := partition[0] |
| 429 | path = partition[1] |
| 430 | key = field.KeyFromStr(key).(string) |
| 431 | if path != "" { |
| 432 | copy(children, rev.Children[name]) |
| 433 | idx, childRev := n.findRevByKey(children, field.Key, key) |
| 434 | childNode := childRev.getNode() |
| 435 | newChildRev := childNode.Remove(path, txid, makeBranch) |
| 436 | children[idx] = newChildRev |
| 437 | rev := rev.UpdateChildren(name, children, branch) |
| 438 | n.makeLatest(branch, rev, nil) |
| 439 | return rev |
| 440 | } else { |
| 441 | copy(children, rev.Children[name]) |
| 442 | idx, childRev := n.findRevByKey(children, field.Key, key) |
| 443 | if n.Proxy != nil { |
| 444 | data := childRev.getData() |
| 445 | fmt.Println("invoking proxy PRE_REMOVE Callbacks") |
| 446 | fmt.Printf("setting POST_REMOVE Callbacks : %+v\n", data) |
| 447 | } else { |
| 448 | fmt.Println("setting POST_REMOVE Callbacks") |
| 449 | } |
| 450 | children = append(children[:idx], children[idx+1:]...) |
| 451 | rev := rev.UpdateChildren(name, children, branch) |
| 452 | n.makeLatest(branch, rev, post_anno) |
| 453 | return rev |
| 454 | } |
| 455 | } else { |
| 456 | fmt.Errorf("cannot add to non-keyed container\n") |
| 457 | } |
| 458 | } else { |
| 459 | fmt.Errorf("cannot add to non-container field\n") |
| 460 | } |
| 461 | |
| 462 | return nil |
| 463 | } |
| 464 | |
| 465 | func (n *Node) LoadLatest(kvStore *Backend, hash string) { |
| 466 | branch := NewBranch(n, "", nil, n.AutoPrune) |
| 467 | pr := &PersistedRevision{} |
| 468 | rev := pr.load(branch, kvStore, n.Type, hash) |
| 469 | n.makeLatest(branch, rev.Revision, nil) |
| 470 | n.Branches[NONE] = branch |
| 471 | } |