Search in sources :

Example 1 with Infomodel

use of org.eclipse.vorto.model.Infomodel in project vorto by eclipse.

the class TwinPayloadFactoryTest method testCreateRequestSingleFbProperty.

@Test
public void testCreateRequestSingleFbProperty() {
    FunctionblockModel fbm = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Temperature:1.0.0")).statusProperty(ModelProperty.createPrimitiveProperty("value", true, PrimitiveType.FLOAT)).build();
    Infomodel infomodel = Infomodel.Builder(ModelId.fromPrettyFormat("com.acme:TemperatureSensor:1.0.0")).withProperty(ModelProperty.Builder("temperature", fbm).build()).build();
    FunctionblockValue fbValue = new FunctionblockValue(fbm);
    fbValue.withStatusProperty("value", 20.2);
    InfomodelValue infomodelValue = new InfomodelValue(infomodel);
    infomodelValue.withFunctionblock("temperature", fbValue);
    System.out.println(gson.toJson(TwinPayloadFactory.toShadowUpdateRequest(infomodelValue)));
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) Infomodel(org.eclipse.vorto.model.Infomodel) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) Test(org.junit.Test)

Example 2 with Infomodel

use of org.eclipse.vorto.model.Infomodel in project vorto by eclipse.

the class ModelDtoFactory method createResource.

private static Infomodel createResource(InformationModel model, Optional<MappingModel> mappingModel) {
    Infomodel infoResource = new Infomodel(new ModelId(model.getName(), model.getNamespace(), model.getVersion()));
    for (FunctionblockProperty property : model.getProperties()) {
        infoResource.getFunctionblocks().add(createProperty(property, mappingModel));
    }
    infoResource.setDescription(model.getDescription());
    infoResource.setDisplayName(model.getDisplayname());
    infoResource.setReferences(model.getReferences().stream().map(reference -> createModelId(reference)).collect(Collectors.toList()));
    infoResource.setCategory(model.getCategory());
    if (mappingModel.isPresent()) {
        MappingModel _mappingModel = mappingModel.get();
        infoResource.setTargetPlatformKey(_mappingModel.getTargetPlatform());
        for (MappingRule rule : getInfoModelRule(_mappingModel.getRules())) {
            if (rule.getTarget() instanceof StereoTypeTarget) {
                StereoTypeTarget target = (StereoTypeTarget) rule.getTarget();
                infoResource.addStereotype(Stereotype.create(target.getName(), convertAttributesToMap(target.getAttributes())));
            } else if (rule.getTarget() instanceof ReferenceTarget) {
                ReferenceTarget target = (ReferenceTarget) rule.getTarget();
                infoResource.setMappingReference(createModelId(target.getMappingModel()));
            }
        }
    }
    return infoResource;
}
Also used : Infomodel(org.eclipse.vorto.model.Infomodel) ReferenceTarget(org.eclipse.vorto.core.api.model.mapping.ReferenceTarget) MappingRule(org.eclipse.vorto.core.api.model.mapping.MappingRule) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) ModelId(org.eclipse.vorto.model.ModelId) FunctionblockProperty(org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 3 with Infomodel

use of org.eclipse.vorto.model.Infomodel in project vorto by eclipse.

the class DataMapperJxpath method map.

public InfomodelValue map(Object input, MappingContext mappingContext) {
    JXPathContext context = jxpathHelper.newContext(input);
    InfomodelValue normalized = new InfomodelValue(specification.getInfoModel());
    final Infomodel deviceInfoModel = specification.getInfoModel();
    for (ModelProperty fbProperty : deviceInfoModel.getFunctionblocks()) {
        FunctionblockValue mappedFb = mapFunctionBlock(fbProperty, context);
        if (mappedFb != null) {
            normalized.withFunctionblock(fbProperty.getName(), mappedFb);
        }
    }
    return normalized;
}
Also used : FunctionblockValue(org.eclipse.vorto.model.runtime.FunctionblockValue) JXPathContext(org.apache.commons.jxpath.JXPathContext) Infomodel(org.eclipse.vorto.model.Infomodel) ModelProperty(org.eclipse.vorto.model.ModelProperty) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue)

