aboutsummaryrefslogtreecommitdiffstats
path: root/src/orchestrator/pkg/module/add_intents.go
blob: 9c47863a9d9560557022a6061073ff67802184c2 (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright 2020 Intel Corporation, Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by Addlicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package module

/*
This files deals with the backend implementation of adding
genericPlacementIntents to deployementIntentGroup
*/

import (
	"encoding/json"
	"reflect"

	"github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"

	pkgerrors "github.com/pkg/errors"
)

// Intent shall have 2 fields - MetaData and Spec
type Intent struct {
	MetaData IntentMetaData `json:"metadata"`
	Spec     IntentSpecData `json:"spec"`
}

// IntentMetaData has Name, Description, userdata1, userdata2
type IntentMetaData struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	UserData1   string `json:"userData1"`
	UserData2   string `json:"userData2"`
}

// IntentSpecData has Intent
type IntentSpecData struct {
	Intent map[string]string `json:"intent"`
}

// ListOfIntents is a list of intents
type ListOfIntents struct {
	ListOfIntents []map[string]string `json:"intent"`
}

// IntentManager is an interface which exposes the IntentManager functionality
type IntentManager interface {
	AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error)
	GetIntent(i string, p string, ca string, v string, di string) (Intent, error)
	GetAllIntents(p, ca, v, di string) (ListOfIntents, error)
	GetIntentByName(i, p, ca, v, di string) (IntentSpecData, error)
	DeleteIntent(i string, p string, ca string, v string, di string) error
}

// IntentKey consists of Name if the intent, Project name, CompositeApp name,
// CompositeApp version
type IntentKey struct {
	Name                  string `json:"intentname"`
	Project               string `json:"project"`
	CompositeApp          string `json:"compositeapp"`
	Version               string `json:"compositeappversion"`
	DeploymentIntentGroup string `json:"deploymentintentgroup"`
}

// We will use json marshalling to convert to string to
// preserve the underlying structure.
func (ik IntentKey) String() string {
	out, err := json.Marshal(ik)
	if err != nil {
		return ""
	}
	return string(out)
}

// IntentClient implements the AddIntentManager interface
type IntentClient struct {
	storeName   string
	tagMetaData string
}

// NewIntentClient returns an instance of AddIntentClient
func NewIntentClient() *IntentClient {
	return &IntentClient{
		storeName:   "orchestrator",
		tagMetaData: "addintent",
	}
}

/*
AddIntent adds a given intent to the deployment-intent-group and stores in the db.
Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName
*/
func (c *IntentClient) AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) {

	//Check for the AddIntent already exists here.
	res, err := c.GetIntent(a.MetaData.Name, p, ca, v, di)
	if !reflect.DeepEqual(res, Intent{}) {
		return Intent{}, pkgerrors.New("Intent already exists")
	}

	//Check if project exists
	_, err = NewProjectClient().GetProject(p)
	if err != nil {
		return Intent{}, pkgerrors.New("Unable to find the project")
	}

	//check if compositeApp exists
	_, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
	if err != nil {
		return Intent{}, pkgerrors.New("Unable to find the composite-app")
	}

	//check if DeploymentIntentGroup exists
	_, err = NewDeploymentIntentGroupClient().GetDeploymentIntentGroup(di, p, ca, v)
	if err != nil {
		return Intent{}, pkgerrors.New("Unable to find the intent")
	}

	akey := IntentKey{
		Name:                  a.MetaData.Name,
		Project:               p,
		CompositeApp:          ca,
		Version:               v,
		DeploymentIntentGroup: di,
	}

	err = db.DBconn.Insert(c.storeName, akey, nil, c.tagMetaData, a)
	if err != nil {
		return Intent{}, pkgerrors.Wrap(err, "Create DB entry error")
	}
	return a, nil
}

/*
GetIntent takes in an IntentName, ProjectName, CompositeAppName, Version and DeploymentIntentGroup.
It returns the Intent.
*/
func (c *IntentClient) GetIntent(i string, p string, ca string, v string, di string) (Intent, error) {

	k := IntentKey{
		Name:                  i,
		Project:               p,
		CompositeApp:          ca,
		Version:               v,
		DeploymentIntentGroup: di,
	}

	result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
	if err != nil {
		return Intent{}, pkgerrors.Wrap(err, "Get Intent error")
	}

	if result != nil {
		a := Intent{}
		err = db.DBconn.Unmarshal(result[0], &a)
		if err != nil {
			return Intent{}, pkgerrors.Wrap(err, "Unmarshalling  AppIntent")
		}
		return a, nil

	}
	return Intent{}, pkgerrors.New("Error getting Intent")
}

/*
GetIntentByName takes in IntentName, projectName, CompositeAppName, CompositeAppVersion
and deploymentIntentGroupName returns the list of intents under the IntentName.
*/
func (c IntentClient) GetIntentByName(i string, p string, ca string, v string, di string) (IntentSpecData, error) {
	k := IntentKey{
		Name:                  i,
		Project:               p,
		CompositeApp:          ca,
		Version:               v,
		DeploymentIntentGroup: di,
	}
	result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
	if err != nil {
		return IntentSpecData{}, pkgerrors.Wrap(err, "Get AppIntent error")
	}
	var a Intent
	err = db.DBconn.Unmarshal(result[0], &a)
	if err != nil {
		return IntentSpecData{}, pkgerrors.Wrap(err, "Unmarshalling  Intent")
	}
	return a.Spec, nil
}

/*
GetAllIntents takes in projectName, CompositeAppName, CompositeAppVersion,
DeploymentIntentName . It returns ListOfIntents.
*/
func (c IntentClient) GetAllIntents(p string, ca string, v string, di string) (ListOfIntents, error) {
	k := IntentKey{
		Name:                  "",
		Project:               p,
		CompositeApp:          ca,
		Version:               v,
		DeploymentIntentGroup: di,
	}

	result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
	if err != nil {
		return ListOfIntents{}, pkgerrors.Wrap(err, "Get AppIntent error")
	}
	var a Intent
	var listOfMapOfIntents []map[string]string

	if len(result) != 0 {
		for i := range result {
			a = Intent{}
			err = db.DBconn.Unmarshal(result[i], &a)
			if err != nil {
				return ListOfIntents{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
			}
			listOfMapOfIntents = append(listOfMapOfIntents, a.Spec.Intent)
		}
		return ListOfIntents{listOfMapOfIntents}, nil
	}
	return ListOfIntents{}, err
}

// DeleteIntent deletes a given intent tied to project, composite app and deployment intent group
func (c IntentClient) DeleteIntent(i string, p string, ca string, v string, di string) error {
	k := IntentKey{
		Name:                  i,
		Project:               p,
		CompositeApp:          ca,
		Version:               v,
		DeploymentIntentGroup: di,
	}

	err := db.DBconn.Remove(c.storeName, k)
	if err != nil {
		return pkgerrors.Wrap(err, "Delete Project entry;")
	}
	return nil
}