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 ==================================================================================
23 "github.com/go-openapi/loads"
24 httptransport "github.com/go-openapi/runtime/client"
25 "github.com/go-openapi/runtime/middleware"
26 "github.com/go-openapi/strfmt"
29 apiclient "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi"
30 apicontrol "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/control"
31 apipolicy "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/policy"
32 apireport "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/report"
33 apimodel "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientmodel"
35 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
36 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi"
37 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations"
38 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/control"
39 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/policy"
40 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/report"
43 type SubscriptionReportHandler func(models.SubscriptionType, interface{}) (models.SubscriptionResult, error)
45 type Subscriber struct {
54 func NewSubscriber(host string, timo int) *Subscriber {
56 host = "service-ricplt-submgr-http:8088"
66 remoteProt: []string{"http"},
67 timeout: time.Duration(timo) * time.Second,
73 // Server interface: listen and receive subscription requests
74 func (r *Subscriber) Listen(handler SubscriptionReportHandler) error {
75 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
80 api := operations.NewXappFrameworkAPI(swaggerSpec)
82 // SubscriptionType: Report
83 api.ReportSubscribeReportHandler = report.SubscribeReportHandlerFunc(
84 func(p report.SubscribeReportParams) middleware.Responder {
85 if resp, err := handler(models.SubscriptionTypeReport, p.ReportParams); err == nil {
86 return report.NewSubscribeReportCreated().WithPayload(resp)
88 return report.NewSubscribeReportInternalServerError()
91 // SubscriptionType: Control
92 api.ControlSubscribeControlHandler = control.SubscribeControlHandlerFunc(
93 func(p control.SubscribeControlParams) middleware.Responder {
94 if resp, err := handler(models.SubscriptionTypeControl, p.ControlParams); err == nil {
95 return control.NewSubscribeControlCreated().WithPayload(resp)
97 return control.NewSubscribeControlInternalServerError()
100 // SubscriptionType: policy
101 api.PolicySubscribePolicyHandler = policy.SubscribePolicyHandlerFunc(
102 func(p policy.SubscribePolicyParams) middleware.Responder {
103 if resp, err := handler(models.SubscriptionTypePolicy, p.PolicyParams); err == nil {
104 return policy.NewSubscribePolicyCreated().WithPayload(resp)
106 return policy.NewSubscribePolicyInternalServerError()
109 server := restapi.NewServer(api)
110 defer server.Shutdown()
111 server.Host = r.localAddr
112 server.Port = r.localPort
114 Logger.Info("Serving subscriptions on %s:%d\n", server.Host, server.Port)
115 if err := server.Serve(); err != nil {
121 // Subscription interface for xApp: REPORT
122 func (r *Subscriber) SubscribeReport(p *apimodel.ReportParams) (apimodel.SubscriptionResult, error) {
123 params := apireport.NewSubscribeReportParamsWithTimeout(r.timeout).WithReportParams(p)
124 result, err := r.CreateTransport().Report.SubscribeReport(params)
126 return apimodel.SubscriptionResult{}, err
129 return result.Payload, err
132 // Subscription interface for xApp: CONTROL
133 func (r *Subscriber) SubscribeControl(p *apimodel.ControlParams) (apimodel.SubscriptionResult, error) {
134 params := apicontrol.NewSubscribeControlParamsWithTimeout(r.timeout).WithControlParams(p)
135 result, err := r.CreateTransport().Control.SubscribeControl(params)
137 return apimodel.SubscriptionResult{}, err
140 return result.Payload, err
143 // Subscription interface for xApp: POLICY
144 func (r *Subscriber) SubscribePolicy(p *apimodel.PolicyParams) (apimodel.SubscriptionResult, error) {
145 params := apipolicy.NewSubscribePolicyParamsWithTimeout(r.timeout).WithPolicyParams(p)
146 result, err := r.CreateTransport().Policy.SubscribePolicy(params)
148 return apimodel.SubscriptionResult{}, err
151 return result.Payload, err
154 func (s *Subscriber) CreateTransport() *apiclient.RICSubscription {
155 return apiclient.New(httptransport.New(s.remoteHost, s.remoteUrl, s.remoteProt), strfmt.Default)