Search in sources :

Example 1 with VDBImport

use of org.teiid.adminapi.VDBImport in project teiid by teiid.

the class VDBDeployer method deploy.

public void deploy(final DeploymentPhaseContext context) throws DeploymentUnitProcessingException {
    final DeploymentUnit deploymentUnit = context.getDeploymentUnit();
    if (!TeiidAttachments.isVDBDeployment(deploymentUnit)) {
        return;
    }
    final VDBMetaData deployment = deploymentUnit.getAttachment(TeiidAttachments.VDB_METADATA);
    VDBMetaData other = this.vdbRepository.getVDB(deployment.getName(), deployment.getVersion());
    if (other != null) {
        String deploymentName = other.getPropertyValue(TranslatorUtil.DEPLOYMENT_NAME);
        throw new DeploymentUnitProcessingException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50106, deployment, deploymentName));
    }
    deployment.addProperty(TranslatorUtil.DEPLOYMENT_NAME, deploymentUnit.getName());
    // check to see if there is old vdb already deployed.
    final ServiceController<?> controller = context.getServiceRegistry().getService(TeiidServiceNames.vdbServiceName(deployment.getName(), deployment.getVersion()));
    if (controller != null) {
        LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50019, deployment));
        controller.setMode(ServiceController.Mode.REMOVE);
    }
    boolean preview = deployment.isPreview();
    if (!preview && deployment.hasErrors()) {
        throw new DeploymentUnitProcessingException(IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50074, deployment));
    }
    // make sure the translator defined exists in configuration; otherwise add as error
    for (ModelMetaData model : deployment.getModelMetaDatas().values()) {
        if (!model.isSource() || model.getSourceNames().isEmpty()) {
            continue;
        }
        for (String source : model.getSourceNames()) {
            String translatorName = model.getSourceTranslatorName(source);
            if (deployment.isOverideTranslator(translatorName)) {
                VDBTranslatorMetaData parent = deployment.getTranslator(translatorName);
                translatorName = parent.getType();
            }
            Translator translator = this.translatorRepository.getTranslatorMetaData(translatorName);
            if (translator == null) {
                String msg = IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50077, translatorName, deployment.getName(), deployment.getVersion());
                LogManager.logWarning(LogConstants.CTX_RUNTIME, msg);
            }
        }
    }
    // add VDB module's classloader as an attachment
    ModuleClassLoader classLoader = deploymentUnit.getAttachment(Attachments.MODULE).getClassLoader();
    deployment.addAttchment(ClassLoader.class, classLoader);
    deployment.addAttchment(ScriptEngineManager.class, new ScriptEngineManager(classLoader));
    try {
        EmbeddedServer.createPreParser(deployment);
    } catch (TeiidException e1) {
        throw new DeploymentUnitProcessingException(e1);
    }
    UDFMetaData udf = deploymentUnit.removeAttachment(TeiidAttachments.UDF_METADATA);
    if (udf == null) {
        udf = new UDFMetaData();
    }
    udf.setFunctionClassLoader(classLoader);
    deployment.addAttchment(UDFMetaData.class, udf);
    VirtualFile file = deploymentUnit.getAttachment(Attachments.DEPLOYMENT_ROOT).getRoot();
    VDBResources resources;
    try {
        resources = new VDBResources(file, deployment);
    } catch (IOException e) {
        throw new DeploymentUnitProcessingException(IntegrationPlugin.Event.TEIID50017.name(), e);
    }
    this.vdbRepository.addPendingDeployment(deployment);
    // build a VDB service
    final VDBService vdb = new VDBService(deployment, resources, shutdownListener);
    // $NON-NLS-1$
    vdb.addMetadataRepository("index", new IndexMetadataRepository());
    final ServiceBuilder<RuntimeVDB> vdbService = context.getServiceTarget().addService(TeiidServiceNames.vdbServiceName(deployment.getName(), deployment.getVersion()), vdb);
    // add dependencies to data-sources
    dataSourceDependencies(deployment, context.getServiceTarget());
    for (VDBImport vdbImport : deployment.getVDBImports()) {
        VDBKey vdbKey = new VDBKey(vdbImport.getName(), vdbImport.getVersion());
        if (vdbKey.isAtMost()) {
            // TODO: could allow partial versions here if we canonicalize
            throw new DeploymentUnitProcessingException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40144, deployment, vdbKey));
        }
        LogManager.logInfo(LogConstants.CTX_RUNTIME, IntegrationPlugin.Util.gs(IntegrationPlugin.Event.TEIID50115, deployment, vdbKey));
        vdbService.addDependency(TeiidServiceNames.vdbFinishedServiceName(vdbImport.getName(), vdbKey.getVersion()));
    }
    // adding the translator services is redundant, however if one is removed then it is an issue.
    for (Model model : deployment.getModels()) {
        List<String> sourceNames = model.getSourceNames();
        for (String sourceName : sourceNames) {
            String translatorName = model.getSourceTranslatorName(sourceName);
            if (deployment.isOverideTranslator(translatorName)) {
                VDBTranslatorMetaData translator = deployment.getTranslator(translatorName);
                translatorName = translator.getType();
            }
            vdbService.addDependency(TeiidServiceNames.translatorServiceName(translatorName));
        }
    }
    ServiceName vdbSwitchServiceName = TeiidServiceNames.vdbSwitchServiceName(deployment.getName(), deployment.getVersion());
    vdbService.addDependency(TeiidServiceNames.VDB_REPO, VDBRepository.class, vdb.vdbRepositoryInjector);
    vdbService.addDependency(TeiidServiceNames.TRANSLATOR_REPO, TranslatorRepository.class, vdb.translatorRepositoryInjector);
    vdbService.addDependency(TeiidServiceNames.THREAD_POOL_SERVICE, Executor.class, vdb.executorInjector);
    vdbService.addDependency(TeiidServiceNames.OBJECT_SERIALIZER, ObjectSerializer.class, vdb.serializerInjector);
    vdbService.addDependency(TeiidServiceNames.VDB_STATUS_CHECKER, VDBStatusChecker.class, vdb.vdbStatusCheckInjector);
    vdbService.addDependency(vdbSwitchServiceName, CountDownLatch.class, new InjectedValue<CountDownLatch>());
    // VDB restart switch, control the vdbservice by adding removing the switch service. If you
    // remove the service by setting status remove, there is no way start it back up if vdbservice used alone
    installVDBSwitchService(context.getServiceTarget(), vdbSwitchServiceName);
    vdbService.addListener(new AbstractServiceListener<Object>() {

        @Override
        public void transition(final ServiceController controller, final ServiceController.Transition transition) {
            if (transition.equals(ServiceController.Transition.DOWN_to_WAITING)) {
                RuntimeVDB runtimeVDB = RuntimeVDB.class.cast(controller.getValue());
                if (runtimeVDB != null && runtimeVDB.isRestartInProgress()) {
                    ServiceName vdbSwitchServiceName = TeiidServiceNames.vdbSwitchServiceName(deployment.getName(), deployment.getVersion());
                    ServiceController<?> switchSvc = controller.getServiceContainer().getService(vdbSwitchServiceName);
                    if (switchSvc != null) {
                        CountDownLatch latch = CountDownLatch.class.cast(switchSvc.getValue());
                        try {
                            latch.await(5, TimeUnit.SECONDS);
                        } catch (InterruptedException e) {
                        // todo:log it?
                        }
                    }
                    installVDBSwitchService(controller.getServiceContainer(), vdbSwitchServiceName);
                }
            }
        }
    });
    vdbService.setInitialMode(Mode.PASSIVE).install();
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) VirtualFile(org.jboss.vfs.VirtualFile) ScriptEngineManager(javax.script.ScriptEngineManager) IndexMetadataRepository(org.teiid.metadata.index.IndexMetadataRepository) Translator(org.teiid.adminapi.Translator) RuntimeVDB(org.teiid.deployers.RuntimeVDB) ModuleClassLoader(org.jboss.modules.ModuleClassLoader) UDFMetaData(org.teiid.deployers.UDFMetaData) VDBResources(org.teiid.query.metadata.VDBResources) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) TeiidException(org.teiid.core.TeiidException) VDBKey(org.teiid.vdb.runtime.VDBKey) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) VDBImport(org.teiid.adminapi.VDBImport) Model(org.teiid.adminapi.Model) DeploymentUnit(org.jboss.as.server.deployment.DeploymentUnit) VDBTranslatorMetaData(org.teiid.adminapi.impl.VDBTranslatorMetaData)

