Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 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 xapp |
| 21 | |
| 22 | import ( |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 23 | "encoding/json" |
| 24 | "fmt" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 25 | "github.com/go-openapi/loads" |
| 26 | httptransport "github.com/go-openapi/runtime/client" |
| 27 | "github.com/go-openapi/runtime/middleware" |
| 28 | "github.com/go-openapi/strfmt" |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 29 | "io/ioutil" |
| 30 | "net/http" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 31 | "time" |
| 32 | |
| 33 | apiclient "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi" |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 34 | apicommon "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/common" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 35 | apipolicy "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/policy" |
| 36 | apireport "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/report" |
| 37 | apimodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel" |
| 38 | |
| 39 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" |
| 40 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi" |
| 41 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations" |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 42 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/common" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 43 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/policy" |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 44 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/query" |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 45 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/report" |
| 46 | ) |
| 47 | |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame^] | 48 | type SubscriptionHandler func(models.SubscriptionType, interface{}) (*models.SubscriptionResponse, error) |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 49 | type SubscriptionQueryHandler func() (models.SubscriptionList, error) |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 50 | type SubscriptionDeleteHandler func(string) error |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 51 | |
| 52 | type Subscriber struct { |
| 53 | localAddr string |
| 54 | localPort int |
| 55 | remoteHost string |
| 56 | remoteUrl string |
| 57 | remoteProt []string |
| 58 | timeout time.Duration |
| 59 | } |
| 60 | |
| 61 | func NewSubscriber(host string, timo int) *Subscriber { |
| 62 | if host == "" { |
| 63 | host = "service-ricplt-submgr-http:8088" |
| 64 | } |
| 65 | |
| 66 | if timo == 0 { |
| 67 | timo = 20 |
| 68 | } |
| 69 | |
| 70 | return &Subscriber{ |
| 71 | remoteHost: host, |
| 72 | remoteUrl: "/ric/v1", |
| 73 | remoteProt: []string{"http"}, |
| 74 | timeout: time.Duration(timo) * time.Second, |
| 75 | localAddr: "0.0.0.0", |
| 76 | localPort: 8088, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Server interface: listen and receive subscription requests |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 81 | func (r *Subscriber) Listen(add SubscriptionHandler, get SubscriptionQueryHandler, del SubscriptionDeleteHandler) error { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 82 | swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | |
| 87 | api := operations.NewXappFrameworkAPI(swaggerSpec) |
| 88 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 89 | // Subscription: query |
| 90 | api.QueryGetAllSubscriptionsHandler = query.GetAllSubscriptionsHandlerFunc( |
| 91 | func(p query.GetAllSubscriptionsParams) middleware.Responder { |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 92 | if resp, err := get(); err == nil { |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 93 | return query.NewGetAllSubscriptionsOK().WithPayload(resp) |
| 94 | } |
| 95 | return query.NewGetAllSubscriptionsInternalServerError() |
| 96 | }) |
| 97 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 98 | // SubscriptionType: Report |
| 99 | api.ReportSubscribeReportHandler = report.SubscribeReportHandlerFunc( |
| 100 | func(p report.SubscribeReportParams) middleware.Responder { |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 101 | if resp, err := add(models.SubscriptionTypeReport, p.ReportParams); err == nil { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 102 | return report.NewSubscribeReportCreated().WithPayload(resp) |
| 103 | } |
| 104 | return report.NewSubscribeReportInternalServerError() |
| 105 | }) |
| 106 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 107 | // SubscriptionType: policy |
| 108 | api.PolicySubscribePolicyHandler = policy.SubscribePolicyHandlerFunc( |
| 109 | func(p policy.SubscribePolicyParams) middleware.Responder { |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 110 | if resp, err := add(models.SubscriptionTypePolicy, p.PolicyParams); err == nil { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 111 | return policy.NewSubscribePolicyCreated().WithPayload(resp) |
| 112 | } |
| 113 | return policy.NewSubscribePolicyInternalServerError() |
| 114 | }) |
| 115 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 116 | // SubscriptionType: delete |
| 117 | api.CommonUnsubscribeHandler = common.UnsubscribeHandlerFunc( |
| 118 | func(p common.UnsubscribeParams) middleware.Responder { |
| 119 | if err := del(p.SubscriptionID); err == nil { |
| 120 | return common.NewUnsubscribeNoContent() |
| 121 | } |
| 122 | return common.NewUnsubscribeInternalServerError() |
| 123 | }) |
| 124 | |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 125 | server := restapi.NewServer(api) |
| 126 | defer server.Shutdown() |
| 127 | server.Host = r.localAddr |
| 128 | server.Port = r.localPort |
| 129 | |
| 130 | Logger.Info("Serving subscriptions on %s:%d\n", server.Host, server.Port) |
| 131 | if err := server.Serve(); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | return nil |
| 135 | } |
| 136 | |
| 137 | // Subscription interface for xApp: REPORT |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame^] | 138 | func (r *Subscriber) SubscribeReport(p *apimodel.ReportParams) (*apimodel.SubscriptionResponse, error) { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 139 | params := apireport.NewSubscribeReportParamsWithTimeout(r.timeout).WithReportParams(p) |
| 140 | result, err := r.CreateTransport().Report.SubscribeReport(params) |
| 141 | if err != nil { |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame^] | 142 | return &apimodel.SubscriptionResponse{}, err |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | return result.Payload, err |
| 146 | } |
| 147 | |
| 148 | // Subscription interface for xApp: POLICY |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame^] | 149 | func (r *Subscriber) SubscribePolicy(p *apimodel.PolicyParams) (*apimodel.SubscriptionResponse, error) { |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 150 | params := apipolicy.NewSubscribePolicyParamsWithTimeout(r.timeout).WithPolicyParams(p) |
| 151 | result, err := r.CreateTransport().Policy.SubscribePolicy(params) |
| 152 | if err != nil { |
Mohamed Abukar | b6341a5 | 2020-03-23 08:55:05 +0200 | [diff] [blame^] | 153 | return &apimodel.SubscriptionResponse{}, err |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | return result.Payload, err |
| 157 | } |
| 158 | |
Mohamed Abukar | 429da19 | 2020-02-26 16:46:34 +0200 | [diff] [blame] | 159 | // Subscription interface for xApp: DELETE |
| 160 | func (r *Subscriber) UnSubscribe(subId string) error { |
| 161 | params := apicommon.NewUnsubscribeParamsWithTimeout(r.timeout).WithSubscriptionID(subId) |
| 162 | _, err := r.CreateTransport().Common.Unsubscribe(params) |
| 163 | |
| 164 | return err |
| 165 | } |
| 166 | |
Mohamed Abukar | 9568a2d | 2020-02-18 16:50:32 +0200 | [diff] [blame] | 167 | // Subscription interface for xApp: QUERY |
| 168 | func (r *Subscriber) QuerySubscriptions() (models.SubscriptionList, error) { |
| 169 | resp, err := http.Get(fmt.Sprintf("http://%s/%s/subscriptions", r.remoteHost, r.remoteUrl)) |
| 170 | if err != nil { |
| 171 | return models.SubscriptionList{}, err |
| 172 | } |
| 173 | |
| 174 | defer resp.Body.Close() |
| 175 | |
| 176 | contents, err := ioutil.ReadAll(resp.Body) |
| 177 | if err != nil { |
| 178 | return models.SubscriptionList{}, err |
| 179 | } |
| 180 | |
| 181 | subscriptions := models.SubscriptionList{} |
| 182 | err = json.Unmarshal([]byte(string(contents)), &subscriptions) |
| 183 | if err != nil { |
| 184 | return models.SubscriptionList{}, err |
| 185 | } |
| 186 | |
| 187 | return subscriptions, nil |
| 188 | } |
| 189 | |
| 190 | func (r *Subscriber) CreateTransport() *apiclient.RICSubscription { |
| 191 | return apiclient.New(httptransport.New(r.remoteHost, r.remoteUrl, r.remoteProt), strfmt.Default) |
Mohamed Abukar | 5120ec1 | 2020-02-04 11:01:24 +0200 | [diff] [blame] | 192 | } |