From 232ff312f440730f46c343aa3819de45ea68fb94 Mon Sep 17 00:00:00 2001 From: Jack Ding <7378668+jzding@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:08:50 -0400 Subject: [PATCH] O-RAN V3 Rest Api: Status Notification (#71) Signed-off-by: Jack Ding --- go.mod | 2 +- go.sum | 4 +- server_test.go | 2 +- v2/routes.go | 72 +++++++++---------- v2/server.go | 4 +- v2/server_test.go | 11 +-- .../redhat-cne/sdk-go/pkg/common/common.go | 5 +- .../redhat-cne/sdk-go/pkg/event/event.go | 2 + .../redhat-cne/sdk-go/pkg/event/event_ce.go | 14 ++++ .../sdk-go/pkg/event/ptp/resource.go | 11 ++- .../sdk-go/pkg/event/ptp/syncstate.go | 12 ++++ .../redhat-cne/sdk-go/pkg/event/ptp/types.go | 13 +++- .../redhat-cne/sdk-go/pkg/pubsub/pubsub.go | 2 +- .../sdk-go/pkg/pubsub/pubsub_unmarshal.go | 4 +- .../sdk-go/pkg/subscriber/subscriber.go | 6 +- .../pkg/subscriber/subscriber_writer.go | 8 +-- .../sdk-go/v1/subscriber/subscriber.go | 38 +++++++--- vendor/modules.txt | 2 +- 18 files changed, 135 insertions(+), 77 deletions(-) diff --git a/go.mod b/go.mod index a6404a3..39850dd 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ 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.20240702163442-605f629084b9 + github.com/redhat-cne/sdk-go v1.0.1-0.20240715150244-f435c154a612 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.8.0 golang.org/x/net v0.7.0 diff --git a/go.sum b/go.sum index 7ff684e..8e618d8 100644 --- a/go.sum +++ b/go.sum @@ -53,8 +53,8 @@ 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.20240702163442-605f629084b9 h1:qDOGSHOtHRszd8FnM0GZVUvbIvHhZrw5GeccXYPwT04= -github.com/redhat-cne/sdk-go v1.0.1-0.20240702163442-605f629084b9/go.mod h1:q9LxxPbK1tGpDbQm/KIPujqdP0bK1hhuHrIXV3vuUrM= +github.com/redhat-cne/sdk-go v1.0.1-0.20240715150244-f435c154a612 h1:TnnP33wqdtZ4GCp8WYHVFVywWxrcGonc0ijGCpfqTdU= +github.com/redhat-cne/sdk-go v1.0.1-0.20240715150244-f435c154a612/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= diff --git a/server_test.go b/server_test.go index f72c7ee..a620c00 100644 --- a/server_test.go +++ b/server_test.go @@ -100,7 +100,7 @@ func TestMain(m *testing.M) { Subject: func(s string) *string { return &s }("topic"), }.AsV1(), } - _ = e.SetData(cloudevents.ApplicationJSON, cneEvent) + _ = e.SetData("", cneEvent) func() { defer func() { if err := recover(); err != nil { diff --git a/v2/routes.go b/v2/routes.go index 668dba5..89706d8 100644 --- a/v2/routes.go +++ b/v2/routes.go @@ -56,6 +56,7 @@ func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) { return } sub := pubsub.PubSub{} + sub.SetVersion(API_VERSION) if err = json.Unmarshal(bodyBytes, &sub); err != nil { respondWithStatusCode(w, http.StatusBadRequest, fmt.Sprintf("marshalling error %v", err)) localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1) @@ -67,26 +68,18 @@ func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) { localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1) return } - if subExists, ok := s.pubSubAPI.HasSubscription(sub.GetResource()); ok { + clientIDs := s.subscriberAPI.GetClientIDByResource(sub.GetResource()) + if len(clientIDs) != 0 { respondWithStatusCode(w, http.StatusConflict, - fmt.Sprintf("subscription (id: %s) with same resource already exists, skipping creation", - subExists.GetID())) + fmt.Sprintf("subscription (clientID: %s) with same resource already exists, skipping creation", + clientIDs[0])) return } id := uuid.New().String() sub.SetID(id) - sub.SetVersion(API_VERSION) - sub.SetURILocation(fmt.Sprintf("http://localhost:%d%s%s/%s", s.port, s.apiPath, "subscriptions", sub.ID)) //nolint:errcheck - - // TODO: cleanup: local pubsub is no longer needed since we are using configMap - newSub, err := s.pubSubAPI.CreateSubscription(sub) - if err != nil { - respondWithStatusCode(w, http.StatusNotFound, fmt.Sprintf("error creating subscription %v", err)) - localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1) - return - } - addr := newSub.GetResource() + sub.SetURILocation(fmt.Sprintf("http://%s:%d%s%s/%s", s.apiHost, s.port, s.apiPath, "subscriptions", sub.ID)) //nolint:errcheck + addr := sub.GetResource() // this is placeholder not sending back to report out := channel.DataChan{ @@ -119,7 +112,8 @@ func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) { } restClient := restclient.New() - out.Data.SetID(newSub.ID) // set ID to the subscriptionID + // make sure event ID is unique + out.Data.SetID(uuid.New().String()) status, err := restClient.PostCloudEvent(sub.EndPointURI, *out.Data) if err != nil { respondWithStatusCode(w, http.StatusBadRequest, @@ -139,7 +133,7 @@ func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) { subs := subscriber.New(s.getClientIDFromURI(endPointURI)) _ = subs.SetEndPointURI(endPointURI) - subs.AddSubscription(newSub) + subs.AddSubscription(sub) subs.Action = channel.NEW cevent, _ := subs.CreateCloudEvents() cevent.SetSource(addr) @@ -162,10 +156,10 @@ func (s *Server) createSubscription(w http.ResponseWriter, r *http.Request) { localmetrics.UpdateSubscriptionCount(localmetrics.FAILCREATE, 1) } else { out.Status = channel.SUCCESS - _ = out.Data.SetData(cloudevents.ApplicationJSON, updatedObj) + _ = out.Data.SetData("", updatedObj) log.Infof("subscription created successfully.") localmetrics.UpdateSubscriptionCount(localmetrics.ACTIVE, 1) - respondWithJSON(w, http.StatusCreated, newSub) + respondWithJSON(w, http.StatusCreated, sub) } s.dataOut <- &out @@ -188,6 +182,7 @@ func (s *Server) createPublisher(w http.ResponseWriter, r *http.Request) { return } pub := pubsub.PubSub{} + pub.SetVersion(API_VERSION) if err = json.Unmarshal(bodyBytes, &pub); err != nil { localmetrics.UpdatePublisherCount(localmetrics.FAILCREATE, 1) respondWithError(w, "marshalling error") @@ -256,12 +251,15 @@ func (s *Server) getSubscriptionByID(w http.ResponseWriter, r *http.Request) { respondWithStatusCode(w, http.StatusNotFound, "") return } - sub, err := s.pubSubAPI.GetSubscription(subscriptionID) - if err != nil { - respondWithStatusCode(w, http.StatusNotFound, "") - return + + for _, c := range s.subscriberAPI.GetClientIDBySubID(subscriptionID) { + sub, err := s.subscriberAPI.GetSubscription(c, subscriptionID) + if err == nil { + respondWithJSON(w, http.StatusOK, sub) + return + } } - respondWithJSON(w, http.StatusOK, sub) + respondWithStatusCode(w, http.StatusNotFound, "") } func (s *Server) getPublisherByID(w http.ResponseWriter, r *http.Request) { @@ -278,9 +276,11 @@ func (s *Server) getPublisherByID(w http.ResponseWriter, r *http.Request) { } respondWithJSON(w, http.StatusOK, pub) } + func (s *Server) getSubscriptions(w http.ResponseWriter, _ *http.Request) { - b, err := s.pubSubAPI.GetSubscriptionsFromFile() + b, err := s.subscriberAPI.GetSubscriptions() if err != nil { + log.Errorf("error loading subscriber data %v", err) respondWithError(w, "error loading subscriber data") return } @@ -322,14 +322,13 @@ func (s *Server) deleteSubscription(w http.ResponseWriter, r *http.Request) { return } - if err := s.pubSubAPI.DeleteSubscription(subscriptionID); err != nil { - localmetrics.UpdateSubscriptionCount(localmetrics.FAILDELETE, 1) - respondWithStatusCode(w, http.StatusNotFound, err.Error()) + clientIDs := s.subscriberAPI.GetClientIDBySubID(subscriptionID) + if len(clientIDs) == 0 { + respondWithStatusCode(w, http.StatusNotFound, "") return } - // update configMap - for _, c := range s.subscriberAPI.GetClientIDBySubID(subscriptionID) { + for _, c := range clientIDs { if err := s.subscriberAPI.DeleteSubscription(c, subscriptionID); err != nil { localmetrics.UpdateSubscriptionCount(localmetrics.FAILDELETE, 1) respondWithStatusCode(w, http.StatusNotFound, err.Error()) @@ -337,6 +336,7 @@ func (s *Server) deleteSubscription(w http.ResponseWriter, r *http.Request) { } } + // update configMap for _, subs := range s.subscriberAPI.SubscriberStore.Store { cevent, _ := subs.CreateCloudEvents() out := channel.DataChan{ @@ -400,7 +400,7 @@ func (s *Server) publishEvent(w http.ResponseWriter, r *http.Request) { respondWithError(w, fmt.Sprintf("no publisher data for id %s found to publish event for", cneEvent.ID)) return } - ceEvent, err := cneEvent.NewCloudEvent(&pub) + ceEvent, err := cneEvent.NewCloudEventV2(&pub) if err != nil { localmetrics.UpdateEventPublishedCount(pub.Resource, localmetrics.FAIL, 1) respondWithError(w, err.Error()) @@ -441,12 +441,12 @@ func (s *Server) getCurrentState(w http.ResponseWriter, r *http.Request) { } } } else { - respondWithError(w, "subscription not found") + respondWithStatusCode(w, http.StatusNotFound, "subscription not found") return } if sub == nil { - respondWithError(w, "subscription not found") + respondWithStatusCode(w, http.StatusNotFound, "subscription not found") return } @@ -471,14 +471,14 @@ func (s *Server) getCurrentState(w http.ResponseWriter, r *http.Request) { // statusReceiveOverrideFn must return value for if s.statusReceiveOverrideFn != nil { if statusErr := s.statusReceiveOverrideFn(*e, &out); statusErr != nil { - respondWithError(w, statusErr.Error()) + respondWithStatusCode(w, http.StatusNotFound, statusErr.Error()) } else if out.Data != nil { respondWithJSON(w, http.StatusOK, *out.Data) } else { - respondWithError(w, "event not found") + respondWithStatusCode(w, http.StatusNotFound, "event not found") } } else { - respondWithError(w, "onReceive function not defined") + respondWithStatusCode(w, http.StatusNotFound, "onReceive function not defined") } } @@ -504,7 +504,7 @@ func (s *Server) pingForSubscribedEventStatus(w http.ResponseWriter, r *http.Req Version: "v1", }) - ceEvent, err := cneEvent.NewCloudEvent(&sub) + ceEvent, err := cneEvent.NewCloudEventV2(&sub) if err != nil { respondWithError(w, err.Error()) diff --git a/v2/server.go b/v2/server.go index eff5cc1..37678f9 100644 --- a/v2/server.go +++ b/v2/server.go @@ -81,6 +81,7 @@ const ( // Server defines rest routes server object type Server struct { port int + apiHost string apiPath string //use dataOut chanel to write to configMap dataOut chan<- *channel.DataChan @@ -138,12 +139,13 @@ type swaggReqAccepted struct { //nolint:deadcode,unused } // InitServer is used to supply configurations for rest routes server -func InitServer(port int, apiPath, storePath string, +func InitServer(port int, apiHost, apiPath, storePath string, dataOut chan<- *channel.DataChan, closeCh <-chan struct{}, onStatusReceiveOverrideFn func(e cloudevents.Event, dataChan *channel.DataChan) error) *Server { once.Do(func() { ServerInstance = &Server{ port: port, + apiHost: apiHost, apiPath: apiPath, dataOut: dataOut, closeCh: closeCh, diff --git a/v2/server_test.go b/v2/server_test.go index d805442..955a93f 100644 --- a/v2/server_test.go +++ b/v2/server_test.go @@ -30,7 +30,7 @@ import ( types2 "github.com/cloudevents/sdk-go/v2/types" "github.com/google/uuid" - restapi "github.com/redhat-cne/rest-api" + restapi "github.com/redhat-cne/rest-api/v2" "github.com/redhat-cne/sdk-go/pkg/channel" "github.com/redhat-cne/sdk-go/pkg/event" "github.com/redhat-cne/sdk-go/pkg/event/ptp" @@ -50,6 +50,7 @@ var ( closeCh chan struct{} wg sync.WaitGroup port = 8989 + apHost = "localhost" apPath = "/routes/cne/v1/" resource = "test/test" resourceNoneSubscribed = "test/nonesubscribed" @@ -64,7 +65,7 @@ func init() { } func TestMain(m *testing.M) { - server = restapi.InitServer(port, apPath, storePath, eventOutCh, closeCh) + server = restapi.InitServer(port, apHost, apPath, storePath, eventOutCh, closeCh, nil) //start http server server.Start() @@ -100,7 +101,7 @@ func TestMain(m *testing.M) { Subject: func(s string) *string { return &s }("topic"), }.AsV1(), } - _ = e.SetData(cloudevents.ApplicationJSON, cneEvent) + _ = e.SetData("", cneEvent) func() { defer func() { if err := recover(); err != nil { @@ -362,7 +363,7 @@ func TestServer_GetCurrentState_withoutSubscription(t *testing.T) { s, err2 := io.ReadAll(resp.Body) assert.Nil(t, err2) log.Infof("tedt %s ", string(s)) - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + assert.Equal(t, http.StatusNotFound, resp.StatusCode) } func TestServer_TestPingStatusStatusCode(t *testing.T) { @@ -424,7 +425,7 @@ func TestServer_GetNonExistingSubscription(t *testing.T) { resp, err := server.HTTPClient.Do(req) assert.Nil(t, err) defer resp.Body.Close() - assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + assert.Equal(t, http.StatusNotFound, resp.StatusCode) } func TestServer_TestDummyStatusCode(t *testing.T) { diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/common/common.go b/vendor/github.com/redhat-cne/sdk-go/pkg/common/common.go index 81815c0..83b5438 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/common/common.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/common/common.go @@ -21,10 +21,9 @@ import ( log "github.com/sirupsen/logrus" ) -// REST API versions const ( - V1 = "1.0" - V2 = "2.0" + RestAPIV1 = "1.0" + RestAPIV2 = "2.0" ) func getMajorVersion(version string) (int, error) { diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/event/event.go b/vendor/github.com/redhat-cne/sdk-go/pkg/event/event.go index d1cdd83..b99423f 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/event/event.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/event/event.go @@ -23,6 +23,8 @@ import ( "github.com/redhat-cne/sdk-go/pkg/types" ) +const APISchemaVersion = "1.0" + // Event represents the canonical representation of a Cloud Native Event. // Event Json payload is as follows, // diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/event/event_ce.go b/vendor/github.com/redhat-cne/sdk-go/pkg/event/event_ce.go index 5fe66d4..228b5a2 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/event/event_ce.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/event/event_ce.go @@ -39,6 +39,20 @@ func (e *Event) NewCloudEvent(ps *pubsub.PubSub) (*cloudevent.Event, error) { return &ce, nil } +// NewCloudEvent create new cloud event from cloud native events and pubsub +func (e *Event) NewCloudEventV2(ps *pubsub.PubSub) (*cloudevent.Event, error) { + ce := cloudevent.NewEvent(cloudevent.VersionV1) + ce.SetTime(e.GetTime()) + ce.SetType(e.Type) + ce.SetSource(ps.Resource) // bus address + ce.SetSpecVersion(cloudevent.VersionV1) + ce.SetID(uuid.New().String()) + if err := ce.SetData("", e.GetData()); err != nil { + return nil, err + } + return &ce, nil +} + // GetCloudNativeEvents get event data from cloud events object if its valid else return error func (e *Event) GetCloudNativeEvents(ce *cloudevent.Event) (err error) { if ce.Data() == nil { diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/resource.go b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/resource.go index 10cd127..7d21726 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/resource.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/resource.go @@ -18,27 +18,34 @@ package ptp type EventResource string const ( + // O-RAN 7.2.3.6 // GnssSyncStatus notification is signalled from equipment at state change GnssSyncStatus EventResource = "/sync/gnss-status/gnss-sync-status" + // O-RAN 7.2.3.8 // OsClockSyncState State of node OS clock synchronization is notified at state change OsClockSyncState EventResource = "/sync/sync-status/os-clock-sync-state" + // O-RAN 7.2.3.10 // PtpClockClass notification is generated when the clock-class changes. - PtpClockClass EventResource = "/sync/ptp-status/ptp-clock-class-change" + PtpClockClass EventResource = "/sync/ptp-status/clock-class" + // O-RAN 7.2.3.3 // PtpLockState notification is signalled from equipment at state change PtpLockState EventResource = "/sync/ptp-status/lock-state" + // O-RAN 7.2.3.11 // SynceClockQuality notification is generated when the clock-quality changes. SynceClockQuality EventResource = "/sync/synce-status/clock-quality" + // O-RAN 7.2.3.9 // SynceLockState Notification used to inform about synce synchronization state change SynceLockState EventResource = "/sync/synce-status/lock-state" // SynceLockStateExtended notification is signalled from equipment at state change, enhanced information SynceLockStateExtended EventResource = "/sync/synce-status/lock-state-extended" - // SyncStatusState State of equipment synchronization is notified at state change + // O-RAN 7.2.3.1 + // SyncStatusState is the overall synchronization health of the node, including the OS System Clock SyncStatusState EventResource = "/sync/sync-status/sync-state" ) diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/syncstate.go b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/syncstate.go index 741e337..4b2ee93 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/syncstate.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/syncstate.go @@ -30,6 +30,18 @@ const ( // BOOTING ... BOOTING SyncState = "BOOTING" + // FAILURE_MULTIPATH is GNSS Sync Failure - Multipath condition detected + FAILURE_MULTIPATH SyncState = "FAILURE-MULTIPATH" + + // FAILURE_NOFIX is GNSS Sync Failure - Unknown + FAILURE_NOFIX SyncState = "FAILURE-NOFIX" + + // FAILURE_LOW_SNR is GNSS Sync Failure - Low SNR condition detected + FAILURE_LOW_SNR SyncState = "FAILURE-LOW-SNR" + + // FAILURE_PLL is GNSS Sync Failure - PLL is not functioning + FAILURE_PLL SyncState = "FAILURE-PLL" + // FREERUN ... FREERUN SyncState = "FREERUN" diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/types.go b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/types.go index 9ff2865..ff6e5ea 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/types.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/event/ptp/types.go @@ -18,27 +18,34 @@ package ptp type EventType string const ( + // O-RAN 7.2.3.6 // GnssStateChange is Notification used to inform about gnss synchronization state change GnssStateChange EventType = "event.sync.gnss-status.gnss-state-change" + // O-RAN 7.2.3.8 // OsClockSyncStateChange is the object contains information related to a notification OsClockSyncStateChange EventType = "event.sync.sync-status.os-clock-sync-state-change" + // O-RAN 7.2.3.10 // PtpClockClassChange is Notification used to inform about ptp clock class changes. PtpClockClassChange EventType = "event.sync.ptp-status.ptp-clock-class-change" + // O-RAN 7.2.3.3 // PtpStateChange is Notification used to inform about ptp synchronization state change PtpStateChange EventType = "event.sync.ptp-status.ptp-state-change" + // O-RAN 7.2.3.11 // SynceClockQualityChange is Notification used to inform about changes in the clock quality of the primary SyncE signal advertised in ESMC packets - SynceClockQualityChange EventType = "event.sync.synce-status.sync-clock-quality-change" + SynceClockQualityChange EventType = "event.sync.synce-status.synce-clock-quality-change" + // O-RAN 7.2.3.9 // SynceStateChange is Notification used to inform about synce synchronization state change - SynceStateChange EventType = "event.sync.sync-status.synce-state-change" + SynceStateChange EventType = "event.sync.synce-status.synce-state-change" // SynceStateChangeExtended is Notification used to inform about synce synchronization state change, enhanced state information SynceStateChangeExtended EventType = "event.sync.synce-status.synce-state-change-extended" - // SyncStateChange is Notification used to inform about synchronization state change + // O-RAN 7.2.3.1 + // SyncStateChange is Notification used to inform about the overall synchronization state change SyncStateChange EventType = "event.sync.sync-status.synchronization-state-change" ) diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub.go b/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub.go index 428c9bf..be7afd0 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub.go @@ -52,7 +52,7 @@ func (ps *PubSub) String() string { b := strings.Builder{} b.WriteString(" EndpointUri: " + ps.GetEndpointURI() + "\n") b.WriteString(" UriLocation: " + ps.GetURILocation() + "\n") - b.WriteString(" ID: " + ps.GetID() + "\n") + b.WriteString(" SubscriptionId: " + ps.GetID() + "\n") b.WriteString(" Resource: " + ps.GetResource() + "\n") return b.String() } diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub_unmarshal.go b/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub_unmarshal.go index 973b37f..dd95e7d 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub_unmarshal.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/pubsub/pubsub_unmarshal.go @@ -73,7 +73,7 @@ func readJSONFromIterator(out *PubSub, iterator *jsoniter.Iterator) error { uriLocation = iterator.ReadString() case "ResourceAddress": resource = iterator.ReadString() - version = common.V2 + version = common.RestAPIV2 case "id": id = iterator.ReadString() case "endpointUri": @@ -82,7 +82,7 @@ func readJSONFromIterator(out *PubSub, iterator *jsoniter.Iterator) error { uriLocation = iterator.ReadString() case "resource": resource = iterator.ReadString() - version = common.V1 + version = common.RestAPIV1 default: iterator.Skip() } diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber.go b/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber.go index 3c8f3fd..ce8e82c 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber.go @@ -49,7 +49,7 @@ type Subscriber struct { SubStore *store.PubSubStore `json:"subStore" omit:"empty"` // EndPointURI - A URI describing the subscriber link . // +required - EndPointURI *types.URI `json:"endPointURI" omit:"empty"` + EndPointURI *types.URI `json:"EndpointUri" omit:"empty"` // Status ... Status Status `json:"status" omit:"empty"` // Action ... @@ -84,8 +84,8 @@ func (s *Subscriber) FailedCount() int { // String returns a pretty-printed representation of the Event. func (s *Subscriber) String() string { b := strings.Builder{} - b.WriteString(" EndPointURI: " + s.GetEndPointURI() + "\n") - b.WriteString(" ID: " + s.GetClientID().String() + "\n") + b.WriteString(" EndpointUri: " + s.GetEndPointURI() + "\n") + b.WriteString(" clientID: " + s.GetClientID().String() + "\n") b.WriteString(" sub :{") if s.SubStore != nil { for _, v := range s.SubStore.Store { diff --git a/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber_writer.go b/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber_writer.go index 5b9bad9..9d3d3b8 100644 --- a/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber_writer.go +++ b/vendor/github.com/redhat-cne/sdk-go/pkg/subscriber/subscriber_writer.go @@ -42,12 +42,6 @@ func (s *Subscriber) SetStatus(status Status) { // AddSubscription ... func (s *Subscriber) AddSubscription(subs ...pubsub.PubSub) { for _, ss := range subs { - newS := &pubsub.PubSub{ - ID: ss.GetID(), - EndPointURI: nil, - URILocation: nil, - Resource: ss.Resource, - } - s.SubStore.Store[ss.GetID()] = newS + s.SubStore.Store[ss.GetID()] = &ss } } diff --git a/vendor/github.com/redhat-cne/sdk-go/v1/subscriber/subscriber.go b/vendor/github.com/redhat-cne/sdk-go/v1/subscriber/subscriber.go index 6fb59c8..20f9c1d 100644 --- a/vendor/github.com/redhat-cne/sdk-go/v1/subscriber/subscriber.go +++ b/vendor/github.com/redhat-cne/sdk-go/v1/subscriber/subscriber.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "os" + "strings" "sync" "github.com/google/uuid" @@ -73,12 +74,19 @@ func (p *API) ReloadStore() { log.Infof("reloading subscribers from the store %s", p.storeFilePath) if files, err := loadFileNamesFromDir(p.storeFilePath); err == nil { for _, f := range files { - if b, err1 := loadFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, f)); err1 == nil { + // valid subscription filename is .json + if uuid.Validate(strings.Split(f, ".")[0]) != nil { + continue + } else if b, err1 := loadFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, f)); err1 == nil { if len(b) > 0 { var sub subscriber.Subscriber var err2 error if err2 = json.Unmarshal(b, &sub); err2 == nil { - p.SubscriberStore.Set(sub.ClientID, sub) + if sub.ClientID != uuid.Nil { + p.SubscriberStore.Set(sub.ClientID, sub) + } else { + log.Errorf("subscriber data from file %s is not valid", f) + } } else { log.Errorf("error parsing subscriber %s \n %s", string(b), err2.Error()) } @@ -201,21 +209,33 @@ func (p *API) GetSubscriptionsFromFile(clientID uuid.UUID) ([]byte, error) { return b, err } -// GetSubscriptions get all subscriptionOne inforamtions -func (p *API) GetSubscriptions(clientID uuid.UUID) (sub map[string]*pubsub.PubSub) { +// GetSubscriptionsFromClientID get all subs from the client +func (p *API) GetSubscriptionsFromClientID(clientID uuid.UUID) (sub map[string]*pubsub.PubSub) { if subs, ok := p.SubscriberStore.Get(clientID); ok { sub = subs.SubStore.Store } - return } -// GetSubscription get subscriptionOne inforamtions -func (p *API) GetSubscription(clientID uuid.UUID, subID string) pubsub.PubSub { +// GetSubscriptions get all subs +func (p *API) GetSubscriptions() ([]byte, error) { + p.SubscriberStore.RLock() + defer p.SubscriberStore.RUnlock() + var allSubs []pubsub.PubSub + for _, s := range p.SubscriberStore.Store { + for _, sub := range s.SubStore.Store { + allSubs = append(allSubs, *sub) + } + } + return json.MarshalIndent(&allSubs, "", " ") +} + +// GetSubscription get sub info from clientID and subID +func (p *API) GetSubscription(clientID uuid.UUID, subID string) (pubsub.PubSub, error) { if subs, ok := p.SubscriberStore.Get(clientID); ok { - return subs.Get(subID) + return subs.Get(subID), nil } - return pubsub.PubSub{} + return pubsub.PubSub{}, fmt.Errorf("subscription data was not found for id %s", subID) } // GetSubscriberURLByResourceAndClientID get subscription information by client id/resource diff --git a/vendor/modules.txt b/vendor/modules.txt index aa28cf3..2222791 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -71,7 +71,7 @@ github.com/prometheus/common/model github.com/prometheus/procfs github.com/prometheus/procfs/internal/fs github.com/prometheus/procfs/internal/util -# github.com/redhat-cne/sdk-go v1.0.1-0.20240702163442-605f629084b9 +# github.com/redhat-cne/sdk-go v1.0.1-0.20240715150244-f435c154a612 ## explicit; go 1.21 github.com/redhat-cne/sdk-go/pkg/channel github.com/redhat-cne/sdk-go/pkg/common