Example 2 with VDBImport

use of org.teiid.adminapi.VDBImport in project teiid by teiid.

the class VDBMetadataParser method writeVDB.

private void writeVDB(VDBMetaData vdb, OutputStream out) throws XMLStreamException, IOException {
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(out);
    writer.writeStartDocument();
    writer.writeStartElement(Element.VDB.getLocalName());
    writeAttribute(writer, Element.NAME.getLocalName(), vdb.getName());
    writeAttribute(writer, Element.VERSION.getLocalName(), String.valueOf(vdb.getVersion()));
    if (vdb.getDescription() != null) {
        writeElement(writer, Element.DESCRIPTION, vdb.getDescription());
    }
    writeElement(writer, Element.CONNECTION_TYPE, vdb.getConnectionType().name());
    writeProperties(writer, vdb.getPropertiesMap());
    for (VDBImport vdbImport : vdb.getVDBImports()) {
        writer.writeStartElement(Element.IMPORT_VDB.getLocalName());
        writeAttribute(writer, Element.NAME.getLocalName(), vdbImport.getName());
        writeAttribute(writer, Element.VERSION.getLocalName(), String.valueOf(vdbImport.getVersion()));
        writeAttribute(writer, Element.IMPORT_POLICIES.getLocalName(), String.valueOf(vdbImport.isImportDataPolicies()));
        writer.writeEndElement();
    }
    // models
    Collection<ModelMetaData> models = vdb.getModelMetaDatas().values();
    for (ModelMetaData model : models) {
        if (vdb.getImportedModels().contains(model.getName())) {
            continue;
        }
        writeModel(writer, model);
    }
    // override translators
    for (VDBTranslatorMetaData translator : vdb.getOverrideTranslatorsMap().values()) {
        writeTranslator(writer, translator);
    }
    // data-roles
    for (DataPolicy dp : vdb.getDataPolicies()) {
        writeDataPolicy(writer, dp);
    }
    // designer only
    for (EntryMetaData em : vdb.getEntries()) {
        writer.writeStartElement(Element.ENTRY.getLocalName());
        writeAttribute(writer, Element.PATH.getLocalName(), em.getPath());
        if (em.getDescription() != null) {
            writeElement(writer, Element.DESCRIPTION, em.getDescription());
        }
        writeProperties(writer, em.getPropertiesMap());
        writer.writeEndElement();
    }
    writer.writeEndElement();
    writer.writeEndDocument();
    writer.close();
    out.close();
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) VDBImport(org.teiid.adminapi.VDBImport) DataPolicy(org.teiid.adminapi.DataPolicy)

