Commit 13d2023b authored by Mudassar Khan's avatar Mudassar Khan
Browse files

Register CCF as MEC011 service for CAPIF API discovery

Expose the CAPIF Register API (createUser currently) as a MEC service
in MEC011, enabling MEC apps to discover the CCF and access published
APIs.
parent 5670f273
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -209,7 +209,11 @@ func Run() (err error) {
	go func() {
		if err := capifClient.Init(); err != nil {
			log.Warn("CAPIF client init failed: ", err.Error())
			return
		}
		// Register CAPIF as a MEC service once onboarded
		cfg := capifClient.GetConfig()
		sm.RegisterCapifService(cfg.CapifHostname, cfg.RegisterPort)
	}()

	// Start Swagger API Manager (provider)
+5 −0
Original line number Diff line number Diff line
@@ -648,3 +648,8 @@ func (c *CapifClient) GetAefId() string {
	defer c.mu.RUnlock()
	return c.aefId
}

// GetConfig returns the CAPIF configuration
func (c *CapifClient) GetConfig() CapifConfig {
	return c.config
}
+79 −0
Original line number Diff line number Diff line
@@ -1805,6 +1805,85 @@ func deleteCapifMapping(localId string) {

// UnpublishAllServices unpublishes all CAPIF-published services and clears their mappings.
// Called during scenario termination / service shutdown.
// RegisterCapifService registers the CAPIF Register API as a MEC service in MEC011.
// capifHostname is CAPIF_HOSTNAME and capifRegisterPort is CAPIF_REGISTER_PORT.
func RegisterCapifService(capifHostname string, capifRegisterPort string) {
	if capifClient == nil || !capifClient.IsReady() {
		log.Warn("Cannot register CAPIF service: CAPIF client not ready")
		return
	}
	//TODO: Append all of the endpoints which are required for the CCF discovery process, currently only the createUser endpoint is added for simplicity.
	createUserUrl := "https://" + capifHostname + ":" + capifRegisterPort + "/createUser"
	state := ACTIVE_ServiceState
	tType := REST_HTTP_TransportType
	serializer := JSON
	locality := MEC_SYSTEM_LocalityType

	sInfo := &ServiceInfo{
		SerInstanceId: uuid.New().String(),
		SerName:       "capif-register-" + sandboxName,
		SerCategory: &CategoryRef{
			Href:    "catalogueHref",
			Id:      "capifId",
			Name:    "CAPIF",
			Version: "v3",
		},
		Version: "3.0.0",
		State:   &state,
		TransportInfo: &TransportInfo{
			Id:       "capifTransport",
			Name:     "REST",
			Type_:    &tType,
			Protocol: "HTTP",
			Version:  "2.0",
			Endpoint: &OneOfTransportInfoEndpoint{
				EndPointInfoUris: EndPointInfoUris{
					Uris: []string{createUserUrl},
				},
			},
			Security: &SecurityInfo{},
		},
		Serializer:      &serializer,
		ScopeOfLocality: &locality,
		ConsumedLocalOnly: true,
		IsLocal:           true,
	}
	sInfo.Links = &ServiceInfoLinks{
		Self: &LinkType{
			Href: hostUrl.String() + basePath + "applications/capif-register/services/" + sInfo.SerInstanceId,
		},
	}

	// Use a dedicated appId for CAPIF services
	appId := "capif-register"
	err, _ := setService(appId, sInfo, ADDED_ServiceAvailabilityNotificationChangeType)
	if err != nil {
		log.Error("Failed to register CAPIF service in MEC011: ", err.Error())
		return
	}
	log.Info("CAPIF Register service registered in MEC011: ", createUserUrl)

	// Also publish to CAPIF
	if capifClient.IsReady() {
		go func() {
			apiId, err := capifClient.PublishServiceAPI(
				"capif-register-"+sandboxName,
				"1.0.0",
				"/createUser",
				"CAPIF Register",
				"CAPIF Register API. To discover services on CCF: 1) Create user via POST /createUser 2) Get auth for newly created user via GET /getauth 3) Onboard invoker via POST on ccf_onboarding_url 4) Discover APIs via GET on ccf_discover_url",
				capifHostname,
				func() int { p, _ := strconv.Atoi(capifRegisterPort); return p }(),
			)
			if err != nil {
				log.Error("CAPIF publish failed for CAPIF register service: ", err.Error())
				return
			}
			storeCapifMapping(sInfo.SerInstanceId, apiId)
		}()
	}
}

func UnpublishAllServices() {
	if capifClient == nil || !capifClient.IsReady() {
		return