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