Search in sources :

Example 6 with Group

use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.

the class ZooKeeperGroupTest method testMaster_withStaleNodes2.

@Test
public void testMaster_withStaleNodes2() throws Exception {
    // stale
    putChildData(group, PATH + "/001", "container1");
    putChildData(group, PATH + "/002", "container2");
    putChildData(group, PATH + "/003", "container1");
    // stale
    putChildData(group, PATH + "/004", "container3");
    putChildData(group, PATH + "/005", "container3");
    NodeState master = group.master();
    assertThat(master, notNullValue());
    assertThat(master.getContainer(), equalTo("container2"));
}
Also used : NodeState(io.fabric8.groups.NodeState) Test(org.junit.Test)

Example 7 with Group

use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.

the class ZooKeeperGroupTest method putChildData.

private static void putChildData(ZooKeeperGroup<NodeState> group, String path, String container) throws Exception {
    NodeState node = new NodeState("test", container);
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).writeValue(data, node);
    ChildData<NodeState> child = new ChildData<>(path, new Stat(), data.toByteArray(), node);
    group.currentData.put(path, child);
}
Also used : NodeState(io.fabric8.groups.NodeState) Stat(org.apache.zookeeper.data.Stat) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with Group

use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.

the class ZooKeeperGroupTest method setUp.

@Before
public void setUp() throws Exception {
    int port = findFreePort();
    curator = CuratorFrameworkFactory.builder().connectString("localhost:" + port).retryPolicy(new RetryOneTime(1)).build();
    // curator.start();
    group = new ZooKeeperGroup<>(curator, PATH, NodeState.class);
// group.start();
// Starting curator and group is not necessary for the current tests.
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) NodeState(io.fabric8.groups.NodeState) Before(org.junit.Before)

Example 9 with Group

use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.

the class AutoScaleController method groupEvent.

@Override
public void groupEvent(Group<AutoScalerNode> group, GroupEvent event) {
    DataStore dataStore = fabricService.get().adapt(DataStore.class);
    switch(event) {
        case CONNECTED:
        case CHANGED:
            if (isValid()) {
                AutoScalerNode state = createState();
                try {
                    if (group.isMaster()) {
                        enableMasterZkCache(curator.get());
                        LOGGER.info("AutoScaleController is the master");
                        group.update(state);
                        dataStore.trackConfiguration(runnable);
                        enableTimer();
                        onConfigurationChanged();
                    } else {
                        LOGGER.info("AutoScaleController is not the master");
                        group.update(state);
                        disableTimer();
                        dataStore.untrackConfiguration(runnable);
                        disableMasterZkCache();
                    }
                } catch (IllegalStateException e) {
                // Ignore
                }
            } else {
                LOGGER.info("Not valid with master: " + group.isMaster() + " fabric: " + fabricService.get() + " curator: " + curator.get());
            }
            break;
        case DISCONNECTED:
            dataStore.untrackConfiguration(runnable);
    }
}
Also used : DataStore(io.fabric8.api.DataStore)

Example 10 with Group

use of io.fabric8.groups.Group in project fabric8 by jboss-fuse.

the class MQServiceImpl method createOrUpdateMQProfile.

