blob: 7879a89abf0ed4f6e6834d42b548eb8cd42944d8 [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 */
npujar9a30c702019-11-14 17:06:39 +053016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040019import (
Stephane Barbarieef6650d2019-07-18 12:15:09 -040020 "context"
Thomas Lee Se5a44012019-11-07 20:32:24 +053021 "fmt"
npujar9a30c702019-11-14 17:06:39 +053022
serkant.uluderya2ae470f2020-01-21 11:13:09 -080023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040024)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025
npujar9a30c702019-11-14 17:06:39 +053026// Transaction -
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040027type Transaction struct {
28 proxy *Proxy
29 txid string
30}
31
npujar9a30c702019-11-14 17:06:39 +053032// NewTransaction -
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040033func NewTransaction(proxy *Proxy, txid string) *Transaction {
34 tx := &Transaction{
35 proxy: proxy,
36 txid: txid,
37 }
38 return tx
39}
Thomas Lee Se5a44012019-11-07 20:32:24 +053040func (t *Transaction) Get(ctx context.Context, path string, depth int, deep bool) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040041 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040042 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053043 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040044 }
45 // TODO: need to review the return values at the different layers!!!!!
Stephane Barbarieef6650d2019-07-18 12:15:09 -040046 return t.proxy.Get(ctx, path, depth, deep, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040047}
Thomas Lee Se5a44012019-11-07 20:32:24 +053048func (t *Transaction) Update(ctx context.Context, path string, data interface{}, strict bool) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040049 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040050 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053051 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040052 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040053 return t.proxy.Update(ctx, path, data, strict, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040054}
Thomas Lee Se5a44012019-11-07 20:32:24 +053055func (t *Transaction) Add(ctx context.Context, path string, data interface{}) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040056 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040057 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053058 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040059 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040060 return t.proxy.Add(ctx, path, data, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040061}
Thomas Lee Se5a44012019-11-07 20:32:24 +053062func (t *Transaction) Remove(ctx context.Context, path string) (interface{}, error) {
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040063 if t.txid == "" {
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040064 log.Errorf("closed transaction")
Thomas Lee Se5a44012019-11-07 20:32:24 +053065 return nil, fmt.Errorf("closed transaction")
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040066 }
Stephane Barbarieef6650d2019-07-18 12:15:09 -040067 return t.proxy.Remove(ctx, path, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040068}
npujar9a30c702019-11-14 17:06:39 +053069
70// Cancel -
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040071func (t *Transaction) Cancel() {
72 t.proxy.cancelTransaction(t.txid)
73 t.txid = ""
74}
npujar9a30c702019-11-14 17:06:39 +053075
76// Commit -
npujar467fe752020-01-16 20:17:45 +053077func (t *Transaction) Commit(ctx context.Context) {
78 t.proxy.commitTransaction(ctx, t.txid)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040079 t.txid = ""
80}