Bug fixes
[ealt-edge.git] / mecm / mepm / applcm / broker / pkg / handlers / adapter / pluginAdapter / pluginAdapter.go
1 /*
2  * Copyright 2020 Huawei Technologies Co., Ltd.
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 package pluginAdapter
17
18 import (
19         "broker/pkg/plugin"
20         "context"
21         "github.com/sirupsen/logrus"
22         "time"
23 )
24
25 const (
26         chunkSize = 1024
27         rootCertificate  = ""
28 )
29
30 // Plugin adapter which decides a specific client based on plugin info
31 // TODO PluginInfo to have other information about plugins to find the client and implementation to handle accordingly.
32 type PluginAdapter struct {
33         pluginInfo string
34         logger *logrus.Logger
35 }
36
37 // Constructor of PluginAdapter
38 func NewPluginAdapter(pluginInfo string, logger *logrus.Logger) *PluginAdapter {
39         return &PluginAdapter{pluginInfo: pluginInfo, logger: logger}
40 }
41
42 // Instantiate application
43 func (c *PluginAdapter) Instantiate(pluginInfo string, host string, deployArtifact string) (workloadId string, error error, status string) {
44         c.logger.Infof("Instantation started")
45         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate, Logger: c.logger}
46         var client, err = plugin.NewClientGRPC(clientConfig)
47         if err != nil {
48                 c.logger.Errorf("failed to create client: %v", err)
49                 return "", err, "Failure"
50         }
51
52         ctx, cancel := context.WithTimeout(context.Background(), 50*time.Second)
53         defer cancel()
54
55         //Instantiate
56         workloadId, status, err = client.Instantiate(ctx, deployArtifact, host)
57         if err != nil {
58                 c.logger.Errorf("server failed to respond %s", err.Error())
59                 return "", err, "Failure"
60         }
61         c.logger.Infof("Instantiation completed with workloadId %s, status: %s ", workloadId, status)
62         return workloadId, nil, status
63 }
64
65 // Query application
66 func (c *PluginAdapter) Query(pluginInfo string, host string, workloadId string) (status string, error error) {
67         c.logger.Infof("Query started")
68         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate}
69         var client, err = plugin.NewClientGRPC(clientConfig)
70         if err != nil {
71                 c.logger.Errorf("failed to create client: %v", err)
72                 return "", err
73         }
74
75         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
76         defer cancel()
77
78         //Query
79         status, err = client.Query(ctx, host, workloadId)
80         if err != nil {
81                 c.logger.Errorf("failed to query: %v", err)
82                 return "", err
83         }
84         c.logger.Infof("query status: ", status)
85         return status, nil
86 }
87
88 // Terminate application
89 func (c *PluginAdapter) Terminate(pluginInfo string, host string, workloadId string) (status string, error error) {
90         c.logger.Infof("Terminate started")
91         clientConfig := plugin.ClientGRPCConfig{Address: pluginInfo, ChunkSize: chunkSize, RootCertificate: rootCertificate}
92         var client, err = plugin.NewClientGRPC(clientConfig)
93         if err != nil {
94                 c.logger.Errorf("failed to create client: %v", err)
95                 return "Failure", err
96         }
97
98         ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
99         defer cancel()
100
101         //Terminate
102         status, err = client.Terminate(ctx, host, workloadId)
103
104         if err != nil {
105                 c.logger.Errorf("failed to instantiate: %v", err)
106                 return "Failure", err
107         }
108
109         c.logger.Infof("termination success with status: ", status)
110         return status, nil
111 }