Search in sources :

Example 1 with PhotonModelAdapterConfig

use of com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig in project photon-model by vmware.

the class ImageEnumerationTaskService method getImageEnumerationAdapterReference.

/**
 * Go to {@link PhotonModelAdaptersRegistryService Service Registry} and get the
 * 'image-enumeration' URI for passed end-point.
 *
 * @return <code>null</code> is returned if 'image-enumeration' adapter is not registered by
 *         passed end-point.
 *
 * @see PhotonModelAdapterConfig
 * @see AdapterTypePath#IMAGE_ENUMERATION_ADAPTER
 */
private DeferredResult<SendImageEnumerationAdapterContext> getImageEnumerationAdapterReference(SendImageEnumerationAdapterContext ctx) {
    // Use 'endpointType' (such as aws, azure) as AdapterConfig id/selfLink!
    String configLink = buildUriPath(PhotonModelAdaptersConfigAccessService.SELF_LINK, ctx.endpointState.endpointType);
    Operation getConfigOp = Operation.createGet(this, configLink);
    return sendWithDeferredResult(getConfigOp, PhotonModelAdapterConfig.class).thenApply(endpointConfig -> {
        // Lookup the 'image-enumeration' URI for passed end-point
        if (endpointConfig.adapterEndpoints != null) {
            String uriStr = endpointConfig.adapterEndpoints.get(AdapterTypePath.IMAGE_ENUMERATION_ADAPTER.key);
            if (uriStr != null && !uriStr.isEmpty()) {
                ctx.adapterRef = URI.create(uriStr);
            }
        }
        logInfo(() -> String.format("[getImageEnumerationAdapterReference] adapterRef = %s: SUCCESS", ctx.adapterRef));
        return ctx;
    });
}
Also used : Operation(com.vmware.xenon.common.Operation) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig)

Example 2 with PhotonModelAdapterConfig

use of com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig in project photon-model by vmware.

the class PhotonModelAdaptersRegistryServiceTest method testCreateDeleteCreate.

@Test
public void testCreateDeleteCreate() {
    PhotonModelAdapterConfig config = getPhotonModelAdapterConfig("testCreateDeleteCreate", "Test CreateDelete Create", "testCreateDeleteCreate.png", AdapterTypePath.ENUMERATION_ADAPTER.key, AdapterTypePath.INSTANCE_ADAPTER.key, AdapterTypePath.ENDPOINT_CONFIG_ADAPTER.key);
    this.logger.info("register: " + config);
    Operation registerOp = super.host.waitForResponse(Operation.createPost(super.host, PhotonModelAdaptersRegistryService.FACTORY_LINK).setBody(config));
    Assert.assertNotNull(registerOp);
    PhotonModelAdapterConfig body = registerOp.getBody(PhotonModelAdapterConfig.class);
    this.logger.info("delete: " + body.documentSelfLink);
    Operation deleteAdapterConfig = super.host.waitForResponse(Operation.createDelete(super.host, body.documentSelfLink));
    Assert.assertEquals(200, deleteAdapterConfig.getStatusCode());
    this.logger.info("get: " + body.documentSelfLink);
    Operation getAdapterConfig = super.host.waitForResponse(Operation.createGet(super.host, body.documentSelfLink));
    Assert.assertEquals(404, getAdapterConfig.getStatusCode());
    {
        this.logger.info("register again: " + config);
        registerOp = super.host.waitForResponse(Operation.createPost(super.host, PhotonModelAdaptersRegistryService.FACTORY_LINK).setBody(config));
        Assert.assertNotNull(registerOp);
        Assert.assertEquals(200, registerOp.getStatusCode());
    }
}
Also used : Operation(com.vmware.xenon.common.Operation) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) Test(org.junit.Test)

Example 3 with PhotonModelAdapterConfig

use of com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig in project photon-model by vmware.

the class EndpointAdapterUtils method registerEndpointAdapters.