Example 4 with Infomodel

use of org.eclipse.vorto.model.Infomodel in project vorto by eclipse.

the class ModelRepositoryControllerTest method verifyFullModelPayloadForUI.

/**
 * This test performs the following:
 * <ol>
 *   <li>
 *     Creates the {@literal com.test} namespace with a sysadmin user, to hold the models.
 *   </li>
 *   <li>
 *     Adds a non-sysadmin user with {@literal model_creator} role to the {@literal com.test}
 *     namespace.
 *   </li>
 *   <li>
 *     Creates a "Zone" datatype model from the corresponding file resource.
 *   </li>
 *   <li>
 *     Creates a "Lamp" functionblock model from the corresponding file resource.
 *   </li>
 *   <li>
 *     Creates an "Address" functionblock model from the corresponding file resource.
 *   </li>
 *   <li>
 *     Creates a "StreetLamp" model from the corresponding file resource, using the above
 *     functionblocks as dependencies.
 *   </li>
 *   <li>
 *     Adds an attachment to the "StreetLamp" model.
 *   </li>
 *   <li>
 *     Adds a link to the "StreetLamp" model.
 *   </li>
 *   <li>
 *     Adds a mapping to the "StreetLamp" model.
 *   </li>
 *   <li>
 *     Loads the "StreetLamp" model with the REST call used by the UI, and verifies / validates:
 *     <ul>
 *       <li>
 *         Basic {@link org.eclipse.vorto.repository.core.ModelInfo} properties.
 *       </li>
 *       <li>
 *         The Base64-encoded model syntax
 *         (see {@link ModelFullDetailsDTO#getEncodedModelSyntax()})
 *       </li>
 *       <li>
 *         Mapping (see {@link ModelFullDetailsDTO#getMappings()}).
 *       </li>
 *       <li>
 *         Attachment (see {@link ModelFullDetailsDTO#getAttachments()}
 *       </li>
 *       <li>
 *         Link (see {@link ModelFullDetailsDTO#getLinks()}
 *       </li>
 *       <li>
 *         References (see {@link ModelFullDetailsDTO#getReferences()}
 *       </li>
 *       <li>
 *         "Referenced by" (see {@link ModelFullDetailsDTO#getReferencedBy()})
 *       </li>
 *       <li>
 *         Policies (see {@link ModelFullDetailsDTO#getPolicies()} and
 *         {@link ModelFullDetailsDTO#getBestPolicy()} for the creating user, and conversely, for
 *         an extraneous user with no access.
 *       </li>
 *       <li>
 *         Workflow actions (see {@link ModelFullDetailsDTO#getActions()} for the creating user,
 *         and conversely, for an extraneous user with no access.
 *       </li>
 *     </ul>
 *   </li>
 *   <li>
 *     Loads the "Lamp" functionblock model with the REST call used by the UI, and verifies the
 *     "referenced by" node (see {@link ModelFullDetailsDTO#getReferencedBy()}.
 *   </li>
 *   <li>
 *     Finally, cleans up and deletes all 4 models, then the namespace.
 *   </li>
 * </ol>
 *
 * @throws Exception
 */
