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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "compress/gzip" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -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 | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | "runtime/debug" |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 26 | "strings" |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | "sync" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 30 | // PersistedRevision holds information of revision meant to be saved in a persistent storage |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 31 | type PersistedRevision struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 32 | mutex sync.RWMutex |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 33 | Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | Compress bool |
| 35 | kvStore *Backend |
| 36 | } |
| 37 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 38 | // NewPersistedRevision creates a new instance of a PersistentRevision structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 39 | func NewPersistedRevision(branch *Branch, data interface{}, children map[string][]Revision) Revision { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 40 | pr := &PersistedRevision{} |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 41 | pr.kvStore = branch.Node.GetRoot().KvStore |
| 42 | pr.Revision = NewNonPersistedRevision(nil, branch, data, children) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 43 | return pr |
| 44 | } |
| 45 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 46 | // Finalize is responsible of saving the revision in the persistent storage |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 47 | func (pr *PersistedRevision) Finalize(skipOnExist bool) { |
| 48 | pr.store(skipOnExist) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | type revData struct { |
| 52 | Children map[string][]string |
| 53 | Config string |
| 54 | } |
| 55 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 56 | func (pr *PersistedRevision) store(skipOnExist bool) { |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 57 | if pr.GetBranch().Txid != "" { |
| 58 | return |
| 59 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 60 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 61 | if pair, _ := pr.kvStore.Get(pr.GetHash()); pair != nil && skipOnExist { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 62 | log.Debugf("Config already exists - hash:%s, stack: %s", pr.GetConfig().Hash, string(debug.Stack())) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 63 | return |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 64 | //} |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 66 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 67 | if blob, err := proto.Marshal(pr.GetConfig().Data.(proto.Message)); err != nil { |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 68 | // TODO report error |
| 69 | } else { |
| 70 | if pr.Compress { |
| 71 | var b bytes.Buffer |
| 72 | w := gzip.NewWriter(&b) |
| 73 | w.Write(blob) |
| 74 | w.Close() |
| 75 | blob = b.Bytes() |
| 76 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 77 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 78 | if err := pr.kvStore.Put(pr.GetHash(), blob); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 79 | log.Warnf("Problem storing revision config - error: %s, hash: %s, data: %+v", err.Error(), |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 80 | pr.GetHash(), |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 81 | pr.GetConfig().Data) |
| 82 | } else { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 83 | log.Debugf("Stored config - hash:%s, blob: %+v, stack: %s", pr.GetHash(), pr.GetConfig().Data, |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | string(debug.Stack())) |
| 85 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 89 | func (pr *PersistedRevision) LoadFromPersistence(path string, txid string) []Revision { |
| 90 | var response []Revision |
| 91 | var rev Revision |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 92 | |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 93 | rev = pr |
| 94 | |
| 95 | if pr.kvStore != nil { |
| 96 | blobMap, _ := pr.kvStore.List(path) |
| 97 | |
| 98 | partition := strings.SplitN(path, "/", 2) |
| 99 | name := partition[0] |
| 100 | |
| 101 | if len(partition) < 2 { |
| 102 | path = "" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 103 | } else { |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 104 | path = partition[1] |
| 105 | } |
| 106 | |
| 107 | field := ChildrenFields(rev.GetBranch().Node.Type)[name] |
| 108 | |
| 109 | if field.IsContainer { |
| 110 | for _, blob := range blobMap { |
| 111 | output := blob.Value.([]byte) |
| 112 | |
| 113 | data := reflect.New(field.ClassType.Elem()) |
| 114 | |
| 115 | if err := proto.Unmarshal(output, data.Interface().(proto.Message)); err != nil { |
| 116 | // TODO report error |
| 117 | } else { |
| 118 | |
| 119 | var children []Revision |
| 120 | |
| 121 | if path == "" { |
| 122 | if field.Key != "" { |
| 123 | // e.g. /logical_devices/abcde --> path="" name=logical_devices key=abcde |
| 124 | if field.Key != "" { |
| 125 | children = make([]Revision, len(rev.GetChildren()[name])) |
| 126 | copy(children, rev.GetChildren()[name]) |
| 127 | |
| 128 | _, key := GetAttributeValue(data.Interface(), field.Key, 0) |
| 129 | |
| 130 | childRev := rev.GetBranch().Node.MakeNode(data.Interface(), txid).Latest(txid) |
| 131 | childRev.SetHash(name + "/" + key.String()) |
| 132 | children = append(children, childRev) |
| 133 | rev = rev.UpdateChildren(name, children, rev.GetBranch()) |
| 134 | |
| 135 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
| 136 | |
| 137 | response = append(response, childRev) |
| 138 | continue |
| 139 | } |
| 140 | } |
| 141 | } else if field.Key != "" { |
| 142 | // e.g. /logical_devices/abcde/flows/vwxyz --> path=abcde/flows/vwxyz |
| 143 | |
| 144 | partition := strings.SplitN(path, "/", 2) |
| 145 | key := partition[0] |
| 146 | if len(partition) < 2 { |
| 147 | path = "" |
| 148 | } else { |
| 149 | path = partition[1] |
| 150 | } |
| 151 | keyValue := field.KeyFromStr(key) |
| 152 | |
| 153 | children = make([]Revision, len(rev.GetChildren()[name])) |
| 154 | copy(children, rev.GetChildren()[name]) |
| 155 | |
| 156 | idx, childRev := rev.GetBranch().Node.findRevByKey(children, field.Key, keyValue) |
| 157 | |
| 158 | newChildRev := childRev.LoadFromPersistence(path, txid) |
| 159 | |
| 160 | children[idx] = newChildRev[0] |
| 161 | |
| 162 | rev := rev.UpdateChildren(name, rev.GetChildren()[name], rev.GetBranch()) |
| 163 | rev.GetBranch().Node.makeLatest(rev.GetBranch(), rev, nil) |
| 164 | |
| 165 | response = append(response, newChildRev[0]) |
| 166 | continue |
| 167 | } |
| 168 | } |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } |
Stephane Barbarie | 1ab4327 | 2018-12-08 21:42:13 -0500 | [diff] [blame] | 172 | return response |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 173 | } |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 174 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 175 | // UpdateData modifies the information in the data model and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 176 | func (pr *PersistedRevision) UpdateData(data interface{}, branch *Branch) Revision { |
| 177 | newNPR := pr.Revision.UpdateData(data, branch) |
| 178 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 179 | log.Debugf("Updating data %+v", data) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 180 | newPR := &PersistedRevision{ |
| 181 | Revision: newNPR, |
| 182 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 183 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 186 | return newPR |
| 187 | } |
| 188 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 189 | // UpdateChildren modifies the children of a revision and of a specific component and saves it in the persistent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 190 | func (pr *PersistedRevision) UpdateChildren(name string, children []Revision, branch *Branch) Revision { |
| 191 | newNPR := pr.Revision.UpdateChildren(name, children, branch) |
| 192 | |
| 193 | newPR := &PersistedRevision{ |
| 194 | Revision: newNPR, |
| 195 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 196 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 197 | } |
| 198 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 199 | return newPR |
| 200 | } |
| 201 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 202 | // UpdateAllChildren modifies the children for all components of a revision and saves it in the peristent storage |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 203 | func (pr *PersistedRevision) UpdateAllChildren(children map[string][]Revision, branch *Branch) Revision { |
| 204 | newNPR := pr.Revision.UpdateAllChildren(children, branch) |
| 205 | |
| 206 | newPR := &PersistedRevision{ |
| 207 | Revision: newNPR, |
| 208 | Compress: pr.Compress, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 209 | kvStore: pr.kvStore, |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 212 | return newPR |
| 213 | } |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 214 | |
| 215 | // Drop takes care of eliminating a revision hash that is no longer needed |
| 216 | // and its associated config when required |
| 217 | func (pr *PersistedRevision) Drop(txid string, includeConfig bool) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 218 | pr.mutex.Lock() |
| 219 | defer pr.mutex.Unlock() |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 220 | if pr.kvStore != nil && txid == "" { |
| 221 | if includeConfig { |
| 222 | log.Debugf("removing rev config - hash: %s", pr.GetConfig().Hash) |
| 223 | if err := pr.kvStore.Delete(pr.GetConfig().Hash); err != nil { |
| 224 | log.Errorf( |
| 225 | "failed to remove rev config - hash: %s, err: %s", |
| 226 | pr.GetConfig().Hash, |
| 227 | err.Error(), |
| 228 | ) |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | log.Debugf("removing rev - hash: %s", pr.GetHash()) |
| 233 | if err := pr.kvStore.Delete(pr.GetHash()); err != nil { |
| 234 | log.Errorf("failed to remove rev - hash: %s, err: %s", pr.GetHash(), err.Error()) |
| 235 | } |
| 236 | } else { |
| 237 | if includeConfig { |
| 238 | log.Debugf("Attempted to remove revision config:%s linked to transaction:%s", pr.GetConfig().Hash, txid) |
| 239 | } |
| 240 | log.Debugf("Attempted to remove revision:%s linked to transaction:%s", pr.GetHash(), txid) |
| 241 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 242 | |
| 243 | pr.Revision.Drop(txid, includeConfig) |
Stephane Barbarie | 88fbe7f | 2018-09-25 12:25:23 -0400 | [diff] [blame] | 244 | } |