blob: 8085eaf4590adb88750d83a7bd709e2dccf1840e [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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import (
19 "context"
20 "fmt"
21 "strings"
22)
23
Stephane Barbarieec0919b2018-09-05 14:14:29 -040024type OperationContext struct {
25 Path string
26 Data interface{}
27 FieldName string
28 ChildKey string
29}
30
31func NewOperationContext(path string, data interface{}, fieldName string, childKey string) *OperationContext {
32 oc := &OperationContext{
33 Path: path,
34 Data: data,
35 FieldName: fieldName,
36 ChildKey: childKey,
37 }
38 return oc
39}
40
41func (oc *OperationContext) Update(data interface{}) *OperationContext {
42 oc.Data = data
43 return oc
44}
45
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040046type Proxy struct {
47 Root *Root
48 Node *Node
49 Path string
50 Exclusive bool
51 Callbacks []interface{}
52}
53
54func NewProxy(root *Root, node *Node, path string, exclusive bool) *Proxy {
55 p := &Proxy{
56 Root: root,
57 Node: node,
58 Exclusive: exclusive,
59 Path: path,
60 Callbacks: []interface{}{},
61 }
62 return p
63}
64
65func (p *Proxy) Get(path string, depth int, deep bool, txid string) interface{} {
66 return p.Node.Get(path, "", depth, deep, txid)
67}
68
69func (p *Proxy) Update(path string, data interface{}, strict bool, txid string) interface{} {
70 if !strings.HasPrefix(path, "/") {
71 fmt.Errorf("invalid path: %s", path)
72 return nil
73 }
74 var fullPath string
75 if path == "/" {
76 fullPath = p.Path
77 } else {
78 fullPath = p.Path + path
79 }
80 return p.Node.Update(fullPath, data, strict, txid, nil)
81}
82
83func (p *Proxy) Add(path string, data interface{}, txid string) interface{} {
84 if !strings.HasPrefix(path, "/") {
85 fmt.Errorf("invalid path: %s", path)
86 return nil
87 }
88 var fullPath string
89 if path == "/" {
90 fullPath = p.Path
91 } else {
92 fullPath = p.Path + path
93 }
94 return p.Node.Add(fullPath, data, txid, nil)
95}
96
97func (p *Proxy) Remove(path string, txid string) interface{} {
98 if !strings.HasPrefix(path, "/") {
99 fmt.Errorf("invalid path: %s", path)
100 return nil
101 }
102 var fullPath string
103 if path == "/" {
104 fullPath = p.Path
105 } else {
106 fullPath = p.Path + path
107 }
108 return p.Node.Remove(fullPath, txid, nil)
109}
110
111func (p *Proxy) openTransaction() *Transaction {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400112 txid := p.Root.MakeTxBranch()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400113 return NewTransaction(p, txid)
114}
115
116func (p *Proxy) commitTransaction(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400117 p.Root.FoldTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400118}
119
120func (p *Proxy) cancelTransaction(txid string) {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400121 p.Root.DeleteTxBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400122}
123
124func (p *Proxy) RegisterCallback(callbackType CallbackType, callback func(), args ...interface{}) {
125}
126
127func (p *Proxy) UnregisterCallback(callbackType CallbackType, callback func(), args ...interface{}) {
128}
129
130func (p *Proxy) InvokeCallback(callbackType CallbackType, context context.Context, proceedOnError bool) {
131}