Search in sources :

Example 1 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class AWSRebootServiceTest method createTaskResultListener.

private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
    StatelessService service = new StatelessService() {

        @Override
        public void handleRequest(Operation update) {
            if (!h.apply(update)) {
                super.handleRequest(update);
            }
        }
    };
    Operation startOp = Operation.createPost(host, taskLink).setCompletion(this.host.getCompletion()).setReferer(this.host.getReferer());
    this.host.startService(startOp, service);
}
Also used : StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Example 2 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class AWSPowerServiceTest method createTaskResultListener.

private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
    StatelessService service = new StatelessService() {

        @Override
        public void handleRequest(Operation update) {
            if (!h.apply(update)) {
                super.handleRequest(update);
            }
        }
    };
    TestContext ctx = this.host.testCreate(1);
    Operation startOp = Operation.createPost(host, taskLink).setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    }).setReferer(this.host.getReferer());
    this.host.startService(startOp, service);
    ctx.await();
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) TestAWSSetupUtils.tearDownTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.tearDownTestVpc) TestAWSSetupUtils.setUpTestVpc(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.setUpTestVpc) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Map(java.util.Map) After(org.junit.After) TestUtils.getExecutor(com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getExecutor) EnumSet(java.util.EnumSet) AwsNicSpecs(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.AwsNicSpecs) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) TestAWSSetupUtils.waitForProvisioningToComplete(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.waitForProvisioningToComplete) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) UUID(java.util.UUID) UriUtils(com.vmware.xenon.common.UriUtils) Action(com.vmware.xenon.common.Service.Action) TaskState(com.vmware.xenon.common.TaskState) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) PRIVATE_KEY_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEY_KEY) ResourceRemovalTaskService(com.vmware.photon.controller.model.tasks.ResourceRemovalTaskService) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) HashMap(java.util.HashMap) PRIVATE_KEYID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.PRIVATE_KEYID_KEY) TestAWSSetupUtils.waitForInstancesToBeStopped(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.waitForInstancesToBeStopped) TestAWSSetupUtils.zoneId(com.vmware.photon.controller.model.adapters.awsadapter.TestAWSSetupUtils.zoneId) Function(java.util.function.Function) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) ArrayList(java.util.ArrayList) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceRemovalTaskState(com.vmware.photon.controller.model.tasks.ResourceRemovalTaskService.ResourceRemovalTaskState) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) BasicReusableHostTestCase(com.vmware.xenon.common.BasicReusableHostTestCase) Before(org.junit.Before) ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) REGION_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.REGION_KEY) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) Assert.assertEquals(org.junit.Assert.assertEquals) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StatelessService(com.vmware.xenon.common.StatelessService) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 3 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class TestAzureProvisionTask method issueStatsRequest.

private void issueStatsRequest(ComputeState vm) throws Throwable {
    // spin up a stateless service that acts as the parent link to patch back to
    StatelessService parentService = new StatelessService() {

        @Override
        public void handleRequest(Operation op) {
            if (op.getAction() == Action.PATCH) {
                if (!TestAzureProvisionTask.this.isMock) {
                    ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
                    if (resp.statsList.size() != 1) {
                        TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
                        return;
                    }
                    if (resp.statsList.get(0).statValues.size() == 0) {
                        TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
                        return;
                    }
                    if (!resp.statsList.get(0).computeLink.equals(vm.documentSelfLink)) {
                        TestAzureProvisionTask.this.host.failIteration(new IllegalStateException("Incorrect computeReference returned."));
                        return;
                    }
                }
                TestAzureProvisionTask.this.host.completeIteration();
            }
        }
    };
    String servicePath = UUID.randomUUID().toString();
    Operation startOp = Operation.createPost(UriUtils.buildUri(getHost(), servicePath));
    getHost().startService(startOp, parentService);
    ComputeStatsRequest statsRequest = new ComputeStatsRequest();
    statsRequest.resourceReference = UriUtils.buildUri(getHost(), vm.documentSelfLink);
    statsRequest.isMockRequest = this.isMock;
    statsRequest.taskReference = UriUtils.buildUri(getHost(), servicePath);
    getHost().sendAndWait(Operation.createPatch(UriUtils.buildUri(getHost(), AzureUriPaths.AZURE_STATS_ADAPTER)).setBody(statsRequest).setReferer(getHost().getUri()));
}
Also used : ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation)

Example 4 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class TestAzureEnumerationTask method issueStatsRequest.