Example 3 with VDBImport

use of org.teiid.adminapi.VDBImport in project teiid by teiid.

the class TestVDBUtility method validateVDB.

public static void validateVDB(VDBMetaData vdb) {
    ModelMetaData modelOne;
    ModelMetaData modelTwo;
    // $NON-NLS-1$
    assertEquals("myVDB", vdb.getName());
    // $NON-NLS-1$
    assertEquals("vdb description", vdb.getDescription());
    assertEquals("connection-type", "NONE", vdb.getConnectionType().name());
    assertEquals("1", vdb.getVersion());
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("vdb-value", vdb.getPropertyValue("vdb-property"));
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("vdb-value2", vdb.getPropertyValue("vdb-property2"));
    // $NON-NLS-1$
    assertNotNull(vdb.getModel("model-one"));
    // $NON-NLS-1$
    assertNotNull(vdb.getModel("model-two"));
    // $NON-NLS-1$
    assertNull(vdb.getModel("model-unknown"));
    assertEquals(1, vdb.getVDBImports().size());
    VDBImport vdbImport = vdb.getVDBImports().get(0);
    assertEquals("x", vdbImport.getName());
    assertEquals("2", vdbImport.getVersion());
    // $NON-NLS-1$
    modelOne = vdb.getModel("model-one");
    // $NON-NLS-1$
    assertEquals("model-one", modelOne.getName());
    // $NON-NLS-1$
    assertEquals("s1", modelOne.getSourceNames().get(0));
    assertEquals(Model.Type.PHYSICAL, modelOne.getModelType());
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("model-value-override", modelOne.getPropertyValue("model-prop"));
    assertFalse(modelOne.isVisible());
    assertEquals("model description", modelOne.getDescription());
    assertEquals("DDL", modelOne.getSourceMetadataType().get(0));
    assertEquals("DDL Here", modelOne.getSourceMetadataText().get(0));
    assertEquals("OTHER", modelOne.getSourceMetadataType().get(1));
    assertEquals("other text", modelOne.getSourceMetadataText().get(1));
    // $NON-NLS-1$
    modelTwo = vdb.getModel("model-two");
    // $NON-NLS-1$
    assertEquals("model-two", modelTwo.getName());
    // $NON-NLS-1$
    assertTrue(modelTwo.getSourceNames().contains("s1"));
    // $NON-NLS-1$
    assertTrue(modelTwo.getSourceNames().contains("s2"));
    // this is not persisted in the XML
    assertEquals(Model.Type.VIRTUAL, modelTwo.getModelType());
    // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals("model-value", modelTwo.getPropertyValue("model-prop"));
    assertEquals("DDL", modelTwo.getSourceMetadataType().get(0));
    assertEquals("DDL Here", modelTwo.getSourceMetadataText().get(0));
    // $NON-NLS-1$
    assertTrue(vdb.getValidityErrors().contains("There is an error in VDB"));
    List<Translator> translators = vdb.getOverrideTranslators();
    assertTrue(translators.size() == 1);
    Translator translator = translators.get(0);
    assertEquals("oracleOverride", translator.getName());
    assertEquals("oracle", translator.getType());
    assertEquals("my-value", translator.getPropertyValue("my-property"));
    assertEquals("hello world", translator.getDescription());
    List<DataPolicy> roles = vdb.getDataPolicies();
    assertTrue(roles.size() == 1);
    // $NON-NLS-1$
    DataPolicyMetadata role = vdb.getDataPolicyMap().get("roleOne");
    assertTrue(role.isGrantAll());
    assertTrue(role.isAllowCreateTemporaryTables());
    // $NON-NLS-1$
    assertEquals("roleOne described", role.getDescription());
    assertNotNull(role.getMappedRoleNames());
    // $NON-NLS-1$
    assertTrue(role.getMappedRoleNames().contains("ROLE1"));
    // $NON-NLS-1$
    assertTrue(role.getMappedRoleNames().contains("ROLE2"));
    List<DataPolicy.DataPermission> permissions = role.getPermissions();
    assertEquals(4, permissions.size());
    boolean lang = false;
    for (DataPolicy.DataPermission p : permissions) {
        if (p.getAllowLanguage() != null) {
            assertTrue(p.getAllowLanguage());
            assertEquals("javascript", p.getResourceName());
            lang = true;
            continue;
        }
        if (p.getResourceName().equalsIgnoreCase("myTable.T1")) {
            // $NON-NLS-1$
            assertTrue(p.getAllowRead());
            assertNull(p.getAllowDelete());
            continue;
        }
        if (p.getResourceName().equalsIgnoreCase("myTable.T2.col1")) {
            // $NON-NLS-1$
            assertEquals("col2", p.getMask());
            assertEquals(1, p.getOrder().intValue());
            continue;
        }
        assertFalse(p.getAllowRead());
        assertTrue(p.getAllowDelete());
        assertEquals("col1 = user()", p.getCondition());
        assertFalse(p.getConstraint());
    }
    assertTrue(lang);
}
Also used : Translator(org.teiid.adminapi.Translator) VDBImport(org.teiid.adminapi.VDBImport) DataPolicy(org.teiid.adminapi.DataPolicy)

