Reorganize ncm packages to align with architecture
[multicloud/k8s.git] / src / ncm / pkg / networkintents / network.go
1 /*
2  * Copyright 2020 Intel Corporation, Inc
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  */
16
17 package networkintents
18
19 import (
20         clusterPkg "github.com/onap/multicloud-k8s/src/ncm/pkg/cluster"
21         ncmtypes "github.com/onap/multicloud-k8s/src/ncm/pkg/module/types"
22         nettypes "github.com/onap/multicloud-k8s/src/ncm/pkg/networkintents/types"
23         "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
24         mtypes "github.com/onap/multicloud-k8s/src/orchestrator/pkg/module/types"
25
26         pkgerrors "github.com/pkg/errors"
27 )
28
29 // Network contains the parameters needed for dynamic networks
30 type Network struct {
31         Metadata mtypes.Metadata `json:"metadata" yaml:"metadata"`
32         Spec     NetworkSpec     `json:"spec" yaml:"spec"`
33 }
34
35 type NetworkSpec struct {
36         CniType     string                `json:"cniType" yaml:"cniType"`
37         Ipv4Subnets []nettypes.Ipv4Subnet `json:"ipv4Subnets" yaml:"ipv4Subnets"`
38 }
39
40 // NetworkKey is the key structure that is used in the database
41 type NetworkKey struct {
42         ClusterProviderName string `json:"provider"`
43         ClusterName         string `json:"cluster"`
44         NetworkName         string `json:"network"`
45 }
46
47 // structure for the Network Custom Resource
48 type CrNetwork struct {
49         ApiVersion string  `yaml:"apiVersion"`
50         Kind       string  `yaml:"kind"`
51         Network    Network `yaml:",inline"`
52 }
53
54 const NETWORK_APIVERSION = "k8s.plugin.opnfv.org/v1alpha1"
55 const NETWORK_KIND = "Network"
56
57 // Manager is an interface exposing the Network functionality
58 type NetworkManager interface {
59         CreateNetwork(pr Network, clusterProvider, cluster string, exists bool) (Network, error)
60         GetNetwork(name, clusterProvider, cluster string) (Network, error)
61         GetNetworks(clusterProvider, cluster string) ([]Network, error)
62         DeleteNetwork(name, clusterProvider, cluster string) error
63 }
64
65 // NetworkClient implements the Manager
66 // It will also be used to maintain some localized state
67 type NetworkClient struct {
68         db ncmtypes.ClientDbInfo
69 }
70
71 // NewNetworkClient returns an instance of the NetworkClient
72 // which implements the Manager
73 func NewNetworkClient() *NetworkClient {
74         return &NetworkClient{
75                 db: ncmtypes.ClientDbInfo{
76                         StoreName: "cluster",
77                         TagMeta:   "networkmetadata",
78                 },
79         }
80 }
81
82 // CreateNetwork - create a new Network
83 func (v *NetworkClient) CreateNetwork(p Network, clusterProvider, cluster string, exists bool) (Network, error) {
84
85         //Construct key and tag to select the entry
86         key := NetworkKey{
87                 ClusterProviderName: clusterProvider,
88                 ClusterName:         cluster,
89                 NetworkName:         p.Metadata.Name,
90         }
91
92         //Check if cluster exists
93         _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster)
94         if err != nil {
95                 return Network{}, pkgerrors.New("Unable to find the cluster")
96         }
97
98         //Check if this Network already exists
99         _, err = v.GetNetwork(p.Metadata.Name, clusterProvider, cluster)
100         if err == nil && !exists {
101                 return Network{}, pkgerrors.New("Network already exists")
102         }
103
104         err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p)
105         if err != nil {
106                 return Network{}, pkgerrors.Wrap(err, "Creating DB Entry")
107         }
108
109         return p, nil
110 }
111
112 // GetNetwork returns the Network for corresponding name
113 func (v *NetworkClient) GetNetwork(name, clusterProvider, cluster string) (Network, error) {
114
115         //Construct key and tag to select the entry
116         key := NetworkKey{
117                 ClusterProviderName: clusterProvider,
118                 ClusterName:         cluster,
119                 NetworkName:         name,
120         }
121
122         value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta)
123         if err != nil {
124                 return Network{}, pkgerrors.Wrap(err, "Get Network")
125         }
126
127         //value is a byte array
128         if value != nil {
129                 cp := Network{}
130                 err = db.DBconn.Unmarshal(value[0], &cp)
131                 if err != nil {
132                         return Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
133                 }
134                 return cp, nil
135         }
136
137         return Network{}, pkgerrors.New("Error getting Network")
138 }
139
140 // GetNetworkList returns all of the Network for corresponding name
141 func (v *NetworkClient) GetNetworks(clusterProvider, cluster string) ([]Network, error) {
142
143         //Construct key and tag to select the entry
144         key := NetworkKey{
145                 ClusterProviderName: clusterProvider,
146                 ClusterName:         cluster,
147                 NetworkName:         "",
148         }
149
150         var resp []Network
151         values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta)
152         if err != nil {
153                 return []Network{}, pkgerrors.Wrap(err, "Get Networks")
154         }
155
156         for _, value := range values {
157                 cp := Network{}
158                 err = db.DBconn.Unmarshal(value, &cp)
159                 if err != nil {
160                         return []Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
161                 }
162                 resp = append(resp, cp)
163         }
164
165         return resp, nil
166 }
167
168 // Delete the  Network from database
169 func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) error {
170
171         //Construct key and tag to select the entry
172         key := NetworkKey{
173                 ClusterProviderName: clusterProvider,
174                 ClusterName:         cluster,
175                 NetworkName:         name,
176         }
177
178         err := db.DBconn.Remove(v.db.StoreName, key)
179         if err != nil {
180                 return pkgerrors.Wrap(err, "Delete Network Entry;")
181         }
182
183         return nil
184 }