Search in sources :

Example 56 with ComputeDescription

use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.

the class TestVSphereOvfImport method importOvfAsDescriptions.

@Test
public void importOvfAsDescriptions() throws Throwable {
    this.resourcePool = createResourcePool();
    this.auth = createAuth();
    this.computeHostDescription = createComputeDescription();
    createComputeHost();
    ComputeDescription computeDesc = new ComputeDescription();
    computeDesc.supportedChildren = new ArrayList<>();
    computeDesc.instanceAdapterReference = UriUtils.buildUri(this.host, VSphereUriPaths.INSTANCE_SERVICE);
    computeDesc.authCredentialsLink = this.auth.documentSelfLink;
    computeDesc.name = computeDesc.id;
    computeDesc.dataStoreId = this.dataStoreId;
    ImportOvfRequest req = new ImportOvfRequest();
    req.ovfUri = new File("src/test/resources/vcenter.ovf").toURI();
    req.template = computeDesc;
    Operation op = Operation.createPatch(this.host, OvfImporterService.SELF_LINK).setBody(req).setReferer(this.host.getPublicUri());
    op = this.host.waitForResponse(op);
    assertEquals(Operation.STATUS_CODE_OK, op.getStatusCode());
    Query q = Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_ID, "ovf-", MatchType.PREFIX).build();
    QueryTask task = QueryTask.Builder.createDirectTask().setQuery(q).build();
    QueryUtils.createQueryTaskOperation(this.host, task, ServiceTypeCluster.INVENTORY_SERVICE);
    task = this.host.waitForResponse(op).getBody(QueryTask.class);
    assertTrue(task.results.documentLinks.size() > 5);
    snapshotFactoryState("ovf", ComputeDescriptionService.class);
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ImportOvfRequest(com.vmware.photon.controller.model.adapters.vsphere.ovf.ImportOvfRequest) Operation(com.vmware.xenon.common.Operation) File(java.io.File) Test(org.junit.Test)

Example 57 with ComputeDescription

use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.

the class EnumerationClientTest method test.

@Test
public void test() throws Exception {
    String url = System.getProperty(TestProperties.VC_URL);
    if (url == null) {
        return;
    }
    String username = System.getProperty(TestProperties.VC_USERNAME);
    String password = System.getProperty(TestProperties.VC_PASSWORD);
    String datacenter = System.getProperty(TestProperties.VC_DATECENTER_ID);
    ManagedObjectReference datacenterMoRef = VimUtils.convertStringToMoRef(datacenter);
    BasicConnection conn = new BasicConnection();
    conn.setURI(URI.create(url));
    conn.setUsername(username);
    conn.setPassword(password);
    conn.setIgnoreSslErrors(true);
    conn.setRequestTimeout(30, TimeUnit.SECONDS);
    conn.connect();
    ComputeStateWithDescription parent = new ComputeStateWithDescription();
    ComputeDescription desc = new ComputeDescription();
    parent.description = desc;
    EnumerationClient client = new EnumerationClient(conn, parent);
    PropertyFilterSpec spec = client.createResourcesFilterSpec();
    for (List<ObjectContent> page : client.retrieveObjects(spec)) {
        for (ObjectContent cont : page) {
            this.logger.info(VimUtils.convertMoRefToString(cont.getObj()));
        }
    }
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) BasicConnection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.BasicConnection) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test)

Example 58 with ComputeDescription

use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.

the class CustomPropertiesTest method putAndGetDescription.

@Test
public void putAndGetDescription() {
    ManagedObjectReference ref = new ManagedObjectReference();
    ref.setType("type");
    ref.setValue("v1");
    ComputeDescription desc = new ComputeDescription();
    CustomProperties.of(desc).put("12", 12).put("s", "s").put("4", 4L).put("ref", ref);
    assertEquals("12", desc.customProperties.get("12"));
    assertEquals("4", desc.customProperties.get("4"));
    assertEquals("s", desc.customProperties.get("s"));
    assertEquals(VimUtils.convertMoRefToString(ref), desc.customProperties.get("ref"));
    CustomProperties access = CustomProperties.of(desc);
    assertEquals((Integer) 12, access.getInt("12", null));
    assertEquals("s", access.getString("s"));
    assertEquals(Long.valueOf(4), access.getLong("4", null));
    assertMoFefEquals(ref, access.getMoRef("ref"));
}
Also used : ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test)

Example 59 with ComputeDescription

use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.

the class OvfParser method parse.

/**
 * Produces several descriptions based on the different hardware configuration defined in the
 * OVF descriptor.
 *
 * @param doc
 *            OVF descriptor to parse
 * @param template
 *            use as a basis of the ComputeDescription.
 * @return
 */