@Override
public Profile createOrUpdateMQProfile(String versionId, String profileId, String brokerName, Map<String, String> configs, boolean replicated) {
    Version version = profileService.getRequiredVersion(versionId);
    String parentProfileName = null;
    if (configs != null && configs.containsKey("parent")) {
        parentProfileName = configs.remove("parent");
    }
    if (Strings.isNullOrBlank(parentProfileName)) {
        parentProfileName = replicated ? MQ_PROFILE_REPLICATED : MQ_PROFILE_BASE;
    }
    Profile parentProfile = version.getRequiredProfile(parentProfileName);
    if (brokerName == null || profileId == null) {
        return parentProfile;
    }
    String pidName = getBrokerPID(brokerName);
    // lets check we have a config value
    ProfileBuilder builder;
    Profile overlay;
    // create a profile if it doesn't exist
    Map<String, String> config = null;
    boolean create = !version.hasProfile(profileId);
    if (create) {
        builder = ProfileBuilder.Factory.create(versionId, profileId);
        if (parentProfile != null) {
            builder.addParent(parentProfile.getId());
        }
        overlay = profileService.getOverlayProfile(parentProfile);
    } else {
        Profile profile = version.getRequiredProfile(profileId);
        builder = ProfileBuilder.Factory.createFrom(profile);
        config = builder.getConfiguration(pidName);
        overlay = profileService.getOverlayProfile(profile);
    }
    Map<String, String> parentProfileConfig = ProfileBuilder.Factory.createFrom(overlay).getConfiguration(MQ_PID_TEMPLATE);
    if (config == null) {
        config = parentProfileConfig;
    }
    if (configs != null && "true".equals(configs.get("ssl"))) {
        // Only generate the keystore file if it does not exist.
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] keystore = overlay.getFileConfiguration("keystore.jks");
        if (keystore == null) {
            try {
                String host = configs.get("keystore.cn");
                if (host == null) {
                    host = configs.get(GROUP);
                    if (host == null) {
                        host = "localhost";
                    }
                    configs.put("keystore.cn", host);
                }
                String password = configs.get("keystore.password");
                if (password == null) {
                    password = generatePassword(8);
                    configs.put("keystore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                keystoreFile.delete();
                LOG.info("Generating ssl keystore...");
                int rc = system("keytool", "-genkey", "-storetype", "JKS", "-storepass", password, "-keystore", keystoreFile.getCanonicalPath(), "-keypass", password, "-alias", host, "-keyalg", "RSA", "-keysize", "4096", "-dname", String.format("cn=%s", host), "-validity", "3650");
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                keystore = Files.readBytes(keystoreFile);
                keystoreFile.delete();
                LOG.info("Keystore generated");
                builder.addFileConfiguration("keystore.jks", keystore);
                configs.put("keystore.file", "profile:keystore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate keystore.jks: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        // [TOOD] Fix direct data access! This should be part of the ProfileBuilder
        byte[] truststore = overlay.getFileConfiguration("truststore.jks");
        if (truststore == null && configs.get("keystore.password") != null) {
            try {
                String password = configs.get("truststore.password");
                if (password == null) {
                    password = configs.get("keystore.password");
                    configs.put("truststore.password", password);
                }
                File keystoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                Files.writeToFile(keystoreFile, keystore);
                File certFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                certFile.delete();
                LOG.info("Exporting broker certificate to create truststore.jks");
                int rc = system("keytool", "-exportcert", "-rfc", "-keystore", keystoreFile.getCanonicalPath(), "-storepass", configs.get("keystore.password"), "-alias", configs.get("keystore.cn"), "--file", certFile.getCanonicalPath());
                keystoreFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                LOG.info("Creating truststore.jks");
                File truststoreFile = io.fabric8.utils.Files.createTempFile(runtimeProperties.getDataPath());
                truststoreFile.delete();
                rc = system("keytool", "-importcert", "-noprompt", "-keystore", truststoreFile.getCanonicalPath(), "-storepass", password, "--file", certFile.getCanonicalPath());
                certFile.delete();
                if (rc != 0) {
                    throw new IOException("keytool failed with exit code: " + rc);
                }
                truststore = Files.readBytes(truststoreFile);
                truststoreFile.delete();
                builder.addFileConfiguration("truststore.jks", truststore);
                configs.put("truststore.file", "profile:truststore.jks");
            } catch (IOException e) {
                LOG.error("Failed to generate truststore.jks due: " + e.getMessage(), e);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
    config.put("broker-name", brokerName);
    if (configs != null) {
        config.putAll(configs);
    }
    // lets check we've a bunch of config values inherited from the template
    String[] propertiesToDefault = { CONFIG_URL, STANDBY_POOL, CONNECTORS };
    for (String key : propertiesToDefault) {
        if (config.get(key) == null) {
            String defaultValue = parentProfileConfig.get(key);
            if (Strings.isNotBlank(defaultValue)) {
                config.put(key, defaultValue);
            }
        }
    }
    // config map is not from "official" profile, so it doesn't have to use felix' Properties class
    builder.addConfiguration(pidName, config);
    Profile profile = builder.getProfile();
    return create ? profileService.createProfile(profile) : profileService.updateProfile(profile);
}
Also used : Version(io.fabric8.api.Version) IOException(java.io.IOException) ProfileBuilder(io.fabric8.api.ProfileBuilder) File(java.io.File) Profile(io.fabric8.api.Profile)

Aggregations

Group (org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group)50 Test (org.junit.Test)28 ArrayList (java.util.ArrayList)27 FlowCapableNode (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode)18 ActionGroup (org.opendaylight.genius.mdsalutil.actions.ActionGroup)15 GroupKey (org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey)15 CuratorFramework (org.apache.curator.framework.CuratorFramework)12 Bucket (org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket)11 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)11 IOException (java.io.IOException)9 StaleGroup (org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.StaleGroup)9 Nodes (org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 RetryNTimes (org.apache.curator.retry.RetryNTimes)8 NodeState (io.fabric8.groups.NodeState)7 Map (java.util.Map)7 ItemSyncBox (org.opendaylight.openflowplugin.applications.frsync.util.ItemSyncBox)7 BigInteger (java.math.BigInteger)6 HashMap (java.util.HashMap)6 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)6