This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sfc_client.go
142 lines (119 loc) · 4.44 KB
/
sfc_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// SPDX-License-Identifier: Apache-2.0
// C[]model.SfcClientIntent{}opyright (c) 2021 Intel Corporation
package module
import (
"strings"
"github.com/open-ness/EMCO/src/orchestrator/pkg/infra/db"
ovn "github.com/open-ness/EMCO/src/ovnaction/pkg/module"
"github.com/open-ness/EMCO/src/sfcclient/pkg/model"
pkgerrors "github.com/pkg/errors"
)
// CreateSfcClientIntent - create a new SfcClientIntent
func (v *SfcClient) CreateSfcClientIntent(intent model.SfcClientIntent, pr, ca, caver, dig, netctrlint string, exists bool) (model.SfcClientIntent, error) {
//Construct key and tag to select the entry
key := model.SfcClientIntentKey{
Project: pr,
CompositeApp: ca,
CompositeAppVersion: caver,
DigName: dig,
NetControlIntent: netctrlint,
SfcClientIntent: intent.Metadata.Name,
}
// Check for existence of parent NetControlIntent resource
_, err := ovn.NewNetControlIntentClient().GetNetControlIntent(netctrlint, pr, ca, caver, dig)
if err != nil {
return model.SfcClientIntent{}, pkgerrors.Errorf("Parent NetControlIntent resource does not exist: %v", netctrlint)
}
//Check if this SFC Client Intent already exists
_, err = v.GetSfcClientIntent(intent.Metadata.Name, pr, ca, caver, dig, netctrlint)
if err == nil && !exists {
return model.SfcClientIntent{}, pkgerrors.New("SFC Client Intent already exists")
}
err = db.DBconn.Insert(v.db.storeName, key, nil, v.db.tagMeta, intent)
if err != nil {
return model.SfcClientIntent{}, pkgerrors.Wrap(err, "Creating DB Entry")
}
return intent, nil
}
// GetSfcClientIntent returns the SfcClientIntent for corresponding name
func (v *SfcClient) GetSfcClientIntent(name, pr, ca, caver, dig, netctrlint string) (model.SfcClientIntent, error) {
//Construct key and tag to select the entry
key := model.SfcClientIntentKey{
Project: pr,
CompositeApp: ca,
CompositeAppVersion: caver,
DigName: dig,
NetControlIntent: netctrlint,
SfcClientIntent: name,
}
value, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
if err != nil {
return model.SfcClientIntent{}, pkgerrors.Wrap(err, "db Find error")
} else if len(value) == 0 {
return model.SfcClientIntent{}, pkgerrors.New("SFC Client Intent not found")
}
//value is a byte array
if value != nil {
intent := model.SfcClientIntent{}
err = db.DBconn.Unmarshal(value[0], &intent)
if err != nil {
return model.SfcClientIntent{}, pkgerrors.Wrap(err, "Unmarshalling Value")
}
return intent, nil
}
return model.SfcClientIntent{}, pkgerrors.New("Error getting SFC Client Intent")
}
// GetAllSfcClientIntent returns all of the SFC Client Intents for for the given network control intent
func (v *SfcClient) GetAllSfcClientIntents(pr, ca, caver, dig, netctrlint string) ([]model.SfcClientIntent, error) {
//Construct key and tag to select the entry
key := model.SfcClientIntentKey{
Project: pr,
CompositeApp: ca,
CompositeAppVersion: caver,
DigName: dig,
NetControlIntent: netctrlint,
SfcClientIntent: "",
}
resp := make([]model.SfcClientIntent, 0)
// Verify the Net Control Intent exists
_, err := ovn.NewNetControlIntentClient().GetNetControlIntent(netctrlint, pr, ca, caver, dig)
if err != nil {
return resp, err
}
values, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
if err != nil {
return resp, pkgerrors.Wrap(err, "db Find error")
}
for _, value := range values {
cp := model.SfcClientIntent{}
err = db.DBconn.Unmarshal(value, &cp)
if err != nil {
return resp, pkgerrors.Wrap(err, "Unmarshalling Value")
}
resp = append(resp, cp)
}
return resp, nil
}
// DeleteSfcClientIntent deletes the SfcClientIntent from the database
func (v *SfcClient) DeleteSfcClientIntent(name, pr, ca, caver, dig, netctrlint string) error {
//Construct key and tag to select the entry
key := model.SfcClientIntentKey{
Project: pr,
CompositeApp: ca,
CompositeAppVersion: caver,
DigName: dig,
NetControlIntent: netctrlint,
SfcClientIntent: name,
}
err := db.DBconn.Remove(v.db.storeName, key)
if err != nil {
if strings.Contains(err.Error(), "Error finding:") {
return pkgerrors.Wrap(err, "db Remove error - not found")
} else if strings.Contains(err.Error(), "Can't delete parent without deleting child") {
return pkgerrors.Wrap(err, "db Remove error - conflict")
} else {
return pkgerrors.Wrap(err, "db Remove error - general")
}
}
return nil
}