/**
 * Register end-point adapters into End-point Adapters Registry.
 *
 * @param host
 *         The host the end-point is running on.
 * @param endpointType
 *         The type of the end-point.
 * @param startedAdapterLinks
 *         The array of started adapter links.
 * @param adapterLinksToRegister
 *         Map of adapter links (to be registered) to their adapter type key. e.g for
 *         standard adapters this is {@link com.vmware.photon.controller.model.UriPaths.AdapterTypePath#key}
 * @param registryLocator
 *         ServiceEndpointLocator containing the host of the adapter registry. Can be null if the
 *         registry is on the same host.
 * @see #handleEndpointRegistration(ServiceHost, EndpointType, Consumer, ServiceEndpointLocator)
 */
public static void registerEndpointAdapters(ServiceHost host, EndpointType endpointType, String[] startedAdapterLinks, Map<String, String> adapterLinksToRegister, ServiceEndpointLocator registryLocator) {
    // Count all adapters - both FAILED and STARTED
    AtomicInteger adaptersCountDown = new AtomicInteger(startedAdapterLinks.length);
    // Keep started adapters only...
    // - key = adapter type ket (e.g. AdapterTypePath.key)
    // - value = adapter URI
    Map<String, String> startedAdapters = new ConcurrentHashMap<>();
    // Wait for all adapter services to start
    host.registerForServiceAvailability((op, ex) -> {
        if (ex != null) {
            String adapterPath = op.getUri().getPath();
            host.log(Level.WARNING, "Starting '%s' adapter [%s]: FAILED - %s", endpointType, adapterPath, Utils.toString(ex));
        } else {
            String adapterPath = op.getUri().getPath();
            host.log(Level.FINE, "Starting '%s' adapter [%s]: SUCCESS", endpointType, adapterPath);
            String adapterKey = adapterLinksToRegister.get(adapterPath);
            if (adapterKey != null) {
                startedAdapters.put(adapterKey, AdapterUriUtil.buildPublicAdapterUri(host, adapterPath).toString());
            }
        }
        if (adaptersCountDown.decrementAndGet() == 0) {
            // Once ALL Adapters are started register them into End-point Adapters Registry
            host.log(Level.INFO, "Starting %d '%s' adapters: SUCCESS", startedAdapters.size(), endpointType);
            // Populate end-point config with started adapters
            Consumer<PhotonModelAdapterConfig> endpointConfigEnhancer = ep -> ep.adapterEndpoints.putAll(startedAdapters);
            // Delegate to core end-point config/registration logic
            handleEndpointRegistration(host, endpointType, endpointConfigEnhancer, registryLocator);
        }
    }, /* this services are not replicated */
    false, startedAdapterLinks);
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) HashMap(java.util.HashMap) ServiceEndpointLocator(com.vmware.photon.controller.model.util.ServiceEndpointLocator) Level(java.util.logging.Level) Utils(com.vmware.xenon.common.Utils) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) INVENTORY_SERVICE(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster.INVENTORY_SERVICE) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE(com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) Consumer(java.util.function.Consumer) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) Stream(java.util.stream.Stream) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils.buildUri(com.vmware.xenon.common.UriUtils.buildUri) Optional(java.util.Optional) PhotonModelAdaptersRegistryService(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig)

Example 4 with PhotonModelAdapterConfig

use of com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig in project photon-model by vmware.

the class PhotonModelAdaptersRegistryServiceTest method testRegisterAdapter.

