Initial commit
[ric-plt/o1.git] / agent / pkg / sbi / sbi.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package sbi
21
22 import (
23         "time"
24         httptransport "github.com/go-openapi/runtime/client"
25         "github.com/go-openapi/strfmt"
26         
27         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
28         apiclient "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrclient"
29         apixapp "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrclient/xapp"
30         apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
31 )
32
33 var log = xapp.Logger
34
35 func NewSBIClient(host, baseUrl string, prot []string, timo int) *SBIClient {
36         return &SBIClient{host, baseUrl, prot, time.Duration(timo) * time.Second}
37 }
38
39 func (s *SBIClient) CreateTransport() *apiclient.RICAppmgr {
40         return apiclient.New(httptransport.New(s.host, s.baseUrl, s.prot), strfmt.Default)
41 }
42
43 func (s *SBIClient) BuildXappDescriptor(name, namespace, release, version string) *apimodel.XappDescriptor {
44         return &apimodel.XappDescriptor{
45                 XappName:    &name,
46                 HelmVersion: version,
47                 ReleaseName: release,
48                 Namespace:   namespace,
49         }
50 }
51
52 func (s *SBIClient) DeployXapp(xappDesc *apimodel.XappDescriptor) error {
53         params := apixapp.NewDeployXappParamsWithTimeout(s.timeout).WithXappDescriptor(xappDesc)
54         log.Info("SBI: DeployXapp=%v", params)
55
56         result, err := s.CreateTransport().Xapp.DeployXapp(params)
57         if err != nil {
58                 log.Error("SBI: DeployXapp unsuccessful: %v", err)
59         } else {
60                 log.Info("SBI: DeployXapp successful: payload=%v", result.Payload)
61         }
62         return err
63 }
64
65 func (s *SBIClient) UndeployXapp(xappDesc *apimodel.XappDescriptor) error {
66         name := *xappDesc.XappName
67         if xappDesc.ReleaseName != "" {
68                 name = xappDesc.ReleaseName
69         }
70
71         params := apixapp.NewUndeployXappParamsWithTimeout(s.timeout).WithXAppName(name)
72         log.Info("SBI: UndeployXapp=%v", params)
73
74         result, err := s.CreateTransport().Xapp.UndeployXapp(params)
75         if err != nil {
76                 log.Error("SBI: UndeployXapp unsuccessful: %v", err)
77         } else {
78                 log.Info("SBI: UndeployXapp successful: payload=%v", result)
79         }
80         return err
81 }
82
83 func (s *SBIClient) GetDeployedXapps() error {
84         params := apixapp.NewGetAllXappsParamsWithTimeout(s.timeout)
85         result, err := s.CreateTransport().Xapp.GetAllXapps(params)
86         if err != nil {
87                 log.Error("GET unsuccessful: %v", err)
88         } else {
89                 log.Info("GET successful: payload=%v", result.Payload)
90         }
91         return err
92 }
93
94 func (s *SBIClient) BuildXappConfig(name, namespace string, configData interface{}) *apimodel.XAppConfig {
95         metadata := &apimodel.ConfigMetadata{
96                 XappName:    &name,
97                 Namespace: &namespace,
98         }
99
100         return &apimodel.XAppConfig{
101                 Metadata: metadata,
102                 Config: configData,
103         }
104 }
105
106 func (s *SBIClient) ModifyXappConfig(xappConfig *apimodel.XAppConfig) error {
107         params := apixapp.NewModifyXappConfigParamsWithTimeout(s.timeout).WithXAppConfig(xappConfig)
108         result, err := s.CreateTransport().Xapp.ModifyXappConfig(params)
109         if err != nil {
110                 log.Error("SBI: ModifyXappConfig unsuccessful: %v", err)
111         } else {
112                 log.Info("SBI: ModifyXappConfig successful: payload=%v", result.Payload)
113         }
114         return err
115 }