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"`
52 // ListOfIntents is a list of intents
53 type ListOfIntents struct {
54 ListOfIntents []map[string]string `json:"intent"`
57 // IntentManager is an interface which exposes the IntentManager functionality
58 type IntentManager interface {
59 AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error)
60 GetIntent(i string, p string, ca string, v string, di string) (Intent, error)
61 GetAllIntents(p, ca, v, di string) (ListOfIntents, error)
62 GetIntentByName(i, p, ca, v, di string) (IntentSpecData, error)
63 DeleteIntent(i string, p string, ca string, v string, di string) error
66 // IntentKey consists of Name if the intent, Project name, CompositeApp name,
67 // CompositeApp version
68 type IntentKey struct {
69 Name string `json:"intentname"`
70 Project string `json:"project"`
71 CompositeApp string `json:"compositeapp"`
72 Version string `json:"compositeappversion"`
73 DeploymentIntentGroup string `json:"deploymentintentgroup"`
76 // We will use json marshalling to convert to string to
77 // preserve the underlying structure.
78 func (ik IntentKey) String() string {
79 out, err := json.Marshal(ik)
86 // IntentClient implements the AddIntentManager interface
87 type IntentClient struct {
92 // NewIntentClient returns an instance of AddIntentClient
93 func NewIntentClient() *IntentClient {
95 storeName: "orchestrator",
96 tagMetaData: "addintent",
101 AddIntent adds a given intent to the deployment-intent-group and stores in the db.
102 Other input parameters for it - projectName, compositeAppName, version, DeploymentIntentgroupName
104 func (c *IntentClient) AddIntent(a Intent, p string, ca string, v string, di string) (Intent, error) {
106 //Check for the AddIntent already exists here.
107 res, err := c.GetIntent(a.MetaData.Name, p, ca, v, di)
108 if !reflect.DeepEqual(res, Intent{}) {
109 return Intent{}, pkgerrors.New("Intent already exists")
112 //Check if project exists
113 _, err = NewProjectClient().GetProject(p)
115 return Intent{}, pkgerrors.New("Unable to find the project")
118 //check if compositeApp exists
119 _, err = NewCompositeAppClient().GetCompositeApp(ca, v, p)
121 return Intent{}, pkgerrors.New("Unable to find the composite-app")
124 //check if DeploymentIntentGroup exists
125 _, err = NewDeploymentIntentGroupClient().GetDeploymentIntentGroup(di, p, ca, v)
127 return Intent{}, pkgerrors.New("Unable to find the intent")
131 Name: a.MetaData.Name,
135 DeploymentIntentGroup: di,
138 err = db.DBconn.Insert(c.storeName, akey, nil, c.tagMetaData, a)
140 return Intent{}, pkgerrors.Wrap(err, "Create DB entry error")
146 GetIntent takes in an IntentName, ProjectName, CompositeAppName, Version and DeploymentIntentGroup.
147 It returns the Intent.
149 func (c *IntentClient) GetIntent(i string, p string, ca string, v string, di string) (Intent, error) {
156 DeploymentIntentGroup: di,
159 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
161 return Intent{}, pkgerrors.Wrap(err, "Get Intent error")
166 err = db.DBconn.Unmarshal(result[0], &a)
168 return Intent{}, pkgerrors.Wrap(err, "Unmarshalling AppIntent")
173 return Intent{}, pkgerrors.New("Error getting Intent")
177 GetIntentByName takes in IntentName, projectName, CompositeAppName, CompositeAppVersion
178 and deploymentIntentGroupName returns the list of intents under the IntentName.
180 func (c IntentClient) GetIntentByName(i string, p string, ca string, v string, di string) (IntentSpecData, error) {
186 DeploymentIntentGroup: di,
188 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
190 return IntentSpecData{}, pkgerrors.Wrap(err, "Get AppIntent error")
193 err = db.DBconn.Unmarshal(result[0], &a)
195 return IntentSpecData{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
201 GetAllIntents takes in projectName, CompositeAppName, CompositeAppVersion,
202 DeploymentIntentName . It returns ListOfIntents.
204 func (c IntentClient) GetAllIntents(p string, ca string, v string, di string) (ListOfIntents, error) {
210 DeploymentIntentGroup: di,
213 result, err := db.DBconn.Find(c.storeName, k, c.tagMetaData)
215 return ListOfIntents{}, pkgerrors.Wrap(err, "Get AppIntent error")
218 var listOfMapOfIntents []map[string]string
220 if len(result) != 0 {
221 for i := range result {
223 err = db.DBconn.Unmarshal(result[i], &a)
225 return ListOfIntents{}, pkgerrors.Wrap(err, "Unmarshalling Intent")
227 //mapOfIntents := ListOfIntents{a.Spec.Intent.ListOfIntents}
228 listOfMapOfIntents = append(listOfMapOfIntents, a.Spec.Intent)
230 return ListOfIntents{listOfMapOfIntents}, nil
232 return ListOfIntents{}, err
235 // DeleteIntent deletes a given intent tied to project, composite app and deployment intent group
236 func (c IntentClient) DeleteIntent(i string, p string, ca string, v string, di string) error {
242 DeploymentIntentGroup: di,
245 err := db.DBconn.Remove(c.storeName, k)
247 return pkgerrors.Wrap(err, "Delete Project entry;")