A production-grade Equipment Identity Register (EIR) system implemented in Go following hexagonal (clean) architecture principles.
This EIR system provides:
- Diameter S13 Interface (4G LTE) - ME-Identity-Check operations
- HTTP N5g-eir Interface (5G) - Equipment status query API (3GPP TS 29.511)
- PostgreSQL Storage - Persistent equipment database with audit logging
- Hexagonal Architecture - Clean separation of domain, ports, and adapters
- Production-Ready - Observability, metrics, health checks, and graceful shutdown
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Adapters (Infrastructure) β
ββββββββββββββββββββ¬βββββββββββββββββββ¬ββββββββββββββββββββββββ€
β Diameter S13 β HTTP (5G) β PostgreSQL β
β Gateway β Gateway β Repository β
ββββββββββ¬ββββββββββ΄ββββββββββ¬βββββββββ΄ββββββββββββ¬ββββββββββββ
β β β
βββββββββββββββββββββΌβββββββββββββββββββββ
β
βββββββββββββββββββββΌβββββββββββββββββββββ
β Ports (Interfaces) β
β - EIRService β
β - IMEIRepository β
β - AuditRepository β
βββββββββββββββββββββ¬βββββββββββββββββββββ
β
βββββββββββββββββββββΌβββββββββββββββββββββ
β Domain Layer (Business Logic) β
β - IMEI Validation (Luhn algorithm) β
β - Policy Evaluation β
β - Equipment Status Mapping β
β - Audit Event Generation β
ββββββββββββββββββββββββββββββββββββββββββ
- Business logic is isolated - Domain layer has NO dependencies on infrastructure
- Dependency Inversion - Interfaces (ports) are owned by the domain
- Testability - Each layer can be tested independently
- Flexibility - Easy to swap implementations (e.g., different databases)
eir/
βββ cmd/
β βββ eir/
β βββ main.go # Application entry point
βββ internal/
β βββ domain/ # Domain layer (business logic)
β β βββ models/ # Domain models
β β β βββ equipment.go # Equipment entity, IMEI validation
β β βββ ports/ # Interfaces (owned by domain)
β β β βββ repository.go # Repository interfaces
β β β βββ service.go # Service interfaces
β β βββ service/ # Business logic implementation
β β βββ eir_service.go # Core EIR service
β βββ adapters/ # Infrastructure adapters
β β βββ diameter/ # Diameter S13 adapter
β β β βββ s13_handler.go # S13 message handler
β β β βββ server.go # Diameter server
β β βββ http/ # HTTP adapter (5G N5g-eir)
β β β βββ handler.go # HTTP handlers
β β β βββ models.go # HTTP DTOs
β β β βββ router.go # HTTP routes
β β βββ postgres/ # PostgreSQL adapter
β β βββ db.go # Database connection
β β βββ imei_repository.go
β β βββ audit_repository.go
β β βββ schema.sql # Database schema
β βββ config/ # Configuration management
β β βββ config.go
β βββ observability/ # Logging & metrics
β βββ logger.go
β βββ metrics.go
βββ config/
β βββ config.yaml # Configuration file
βββ deploy/
β βββ prometheus.yml # Prometheus configuration
βββ Dockerfile # Multi-stage Docker build
βββ docker-compose.yaml # Docker Compose setup
βββ Makefile # Build automation
βββ README.md
- Go 1.21+
- PostgreSQL 15+
- Docker & Docker Compose (optional)
# Start all services (PostgreSQL, Redis, EIR, Prometheus, Grafana)
docker-compose up -d
# Check logs
docker-compose logs -f eir
# Stop services
docker-compose downThe services will be available at:
- EIR HTTP API: http://localhost:8080
- Diameter S13: localhost:3868
- Prometheus Metrics: http://localhost:9090
- Grafana Dashboard: http://localhost:3000 (admin/admin)
- Start PostgreSQL:
docker run -d \
--name eir-postgres \
-e POSTGRES_USER=eir \
-e POSTGRES_PASSWORD=eir_password \
-e POSTGRES_DB=eir \
-p 5432:5432 \
postgres:15-alpine- Apply database schema:
make db-migrate
# Or manually:
psql -h localhost -U eir -d eir -f internal/adapters/postgres/schema.sql- Run the application:
make run
# Or:
go run cmd/eir/main.go# Build binary
make build
# Run tests
make test
# Run tests with coverage
make test-coverage
# Format code
make fmt
# Download dependencies
make depsCheck Equipment Status:
curl "http://localhost:8080/n5g-eir-eic/v1/equipment-status?pei=123456789012345"Response:
{
"status": "WHITELISTED"
}Provision Equipment:
curl -X POST http://localhost:8080/api/v1/equipment \
-H "Content-Type: application/json" \
-d '{
"imei": "123456789012345",
"status": "BLACKLISTED",
"reason": "Reported stolen"
}'Get Equipment:
curl http://localhost:8080/api/v1/equipment/123456789012345List Equipment:
curl "http://localhost:8080/api/v1/equipment?offset=0&limit=100"Delete Equipment:
curl -X DELETE http://localhost:8080/api/v1/equipment/123456789012345The Diameter S13 interface listens on port 3868 and supports:
- ME-Identity-Check-Request (MICR) - Command Code 324
- ME-Identity-Check-Answer (MICA) - Returns equipment status
Example flow:
- MME sends MICR with IMEI in Terminal-Information AVP
- EIR validates IMEI and checks database
- EIR returns MICA with Equipment-Status AVP (0=WHITELISTED, 1=BLACKLISTED, 2=GREYLISTED)
- Primary storage for IMEI records
- Indexed by IMEI, status, manufacturer TAC
- Atomic check count increment via stored procedure
- Partitioned by time (quarterly partitions)
- Records all equipment check operations
- Indexed by IMEI, check_time, status
Configuration can be provided via:
- YAML file (
config/config.yaml) - Environment variables (prefixed with
EIR_)
Example environment variables:
export EIR_DATABASE_HOST=localhost
export EIR_DATABASE_PORT=5432
export EIR_DATABASE_USER=eir
export EIR_DATABASE_PASSWORD=eir_password
export EIR_DIAMETER_ORIGINHOST=eir.example.com
export EIR_LOGGING_LEVEL=debugPrometheus metrics exposed at /metrics:
eir_equipment_check_total- Total check requests by source and statuseir_equipment_check_duration_seconds- Check latency histogrameir_database_query_duration_seconds- Database query latencyeir_cache_hit_total- Cache hit/miss countseir_active_diameter_connections- Active Diameter connections
Structured JSON logging with configurable levels:
debug,info,warn,error
curl http://localhost:8080/healthThe system performs strict IMEI validation:
- Length check: 14-16 digits
- Format check: Numeric only
- Luhn algorithm: Validates check digit for 15-digit IMEIs
- TAC extraction: Extracts Type Allocation Code (first 8 digits)
Unknown equipment (not in database) is WHITELISTED by default.
This is a permissive policy. For restrictive policy, modify applyDefaultPolicy() in eir_service.go to return BLACKLISTED or GREYLISTED.
- Database credentials should be stored in secrets management (e.g., Vault, AWS Secrets Manager)
- Enable TLS for Diameter connections in production
- Enable PostgreSQL SSL mode in production
- Use authentication middleware for HTTP provisioning API
- Implement rate limiting for public APIs
Production optimizations:
- Connection pooling (PostgreSQL, Redis)
- Database indexing strategy
- Optional caching layer (Redis)
- Asynchronous audit logging
- Partitioned audit tables for scalability
# Run all tests
make test
# Run with coverage
make test-coverage
# Run specific package
go test -v ./internal/domain/service/Database connection fails:
- Check PostgreSQL is running
- Verify credentials in config
- Check network connectivity
Diameter connection fails:
- Verify port 3868 is not in use
- Check firewall rules
- Verify origin-host/realm configuration
High latency:
- Enable Redis caching
- Review database query performance
- Check connection pool settings
Copyright Β© 2024. All rights reserved.
This is a production system. All changes require:
- Unit tests with >80% coverage
- Integration tests for adapters
- Documentation updates
- Security review for sensitive changes
For issues and questions, contact the Telecom Core Network team.