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