diff --git a/cmd/adapter/main.go b/cmd/adapter/main.go index 621b24b..a233ea5 100644 --- a/cmd/adapter/main.go +++ b/cmd/adapter/main.go @@ -25,10 +25,10 @@ type Config struct { Log log.Config `yaml:"log"` PluginManager *plugin.ManagerConfig `yaml:"pluginManager"` Modules []module.Config `yaml:"modules"` - HTTP httpConfig `yaml:"http"` // Nest http config + HTTP timeouts `yaml:"http"` // Nest http config } -type httpConfig struct { +type timeouts struct { Port string `yaml:"port"` Timeout timeoutConfig `yaml:"timeout"` } @@ -44,7 +44,7 @@ var runFunc = run func main() { // Define and parse command-line flags. - flag.StringVar(&configPath, "config", "../../config/clientSideHandler-config.yaml", "Path to the configuration file") + flag.StringVar(&configPath, "config", "../../config/onix/adapter.yaml", "Path to the configuration file") flag.Parse() // Use custom log for initial setup messages. diff --git a/cmd/adapter/main_test.go b/cmd/adapter/main_test.go index 3576ca3..14f87e5 100644 --- a/cmd/adapter/main_test.go +++ b/cmd/adapter/main_test.go @@ -73,22 +73,6 @@ func (m *MockPluginManager) SchemaValidator(ctx context.Context, cfg *plugin.Con return nil, nil } -// testConfigContent defines a mock configuration used in tests. -const testConfigContent = ` -appName: "TestApp" -http: - port: "8080" - timeout: - read: 5 - write: 5 - idle: 10 -` - -// initLogger is a mock function to initialize the logger. -var initLogger = func(cfg *Config) error { - return nil -} - // mockRun is a mock implementation of the `run` function, simulating a successful run. func mockRun(ctx context.Context, configPath string) error { return nil // Simulate a successful run @@ -110,7 +94,9 @@ func TestMainFunction(t *testing.T) { fs := flag.NewFlagSet("test", flag.ExitOnError) fs.StringVar(&configPath, "config", "../../config/clientSideHandler-config.yaml", "Path to the configuration file") - fs.Parse(os.Args[1:]) + if err := fs.Parse(os.Args[1:]); err != nil { + t.Fatalf("Failed to parse flags: %v", err) + } main() } @@ -378,7 +364,7 @@ func TestNewServerSuccess(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := &Config{ Modules: tt.modules, - HTTP: httpConfig{ + HTTP: timeouts{ Port: "8080", Timeout: timeoutConfig{ Read: 5, @@ -423,7 +409,7 @@ func TestNewServerFailure(t *testing.T) { t.Run(tt.name, func(t *testing.T) { cfg := &Config{ Modules: tt.modules, - HTTP: httpConfig{ + HTTP: timeouts{ Port: "8080", Timeout: timeoutConfig{ Read: 5, @@ -455,7 +441,7 @@ func TestValidateConfigSuccess(t *testing.T) { name: "Valid Config", cfg: Config{ AppName: "TestApp", - HTTP: httpConfig{ + HTTP: timeouts{ Port: "8080", }, }, @@ -483,7 +469,7 @@ func TestValidateConfigFailure(t *testing.T) { name: "Missing AppName", cfg: Config{ AppName: "", - HTTP: httpConfig{ + HTTP: timeouts{ Port: "8080", }, }, @@ -493,7 +479,7 @@ func TestValidateConfigFailure(t *testing.T) { name: "Missing Port", cfg: Config{ AppName: "TestApp", - HTTP: httpConfig{ + HTTP: timeouts{ Port: "", }, }, diff --git a/config/bap.yaml b/config/bap.yaml new file mode 100644 index 0000000..25c67b2 --- /dev/null +++ b/config/bap.yaml @@ -0,0 +1,69 @@ +appName: "bapAdapter" +log: + level: debug + destinations: + - type: stdout + context_keys: + - transaction_id + - message_id +http: + port: 8080 + timeout: + read: 30 + write: 30 + idle: 30 +plugin: + root: /app/plugins + pluginZipPath: /mnt/gcs/plugins/plugins_bundle.zip + plugins: + - publisher Src version raw comp zip + - nopschemavalidator + - router + - nopsigner + - nopsignvalidator + - reqpreprocessor + - gcpAuthMdw +modules: + - name: reciever + type: transaction + path: /reciever + targetType: msgQ + plugin: + schemaValidator: + id: nopschemavalidator + signValidator: + id: nopsignvalidator + publisher: + id: publisher + config: + project: ondc-seller-dev + topic: bapNetworkReciever + router: + id: router + config: + routingConfigPath: /mnt/gcs/configs/bapRecieverRouting-config.yaml + preProcessors: + - id: reqpreprocessor + steps: + steps: + - addRoute + signValidate + -addRout + customValidate + - name: transactionCaller + path: /caller + targetType: "http" + plugin: + signer: + id: nopsigner + router: + id: router + config: + routingConfigPath: /mnt/gcs/configs/bapCallerRouting-config.yaml + preProcessors: + - id: reqpreprocessor + # postProcessors: + # - id: gcpAuthMdw + # config: + # audience: https://bpp-adapter-903496459467.asia-southeast1.run.app + # serviceAccount: 903496459467-compute@developer.gserviceaccount.com diff --git a/config/bapCallerRouting-config.yaml b/config/bapCallerRouting-config.yaml new file mode 100644 index 0000000..a6702ea --- /dev/null +++ b/config/bapCallerRouting-config.yaml @@ -0,0 +1,3 @@ +routes: + - action: search + target: https://bpp-adapter-903496459467.asia-southeast1.run.app/reciever \ No newline at end of file diff --git a/config/bpp-local.yaml b/config/bpp-local.yaml new file mode 100644 index 0000000..5af87f4 --- /dev/null +++ b/config/bpp-local.yaml @@ -0,0 +1,63 @@ +appName: "bppClientService" +log: + level: debug + destinations: + - type: stdout + context_keys: + - transaction_id + - message_id +http: + port: 8080 + timeout: + read: 30 + write: 30 + idle: 30 +plugin: + root: extracted/plugins + pluginZipPath: plugins_bundle.zip + plugins: + - publisher + - nopschemavalidator + - router + - nopsigner + - nopsignvalidator + - reqpreprocessor + - gcpAuthMdw +module: + modules: + - name: transactionReciever + path: /reciever + targetType: msgQ + plugin: + schemaValidator: + id: nopschemavalidator + signValidator: + id: nopsignValidator + publisher: + id: publisher + config: + project: ondc-seller-dev + topic: clientSideTopic + Router: + id: router + config: + routingConfigPath: configs/bppRecieverRouting-config.yaml + preProcessors: + - id: reqpreprocessor + - name: transactionCaller + path: /caller + targetType: "http" + plugin: + signer: + id: nopsigner + Router: + id: router + config: + routingConfigPath: configs/bppCallerRouting-config.yaml + preProcessors: + - id: reqpreprocessor + postProcessors: + - id: gcpAuthMdw + config: + audience: "target" + diff --git a/config/bpp.yaml b/config/bpp.yaml new file mode 100644 index 0000000..a4d9a65 --- /dev/null +++ b/config/bpp.yaml @@ -0,0 +1,63 @@ +appName: "bppClientService" +log: + level: debug + destinations: + - type: stdout + context_keys: + - transaction_id + - message_id +http: + port: 8080 + timeout: + read: 30 + write: 30 + idle: 30 +plugin: + root: /app/plugins + pluginZipPath: /mnt/gcs/plugins/plugins_bundle.zip + plugins: + - publisher + - nopschemavalidator + - router + - nopsigner + - nopsignvalidator + - reqpreprocessor + - gcpAuthMdw +module: + modules: + - name: transactionReciever + path: /reciever + targetType: msgQ + plugin: + schemaValidator: + id: nopschemavalidator + signValidator: + id: nopsignvalidator + publisher: + id: publisher + config: + project: ondc-seller-dev + topic: bppNetworkReciever + router: + id: router + config: + routingConfigPath: /mnt/gcs/configs/bppRecieverRouting-config.yaml + preProcessors: + - id: reqpreprocessor + - name: transactionCaller + path: /caller + targetType: "http" + plugin: + signer: + id: nopsigner + router: + id: router + config: + routingConfigPath: /mnt/gcs/configs/bppCallerRouting-config.yaml + preProcessors: + - id: reqpreprocessor + # postProcessors: + # - id: gcpAuthMdw + # config: + # audience: https://bap-adapter-903496459467.asia-southeast1.run.app + # serviceAccount: 903496459467-compute@developer.gserviceaccount.com diff --git a/config/bppCallerRouting-config.yaml b/config/bppCallerRouting-config.yaml new file mode 100644 index 0000000..9aa5e39 --- /dev/null +++ b/config/bppCallerRouting-config.yaml @@ -0,0 +1,3 @@ +routes: + - action: on_search + target: targeturl \ No newline at end of file diff --git a/config/bppRecieverRouting-config.yaml b/config/bppRecieverRouting-config.yaml new file mode 100644 index 0000000..15fdb33 --- /dev/null +++ b/config/bppRecieverRouting-config.yaml @@ -0,0 +1,3 @@ +routes: + - action: search + target: https://sellerapp-903496459467.asia-southeast1.run.app \ No newline at end of file diff --git a/config/byuerApp-config.yaml b/config/byuerApp-config.yaml new file mode 100644 index 0000000..a441371 --- /dev/null +++ b/config/byuerApp-config.yaml @@ -0,0 +1,2 @@ +bap_url: "https://bap-csr-903496459467.asia-southeast1.run.app" # Replace with actual Beckn API endpoint +port: "8080" # The port on which the server will run \ No newline at end of file diff --git a/config/sellerData.yaml b/config/sellerData.yaml new file mode 100644 index 0000000..9a96fdf --- /dev/null +++ b/config/sellerData.yaml @@ -0,0 +1,69 @@ +- id: pooja-stores + descriptor: + name: Pooja Stores + locations: + - id: koramangala-4th-block-location + gps: "12.9349377,77.6055586" + categories: + - id: fresh_fruits + descriptor: + name: Fresh Fruits + - id: beverages + descriptor: + name: Beverages + items: + - id: item_2 + descriptor: + name: Green Apples Organic + images: + - url: "https://mock_bpp.com/images/item_2.jpg" + category_id: fresh_fruits + location_id: koramangala-4th-block-location + price: + currency: INR + value: "170" + matched: true + - id: item_1 + descriptor: + name: Red Apples + images: + - url: "https://mock_bpp.com/images/item_1.jpg" + category_id: fresh_fruits + location_id: koramangala-4th-block-location + price: + currency: INR + value: "90" + related: true + - id: item_7 + descriptor: + name: Green Apple Juice + images: + - url: "https://mock_bpp.com/images/item_7.jpg" + category_id: beverages + location_id: koramangala-4th-block-location + price: + currency: INR + value: "70" + matched: true +- id: food-mall + descriptor: + name: Food Mall + locations: + - id: food-mall-location + gps: "12.9349377,77.6055586" + categories: + - id: fresh-food + descriptor: + name: Fresh food + items: + - id: item_1_1 + descriptor: + name: Green Apple Salad + images: + - url: "https://mock_bpp.com/images/item_1_1.jpg" + category_id: fresh-food + location_id: food-mall-location + price: + currency: INR + value: "200" + matched: true diff --git a/core/module/handler/config.go b/core/module/handler/config.go index 08d90a3..209de7f 100644 --- a/core/module/handler/config.go +++ b/core/module/handler/config.go @@ -5,7 +5,6 @@ import ( "fmt" "net/http" - "github.com/beckn/beckn-onix/pkg/log" "github.com/beckn/beckn-onix/pkg/model" "github.com/beckn/beckn-onix/pkg/plugin" "github.com/beckn/beckn-onix/pkg/plugin/definition" @@ -25,24 +24,12 @@ type PluginManager interface { SchemaValidator(ctx context.Context, cfg *plugin.Config) (definition.SchemaValidator, error) } -// Provider represents a function that initializes an HTTP handler using a PluginManager. -type Provider func(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error) - // Type defines different handler types for processing requests. type Type string const ( // HandlerTypeStd represents the standard handler type used for general request processing. HandlerTypeStd Type = "std" - - // HandlerTypeRegSub represents the registry subscriber handler type for handling registry subscription requests. - HandlerTypeRegSub Type = "regSub" - - // HandlerTypeNPSub represents the network participant subscriber handler type for handling network participant subscription requests. - HandlerTypeNPSub Type = "npSub" - - // HandlerTypeLookup represents the lookup handler type used for resolving service details. - HandlerTypeLookup Type = "lookUp" ) // PluginCfg holds the configuration for various plugins. @@ -108,13 +95,3 @@ func (s *Step) UnmarshalYAML(unmarshal func(interface{}) error) error { *s = step return nil } - -// DummyHandler is a basic HTTP handler that returns a fixed response. -func DummyHandler(ctx context.Context, mgr PluginManager, cfg *Config) (http.Handler, error) { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.WriteHeader(http.StatusOK) - if _, err := w.Write([]byte("Dummy Handler Response")); err != nil { - log.Error(context.Background(), err, "failed to write nack response") - } - }), nil -} diff --git a/core/module/handler/stdHandler.go b/core/module/handler/stdHandler.go index e0f999b..08e9628 100644 --- a/core/module/handler/stdHandler.go +++ b/core/module/handler/stdHandler.go @@ -230,35 +230,6 @@ func (h *stdHandler) initSteps(ctx context.Context, mgr PluginManager, cfg *Conf steps[c.ID] = step } - // Register processing steps - for _, step := range cfg.Steps { - var s definition.Step - var err error - - switch step { - case "sign": - s, err = newSignStep(h.signer, h.km) - case "validateSign": - s, err = newValidateSignStep(h.signValidator, h.km) - case "validateSchema": - s, err = newValidateSchemaStep(h.schemaValidator) - case "addRoute": - s, err = newRouteStep(h.router) - case "broadcast": - s = &broadcastStep{} - default: - if customStep, exists := steps[step]; exists { - s = customStep - } else { - return fmt.Errorf("unrecognized step: %s", step) - } - } - - if err != nil { - return err - } - h.steps = append(h.steps, s) - } log.Infof(ctx, "Processor steps initialized: %v", cfg.Steps) return nil } diff --git a/core/module/handler/step.go b/core/module/handler/step.go index bcd185d..bbe01fd 100644 --- a/core/module/handler/step.go +++ b/core/module/handler/step.go @@ -155,12 +155,3 @@ func (s *addRouteStep) Run(ctx *model.StepContext) error { log.Debugf(ctx, "ctx.Route to %#v", ctx.Route) return nil } - -// broadcastStep is a stub for broadcasting. -type broadcastStep struct{} - -// Run is a placeholder for future implementation. -func (b *broadcastStep) Run(ctx *model.StepContext) error { - // TODO: Implement broadcast logic if needed - return nil -} diff --git a/core/module/module.go b/core/module/module.go index 9313712..3b0fcef 100644 --- a/core/module/module.go +++ b/core/module/module.go @@ -16,33 +16,22 @@ type Config struct { Handler handler.Config } +// Provider represents a function that initializes an HTTP handler using a PluginManager. +type Provider func(ctx context.Context, mgr handler.PluginManager, cfg *handler.Config) (http.Handler, error) + // handlerProviders maintains a mapping of handler types to their respective providers. -var handlerProviders = map[handler.Type]handler.Provider{ +var handlerProviders = map[handler.Type]Provider{ handler.HandlerTypeStd: handler.NewStdHandler, } -// GetDummyHandlerProviders returns a dummy handler provider mapping for testing purposes. -func GetDummyHandlerProviders() map[handler.Type]handler.Provider { - return map[handler.Type]handler.Provider{ - handler.HandlerTypeStd: handler.DummyHandler, - } -} - -// getHandlerProviders is a function to retrieve handler providers, which can be overridden for testing. -var getHandlerProviders = func() map[handler.Type]handler.Provider { - return handlerProviders -} - // Register initializes and registers handlers based on the provided configuration. // It iterates over the module configurations, retrieves appropriate handler providers, // and registers the handlers with the HTTP multiplexer. func Register(ctx context.Context, mCfgs []Config, mux *http.ServeMux, mgr handler.PluginManager) error { log.Debugf(ctx, "Registering modules with config: %#v", mCfgs) - - providers := getHandlerProviders() // Iterate over the handlers in the configuration. for _, c := range mCfgs { - rmp, ok := providers[c.Handler.Type] + rmp, ok := handlerProviders[c.Handler.Type] if !ok { return fmt.Errorf("invalid module : %s", c.Name) }