Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 3 additions & 24 deletions examples/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"

"github.com/eclipse-dataplane-core/dataplane-sdk-go/pkg/dsdk"
"github.com/go-chi/chi/v5"
)

const (
Expand All @@ -36,29 +35,9 @@ type TokenResponse struct {
Endpoint string `json:"url"`
}

// NewSignalingServer creates and returns a new HTTP server configured with dataplane signaling endpoints.
func NewSignalingServer(sdkApi *dsdk.DataPlaneApi, port int) *http.Server {
r := chi.NewRouter()
r.Post("/dataflows/start", sdkApi.Start)
r.Post("/dataflows/{id}/started", func(writer http.ResponseWriter, request *http.Request) {
id := chi.URLParam(request, "id")
sdkApi.StartById(writer, request, id)
})
r.Post("/dataflows/prepare", sdkApi.Prepare)
r.Post("/dataflows/{id}/terminate", func(writer http.ResponseWriter, request *http.Request) {
id := chi.URLParam(request, "id")
sdkApi.Terminate(id, writer, request)
})
r.Post("/dataflows/{id}/suspend", func(writer http.ResponseWriter, request *http.Request) {
id := chi.URLParam(request, "id")
sdkApi.Suspend(id, writer, request)
})
r.Get("/dataflows/{id}/status", func(writer http.ResponseWriter, request *http.Request) {
id := chi.URLParam(request, "id")
sdkApi.Status(id, writer, request)
})

return &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: r}
// NewSignalingServer creates and returns a new HTTP server serving the data plane signaling endpoints under /v1.
func NewSignalingServer(sdk *dsdk.DataPlaneSDK, port int) *http.Server {
return &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: dsdk.NewHandler(sdk)}
}

// NewDataServer creates and initializes a new HTTP server with a specified port and request handler.
Expand Down
229 changes: 71 additions & 158 deletions examples/controlplane/controlplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

"github.com/eclipse-dataplane-core/dataplane-sdk-go/examples/common"
Expand All @@ -27,216 +27,129 @@ import (
)

const (
startUrl = "http://localhost:%d/dataflows/start"
terminateUrl = "http://localhost:%d/dataflows/%s/terminate"
consumerPrepareURL = "http://localhost:%d/dataflows/prepare"
providerCallbackURL = "http://provider.com/dp/callback"
contentType = "Content-Type"
jsonContentType = "application/json"
signalingUrl = "http://localhost:%d" + dsdk.DefaultBasePath + "/dataflows"
contentType = "Content-Type"
jsonContentType = "application/json"
consumerID = "did:web:consumer.com"
providerID = "did:web:provider.com"
)

// ControlPlaneSimulator simulates control plane interactions between a consumer and provider and drives their respective data planes.
type ControlPlaneSimulator struct {
consumerDataPlane string
providerDataPlane string
client *http.Client
}

func NewSimulator() (*ControlPlaneSimulator, error) {
return &ControlPlaneSimulator{}, nil
return &ControlPlaneSimulator{client: &http.Client{Timeout: 30 * time.Second}}, nil
}

