Skip to content

Commit

Permalink
O-RAN V3 Rest Api: Event Subscription
Browse files Browse the repository at this point in the history
Signed-off-by: Jack Ding <jackding@gmail.com>
  • Loading branch information
jzding committed Jun 23, 2024
1 parent 2b9bd58 commit 5399882
Show file tree
Hide file tree
Showing 33 changed files with 2,161 additions and 109 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ require (
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.0
github.com/prometheus/client_golang v1.14.0
github.com/redhat-cne/sdk-go v1.0.1-0.20240614182056-bfc7566a02ac
github.com/redhat-cne/sdk-go v1.0.1-unpublished
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.8.0
golang.org/x/net v0.7.0
)

replace github.com/redhat-cne/sdk-go v1.0.1-unpublished => ../sdk-go

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -23,6 +25,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ github.com/prometheus/common v0.42.0 h1:EKsfXEYo4JpWMHH5cg+KOUWeuJSov1Id8zGR8eeI
github.com/prometheus/common v0.42.0/go.mod h1:xBwqVerjNdUDjgODMpudtOMwlOwf2SaTr1yjz4b7Zbc=
github.com/prometheus/procfs v0.8.0 h1:ODq8ZFEaYeCaZOJlZZdJA2AbQR98dSHSM1KW/You5mo=
github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0uaxHdg830/4=
github.com/redhat-cne/sdk-go v1.0.1-0.20240614182056-bfc7566a02ac h1:I9dfgug3GIViEQxgKrLdWB8Y3mqVe0alismyTOdTz6w=
github.com/redhat-cne/sdk-go v1.0.1-0.20240614182056-bfc7566a02ac/go.mod h1:q9LxxPbK1tGpDbQm/KIPujqdP0bK1hhuHrIXV3vuUrM=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
Expand Down
179 changes: 179 additions & 0 deletions pkg/restclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright 2020 The Cloud Native Events Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package restclient

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"

ce "github.com/cloudevents/sdk-go/v2/event"
"github.com/redhat-cne/sdk-go/pkg/types"
log "github.com/sirupsen/logrus"

"golang.org/x/net/context"
)

var (
httpTimeout = 2 * time.Second
)

// Rest client to make http request
type Rest struct {
client http.Client
}

// New get new rest client
func New() *Rest {
return &Rest{
client: http.Client{
Timeout: httpTimeout,
},
}
}

// PostEvent post an event to the give url and check for error
func (r *Rest) PostCloudEvent(url *types.URI, e ce.Event) (status int, err error) {
b, err := json.Marshal(e)
if err != nil {
log.Errorf("error marshalling event %v", e)
return status, err
}

if status = r.Post(url, b); status == http.StatusBadRequest {
return status, fmt.Errorf("post returned status %d", status)
}
return status, nil
}

// Post with data
func (r *Rest) Post(url *types.URI, data []byte) int {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "POST", url.String(), bytes.NewBuffer(data))
if err != nil {
log.Errorf("error creating post request %v", err)
return http.StatusBadRequest
}
request.Header.Set("content-type", "application/json")
response, err := r.client.Do(request)
if err != nil {
log.Errorf("error in post response %v", err)
return http.StatusBadRequest
}
if response.Body != nil {
defer response.Body.Close()
// read any content and print
body, readErr := io.ReadAll(response.Body)
if readErr == nil && len(body) > 0 {
log.Debugf("%s return response %s\n", url.String(), string(body))
}
}
return response.StatusCode
}

// PostWithReturn post with data and return data
func (r *Rest) PostWithReturn(url *types.URI, data []byte) (int, []byte) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "POST", url.String(), bytes.NewBuffer(data))
if err != nil {
log.Errorf("error creating post request %v", err)
return http.StatusBadRequest, nil
}
request.Header.Set("content-type", "application/json")
res, err := r.client.Do(request)
if err != nil {
log.Errorf("error in post response %v to %s ", err, url)
return http.StatusBadRequest, nil
}
if res.Body != nil {
defer res.Body.Close()
}

body, readErr := io.ReadAll(res.Body)
if readErr != nil {
return http.StatusBadRequest, nil
}
return res.StatusCode, body
}

// Put http request
func (r *Rest) Put(url *types.URI) int {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "PUT", url.String(), nil)
if err != nil {
log.Errorf("error creating post request %v", err)
return http.StatusBadRequest
}
request.Header.Set("content-type", "application/json")
res, err := r.client.Do(request)
if err != nil {
log.Errorf("error in post response %v to %s ", err, url)
return http.StatusBadRequest
}
defer res.Body.Close()
return res.StatusCode
}

// Delete http request
func (r *Rest) Delete(url *types.URI) int {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "DELETE", url.String(), nil)
if err != nil {
log.Errorf("error creating post request %v", err)
return http.StatusBadRequest
}
request.Header.Set("content-type", "application/json")
res, err := r.client.Do(request)
if err != nil {
log.Errorf("error in post response %v to %s ", err, url)
return http.StatusBadRequest
}
defer res.Body.Close()
return res.StatusCode
}

// Get http request
func (r *Rest) Get(url *types.URI) (int, string) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
request, err := http.NewRequestWithContext(ctx, "GET", url.String(), nil)
if err != nil {
log.Errorf("error creating post request %v", err)
return http.StatusBadRequest, fmt.Sprintf("error creating post request %v", err)
}
request.Header.Set("content-type", "application/json")
res, err := r.client.Do(request)
if err != nil {
log.Errorf("error in post response %v to %s ", err, url)
return http.StatusBadRequest, fmt.Sprintf("error in post response %v to %s ", err, url)
}
defer res.Body.Close()
if body, readErr := io.ReadAll(res.Body); readErr == nil {
return res.StatusCode, string(body)
}
return http.StatusBadRequest, fmt.Sprintf("error in post response %v to %s ", err, url)
}
4 changes: 2 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestServer_CreateSubscription(t *testing.T) {
// create subscription
sub := api.NewPubSub(
&types.URI{URL: url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port), Path: fmt.Sprintf("%s%s", apPath, "dummy")}},
resource)
resource, "1.0")

data, err := json.Marshal(&sub)
assert.Nil(t, err)
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestServer_GetCurrentState_withSubscription(t *testing.T) {
// create subscription
sub := api.NewPubSub(
&types.URI{URL: url.URL{Scheme: "http", Host: fmt.Sprintf("localhost:%d", port), Path: fmt.Sprintf("%s%s", apPath, "dummy")}},
resource)
resource, "1.0")

data, err := json.Marshal(&sub)
assert.Nil(t, err)
Expand Down
Loading

0 comments on commit 5399882

Please sign in to comment.