@Test
public void testRegisterAdapter() throws Exception {
    PhotonModelAdapterConfig config = getPhotonModelAdapterConfig("testRegisterAdapter", "Test Register Adapter", "testRegisterAdapter.png", AdapterTypePath.ENUMERATION_ADAPTER.key, AdapterTypePath.INSTANCE_ADAPTER.key, AdapterTypePath.ENDPOINT_CONFIG_ADAPTER.key);
    Operation registerOp = Operation.createPost(super.host, PhotonModelAdaptersRegistryService.FACTORY_LINK).setBody(config);
    Operation response = super.host.waitForResponse(registerOp);
    Assert.assertNotNull(response);
    this.logger.info("Response: " + response);
    PhotonModelAdapterConfig body = response.getBody(PhotonModelAdapterConfig.class);
    Operation getAdapterConfig = Operation.createGet(super.host, body.documentSelfLink);
    PhotonModelAdapterConfig registered = super.host.waitForResponse(getAdapterConfig).getBody(PhotonModelAdapterConfig.class);
    Assert.assertNotNull(registered);
    Assert.assertEquals(config.name, registered.name);
    Assert.assertEquals(config.customProperties.get(ICON), registered.customProperties.get(ICON));
    Assert.assertEquals(config.adapterEndpoints.get(AdapterTypePath.ENDPOINT_CONFIG_ADAPTER.key), registered.adapterEndpoints.get(AdapterTypePath.ENDPOINT_CONFIG_ADAPTER.key));
}
Also used : Operation(com.vmware.xenon.common.Operation) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) Test(org.junit.Test)

Example 5 with PhotonModelAdapterConfig

use of com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig in project photon-model by vmware.

the class PhotonModelAdaptersRegistryServiceTest method testGetByEndpointType.

@Test
public void testGetByEndpointType() throws Exception {
    String k1 = "k1";
    String k2 = "k2";
    String k3 = "k3";
    PhotonModelAdapterConfig config1 = getPhotonModelAdapterConfig("testGetByEndpointType-1", "Test Get By Endpoint Type 1", "testGetByEndpointType-1.png", k1, k3);
    Operation registerOp1 = Operation.createPost(super.host, PhotonModelAdaptersRegistryService.FACTORY_LINK).setBody(config1);
    super.host.waitForResponse(registerOp1);
    PhotonModelAdapterConfig config2 = getPhotonModelAdapterConfig("testGetByEndpointType-2", "Test Get By Endpoint Type 2", "testGetByEndpointType-2.png", k2, k3);
    Operation registerOp2 = Operation.createPost(super.host, PhotonModelAdaptersRegistryService.FACTORY_LINK).setBody(config2);
    super.host.waitForResponse(registerOp2);
    Query query = Query.Builder.create().addKindFieldClause(PhotonModelAdapterConfig.class).addFieldClause(QuerySpecification.buildCompositeFieldName(PhotonModelAdapterConfig.FIELD_NAME_ADAPTER_ENDPOINTS, k2), "*", MatchType.WILDCARD).build();
    QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).setQuery(query).build();
    Operation queryOp = Operation.createPost(super.host, ServiceUriPaths.CORE_QUERY_TASKS).setReferer(getClass().getSimpleName()).setBody(queryTask);
    Operation response = super.host.waitForResponse(queryOp);
    QueryTask qTask = response.getBody(QueryTask.class);
    Collection<Object> values = qTask.results.documents.values();
    Assert.assertEquals(1, values.size());
    JsonObject k2Config = (JsonObject) values.iterator().next();
    Assert.assertEquals(config2.id, k2Config.getAsJsonPrimitive(PhotonModelAdapterConfig.FIELD_NAME_ID).getAsString());
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) Operation(com.vmware.xenon.common.Operation) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) Test(org.junit.Test)

Aggregations

PhotonModelAdapterConfig (com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig)7 Operation (com.vmware.xenon.common.Operation)6 Query (com.vmware.xenon.services.common.QueryTask.Query)2 URI (java.net.URI)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 JsonObject (com.google.gson.JsonObject)1 EndpointConfigRequest (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest)1 RequestType (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType)1 PhotonModelAdaptersRegistryService (com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService)1 PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE (com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE)1 PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE (com.vmware.photon.controller.model.adapters.util.AdapterConstants.PHOTON_MODEL_ADAPTER_ENDPOINT_NOT_UNIQUE_MESSAGE_CODE)1 EndpointType (com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType)1 QueryTop (com.vmware.photon.controller.model.query.QueryUtils.QueryTop)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)1 PowerState (com.vmware.photon.controller.model.resources.ComputeService.PowerState)1 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)1 ClusterUtil (com.vmware.photon.controller.model.util.ClusterUtil)1 INVENTORY_SERVICE (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster.INVENTORY_SERVICE)1