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 | "encoding/hex" |
| 20 | "encoding/json" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 21 | "github.com/google/uuid" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | "reflect" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 24 | "time" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 27 | type Root interface { |
| 28 | Node |
| 29 | } |
| 30 | |
| 31 | type root struct { |
| 32 | node |
| 33 | |
| 34 | DirtyNodes map[string][]*node |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 35 | KvStore *Backend |
| 36 | Loading bool |
| 37 | RevisionClass interface{} |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 38 | Callbacks []CallbackTuple |
| 39 | NotificationCallbacks []CallbackTuple |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 40 | } |
| 41 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 42 | func NewRoot(initialData interface{}, kvStore *Backend) *root { |
| 43 | root := &root{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 44 | root.KvStore = kvStore |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 45 | root.DirtyNodes = make(map[string][]*node) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 46 | root.Loading = false |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 47 | |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 48 | if kvStore != nil { |
| 49 | root.RevisionClass = reflect.TypeOf(PersistedRevision{}) |
| 50 | } else { |
| 51 | root.RevisionClass = reflect.TypeOf(NonPersistedRevision{}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 52 | } |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 53 | root.Callbacks = []CallbackTuple{} |
| 54 | root.NotificationCallbacks = []CallbackTuple{} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 55 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 56 | root.node = *NewNode(root, initialData, false, "") |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 57 | |
| 58 | return root |
| 59 | } |
| 60 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 61 | func (r *root) MakeRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 62 | if r.RevisionClass.(reflect.Type) == reflect.TypeOf(PersistedRevision{}) { |
| 63 | return NewPersistedRevision(branch, data, children) |
| 64 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 66 | return NewNonPersistedRevision(branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 69 | func (r *root) MakeTxBranch() string { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 70 | txid_bin, _ := uuid.New().MarshalBinary() |
| 71 | txid := hex.EncodeToString(txid_bin)[:12] |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 72 | r.DirtyNodes[txid] = []*node{&r.node} |
| 73 | r.node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 74 | return txid |
| 75 | } |
| 76 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 77 | func (r *root) DeleteTxBranch(txid string) { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 78 | for _, dirtyNode := range r.DirtyNodes[txid] { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 79 | dirtyNode.DeleteBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 80 | } |
| 81 | delete(r.DirtyNodes, txid) |
| 82 | } |
| 83 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 84 | func (r *root) FoldTxBranch(txid string) { |
| 85 | if _, err := r.MergeBranch(txid, true); err != nil { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 86 | r.DeleteTxBranch(txid) |
| 87 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 88 | r.MergeBranch(txid, false) |
| 89 | r.ExecuteCallbacks() |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 90 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 93 | func (r *root) ExecuteCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 94 | for len(r.Callbacks) > 0 { |
| 95 | callback := r.Callbacks[0] |
| 96 | r.Callbacks = r.Callbacks[1:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 97 | callback.Execute(nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 98 | } |
| 99 | for len(r.NotificationCallbacks) > 0 { |
| 100 | callback := r.NotificationCallbacks[0] |
| 101 | r.NotificationCallbacks = r.NotificationCallbacks[1:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 102 | callback.Execute(nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 106 | func (r *root) HasCallbacks() bool { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 107 | return len(r.Callbacks) == 0 |
| 108 | } |
| 109 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 110 | func (r *root) AddCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 111 | r.Callbacks = append(r.Callbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 112 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 113 | func (r *root) AddNotificationCallback(callback CallbackFunction, args ...interface{}) { |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 114 | r.NotificationCallbacks = append(r.NotificationCallbacks, CallbackTuple{callback, args}) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 115 | } |
| 116 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 117 | func (r *root) Update(path string, data interface{}, strict bool, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 118 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 119 | |
| 120 | if makeBranch == nil { |
| 121 | // TODO: raise error |
| 122 | } |
| 123 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 124 | if r.HasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 125 | // TODO: raise error |
| 126 | } |
| 127 | |
| 128 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 129 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 130 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 131 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 132 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 133 | result = r.node.Update(path, data, strict, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 134 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 135 | result = r.node.Update(path, data, strict, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 136 | } |
| 137 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 138 | r.ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 139 | |
| 140 | return result |
| 141 | } |
| 142 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 143 | func (r *root) Add(path string, data interface{}, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 144 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 145 | |
| 146 | if makeBranch == nil { |
| 147 | // TODO: raise error |
| 148 | } |
| 149 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 150 | if r.HasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 151 | // TODO: raise error |
| 152 | } |
| 153 | |
| 154 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 155 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 156 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 157 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 158 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 159 | result = r.node.Add(path, data, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 160 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 161 | result = r.node.Add(path, data, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 164 | r.ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 165 | |
| 166 | return result |
| 167 | } |
| 168 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 169 | func (r *root) Remove(path string, txid string, makeBranch MakeBranchFunction) Revision { |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 170 | var result Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 171 | |
| 172 | if makeBranch == nil { |
| 173 | // TODO: raise error |
| 174 | } |
| 175 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 176 | if r.HasCallbacks() { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 177 | // TODO: raise error |
| 178 | } |
| 179 | |
| 180 | if txid != "" { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 181 | trackDirty := func(node *node) *Branch { |
Stephane Barbarie | e16186c | 2018-09-11 10:46:34 -0400 | [diff] [blame] | 182 | r.DirtyNodes[txid] = append(r.DirtyNodes[txid], node) |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 183 | return node.MakeBranch(txid) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 184 | } |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 185 | result = r.node.Remove(path, txid, trackDirty) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 186 | } else { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 187 | result = r.node.Remove(path, "", nil) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 190 | r.ExecuteCallbacks() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 191 | |
| 192 | return result |
| 193 | } |
| 194 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 195 | func (r *root) Load(rootClass interface{}) *root { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 196 | //fakeKvStore := &Backend{} |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 197 | //root := NewRoot(rootClass, nil) |
| 198 | //root.KvStore = r.KvStore |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 199 | r.LoadFromPersistence(rootClass) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 200 | return r |
| 201 | } |
| 202 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 203 | func (r *root) MakeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 204 | r.makeLatest(branch, revision, changeAnnouncement) |
| 205 | } |
| 206 | |
| 207 | func (r *root) makeLatest(branch *Branch, revision Revision, changeAnnouncement []ChangeTuple) { |
| 208 | r.node.makeLatest(branch, revision, changeAnnouncement) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 209 | |
| 210 | if r.KvStore != nil && branch.Txid == "" { |
| 211 | tags := make(map[string]string) |
| 212 | for k, v := range r.Tags { |
| 213 | tags[k] = v.GetHash() |
| 214 | } |
| 215 | data := &rootData{ |
| 216 | Latest: branch.Latest.GetHash(), |
| 217 | Tags: tags, |
| 218 | } |
| 219 | if blob, err := json.Marshal(data); err != nil { |
| 220 | // TODO report error |
| 221 | } else { |
| 222 | log.Debugf("Changing root to : %s", string(blob)) |
| 223 | if err := r.KvStore.Put("root", blob); err != nil { |
| 224 | log.Errorf("failed to properly put value in kvstore - err: %s", err.Error()) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 230 | func (r *root) LoadLatest(hash string) { |
| 231 | r.node.LoadLatest(r.KvStore, hash) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | type rootData struct { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 235 | Latest string `json:latest` |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 236 | Tags map[string]string `json:tags` |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 239 | func (r *root) LoadFromPersistence(rootClass interface{}) { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 240 | var data rootData |
| 241 | |
| 242 | r.Loading = true |
| 243 | blob, _ := r.KvStore.Get("root") |
| 244 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 245 | start := time.Now() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 246 | if err := json.Unmarshal(blob.Value.([]byte), &data); err != nil { |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame^] | 247 | log.Errorf("problem to unmarshal blob - error:%s\n", err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 248 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 249 | stop := time.Now() |
| 250 | GetProfiling().AddToInMemoryModelTime(stop.Sub(start).Seconds()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 251 | for tag, hash := range data.Tags { |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 252 | r.node.LoadLatest(r.KvStore, hash) |
| 253 | r.node.Tags[tag] = r.node.Latest() |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 254 | } |
| 255 | |
Stephane Barbarie | 06c4a74 | 2018-10-01 11:09:32 -0400 | [diff] [blame] | 256 | r.node.LoadLatest(r.KvStore, data.Latest) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 257 | r.Loading = false |
| 258 | } |