blob: e10236c1cabd837c0ebc9e29f52b6b37d39d2828 [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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040018import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040019 "context"
Thomas Lee Se5a44012019-11-07 20:32:24 +053020 "fmt"
Scott Baker807addd2019-10-24 15:16:21 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040022)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040023
24type Transaction struct {
25 proxy *Proxy
26 txid string
27}
28
29func NewTransaction(proxy *Proxy, txid string) *Transaction {
30 tx := &Transaction{
31 proxy: proxy,
32 txid: txid,
33 }
34 return tx
35}
Thomas Lee Se5a44012019-11-07 20:32:24 +053036func (t *Transaction) Get(ctx context.Context, path string, depth int, deep bool) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040037 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040038 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053039 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040040 }
41 // TODO: need to review the return values at the different layers!!!!!
Stephane Barbarieef6650d2019-07-18 12:15:09 -040042 return t.proxy.Get(ctx, path, depth, deep, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040043}
Thomas Lee Se5a44012019-11-07 20:32:24 +053044func (t *Transaction) Update(ctx context.Context, path string, data interface{}, strict bool) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040045 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040046 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053047 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040048 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040049 return t.proxy.Update(ctx, path, data, strict, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040050}
Thomas Lee Se5a44012019-11-07 20:32:24 +053051func (t *Transaction) Add(ctx context.Context, path string, data interface{}) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040052 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040053 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053054 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040055 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040056 return t.proxy.Add(ctx, path, data, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040057}
Thomas Lee Se5a44012019-11-07 20:32:24 +053058func (t *Transaction) Remove(ctx context.Context, path string) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040059 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040060 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053061 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040062 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040063 return t.proxy.Remove(ctx, path, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040064}
65func (t *Transaction) Cancel() {
66 t.proxy.cancelTransaction(t.txid)
67 t.txid = ""
68}
69func (t *Transaction) Commit() {
70 t.proxy.commitTransaction(t.txid)
71 t.txid = ""
72}