Example 4 with VDBImport

use of org.teiid.adminapi.VDBImport in project teiid by teiid.

the class CompositeVDB method buildCompositeState.

private void buildCompositeState(VDBRepository vdbRepository) throws VirtualDatabaseException {
    if (vdb.getVDBImports().isEmpty()) {
        this.vdb.addAttchment(ConnectorManagerRepository.class, this.cmr);
        return;
    }
    VDBMetaData newMergedVDB = this.vdb.clone();
    ConnectorManagerRepository mergedRepo = this.cmr;
    if (!this.cmr.isShared()) {
        mergedRepo = new ConnectorManagerRepository();
        mergedRepo.getConnectorManagers().putAll(this.cmr.getConnectorManagers());
    }
    newMergedVDB.addAttchment(ConnectorManagerRepository.class, mergedRepo);
    ClassLoader[] toSearch = new ClassLoader[vdb.getVDBImports().size() + 1];
    toSearch[0] = this.vdb.getAttachment(ClassLoader.class);
    this.children = new LinkedHashMap<VDBKey, CompositeVDB>();
    newMergedVDB.setImportedModels(new TreeSet<String>(String.CASE_INSENSITIVE_ORDER));
    int i = 1;
    for (VDBImport vdbImport : vdb.getVDBImports()) {
        VDBKey key = new VDBKey(vdbImport.getName(), vdbImport.getVersion());
        if (key.isAtMost()) {
            // TODO: could allow partial versions
            throw new VirtualDatabaseException(RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40144, vdbKey, key));
        }
        CompositeVDB importedVDB = vdbRepository.getCompositeVDB(key);
        if (importedVDB == null) {
            throw new VirtualDatabaseException(RuntimePlugin.Event.TEIID40083, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40083, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion()));
        }
        VDBMetaData childVDB = importedVDB.getVDB();
        newMergedVDB.getVisibilityOverrides().putAll(childVDB.getVisibilityOverrides());
        toSearch[i++] = childVDB.getAttachment(ClassLoader.class);
        this.children.put(importedVDB.getVDBKey(), importedVDB);
        if (vdbImport.isImportDataPolicies()) {
            for (DataPolicy dp : importedVDB.getVDB().getDataPolicies()) {
                DataPolicyMetadata role = (DataPolicyMetadata) dp;
                if (newMergedVDB.addDataPolicy(role) != null) {
                    throw new VirtualDatabaseException(RuntimePlugin.Event.TEIID40084, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40084, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), role.getName()));
                }
                if (role.isGrantAll()) {
                    role.setSchemas(childVDB.getModelMetaDatas().keySet());
                }
            }
        }
        // add models
        for (ModelMetaData m : childVDB.getModelMetaDatas().values()) {
            if (newMergedVDB.addModel(m) != null) {
                throw new VirtualDatabaseException(RuntimePlugin.Event.TEIID40085, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40085, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), m.getName()));
            }
            newMergedVDB.getImportedModels().add(m.getName());
            // $NON-NLS-1$
            String visibilityOverride = newMergedVDB.getPropertyValue(m.getName() + ".visible");
            if (visibilityOverride != null) {
                boolean visible = Boolean.valueOf(visibilityOverride);
                newMergedVDB.setVisibilityOverride(m.getName(), visible);
            }
        }
        ConnectorManagerRepository childCmr = childVDB.getAttachment(ConnectorManagerRepository.class);
        if (childCmr == null) {
            // $NON-NLS-1$
            throw new AssertionError("childVdb does not have a connector manager repository");
        }
        if (!this.cmr.isShared()) {
            for (Map.Entry<String, ConnectorManager> entry : childCmr.getConnectorManagers().entrySet()) {
                if (mergedRepo.getConnectorManagers().put(entry.getKey(), entry.getValue()) != null) {
                    throw new VirtualDatabaseException(RuntimePlugin.Event.TEIID40086, RuntimePlugin.Util.gs(RuntimePlugin.Event.TEIID40086, vdb.getName(), vdb.getVersion(), vdbImport.getName(), vdbImport.getVersion(), entry.getKey()));
                }
            }
        }
    }
    if (toSearch[0] != null) {
        CombinedClassLoader ccl = new CombinedClassLoader(toSearch[0].getParent(), toSearch);
        this.mergedVDB.addAttchment(ClassLoader.class, ccl);
    }
    this.mergedVDB = newMergedVDB;
}
Also used : ConnectorManagerRepository(org.teiid.dqp.internal.datamgr.ConnectorManagerRepository) ConnectorManager(org.teiid.dqp.internal.datamgr.ConnectorManager) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) VDBKey(org.teiid.vdb.runtime.VDBKey) DataPolicyMetadata(org.teiid.adminapi.impl.DataPolicyMetadata) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) VDBImport(org.teiid.adminapi.VDBImport) DataPolicy(org.teiid.adminapi.DataPolicy) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

VDBImport (org.teiid.adminapi.VDBImport)4 DataPolicy (org.teiid.adminapi.DataPolicy)3 Translator (org.teiid.adminapi.Translator)2 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)2 VDBMetaData (org.teiid.adminapi.impl.VDBMetaData)2 VDBKey (org.teiid.vdb.runtime.VDBKey)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ScriptEngineManager (javax.script.ScriptEngineManager)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 ModuleClassLoader (org.jboss.modules.ModuleClassLoader)1 VirtualFile (org.jboss.vfs.VirtualFile)1 Model (org.teiid.adminapi.Model)1 DataPolicyMetadata (org.teiid.adminapi.impl.DataPolicyMetadata)1 VDBTranslatorMetaData (org.teiid.adminapi.impl.VDBTranslatorMetaData)1 TeiidException (org.teiid.core.TeiidException)1