@Test
public void verifyFullModelPayloadForUI() throws Exception {
    // required for some extra properties in DTOs not annotated with Jackson polymorphism
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // model names, id strings and file names
    String namespace = "com.test";
    String version = "1.0.0";
    String idFormat = "%s.%s:%s";
    String zoneModelName = "Zone";
    String zoneModelID = String.format(idFormat, namespace, zoneModelName, version);
    String zoneFileName = zoneModelName.concat(".type");
    String lampModelName = "Lamp";
    String lampModelID = String.format(idFormat, namespace, lampModelName, version);
    String lampFileName = lampModelName.concat(".fbmodel");
    String addressModelName = "Address";
    String addressModelID = String.format(idFormat, namespace, addressModelName, version);
    String addressFileName = addressModelName.concat(".fbmodel");
    String streetLampModelName = "StreetLamp";
    String streetLampModelID = String.format(idFormat, namespace, streetLampModelName, version);
    String streetLampFileName = streetLampModelName.concat(".infomodel");
    String streetLampAttachmentFileName = "StreetLampAttachment.json";
    String streetLampLinkURL = "https://vorto.eclipse.org/";
    String streetLampLinkName = "Vorto";
    // creates the namespace as sysadmin
    createNamespaceSuccessfully(namespace, userSysadmin);
    // creates the collaborator payload to add userModelCreator to the namespace
    Collaborator userModelCreatorCollaborator = new Collaborator();
    userModelCreatorCollaborator.setAuthenticationProviderId(GITHUB);
    userModelCreatorCollaborator.setRoles(Arrays.asList("model_viewer", "model_creator"));
    userModelCreatorCollaborator.setTechnicalUser(false);
    userModelCreatorCollaborator.setUserId(USER_MODEL_CREATOR_NAME);
    // allows creator rights to userCreator on namespace
    repositoryServer.perform(put(String.format("/rest/namespaces/%s/users", namespace)).with(userSysadmin).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(userModelCreatorCollaborator))).andExpect(status().isOk());
    // creates the Zone model
    createModel(userModelCreator, zoneFileName, zoneModelID);
    // creates the Lamp model
    createModel(userModelCreator, lampFileName, lampModelID);
    // creates the Address model
    createModel(userModelCreator, addressFileName, addressModelID);
    // creates the StreetLamp model
    createModel(userModelCreator, streetLampFileName, streetLampModelID);
    // adds an attachment to the StreetLamp model (still requires sysadmin for this)
    addAttachment(streetLampModelID, userSysadmin, streetLampAttachmentFileName, MediaType.APPLICATION_JSON).andExpect(status().isOk()).andExpect(content().json(objectMapper.writeValueAsString(AttachResult.success(ModelId.fromPrettyFormat(streetLampModelID), streetLampAttachmentFileName))));
    // adds a link to the StreetLamp model
    ModelLink link = new ModelLink(streetLampLinkURL, streetLampLinkName);
    addLink(streetLampModelID, userModelCreator, link);
    // saves a minimal mapping specification for the street lamp model
    Infomodel streetLampInfomodel = new Infomodel(ModelId.fromPrettyFormat(streetLampModelID));
    streetLampInfomodel.setTargetPlatformKey("myTargetPlatform");
    MappingSpecification mapping = new MappingSpecification(streetLampInfomodel);
    repositoryServer.perform(put(String.format("/rest/mappings/specifications/%s", streetLampModelID)).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(mapping)).with(userModelCreator)).andExpect(status().isOk());
    // expected ID of the payload mapping model
    String mappingId = String.format("%s.%s:%sPayloadMapping:%s", // root namespace
    namespace, // virtual namespace for mapping
    streetLampModelName.toLowerCase(), // mapping name part 1 matching root model
    streetLampModelName, // root model version
    version);
    // fetches the full model for the UI
    repositoryServer.perform(get(String.format("/rest/models/ui/%s", streetLampModelID)).with(userModelCreator)).andExpect(status().isOk()).andDo(mvcResult -> {
        ModelFullDetailsDTO output = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), ModelFullDetailsDTO.class);
        LOGGER.info(new StringBuilder("\nReceived response body:\n\n").append(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(output)).toString());
    }).andExpect(jsonPath("$.modelInfo").exists()).andExpect(jsonPath("$.modelInfo.id.name").value(streetLampModelName)).andExpect(jsonPath("$.modelInfo.id.namespace").value(namespace)).andExpect(jsonPath("$.modelInfo.type").value("InformationModel")).andExpect(jsonPath("$.modelInfo.author").value(USER_MODEL_CREATOR_NAME)).andExpect(jsonPath("$.mappings").isNotEmpty()).andExpect(jsonPath("$.mappings[0].id").value(mappingId)).andExpect(jsonPath("$.references", hasSize(2))).andExpect(jsonPath("$.referencedBy", hasSize(1))).andExpect(jsonPath("$.referencedBy[0].id").value(mappingId)).andExpect(jsonPath("$.attachments").exists()).andExpect(jsonPath("$.attachments[0].filename").value(streetLampAttachmentFileName)).andExpect(jsonPath("$.attachments[0].modelId.name").value(streetLampModelName)).andExpect(jsonPath("$.links").exists()).andExpect(jsonPath("$.links[0].url").value(equalTo(link.getUrl()))).andExpect(jsonPath("$.links[0].displayText").value(equalTo(link.getDisplayText()))).andExpect(jsonPath("$.actions").exists()).andExpect(jsonPath("$.actions").isEmpty()).andExpect(jsonPath("$.policies", hasSize(2))).andExpect(jsonPath("$.bestPolicy").exists()).andExpect(jsonPath("$.bestPolicy.principalId").value("model_creator")).andExpect(jsonPath("$.bestPolicy.permission").value(Permission.FULL_ACCESS.toString())).andExpect(jsonPath("$.encodedModelSyntax").value(Base64.getEncoder().encodeToString(createContentAsString(streetLampFileName).getBytes())));
    // cleanup: deletes models in reverse order of creation, then namespace
    repositoryServer.perform(delete(String.format("/rest/models/%s", mappingId)).with(userModelCreator)).andExpect(status().isOk());
    repositoryServer.perform(delete(String.format("/rest/models/%s", streetLampModelID)).with(userModelCreator)).andExpect(status().isOk());
    repositoryServer.perform(delete(String.format("/rest/models/%s", lampModelID)).with(userModelCreator)).andExpect(status().isOk());
    repositoryServer.perform(delete(String.format("/rest/models/%s", addressModelID)).with(userModelCreator)).andExpect(status().isOk());
    repositoryServer.perform(delete(String.format("/rest/models/%s", zoneModelID)).with(userModelCreator)).andExpect(status().isOk());
    repositoryServer.perform(delete(String.format("/rest/namespaces/%s", namespace)).with(userSysadmin)).andExpect(status().isNoContent());
    // removes test-specific configuration
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
}
Also used : Arrays(java.util.Arrays) MockMvcResultMatchers.jsonPath(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath) MappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) ModelFullDetailsDTO(org.eclipse.vorto.repository.web.api.v1.dto.ModelFullDetailsDTO) Autowired(org.springframework.beans.factory.annotation.Autowired) Infomodel(org.eclipse.vorto.model.Infomodel) MockMvcResultMatchers.content(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ModelLink(org.eclipse.vorto.repository.web.api.v1.dto.ModelLink) MockMvcRequestBuilders.delete(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete) Logger(org.apache.log4j.Logger) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) MockMvcRequestBuilders.post(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) MockMvcRequestBuilders.put(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put) MediaType(org.springframework.http.MediaType) Test(org.junit.Test) ModelId(org.eclipse.vorto.model.ModelId) Sets(com.google.common.collect.Sets) ModelType(org.eclipse.vorto.model.ModelType) Base64(java.util.Base64) AttachmentValidator(org.eclipse.vorto.repository.core.impl.validation.AttachmentValidator) MockMvcRequestBuilders.get(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get) Permission(org.eclipse.vorto.repository.core.PolicyEntry.Permission) AttachResult(org.eclipse.vorto.repository.web.api.v1.dto.AttachResult) ModelFullDetailsDTO(org.eclipse.vorto.repository.web.api.v1.dto.ModelFullDetailsDTO) Collaborator(org.eclipse.vorto.repository.web.api.v1.dto.Collaborator) MappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification) ModelLink(org.eclipse.vorto.repository.web.api.v1.dto.ModelLink) Infomodel(org.eclipse.vorto.model.Infomodel) Test(org.junit.Test)

