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.
20 "github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
22 pkgerrors "github.com/pkg/errors"
25 // Network contains the parameters needed for dynamic networks
27 Metadata Metadata `json:"metadata" yaml:"metadata"`
28 Spec NetworkSpec `json:"spec" yaml:"spec"`
31 type NetworkSpec struct {
32 CniType string `json:"cniType" yaml:"cniType"`
33 Ipv4Subnets []Ipv4Subnet `json:"ipv4Subnets" yaml:"ipv4Subnets"`
36 // NetworkKey is the key structure that is used in the database
37 type NetworkKey struct {
38 ClusterProviderName string `json:"provider"`
39 ClusterName string `json:"cluster"`
40 NetworkName string `json:"network"`
43 // structure for the Network Custom Resource
44 type CrNetwork struct {
45 ApiVersion string `yaml:"apiVersion"`
46 Kind string `yaml:"kind"`
47 Network Network `yaml:",inline"`
50 const NETWORK_APIVERSION = "k8s.plugin.opnfv.org/v1alpha1"
51 const NETWORK_KIND = "Network"
53 // Manager is an interface exposing the Network functionality
54 type NetworkManager interface {
55 CreateNetwork(pr Network, clusterProvider, cluster string, exists bool) (Network, error)
56 GetNetwork(name, clusterProvider, cluster string) (Network, error)
57 GetNetworks(clusterProvider, cluster string) ([]Network, error)
58 DeleteNetwork(name, clusterProvider, cluster string) error
61 // NetworkClient implements the Manager
62 // It will also be used to maintain some localized state
63 type NetworkClient struct {
67 // NewNetworkClient returns an instance of the NetworkClient
68 // which implements the Manager
69 func NewNetworkClient() *NetworkClient {
70 return &NetworkClient{
73 tagMeta: "networkmetadata",
78 // CreateNetwork - create a new Network
79 func (v *NetworkClient) CreateNetwork(p Network, clusterProvider, cluster string, exists bool) (Network, error) {
81 //Construct key and tag to select the entry
83 ClusterProviderName: clusterProvider,
85 NetworkName: p.Metadata.Name,
88 //Check if cluster exists
89 _, err := NewClusterClient().GetCluster(clusterProvider, cluster)
91 return Network{}, pkgerrors.New("Unable to find the cluster")
94 //Check if this Network already exists
95 _, err = v.GetNetwork(p.Metadata.Name, clusterProvider, cluster)
96 if err == nil && !exists {
97 return Network{}, pkgerrors.New("Network already exists")
100 err = db.DBconn.Insert(v.db.storeName, key, nil, v.db.tagMeta, p)
102 return Network{}, pkgerrors.Wrap(err, "Creating DB Entry")
108 // GetNetwork returns the Network for corresponding name
109 func (v *NetworkClient) GetNetwork(name, clusterProvider, cluster string) (Network, error) {
111 //Construct key and tag to select the entry
113 ClusterProviderName: clusterProvider,
114 ClusterName: cluster,
118 value, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
120 return Network{}, pkgerrors.Wrap(err, "Get Network")
123 //value is a byte array
126 err = db.DBconn.Unmarshal(value[0], &cp)
128 return Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
133 return Network{}, pkgerrors.New("Error getting Network")
136 // GetNetworkList returns all of the Network for corresponding name
137 func (v *NetworkClient) GetNetworks(clusterProvider, cluster string) ([]Network, error) {
139 //Construct key and tag to select the entry
141 ClusterProviderName: clusterProvider,
142 ClusterName: cluster,
147 values, err := db.DBconn.Find(v.db.storeName, key, v.db.tagMeta)
149 return []Network{}, pkgerrors.Wrap(err, "Get Networks")
152 for _, value := range values {
154 err = db.DBconn.Unmarshal(value, &cp)
156 return []Network{}, pkgerrors.Wrap(err, "Unmarshalling Value")
158 resp = append(resp, cp)
164 // Delete the Network from database
165 func (v *NetworkClient) DeleteNetwork(name, clusterProvider, cluster string) error {
167 //Construct key and tag to select the entry
169 ClusterProviderName: clusterProvider,
170 ClusterName: cluster,
174 err := db.DBconn.Remove(v.db.storeName, key)
176 return pkgerrors.Wrap(err, "Delete Network Entry;")