2 * Copyright 2020 Intel Corporation, Inc
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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by Addlicable 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.
20 This files deals with the backend implementation of adding
21 genericPlacementIntents to deployementIntentGroup
28 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
30 pkgerrors "github.com/pkg/errors"
33 // Intent shall have 2 fields - MetaData and Spec
35 MetaData IntentMetaData `json:"metadata"`
36 Spec IntentSpecData `json:"spec"`
39 // IntentMetaData has Name, Description, userdata1, userdata2
40 type IntentMetaData struct {
41 Name string `json:"name"`
42 Description string `json:"description"`
43 UserData1 string `json:"userData1"`
44 UserData2 string `json:"userData2"`
47 // IntentSpecData has Intent
48 type IntentSpecData struct {
49 Intent map[string]string `json:"intent"`
53 // ListOfIntents is a list of intents
54 type ListOfIntents struct {
55 ListOfIntents []map[string]string `json:"intent"`
59 // IntentManager is an interface which exposes the IntentManager functionality
60 type IntentManager interface {
61 AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error)
62 GetIntent(i string, p string, ca string, v string, di string) (Intent, error)
63 GetAllIntents(p, ca, v, di string) (ListOfIntents, error)
64 GetIntentByName(i, p, ca, v, di string) (IntentSpecData, error)
65 DeleteIntent(i string, p string, ca string, v string, di string) error
68 // IntentKey consists of Name if the intent, Project name, CompositeApp name,
69 // CompositeApp version
70 type IntentKey struct {
71 Name string `json:"intentname"`
72 Project string `json:"project"`
73 CompositeApp string `json:"compositeapp"`
74 Version string `json:"compositeappversion"`
75 DeploymentIntentGroup string `json:"deploymentintentgroup"`
78 // We will use json marshalling to convert to string to
79 // preserve the underlying structure.
80 func (ik IntentKey) String() string {
81 out, err := json.Marshal(ik)
88 // IntentClient implements the AddIntentManager interface
89 type IntentClient struct {
94 // NewIntentClient returns an instance of AddIntentClient
95 func NewIntentClient() *IntentClient {
97 storeName: "orchestrator",
98 tagMetaData: "addintent",
103 AddIntent adds a given intent to the deployment-intent-group and stores in the db.
104 Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName
106 func (c *IntentClient) AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) {
108 //Check for the AddIntent already exists here.
109 res, err := c.GetIntent(a.MetaData.Name, p, ca, v, di)
110 if !reflect.DeepEqual(res, Intent{}) {
111 return Intent{}, pkgerrors.New("Intent already exists")
114 //Check if project exists
115 _, err = NewProjectClient().GetProject(p)
117 return Intent{}, pkgerrors.New("Unable to find the project")
120 //check if compositeApp exists
121 _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
123 return Intent{}, pkgerrors.New("Unable to find the composite-app")
126 //check if DeploymentIntentGroup exists
127 _, err = NewDeploymentIntentGroupClient().GetDeploymentIntentGroup(di, p, ca, v)
129 return Intent{}, pkgerrors.New("Unable to find the intent")
133 Name: a.MetaData.Name,
137 DeploymentIntentGroup: di,
140 err = db.DBconn.Insert(c.storeName, akey, nil, c.tagMetaData, a)
142 return Intent{}, pkgerrors.Wrap(err, "Create DB entry error")
148 GetIntent takes in an IntentName, ProjectName, CompositeAppName, Version and DeploymentIntentGroup.
149 It returns the Intent.
151 func (c *IntentClient) GetIntent(i string, p string, ca string, v string, di string) (Intent, error) {
158 DeploymentIntentGroup: di,
161 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
163 return Intent{}, pkgerrors.Wrap(err, "Get Intent error")
168 err = db.DBconn.Unmarshal(result[0], &a)
170 return Intent{}, pkgerrors.Wrap(err, "Unmarshalling AppIntent")
175 return Intent{}, pkgerrors.New("Error getting Intent")
180 GetIntentByName takes in IntentName, projectName, CompositeAppName, CompositeAppVersion
181 and deploymentIntentGroupName returns the list of intents under the IntentName.
183 func (c IntentClient) GetIntentByName(i string, p string, ca string, v string, di string) (IntentSpecData, error) {
189 DeploymentIntentGroup: di,
191 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
193 return IntentSpecData{}, pkgerrors.Wrap(err, "Get AppIntent error")
196 err = db.DBconn.Unmarshal(result[0], &a)
198 return IntentSpecData{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
205 GetAllIntents takes in projectName, CompositeAppName, CompositeAppVersion,
206 DeploymentIntentName . It returns ListOfIntents.
208 func (c IntentClient) GetAllIntents(p string, ca string, v string, di string) (ListOfIntents, error) {
214 DeploymentIntentGroup: di,
217 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
219 return ListOfIntents{}, pkgerrors.Wrap(err, "Get AppIntent error")
222 var listOfMapOfIntents []map[string]string
224 if len(result) != 0 {
225 for i := range result {
227 err = db.DBconn.Unmarshal(result[i], &a)
229 return ListOfIntents{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
231 //mapOfIntents := ListOfIntents{a.Spec.Intent.ListOfIntents}
232 listOfMapOfIntents = append(listOfMapOfIntents, a.Spec.Intent)
234 return ListOfIntents{listOfMapOfIntents}, nil
236 return ListOfIntents{}, err
240 // DeleteIntent deletes a given intent tied to project, composite app and deployment intent group
241 func (c IntentClient) DeleteIntent(i string, p string, ca string, v string, di string) error {
247 DeploymentIntentGroup: di,
250 err := db.DBconn.Remove(c.storeName, k)
252 return pkgerrors.Wrap(err, "Delete Project entry;")