// ProviderStart sends a start message to the provider data plane. For push transfers da is the consumer's DataAddress;
// for pull transfers it must be nil and the provider's DataAddress is returned.
func (c *ControlPlaneSimulator) ProviderStart(ctx context.Context,
processID string,
dataFlowID string,
agreementID string,
datasetID string,
profile string,
da *dsdk.DataAddress) (*dsdk.DataAddress, error) {
callbackURL, _ := url.Parse(providerCallbackURL)

startMessage := dsdk.DataFlowStartMessage{
DataFlowBaseMessage: dsdk.DataFlowBaseMessage{
MessageID: uuid.NewString(),
AgreementID: agreementID,
DatasetID: datasetID,
ProcessID: processID,
DataspaceContext: "dscontext",
CounterPartyID: "did:web:consumer.com",
ParticipantID: "did:web:provider.com",
CallbackAddress: dsdk.CallbackURL(*callbackURL),
TransferType: dsdk.TransferType{DestinationType: "custom", FlowType: dsdk.Pull},
DataAddress: da,
},
DataFlowBaseMessage: newBaseMessage(dataFlowID, agreementID, datasetID, profile, providerID, consumerID),
DataAddress: da,
}

serialized, err := json.Marshal(startMessage)
if err != nil {
return nil, fmt.Errorf("failed to marshal start message: %w", err)
}

// Create the request
providerSignalingUrl := fmt.Sprintf(startUrl, common.ProviderSignalingPort)
req, err := http.NewRequestWithContext(ctx, "POST", providerSignalingUrl, bytes.NewBuffer(serialized))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set(contentType, jsonContentType)

client := &http.Client{
Timeout: 30 * time.Second,
}

resp, err := client.Do(req)
status, err := c.send(ctx, fmt.Sprintf(signalingUrl+"/start", common.ProviderSignalingPort), startMessage)
if err != nil {
return nil, fmt.Errorf("start request failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var message dsdk.DataFlowResponseMessage
if err := json.NewDecoder(resp.Body).Decode(&message); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return message.DataAddress, nil
return status.DataAddress, nil
}

func (c *ControlPlaneSimulator) ConsumerStart(ctx context.Context, processID string, source *dsdk.DataAddress) error {
callbackURL, _ := url.Parse(providerCallbackURL)
startMessage := dsdk.DataFlowStartMessage{
DataFlowBaseMessage: dsdk.DataFlowBaseMessage{
MessageID: uuid.NewString(),
ProcessID: processID,
AgreementID: uuid.NewString(),
DataspaceContext: "dscontext",
ParticipantID: "did:web:consumer.com",
CounterPartyID: "did:web:provider.com",
CallbackAddress: dsdk.CallbackURL(*callbackURL),
TransferType: dsdk.TransferType{DestinationType: "custom", FlowType: dsdk.Pull},
DataAddress: source,
},
}

serialized, err := json.Marshal(startMessage)
if err != nil {
return fmt.Errorf("failed to marshal start message: %w", err)
// ConsumerPrepare sends a prepare message to the consumer data plane. For push transfers the consumer's DataAddress
// is returned.
func (c *ControlPlaneSimulator) ConsumerPrepare(ctx context.Context, dataFlowID string, agreementID string, datasetID string, profile string) (*dsdk.DataAddress, error) {
prepareMessage := dsdk.DataFlowPrepareMessage{
DataFlowBaseMessage: newBaseMessage(dataFlowID, agreementID, datasetID, profile, consumerID, providerID),
}

// Create the request
consumerSignalingUrl := fmt.Sprintf(startUrl, common.ConsumerSignalingPort)
req, err := http.NewRequestWithContext(ctx, "POST", consumerSignalingUrl, bytes.NewBuffer(serialized))
status, err := c.send(ctx, fmt.Sprintf(signalingUrl+"/prepare", common.ConsumerSignalingPort), prepareMessage)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
return nil, fmt.Errorf("prepare request failed: %w", err)
}
return status.DataAddress, nil
}

req.Header.Set(contentType, jsonContentType)

client := &http.Client{
Timeout: 30 * time.Second,
// ConsumerStarted notifies the consumer data plane that the provider started the transfer. For pull transfers source
// is the provider's DataAddress; for push transfers it must be nil.
func (c *ControlPlaneSimulator) ConsumerStarted(ctx context.Context, dataFlowID string, source *dsdk.DataAddress) error {
message := dsdk.DataFlowStartedNotificationMessage{
MessageID: uuid.NewString(),
DataAddress: source,
}

resp, err := client.Do(req)
_, err := c.send(ctx, fmt.Sprintf(signalingUrl+"/%s/started", common.ConsumerSignalingPort, dataFlowID), message)
if err != nil {
return fmt.Errorf("start request failed: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var message dsdk.DataFlowResponseMessage
if err := json.NewDecoder(resp.Body).Decode(&message); err != nil {
return fmt.Errorf("failed to decode response: %w", err)
return fmt.Errorf("started request failed: %w", err)
}

return nil
}

func (c *ControlPlaneSimulator) ConsumerPrepare(ctx context.Context, processID string, agreementID string, datasetID string) (*dsdk.DataAddress, error) {
callbackURL, _ := url.Parse(providerCallbackURL)
prepareMessage := dsdk.DataFlowPrepareMessage{
DataFlowBaseMessage: dsdk.DataFlowBaseMessage{
MessageID: uuid.NewString(),
AgreementID: agreementID,
DatasetID: datasetID,
ProcessID: processID,
DataspaceContext: "dscontext",
ParticipantID: "did:web:consumer.com",
CounterPartyID: "did:web:provider.com",
CallbackAddress: dsdk.CallbackURL(*callbackURL),
TransferType: dsdk.TransferType{DestinationType: "custom", FlowType: dsdk.Pull},
},
}

serialized, err := json.Marshal(prepareMessage)
if err != nil {
return nil, fmt.Errorf("failed to marshal prepare message: %w", err)
}

// Create the request
consumerSignalingUrl := fmt.Sprintf(consumerPrepareURL, common.ConsumerSignalingPort)
req, err := http.NewRequestWithContext(ctx, "POST", consumerSignalingUrl, bytes.NewBuffer(serialized))
func (c *ControlPlaneSimulator) ProviderTerminate(ctx context.Context, dataFlowID string) error {
terminateMessage := dsdk.DataFlowTerminateMessage{MessageID: uuid.NewString(), Reason: "violation"}
resp, err := c.post(ctx, fmt.Sprintf(signalingUrl+"/%s/terminate", common.ProviderSignalingPort, dataFlowID), terminateMessage)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
return fmt.Errorf("terminate request failed: %w", err)
}
_ = resp.Body.Close()
return nil
}

req.Header.Set(contentType, jsonContentType)

client := &http.Client{
Timeout: 30 * time.Second,
func newBaseMessage(dataFlowID, agreementID, datasetID, profile, participantID, counterPartyID string) dsdk.DataFlowBaseMessage {
return dsdk.DataFlowBaseMessage{
MessageID: uuid.NewString(),
ParticipantID: participantID,
CounterPartyID: counterPartyID,
DataspaceContext: "dscontext",
DataFlowID: dataFlowID,
AgreementID: agreementID,
DatasetID: datasetID,
Profile: profile,
Claims: map[string]any{"membership": "active"},
}
}

resp, err := client.Do(req)
// send posts the message and decodes the DataFlowStatusMessage of a 200 response.
func (c *ControlPlaneSimulator) send(ctx context.Context, url string, message any) (*dsdk.DataFlowStatusMessage, error) {
resp, err := c.post(ctx, url, message)
if err != nil {
return nil, fmt.Errorf("prepare request failed: %w", err)
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var message dsdk.DataFlowResponseMessage
if err := json.NewDecoder(resp.Body).Decode(&message); err != nil {
var status dsdk.DataFlowStatusMessage
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return message.DataAddress, nil
return &status, nil
}

func (c *ControlPlaneSimulator) ProviderTerminate(ctx context.Context, processID string, agreementID string, datasetID string) error {
terminateMessage := dsdk.DataFlowTransitionMessage{Reason: "violation"}

serialized, err := json.Marshal(terminateMessage)
// post sends the message and returns the response if the data plane answered with 200 OK.
func (c *ControlPlaneSimulator) post(ctx context.Context, url string, message any) (*http.Response, error) {
serialized, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("failed to marshal terminate message: %w", err)
return nil, fmt.Errorf("failed to marshal message: %w", err)
}

// Create the request
providerSignalingUrl := fmt.Sprintf(terminateUrl, common.ProviderSignalingPort, processID)
req, err := http.NewRequestWithContext(ctx, "POST", providerSignalingUrl, bytes.NewBuffer(serialized))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(serialized))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set(contentType, jsonContentType)

client := &http.Client{
Timeout: 30 * time.Second,
}

resp, err := client.Do(req)
resp, err := c.client.Do(req)
if err != nil {
return fmt.Errorf("start request failed: %w", err)
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
body, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
return nil, fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, body)
}

return nil
return resp, nil
}
14 changes: 9 additions & 5 deletions examples/natsservices/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ package natsservices

const (
NATSEndpointType = "https://test.org/natsdp/v1/nats"
TokenKey = "token"
ForwardSuffix = "forward"
ReplySuffix = "reply"
ChannelKey = "channel"
ReplyChannelKey = "replyChannel"
// PullProfile and PushProfile are the transfer profiles of the streaming samples; the SDK derives the transfer
// type from the last '-' token.
PullProfile = "https://test.org/natsdp/v1/nats-pull"
PushProfile = "https://test.org/natsdp/v1/nats-push"
TokenKey = "token"
ForwardSuffix = "forward"
ReplySuffix = "reply"
ChannelKey = "channel"
ReplyChannelKey = "replyChannel"
)
Loading
Loading