Example 5 with Infomodel

use of org.eclipse.vorto.model.Infomodel in project vorto by eclipse.

the class DefaultPayloadMappingService method addReferencesRecursive.

/**
 * Adds reference types of the given properties to the mapping Specification (needed for lookup)
 *
 * @param model to traverse properties
 */
private void addReferencesRecursive(IModel model, String targetPlatformKey) {
    if (model instanceof Infomodel) {
        Infomodel infomodel = (Infomodel) model;
        for (ModelProperty property : infomodel.getFunctionblocks()) {
            ModelId referenceModelId = (ModelId) property.getType();
            ModelId mappingId = property.getMappingReference();
            IModel referenceModel = null;
            if (mappingId != null) {
                referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
            } else {
                ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                referenceModel = modelContent.getModels().get(modelContent.getRoot());
            }
            property.setType((FunctionblockModel) referenceModel);
            addReferencesRecursive(referenceModel, targetPlatformKey);
        }
    } else if (model instanceof EntityModel) {
        EntityModel entityModel = (EntityModel) model;
        for (ModelProperty property : entityModel.getProperties()) {
            initStereotypeIfMissing(property);
            if (property.getType() instanceof ModelId) {
                ModelId referenceModelId = (ModelId) property.getType();
                ModelId mappingId = property.getMappingReference();
                IModel referenceModel = null;
                if (mappingId != null) {
                    referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
                } else {
                    ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                    referenceModel = modelContent.getModels().get(modelContent.getRoot());
                }
                if (referenceModel instanceof EntityModel) {
                    property.setType((EntityModel) referenceModel);
                    addReferencesRecursive(referenceModel, targetPlatformKey);
                } else {
                    property.setType((EnumModel) referenceModel);
                }
            }
        }
    } else if (model instanceof FunctionblockModel) {
        FunctionblockModel fbModel = (FunctionblockModel) model;
        for (ModelProperty property : fbModel.getProperties()) {
            initStereotypeIfMissing(property);
            if (property.getType() instanceof ModelId) {
                ModelId referenceModelId = (ModelId) property.getType();
                ModelId mappingId = property.getMappingReference();
                IModel referenceModel = null;
                if (mappingId != null) {
                    referenceModel = getModelContentByModelAndMappingId(referenceModelId.getPrettyFormat(), mappingId.getPrettyFormat());
                } else {
                    ModelContent modelContent = getModelContent(referenceModelId, targetPlatformKey);
                    referenceModel = modelContent.getModels().get(modelContent.getRoot());
                }
                if (referenceModel instanceof EntityModel) {
                    property.setType((EntityModel) referenceModel);
                    addReferencesRecursive(referenceModel, targetPlatformKey);
                } else {
                    property.setType((EnumModel) referenceModel);
                }
            }
        }
    }
}
Also used : FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) IModel(org.eclipse.vorto.model.IModel) Infomodel(org.eclipse.vorto.model.Infomodel) EnumModel(org.eclipse.vorto.model.EnumModel) ModelContent(org.eclipse.vorto.model.ModelContent) ModelProperty(org.eclipse.vorto.model.ModelProperty) EntityModel(org.eclipse.vorto.model.EntityModel) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

Infomodel (org.eclipse.vorto.model.Infomodel)11 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)7 Test (org.junit.Test)6 FunctionblockValue (org.eclipse.vorto.model.runtime.FunctionblockValue)5 InfomodelValue (org.eclipse.vorto.model.runtime.InfomodelValue)5 ModelId (org.eclipse.vorto.model.ModelId)3 ModelProperty (org.eclipse.vorto.model.ModelProperty)3 FunctionblockProperty (org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty)2 MappingSpecification (org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification)2 ModelContent (org.eclipse.vorto.model.ModelContent)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 Sets (com.google.common.collect.Sets)1 Arrays (java.util.Arrays)1 Base64 (java.util.Base64)1 JXPathContext (org.apache.commons.jxpath.JXPathContext)1 Logger (org.apache.log4j.Logger)1 InformationModelBuilder (org.eclipse.vorto.core.api.model.BuilderUtils.InformationModelBuilder)1 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)1 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)1 MappingRule (org.eclipse.vorto.core.api.model.mapping.MappingRule)1