2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
25 httptransport "github.com/go-openapi/runtime/client"
26 "github.com/go-openapi/strfmt"
32 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
33 apiclient "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrclient"
34 apixapp "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrclient/xapp"
35 apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
38 type PodStatus struct {
46 func NewSBIClient(host, baseUrl string, prot []string, timo int) *SBIClient {
47 return &SBIClient{host, baseUrl, prot, time.Duration(timo) * time.Second}
50 func (s *SBIClient) CreateTransport() *apiclient.RICAppmgr {
51 return apiclient.New(httptransport.New(s.host, s.baseUrl, s.prot), strfmt.Default)
54 func (s *SBIClient) BuildXappDescriptor(name, namespace, release, version string) *apimodel.XappDescriptor {
55 return &apimodel.XappDescriptor{
63 func (s *SBIClient) DeployXapp(xappDesc *apimodel.XappDescriptor) error {
64 params := apixapp.NewDeployXappParamsWithTimeout(s.timeout).WithXappDescriptor(xappDesc)
65 log.Info("SBI: DeployXapp=%v", params)
67 result, err := s.CreateTransport().Xapp.DeployXapp(params)
69 log.Error("SBI: DeployXapp unsuccessful: %v", err)
71 log.Info("SBI: DeployXapp successful: payload=%v", result.Payload)
76 func (s *SBIClient) UndeployXapp(xappDesc *apimodel.XappDescriptor) error {
77 name := *xappDesc.XappName
78 if xappDesc.ReleaseName != "" {
79 name = xappDesc.ReleaseName
82 params := apixapp.NewUndeployXappParamsWithTimeout(s.timeout).WithXAppName(name)
83 log.Info("SBI: UndeployXapp=%v", params)
85 result, err := s.CreateTransport().Xapp.UndeployXapp(params)
87 log.Error("SBI: UndeployXapp unsuccessful: %v", err)
89 log.Info("SBI: UndeployXapp successful: payload=%v", result)
94 func (s *SBIClient) GetDeployedXapps() error {
95 params := apixapp.NewGetAllXappsParamsWithTimeout(s.timeout)
96 result, err := s.CreateTransport().Xapp.GetAllXapps(params)
98 log.Error("GET unsuccessful: %v", err)
100 log.Info("GET successful: payload=%v", result.Payload)
105 func (s *SBIClient) BuildXappConfig(name, namespace string, configData interface{}) *apimodel.XAppConfig {
106 metadata := &apimodel.ConfigMetadata{
108 Namespace: &namespace,
111 return &apimodel.XAppConfig{
117 func (s *SBIClient) ModifyXappConfig(xappConfig *apimodel.XAppConfig) error {
118 params := apixapp.NewModifyXappConfigParamsWithTimeout(s.timeout).WithXAppConfig(xappConfig)
119 result, err := s.CreateTransport().Xapp.ModifyXappConfig(params)
121 log.Error("SBI: ModifyXappConfig unsuccessful: %v", err)
123 log.Info("SBI: ModifyXappConfig successful: payload=%v", result.Payload)
128 func (s *SBIClient) GetAllPodStatus(namespace string) ([]PodStatus, error) {
129 output, err := s.RunCommand(fmt.Sprintf("/usr/local/bin/kubectl get pod -n %s", namespace))
131 return []PodStatus{}, err
134 podStatusList := []PodStatus{}
136 re := regexp.MustCompile(fmt.Sprintf(`%s-.*`, namespace))
137 podList := re.FindAllStringSubmatch(string(output), -1)
139 for _, pod := range podList {
141 fmt.Sscanf(pod[0], "%s %s %s", &p.Name, &readyStr, &p.Status)
142 p.Name = strings.Split(p.Name, "-")[1]
143 p.Health = s.GetHealthState(readyStr)
145 podStatusList = append(podStatusList, p)
148 return podStatusList, nil
151 func (s *SBIClient) GetHealthState(ready string) (state string) {
152 result := strings.Split(ready, "/")
157 if result[0] == result[1] {
165 func (s *SBIClient) RunCommand(args string) (string, error) {
166 return CommandExec(args)
169 var CommandExec = func(args string) (string, error) {
170 cmd := exec.Command("/bin/sh", "-c", args)
171 var stdout bytes.Buffer
172 var stderr bytes.Buffer
176 xapp.Logger.Debug("Running command: '%s'", cmd)
177 if err := cmd.Run(); err != nil {
178 xapp.Logger.Error("Command failed (%s): %v - %s", cmd, err.Error(), stderr.String())
181 xapp.Logger.Debug("Command executed successfully!")
182 return stdout.String(), nil