blob: 5bef77e40e7fd3aa79fb0ef4162af8be01cba7c3 [file] [log] [blame]
Matt Jeanneretcab955f2019-04-10 15:45:57 -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 */
16package model
17
18import (
Manikkaraj kb1d51442019-07-23 10:41:02 -040019 "context"
Scott Bakerf8424cc2019-10-18 11:26:50 -070020 "github.com/opencord/voltha-lib-go/pkg/log"
Matt Jeanneretcab955f2019-04-10 15:45:57 -040021)
22
23type Transaction struct {
24 proxy *Proxy
25 txid string
26}
27
28func NewTransaction(proxy *Proxy, txid string) *Transaction {
29 tx := &Transaction{
30 proxy: proxy,
31 txid: txid,
32 }
33 return tx
34}
Manikkaraj kb1d51442019-07-23 10:41:02 -040035func (t *Transaction) Get(ctx context.Context, path string, depth int, deep bool) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -040036 if t.txid == "" {
37 log.Errorf("closed transaction")
38 return nil
39 }
40 // TODO: need to review the return values at the different layers!!!!!
Manikkaraj kb1d51442019-07-23 10:41:02 -040041 return t.proxy.Get(ctx, path, depth, deep, t.txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040042}
Manikkaraj kb1d51442019-07-23 10:41:02 -040043func (t *Transaction) Update(ctx context.Context, path string, data interface{}, strict bool) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -040044 if t.txid == "" {
45 log.Errorf("closed transaction")
46 return nil
47 }
Manikkaraj kb1d51442019-07-23 10:41:02 -040048 return t.proxy.Update(ctx, path, data, strict, t.txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040049}
Manikkaraj kb1d51442019-07-23 10:41:02 -040050func (t *Transaction) Add(ctx context.Context, path string, data interface{}) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -040051 if t.txid == "" {
52 log.Errorf("closed transaction")
53 return nil
54 }
Manikkaraj kb1d51442019-07-23 10:41:02 -040055 return t.proxy.Add(ctx, path, data, t.txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040056}
Manikkaraj kb1d51442019-07-23 10:41:02 -040057func (t *Transaction) Remove(ctx context.Context, path string) interface{} {
Matt Jeanneretcab955f2019-04-10 15:45:57 -040058 if t.txid == "" {
59 log.Errorf("closed transaction")
60 return nil
61 }
Manikkaraj kb1d51442019-07-23 10:41:02 -040062 return t.proxy.Remove(ctx, path, t.txid)
Matt Jeanneretcab955f2019-04-10 15:45:57 -040063}
64func (t *Transaction) Cancel() {
65 t.proxy.cancelTransaction(t.txid)
66 t.txid = ""
67}
68func (t *Transaction) Commit() {
69 t.proxy.commitTransaction(t.txid)
70 t.txid = ""
71}