Use a standard Go project layout
[multicloud/k8s.git] / src / k8splugin / plugins / network / plugin.go
1 /*
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.
12 */
13
14 package main
15
16 import (
17         "k8splugin/plugins/network/v1"
18         "regexp"
19
20         utils "k8splugin/internal"
21
22         pkgerrors "github.com/pkg/errors"
23         "k8s.io/client-go/kubernetes"
24 )
25
26 func extractData(data string) (vnfID, cniType, networkName string) {
27         re := regexp.MustCompile("_")
28         split := re.Split(data, -1)
29         if len(split) != 3 {
30                 return
31         }
32         vnfID = split[0]
33         cniType = split[1]
34         networkName = split[2]
35         return
36 }
37
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")
43         }
44
45         config, err := network.DecodeConfig()
46         if err != nil {
47                 return "", pkgerrors.Wrap(err, "Fail to decode network's configuration")
48         }
49
50         cniType := config["cnitype"].(string)
51         typePlugin, ok := utils.LoadedPlugins[cniType+"-network"]
52         if !ok {
53                 return "", pkgerrors.New("No plugin for resource " + cniType + " found")
54         }
55
56         symCreateNetworkFunc, err := typePlugin.Lookup("CreateNetwork")
57         if err != nil {
58                 return "", pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin")
59         }
60
61         name, err := symCreateNetworkFunc.(func(*v1.OnapNetwork) (string, error))(network)
62         if err != nil {
63                 return "", pkgerrors.Wrap(err, "Error during the creation for "+cniType+" plugin")
64         }
65
66         return data.VnfId + "_" + cniType + "_" + name, nil
67 }
68
69 // List of Networks
70 func List(namespace string, kubeclient kubernetes.Interface) ([]string, error) {
71         return nil, nil
72 }
73
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"]
78         if !ok {
79                 return pkgerrors.New("No plugin for resource " + cniType + " found")
80         }
81
82         symDeleteNetworkFunc, err := typePlugin.Lookup("DeleteNetwork")
83         if err != nil {
84                 return pkgerrors.Wrap(err, "Error fetching "+cniType+" plugin")
85         }
86
87         if err := symDeleteNetworkFunc.(func(string) error)(networkName); err != nil {
88                 return pkgerrors.Wrap(err, "Error during the deletion for "+cniType+" plugin")
89         }
90
91         return nil
92 }
93
94 // Get an existing Network
95 func Get(name string, namespace string, kubeclient kubernetes.Interface) (string, error) {
96         return "", nil
97 }