58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal/*
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * CDDL HEADER START
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The contents of this file are subject to the terms of the
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Common Development and Distribution License, Version 1.0 only
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * (the "License"). You may not use this file except in compliance
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * with the License.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * or http://forgerock.org/license/CDDLv1.0.html.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * See the License for the specific language governing permissions
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * and limitations under the License.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * When distributing Covered Code, include this CDDL HEADER in each
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * file and include the License file at legal-notices/CDDLv1_0.txt.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * If applicable, add the following below this CDDL HEADER, with the
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * fields enclosed by brackets "[]" replaced with your own identifying
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * information:
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Portions Copyright [yyyy] [name of copyright owner]
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * CDDL HEADER END
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Copyright 2015 ForgeRock AS
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalpackage org.opends.server.tools;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismalimport java.io.File;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport java.util.Collection;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport java.util.LinkedList;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport java.util.List;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.config.LDAPProfile;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.config.ManagedObjectDefinition;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.config.client.ManagementContext;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.config.client.ldap.LDAPManagementContext;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.ldap.DN;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.client.BackendCfgClient;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.client.BackendIndexCfgClient;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.client.PluggableBackendCfgClient;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.client.RootCfgClient;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.meta.BackendCfgDefn.WritabilityMode;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.meta.BackendIndexCfgDefn.IndexType;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.forgerock.opendj.server.config.server.BackendCfg;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.opends.guitools.controlpanel.util.Utilities;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalimport org.opends.quicksetup.Installation;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
8196da2152209c8c628611aeeb4adae32079921eJean-Noël Rouvignac/** Utility class which can be used by tools to create a new backend with default indexes. */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismalpublic class BackendCreationHelper
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal{
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /** Describes an attribute index which should be created during installation. */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public static final class DefaultIndex
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal static DefaultIndex withEqualityAndSubstring(final String name)
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal return new DefaultIndex(name, true);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal static DefaultIndex withEquality(final String name)
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal return new DefaultIndex(name, false);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal private final String name;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal private final boolean shouldCreateSubstringIndex;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal private DefaultIndex(final String name, final boolean substringIndex)
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal this.name = name;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal this.shouldCreateSubstringIndex = substringIndex;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /**
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Return the name of this default index.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @return The name of this default index
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public String getName()
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal return name;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /**
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Return {@code true} if the substring index type should be enabled for
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * this index.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @return {@code true} if the substring index type should be enabled for
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * this index.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public boolean shouldCreateSubstringIndex()
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal return shouldCreateSubstringIndex;
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /** Default indexes to add in a new backend. */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public static final DefaultIndex[] DEFAULT_INDEXES = {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEqualityAndSubstring("cn"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEqualityAndSubstring("givenName"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEqualityAndSubstring("mail"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEqualityAndSubstring("sn"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEqualityAndSubstring("telephoneNumber"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEquality("member"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEquality("uid"),
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal DefaultIndex.withEquality("uniqueMember")
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal };
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /**
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Add a new backend with the provided name in the config.ldif file.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param backendName
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The new backend name
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param baseDNs
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The base dns to add in the new backend.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param backendType
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The backend type
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @throws Exception
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * If any problems occurred
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public static void createBackendOffline(String backendName, Collection<DN> baseDNs,
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal ManagedObjectDefinition<? extends BackendCfgClient, ? extends BackendCfg> backendType) throws Exception
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal Utilities.initializeConfigurationFramework();
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal final File configFile = Installation.getLocal().getCurrentConfigurationFile();
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal final LDAPProfile ldapProfile = LDAPProfile.getInstance();
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal try (ManagementContext context = LDAPManagementContext.newLDIFManagementContext(configFile, ldapProfile))
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal {
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal createBackend(context.getRootConfiguration(), backendName, baseDNs, backendType);
1b12eb719f2aeec7624182b983d79fc949d7cea2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal /**
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * Create a backend with the provided name using the provided
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * {@code RootCfgClient}.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal *
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param rootConfiguration
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The root configuration to use to create the new backend
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param backendName
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The new backend name
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param baseDNs
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The base dns to add in the new backend.
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @param backendType
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * The backend type
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * @throws Exception
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal * If any problems occurred
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal */
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal public static void createBackend(RootCfgClient rootConfiguration, String backendName, Collection<DN> baseDNs,
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal ManagedObjectDefinition<? extends BackendCfgClient, ? extends BackendCfg> backendType) throws Exception
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal final BackendCfgClient backendCfgClient = rootConfiguration.createBackend(backendType, backendName, null);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal backendCfgClient.setEnabled(true);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal backendCfgClient.setBaseDN(baseDNs);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal backendCfgClient.setWritabilityMode(WritabilityMode.ENABLED);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal backendCfgClient.commit();
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
8196da2152209c8c628611aeeb4adae32079921eJean-Noël Rouvignac addBackendDefaultIndexes((PluggableBackendCfgClient) backendCfgClient);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal private static void addBackendDefaultIndexes(PluggableBackendCfgClient backendCfgClient) throws Exception
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal for (DefaultIndex defaultIndex : DEFAULT_INDEXES)
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal final BackendIndexCfgClient index =
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal backendCfgClient.createBackendIndex(BackendIndexCfgDefn.getInstance(), defaultIndex.name, null);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal final List<IndexType> indexTypes = new LinkedList<>();
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal indexTypes.add(IndexType.EQUALITY);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal if (defaultIndex.shouldCreateSubstringIndex)
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal {
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal indexTypes.add(IndexType.SUBSTRING);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal index.setIndexType(indexTypes);
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal index.commit();
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal }
58821a211f38cf48a75a9cbc36b472d95b035ce2Gaetan Boismal}