blob: 8331e116065a735ef809f5be9279bad530b4e97d [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
19import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040020 "context"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040021 "encoding/hex"
22 "encoding/json"
Stephane Barbarie1ab43272018-12-08 21:42:13 -050023 "github.com/golang/protobuf/proto"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040024 "github.com/google/uuid"
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/opencord/voltha-go/common/log"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040026 "reflect"
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027 "sync"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028)
29
Stephane Barbariedc5022d2018-11-19 15:21:44 -050030// Root is used to provide an abstraction to the base root structure
Stephane Barbarie06c4a742018-10-01 11:09:32 -040031type Root interface {
32 Node
Stephane Barbariedc5022d2018-11-19 15:21:44 -050033
34 ExecuteCallbacks()
35 AddCallback(callback CallbackFunction, args ...interface{})
36 AddNotificationCallback(callback CallbackFunction, args ...interface{})
Stephane Barbarie06c4a742018-10-01 11:09:32 -040037}
38
Stephane Barbariedc5022d2018-11-19 15:21:44 -050039// root points to the top of the data model tree or sub-tree identified by a proxy
Stephane Barbarie06c4a742018-10-01 11:09:32 -040040type root struct {
Stephane Barbarie126101e2018-10-11 16:18:48 -040041 *node
42
43 Callbacks []CallbackTuple
44 NotificationCallbacks []CallbackTuple
Stephane Barbarie06c4a742018-10-01 11:09:32 -040045
Stephane Barbariedc5022d2018-11-19 15:21:44 -050046 DirtyNodes map[string][]*node
47 KvStore *Backend
48 Loading bool
49 RevisionClass interface{}
50
khenaidoo43c82122018-11-22 18:38:28 -050051 mutex sync.RWMutex
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040052}
53
Stephane Barbariedc5022d2018-11-19 15:21:44 -050054// NewRoot creates an new instance of a root object
Stephane Barbarie06c4a742018-10-01 11:09:32 -040055func NewRoot(initialData interface{}, kvStore *Backend) *root {
56 root := &root{}
Stephane Barbarie126101e2018-10-11 16:18:48 -040057
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040058 root.KvStore = kvStore
Stephane Barbarie06c4a742018-10-01 11:09:32 -040059 root.DirtyNodes = make(map[string][]*node)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040060 root.Loading = false
Stephane Barbarie06c4a742018-10-01 11:09:32 -040061
Stephane Barbariedc5022d2018-11-19 15:21:44 -050062 // If there is no storage in place just revert to
63 // a non persistent mechanism
Stephane Barbarie88fbe7f2018-09-25 12:25:23 -040064 if kvStore != nil {
65 root.RevisionClass = reflect.TypeOf(PersistedRevision{})
66 } else {
67 root.RevisionClass = reflect.TypeOf(NonPersistedRevision{})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068 }
Stephane Barbariedc5022d2018-11-19 15:21:44 -050069
Stephane Barbarie694e2b92018-09-07 12:17:36 -040070 root.Callbacks = []CallbackTuple{}
71 root.NotificationCallbacks = []CallbackTuple{}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040072
Stephane Barbariedc5022d2018-11-19 15:21:44 -050073 root.node = NewNode(root, initialData, false, "")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040074
75 return root
76}
77
Stephane Barbariedc5022d2018-11-19 15:21:44 -050078// MakeTxBranch creates a new transaction branch
Stephane Barbarie06c4a742018-10-01 11:09:32 -040079func (r *root) MakeTxBranch() string {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050080 txidBin, _ := uuid.New().MarshalBinary()
81 txid := hex.EncodeToString(txidBin)[:12]
82
Stephane Barbarie126101e2018-10-11 16:18:48 -040083 r.DirtyNodes[txid] = []*node{r.node}
Stephane Barbarie06c4a742018-10-01 11:09:32 -040084 r.node.MakeBranch(txid)
Stephane Barbariedc5022d2018-11-19 15:21:44 -050085
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040086 return txid
87}
88
Stephane Barbariedc5022d2018-11-19 15:21:44 -050089// DeleteTxBranch removes a transaction branch
Stephane Barbarie06c4a742018-10-01 11:09:32 -040090func (r *root) DeleteTxBranch(txid string) {
Stephane Barbariee16186c2018-09-11 10:46:34 -040091 for _, dirtyNode := range r.DirtyNodes[txid] {
Stephane Barbarie06c4a742018-10-01 11:09:32 -040092 dirtyNode.DeleteBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040093 }
94 delete(r.DirtyNodes, txid)
Stephane Barbariec92d1072019-06-07 16:21:49 -040095 r.node.DeleteBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040096}
97
Stephane Barbariedc5022d2018-11-19 15:21:44 -050098// FoldTxBranch will merge the contents of a transaction branch with the root object
Stephane Barbarie06c4a742018-10-01 11:09:32 -040099func (r *root) FoldTxBranch(txid string) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500100 // Start by doing a dry run of the merge
101 // If that fails, it bails out and the branch is deleted
102 if _, err := r.node.MergeBranch(txid, true); err != nil {
103 // Merge operation fails
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400104 r.DeleteTxBranch(txid)
105 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500106 r.node.MergeBranch(txid, false)
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400107 r.node.GetRoot().ExecuteCallbacks()
Stephane Barbarie260a5632019-02-26 16:12:49 -0500108 r.DeleteTxBranch(txid)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400109 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400110}
111
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500112// ExecuteCallbacks will invoke all the callbacks linked to root object
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400113func (r *root) ExecuteCallbacks() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500114 r.mutex.Lock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500115 defer r.mutex.Unlock()
Stephane Barbariec92d1072019-06-07 16:21:49 -0400116
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400117 for len(r.Callbacks) > 0 {
118 callback := r.Callbacks[0]
119 r.Callbacks = r.Callbacks[1:]
khenaidoo43c82122018-11-22 18:38:28 -0500120 go callback.Execute(nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400121 }
Stephane Barbarie40fd3b22019-04-23 21:50:47 -0400122 //for len(r.NotificationCallbacks) > 0 {
123 // callback := r.NotificationCallbacks[0]
124 // r.NotificationCallbacks = r.NotificationCallbacks[1:]
125 // go callback.Execute(nil)
126 //}
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400127}
128
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500129func (r *root) hasCallbacks() bool {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400130 return len(r.Callbacks) == 0
131}
132
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500133// getCallbacks returns the available callbacks
134func (r *root) GetCallbacks() []CallbackTuple {
135 r.mutex.Lock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500136 defer r.mutex.Unlock()
Stephane Barbariec92d1072019-06-07 16:21:49 -0400137
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500138 return r.Callbacks
139}
140
141// getCallbacks returns the available notification callbacks
142func (r *root) GetNotificationCallbacks() []CallbackTuple {
143 r.mutex.Lock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500144 defer r.mutex.Unlock()
Stephane Barbariec92d1072019-06-07 16:21:49 -0400145
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500146 return r.NotificationCallbacks
147}
148
149// AddCallback inserts a new callback with its arguments
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400150func (r *root) AddCallback(callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500151 r.mutex.Lock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500152 defer r.mutex.Unlock()
Stephane Barbariec92d1072019-06-07 16:21:49 -0400153
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400154 r.Callbacks = append(r.Callbacks, CallbackTuple{callback, args})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400155}
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500156
157// AddNotificationCallback inserts a new notification callback with its arguments
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400158func (r *root) AddNotificationCallback(callback CallbackFunction, args ...interface{}) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500159 r.mutex.Lock()
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500160 defer r.mutex.Unlock()
Stephane Barbariec92d1072019-06-07 16:21:49 -0400161
Stephane Barbarie694e2b92018-09-07 12:17:36 -0400162 r.NotificationCallbacks = append(r.NotificationCallbacks, CallbackTuple{callback, args})
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400163}
164
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500165func (r *root) syncParent(childRev Revision, txid string) {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400166 data := proto.Clone(r.GetProxy().ParentNode.Latest().GetData().(proto.Message))
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500167
168 for fieldName, _ := range ChildrenFields(data) {
169 childDataName, childDataHolder := GetAttributeValue(data, fieldName, 0)
170 if reflect.TypeOf(childRev.GetData()) == reflect.TypeOf(childDataHolder.Interface()) {
171 childDataHolder = reflect.ValueOf(childRev.GetData())
172 reflect.ValueOf(data).Elem().FieldByName(childDataName).Set(childDataHolder)
173 }
174 }
175
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400176 r.GetProxy().ParentNode.Latest().SetConfig(NewDataRevision(r.GetProxy().ParentNode.GetRoot(), data))
177 r.GetProxy().ParentNode.Latest(txid).Finalize(false)
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500178}
179
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500180// Update modifies the content of an object at a given path with the provided data
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400181func (r *root) Update(ctx context.Context, path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400182 var result Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400183
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500184 if makeBranch != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400185 // TODO: raise error
186 }
187
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500188 if r.hasCallbacks() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400189 // TODO: raise error
190 }
191
192 if txid != "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400193 trackDirty := func(node *node) *Branch {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400194 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400195 return node.MakeBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400196 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400197 result = r.node.Update(ctx, path, data, strict, txid, trackDirty)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400198 } else {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400199 result = r.node.Update(ctx, path, data, strict, "", nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400200 }
201
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500202 if result != nil {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400203 if r.GetProxy().FullPath != r.GetProxy().Path {
Stephane Barbariee0a4c792019-01-16 11:26:29 -0500204 r.syncParent(result, txid)
205 } else {
206 result.Finalize(false)
207 }
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500208 }
209
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500210 r.node.GetRoot().ExecuteCallbacks()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400211
212 return result
213}
214
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500215// Add creates a new object at the given path with the provided data
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400216func (r *root) Add(ctx context.Context, path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400217 var result Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400218
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500219 if makeBranch != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400220 // TODO: raise error
221 }
222
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500223 if r.hasCallbacks() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400224 // TODO: raise error
225 }
226
227 if txid != "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400228 trackDirty := func(node *node) *Branch {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400229 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400230 return node.MakeBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400231 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400232 result = r.node.Add(ctx, path, data, txid, trackDirty)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400233 } else {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400234 result = r.node.Add(ctx, path, data, "", nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400235 }
236
Stephane Barbarie1ab43272018-12-08 21:42:13 -0500237 if result != nil {
238 result.Finalize(true)
239 r.node.GetRoot().ExecuteCallbacks()
240 }
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400241 return result
242}
243
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500244// Remove discards an object at a given path
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400245func (r *root) Remove(ctx context.Context, path string, txid string, makeBranch MakeBranchFunction) Revision {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400246 var result Revision
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400247
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500248 if makeBranch != nil {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400249 // TODO: raise error
250 }
251
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500252 if r.hasCallbacks() {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400253 // TODO: raise error
254 }
255
256 if txid != "" {
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400257 trackDirty := func(node *node) *Branch {
Stephane Barbariee16186c2018-09-11 10:46:34 -0400258 r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node)
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400259 return node.MakeBranch(txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400260 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400261 result = r.node.Remove(ctx, path, txid, trackDirty)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400262 } else {
Stephane Barbarieef6650d2019-07-18 12:15:09 -0400263 result = r.node.Remove(ctx, path, "", nil)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400264 }
265
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500266 r.node.GetRoot().ExecuteCallbacks()
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400267
268 return result
269}
270
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500271// MakeLatest updates a branch with the latest node revision
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400272func (r *root) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
273 r.makeLatest(branch, revision, changeAnnouncement)
274}
275
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500276func (r *root) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision {
277 if r.RevisionClass.(reflect.Type) == reflect.TypeOf(PersistedRevision{}) {
278 return NewPersistedRevision(branch, data, children)
279 }
280
281 return NewNonPersistedRevision(r, branch, data, children)
282}
283
Stephane Barbarie06c4a742018-10-01 11:09:32 -0400284func (r *root) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) {
285 r.node.makeLatest(branch, revision, changeAnnouncement)
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400286
287 if r.KvStore != nil && branch.Txid == "" {
288 tags := make(map[string]string)
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500289 for k, v := range r.node.Tags {
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400290 tags[k] = v.GetHash()
291 }
292 data := &rootData{
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500293 Latest: branch.GetLatest().GetHash(),
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400294 Tags: tags,
295 }
296 if blob, err := json.Marshal(data); err != nil {
297 // TODO report error
298 } else {
299 log.Debugf("Changing root to : %s", string(blob))
300 if err := r.KvStore.Put("root", blob); err != nil {
301 log.Errorf("failed to properly put value in kvstore - err: %s", err.Error())
302 }
303 }
304 }
305}
306
Stephane Barbarie4a2564d2018-07-26 11:02:58 -0400307type rootData struct {
Kent Hagerman0ab4cb22019-04-24 13:13:35 -0400308 Latest string `json:"latest"`
309 Tags map[string]string `json:"tags"`
310}