private void issueStatsRequest(String selfLink, boolean isComputeHost) throws Throwable {
    // spin up a stateless service that acts as the parent link to patch back to
    StatelessService parentService = new StatelessService() {

        @Override
        public void handleRequest(Operation op) {
            if (op.getAction() == Action.PATCH) {
                if (!TestAzureEnumerationTask.this.isMock) {
                    ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
                    if (resp == null) {
                        TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response was null."));
                        return;
                    }
                    if (resp.statsList.size() != 1) {
                        TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
                        return;
                    }
                    if (resp.statsList.get(0).statValues.size() == 0) {
                        TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
                        return;
                    }
                    if (!resp.statsList.get(0).computeLink.equals(selfLink)) {
                        TestAzureEnumerationTask.this.host.failIteration(new IllegalStateException("Incorrect resourceReference returned."));
                        return;
                    }
                    // Verify all the stats are obtained
                    verifyStats(resp, isComputeHost);
                    // Persist stats on Verification Host for testing the computeHost stats.
                    URI persistStatsUri = UriUtils.buildUri(getHost(), ResourceMetricsService.FACTORY_LINK);
                    ResourceMetricsService.ResourceMetrics resourceMetric = new ResourceMetricsService.ResourceMetrics();
                    resourceMetric.documentSelfLink = StatsUtil.getMetricKey(selfLink, Utils.getNowMicrosUtc());
                    resourceMetric.entries = new HashMap<>();
                    resourceMetric.timestampMicrosUtc = Utils.getNowMicrosUtc();
                    for (String key : resp.statsList.get(0).statValues.keySet()) {
                        List<ServiceStat> stats = resp.statsList.get(0).statValues.get(key);
                        for (ServiceStat stat : stats) {
                            if (stat == null) {
                                continue;
                            }
                            resourceMetric.entries.put(key, stat.latestValue);
                        }
                    }
                    TestAzureEnumerationTask.this.host.sendRequest(Operation.createPost(persistStatsUri).setReferer(TestAzureEnumerationTask.this.host.getUri()).setBodyNoCloning(resourceMetric));
                }
                TestAzureEnumerationTask.this.host.completeIteration();
            }
        }
    };
    String servicePath = UUID.randomUUID().toString();
    Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
    this.host.startService(startOp, parentService);
    ComputeStatsRequest statsRequest = new ComputeStatsRequest();
    statsRequest.resourceReference = UriUtils.buildUri(this.host, selfLink);
    statsRequest.nextStage = SingleResourceTaskCollectionStage.UPDATE_STATS.name();
    statsRequest.isMockRequest = this.isMock;
    statsRequest.taskReference = UriUtils.buildUri(this.host, servicePath);
    this.host.sendAndWait(Operation.createPatch(UriUtils.buildUri(this.host, AzureUriPaths.AZURE_STATS_ADAPTER)).setBody(statsRequest).setReferer(this.host.getUri()));
}
Also used : ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) URI(java.net.URI) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)

Example 5 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class EndpointAdapterUtils method configureEndpoint.

private static void configureEndpoint(StatelessService service, EndpointConfigRequest body, BiConsumer<AuthCredentialsServiceState, Retriever> credEnhancer, BiConsumer<ComputeDescription, Retriever> descEnhancer, BiConsumer<ComputeState, Retriever> compEnhancer, BiConsumer<EndpointState, Retriever> endpointEnhancer) {
    TaskManager tm = new TaskManager(service, body.taskReference, body.resourceLink());
    Consumer<Throwable> onFailure = tm::patchTaskToFailure;
    Consumer<Operation> onSuccess = (op) -> {
        EndpointState endpoint = op.getBody(EndpointState.class);
        op.complete();
        AuthCredentialsServiceState authState = new AuthCredentialsServiceState();
        Map<String, String> props = new HashMap<>(body.endpointProperties);
        props.put(MOCK_REQUEST, String.valueOf(body.isMockRequest));
        props.put(ENDPOINT_REFERENCE_URI, body.resourceReference.toString());
        Retriever r = Retriever.of(props);
        try {
            credEnhancer.accept(authState, r);
            ComputeDescription cd = new ComputeDescription();
            descEnhancer.accept(cd, r);
            ComputeState cs = new ComputeState();
            cs.powerState = PowerState.ON;
            compEnhancer.accept(cs, r);
            EndpointState es = new EndpointState();
            es.endpointProperties = new HashMap<>();
            es.regionId = r.get(EndpointConfigRequest.REGION_KEY).orElse(null);
            endpointEnhancer.accept(es, r);
            Stream<Operation> operations = Stream.of(Pair.of(authState, endpoint.authCredentialsLink), Pair.of(cd, endpoint.computeDescriptionLink), Pair.of(cs, endpoint.computeLink), Pair.of(es, endpoint.documentSelfLink)).map((p) -> Operation.createPatch(createInventoryUri(service.getHost(), p.right)).setBody(p.left).setReferer(service.getUri()));
            applyChanges(tm, service, endpoint, operations);
        } catch (Exception e) {
            tm.patchTaskToFailure(e);
        }
    };
    AdapterUtils.getServiceState(service, body.resourceReference, onSuccess, onFailure);
}
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) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Operation(com.vmware.xenon.common.Operation) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Stream(java.util.stream.Stream) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

StatelessService (com.vmware.xenon.common.StatelessService)17 Operation (com.vmware.xenon.common.Operation)14 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)6 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)5 ComputeStatsResponse (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse)5 URI (java.net.URI)4 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)3 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)3 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)3 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2 AmazonCloudWatchAsyncClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient)2 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)2 DescribeAlarmsResult (com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult)2 AmazonElasticLoadBalancingAsyncClient (com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)2 PhotonModelMetricServices (com.vmware.photon.controller.model.PhotonModelMetricServices)2 PhotonModelServices (com.vmware.photon.controller.model.PhotonModelServices)2 UriPaths (com.vmware.photon.controller.model.UriPaths)2