blob: 6ceabf98b802dc29e2787fb114ad1e229b98c4e7 [file] [log] [blame]
Mohamed Abukar5120ec12020-02-04 11:01:24 +02001/*
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
20package xapp
21
22import (
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020023 "encoding/json"
24 "fmt"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020025 "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 Abukar9568a2d2020-02-18 16:50:32 +020029 "io/ioutil"
30 "net/http"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020031 "time"
32
33 apiclient "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi"
Mohamed Abukar429da192020-02-26 16:46:34 +020034 apicommon "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/clientapi/common"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020035 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 Abukar429da192020-02-26 16:46:34 +020042 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/common"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020043 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/policy"
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020044 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/query"
Mohamed Abukar5120ec12020-02-04 11:01:24 +020045 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/restapi/operations/report"
46)
47
Mohamed Abukarb6341a52020-03-23 08:55:05 +020048type SubscriptionHandler func(models.SubscriptionType, interface{}) (*models.SubscriptionResponse, error)
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020049type SubscriptionQueryHandler func() (models.SubscriptionList, error)
Mohamed Abukar429da192020-02-26 16:46:34 +020050type SubscriptionDeleteHandler func(string) error
Mohamed Abukar5120ec12020-02-04 11:01:24 +020051
52type Subscriber struct {
53 localAddr string
54 localPort int
55 remoteHost string
56 remoteUrl string
57 remoteProt []string
58 timeout time.Duration
59}
60
61func 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 Abukar429da192020-02-26 16:46:34 +020081func (r *Subscriber) Listen(add SubscriptionHandler, get SubscriptionQueryHandler, del SubscriptionDeleteHandler) error {
Mohamed Abukar5120ec12020-02-04 11:01:24 +020082 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
83 if err != nil {
84 return err
85 }
86
87 api := operations.NewXappFrameworkAPI(swaggerSpec)
88
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020089 // Subscription: query
90 api.QueryGetAllSubscriptionsHandler = query.GetAllSubscriptionsHandlerFunc(
91 func(p query.GetAllSubscriptionsParams) middleware.Responder {
Mohamed Abukar429da192020-02-26 16:46:34 +020092 if resp, err := get(); err == nil {
Mohamed Abukar9568a2d2020-02-18 16:50:32 +020093 return query.NewGetAllSubscriptionsOK().WithPayload(resp)
94 }
95 return query.NewGetAllSubscriptionsInternalServerError()
96 })
97
Mohamed Abukar5120ec12020-02-04 11:01:24 +020098 // SubscriptionType: Report
99 api.ReportSubscribeReportHandler = report.SubscribeReportHandlerFunc(
100 func(p report.SubscribeReportParams) middleware.Responder {
Mohamed Abukar429da192020-02-26 16:46:34 +0200101 if resp, err := add(models.SubscriptionTypeReport, p.ReportParams); err == nil {
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200102 return report.NewSubscribeReportCreated().WithPayload(resp)
103 }
104 return report.NewSubscribeReportInternalServerError()
105 })
106
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200107 // SubscriptionType: policy
108 api.PolicySubscribePolicyHandler = policy.SubscribePolicyHandlerFunc(
109 func(p policy.SubscribePolicyParams) middleware.Responder {
Mohamed Abukar429da192020-02-26 16:46:34 +0200110 if resp, err := add(models.SubscriptionTypePolicy, p.PolicyParams); err == nil {
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200111 return policy.NewSubscribePolicyCreated().WithPayload(resp)
112 }
113 return policy.NewSubscribePolicyInternalServerError()
114 })
115
Mohamed Abukar429da192020-02-26 16:46:34 +0200116 // 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 Abukar5120ec12020-02-04 11:01:24 +0200125 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 Abukarb6341a52020-03-23 08:55:05 +0200138func (r *Subscriber) SubscribeReport(p *apimodel.ReportParams) (*apimodel.SubscriptionResponse, error) {
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200139 params := apireport.NewSubscribeReportParamsWithTimeout(r.timeout).WithReportParams(p)
140 result, err := r.CreateTransport().Report.SubscribeReport(params)
141 if err != nil {
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200142 return &apimodel.SubscriptionResponse{}, err
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200143 }
144
145 return result.Payload, err
146}
147
148// Subscription interface for xApp: POLICY
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200149func (r *Subscriber) SubscribePolicy(p *apimodel.PolicyParams) (*apimodel.SubscriptionResponse, error) {
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200150 params := apipolicy.NewSubscribePolicyParamsWithTimeout(r.timeout).WithPolicyParams(p)
151 result, err := r.CreateTransport().Policy.SubscribePolicy(params)
152 if err != nil {
Mohamed Abukarb6341a52020-03-23 08:55:05 +0200153 return &apimodel.SubscriptionResponse{}, err
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200154 }
155
156 return result.Payload, err
157}
158
Mohamed Abukar429da192020-02-26 16:46:34 +0200159// Subscription interface for xApp: DELETE
160func (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 Abukar9568a2d2020-02-18 16:50:32 +0200167// Subscription interface for xApp: QUERY
168func (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
190func (r *Subscriber) CreateTransport() *apiclient.RICSubscription {
191 return apiclient.New(httptransport.New(r.remoteHost, r.remoteUrl, r.remoteProt), strfmt.Default)
Mohamed Abukar5120ec12020-02-04 11:01:24 +0200192}