public List<ComputeDescription> parse(Document doc, ComputeDescription template) {
    CustomProperties cust = CustomProperties.of(template);
    NodeList props = nodes(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Property");
    for (Element prop : iterableElements(props)) {
        String userConfigurable = attr("ovf:userConfigurable", prop);
        if (!"true".equals(userConfigurable)) {
            continue;
        }
        String key = attr("ovf:key", prop);
        Element section = (Element) prop.getParentNode();
        String instanceId = attr("ovf:instance", section);
        String classId = attr("ovf:class", section);
        String description = text(prop, "ovf:Description/text()");
        cust.put(property(classId, key, instanceId), description);
    }
    if (template.name == null) {
        String productName = text(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Product/text()");
        String productVersion = text(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:ProductSection/ovf:Version/text()");
        template.name = productName + " " + productVersion;
    }
    NodeList hwItems = nodes(doc, "/ovf:Envelope/ovf:VirtualSystem/ovf:VirtualHardwareSection/ovf:Item");
    Map<String, ComputeDescription> hwByConfigName = new HashMap<>();
    for (Element item : iterableElements(hwItems)) {
        String configName = attr("ovf:configuration", item);
        ComputeDescription desc = hwByConfigName.get(configName);
        if (desc == null) {
            desc = Utils.clone(template);
            desc.documentSelfLink = UUID.randomUUID().toString();
            desc.id = "ovf-imported-" + desc.documentSelfLink;
            desc.customProperties.put(PROP_OVF_CONFIGURATION, configName);
            hwByConfigName.put(configName, desc);
        }
        String resourceType = text(item, "rasd:ResourceType/text()");
        if (RESOURCE_TYPE_CPU.equals(resourceType)) {
            long qty = Long.parseLong(text(item, "rasd:VirtualQuantity/text()"));
            desc.cpuCount = qty;
        }
        if (RESOURCE_TYPE_MEMORY.equals(resourceType)) {
            double qty = Double.parseDouble(text(item, "rasd:VirtualQuantity/text()"));
            long mult = memAllocationUnit2Multiplier(text(item, "rasd:AllocationUnits/text()"));
            desc.totalMemoryBytes = (long) (qty * mult);
        }
    }
    for (Iterator<ComputeDescription> it = hwByConfigName.values().iterator(); it.hasNext(); ) {
        ComputeDescription desc = it.next();
        if (desc.cpuCount <= 0) {
            it.remove();
        }
    }
    return new ArrayList<>(hwByConfigName.values());
}
Also used : ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) CustomProperties(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties)

Example 60 with ComputeDescription

use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.

the class SingleResourceStatsAggregationTaskServiceTest method testResourceStatsAggregation.

@Test
public void testResourceStatsAggregation() throws Throwable {
    // create a resource pool
    ResourcePoolState rpState = new ResourcePoolState();
    rpState.name = "testName";
    ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
    ComputeState[] computeStateArray = new ComputeState[NUM_COMPUTE_RESOURCES];
    for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
        ComputeDescription cDesc = new ComputeDescription();
        cDesc.name = rpState.name;
        cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
        ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
        ComputeState computeState = new ComputeState();
        computeState.name = rpState.name;
        computeState.descriptionLink = descReturnState.documentSelfLink;
        computeState.resourcePoolLink = rpReturnState.documentSelfLink;
        computeStateArray[i] = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
    }
    StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
    collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
    collectionTaskState.taskInfo = TaskState.createDirect();
    int counter = 0;
    // executing stats collection multiple times
    while (counter < NUM_COLLECTIONS) {
        this.postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
        counter++;
    }
    // wait for stats to be populated
    this.host.waitFor("Error waiting for stats", () -> {
        boolean returnStatus = false;
        for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
            String statsUriPath = UriUtils.buildUriPath(computeStateArray[i].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
            ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
            for (ServiceStat stat : resStats.entries.values()) {
                if (stat.name.startsWith(UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK))) {
                    returnStatus = true;
                    break;
                }
            }
        }
        return returnStatus;
    });
    // number of keys = 2, number of lastCollectionTimeMetrics = 2
    int numberOfRawMetrics = NUM_COLLECTIONS * NUM_COMPUTE_RESOURCES * 2 * 2;
    // kick off an aggregation task
    SingleResourceStatsAggregationTaskState aggregationTaskState = new SingleResourceStatsAggregationTaskState();
    aggregationTaskState.resourceLink = computeStateArray[0].documentSelfLink;
    aggregationTaskState.metricNames = new HashSet<>(Arrays.asList(MockStatsAdapter.KEY_1, MockStatsAdapter.KEY_2, MockStatsAdapter.KEY_3));
    postServiceSynchronously(SingleResourceStatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, SingleResourceStatsAggregationTaskState.class);
    long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
    this.host.waitFor("Error waiting for rolled up stats", () -> {
        ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, ResourceMetricsService.FACTORY_LINK));
        String randomDocumentLink = result.documentLinks.get(0);
        ResourceMetrics metric = Utils.fromJson(result.documents.get(randomDocumentLink), ResourceMetrics.class);
        // Make sure all the documents have expiration time set.
        Assert.assertTrue("Expiration time is not correctly set.", metric.documentExpirationTimeMicros < expectedExpirationTime);
        return (result.documentCount == 2 + numberOfRawMetrics);
    });
    String statsUriPath = UriUtils.buildUriPath(computeStateArray[0].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
    ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
    int statCount = 0;
    for (ServiceStat stat : resStats.entries.values()) {
        if (stat.name.startsWith(MockStatsAdapter.KEY_1) || stat.name.startsWith(MockStatsAdapter.KEY_2)) {
            statCount++;
        }
    }
    Assert.assertEquals("Did not find in-memory stats", 2, statCount);
    // verify that the aggregation tasks have been deleted
    this.host.waitFor("Timeout waiting for task to expire", () -> {
        ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, SingleResourceStatsAggregationTaskService.FACTORY_LINK));
        return res.documentLinks.size() == 0;
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SingleResourceStatsAggregationTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsAggregationTaskService.SingleResourceStatsAggregationTaskState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ServiceStats(com.vmware.xenon.common.ServiceStats) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) Test(org.junit.Test)

Aggregations

ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)78 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)39 Operation (com.vmware.xenon.common.Operation)21 ArrayList (java.util.ArrayList)21 QueryTask (com.vmware.xenon.services.common.QueryTask)17 Test (org.junit.Test)17 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)15 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)15 HashMap (java.util.HashMap)15 HashSet (java.util.HashSet)15 List (java.util.List)15 Utils (com.vmware.xenon.common.Utils)14 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)14 URI (java.net.URI)14 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)13 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)12 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)12 UriUtils (com.vmware.xenon.common.UriUtils)12 ComputeType (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType)11 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)11