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 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.
17 package networkintents
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"
26 pkgerrors "github.com/pkg/errors"
29 // Network contains the parameters needed for dynamic networks
31 Metadata mtypes.Metadata `json:"metadata" yaml:"metadata"`
32 Spec NetworkSpec `json:"spec" yaml:"spec"`
35 type NetworkSpec struct {
36 CniType string `json:"cniType" yaml:"cniType"`
37 Ipv4Subnets []nettypes.Ipv4Subnet `json:"ipv4Subnets" yaml:"ipv4Subnets"`
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"`
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"`
54 const NETWORK_APIVERSION = "k8s.plugin.opnfv.org/v1alpha1"
55 const NETWORK_KIND = "Network"
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
65 // NetworkClient implements the Manager
66 // It will also be used to maintain some localized state
67 type NetworkClient struct {
68 db ncmtypes.ClientDbInfo
71 // NewNetworkClient returns an instance of the NetworkClient
72 // which implements the Manager
73 func NewNetworkClient() *NetworkClient {
74 return &NetworkClient{
75 db: ncmtypes.ClientDbInfo{
77 TagMeta: "networkmetadata",
82 // CreateNetwork - create a new Network
83 func (v *NetworkClient) CreateNetwork(p Network, clusterProvider, cluster string, exists bool) (Network, error) {
85 //Construct key and tag to select the entry
87 ClusterProviderName: clusterProvider,
89 NetworkName: p.Metadata.Name,
92 //Check if cluster exists
93 _, err := clusterPkg.NewClusterClient().GetCluster(clusterProvider, cluster)
95 return Network{}, pkgerrors.New("Unable to find the cluster")
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")
104 err = db.DBconn.Insert(v.db.StoreName, key, nil, v.db.TagMeta, p)
106 return Network{}, pkgerrors.Wrap(err, "Creating DB Entry")
112 // GetNetwork returns the Network for corresponding name
113 func (v *NetworkClient) GetNetwork(name, clusterProvider, cluster string) (Network, error) {
115 //Construct key and tag to select the entry
117 ClusterProviderName: clusterProvider,
118 ClusterName: cluster,
122 value, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta)
124 return Network{}, pkgerrors.Wrap(err, "Get Network")
127 //value is a byte array
130 err = db.DBconn.Unmarshal(value[0], &cp)
132 return Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
137 return Network{}, pkgerrors.New("Error getting Network")
140 // GetNetworkList returns all of the Network for corresponding name
141 func (v *NetworkClient) GetNetworks(clusterProvider, cluster string) ([]Network, error) {
143 //Construct key and tag to select the entry
145 ClusterProviderName: clusterProvider,
146 ClusterName: cluster,
151 values, err := db.DBconn.Find(v.db.StoreName, key, v.db.TagMeta)
153 return []Network{}, pkgerrors.Wrap(err, "Get Networks")
156 for _, value := range values {
158 err = db.DBconn.Unmarshal(value, &cp)
160 return []Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
162 resp = append(resp, cp)
168 // Delete the Network from database
169 func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) error {
171 //Construct key and tag to select the entry
173 ClusterProviderName: clusterProvider,
174 ClusterName: cluster,
178 err := db.DBconn.Remove(v.db.StoreName, key)
180 return pkgerrors.Wrap(err, "Delete Network Entry;")