2 Copyright 2018 Intel Corporation.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
17 "k8splugin/plugins/network/v1"
20 utils "k8splugin/internal"
22 pkgerrors "github.com/pkg/errors"
23 "k8s.io/client-go/kubernetes"
26 func extractData(data string) (vnfID, cniType, networkName string) {
27 re := regexp.MustCompile("_")
28 split := re.Split(data, -1)
34 networkName = split[2]
38 // Create an ONAP Network object
39 func Create(data *utils.ResourceData, client kubernetes.Interface) (string, error) {
40 network := &v1.OnapNetwork{}
41 if _, err := utils.DecodeYAML(data.YamlFilePath, network); err != nil {
42 return "", pkgerrors.Wrap(err, "Decode network object error")
45 config, err := network.DecodeConfig()
47 return "", pkgerrors.Wrap(err, "Fail to decode network's configuration")
50 cniType := config["cnitype"].(string)
51 typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
53 return "", pkgerrors.New("No plugin for resource " + cniType + " found")
56 symCreateNetworkFunc, err := typePlugin.Lookup("CreateNetwork")
58 return "", pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin")
61 name, err := symCreateNetworkFunc.(func(*v1.OnapNetwork) (string, error))(network)
63 return "", pkgerrors.Wrap(err, "Error during the creation for "+cniType+" plugin")
66 return data.VnfId + "_" + cniType + "_" + name, nil
70 func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
74 // Delete an existing Network
75 func Delete(name string, namespace string, kubeclient kubernetes.Interface) error {
76 _, cniType, networkName := extractData(name)
77 typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
79 return pkgerrors.New("No plugin for resource " + cniType + " found")
82 symDeleteNetworkFunc, err := typePlugin.Lookup("DeleteNetwork")
84 return pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin")
87 if err := symDeleteNetworkFunc.(func(string) error)(networkName); err != nil {
88 return pkgerrors.Wrap(err, "Error during the deletion for "+cniType+" plugin")
94 // Get an existing Network
95 func Get(name string, namespace string, kubeclient kubernetes.Interface) (string, error) {