001/*******************************************************************************
002 * Copyright (c) 2017 Red Hat Inc and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Red Hat Inc - initial API and implementation
011 *******************************************************************************/
012package org.eclipse.kapua.gateway.client.profile.kura;
013
014import static java.util.Objects.requireNonNull;
015
016import java.util.function.Consumer;
017import java.util.function.Supplier;
018
019import org.eclipse.kapua.gateway.client.Client;
020import org.eclipse.kapua.gateway.client.Credentials.UserAndPassword;
021import org.eclipse.kapua.gateway.client.kura.KuraBinaryPayloadCodec;
022import org.eclipse.kapua.gateway.client.kura.KuraBirthCertificateModule;
023import org.eclipse.kapua.gateway.client.kura.KuraNamespace;
024import org.eclipse.kapua.gateway.client.mqtt.MqttClient;
025
026public class KuraMqttProfile<B extends MqttClient.Builder<B>> {
027
028    public static <B extends MqttClient.Builder<B>> KuraMqttProfile<B> newProfile(final Supplier<B> builderSupplier) {
029        requireNonNull(builderSupplier);
030        return new KuraMqttProfile<>(builderSupplier);
031    }
032
033    private final Supplier<B> builderSupplier;
034    private String accountName;
035    private String brokerUrl;
036    private String clientId;
037    private UserAndPassword userAndPassword;
038    private Consumer<B> customizer;
039
040    private KuraMqttProfile(final Supplier<B> builderSupplier) {
041        this.builderSupplier = builderSupplier;
042    }
043
044    public KuraMqttProfile<B> accountName(final String accountName) {
045        this.accountName = accountName;
046        return this;
047    }
048
049    public KuraMqttProfile<B> brokerUrl(final String brokerUrl) {
050        this.brokerUrl = brokerUrl;
051        return this;
052    }
053
054    public KuraMqttProfile<B> customizer(Consumer<B> customizer) {
055        this.customizer = customizer;
056        return this;
057    }
058
059    public KuraMqttProfile<B> clientId(final String clientId) {
060        this.clientId = clientId;
061        return this;
062    }
063
064    public KuraMqttProfile<B> credentials(final UserAndPassword userAndPassword) {
065        this.userAndPassword = userAndPassword;
066        return this;
067    }
068
069    public Client build() throws Exception {
070        validate();
071
072        B builder = builderSupplier.get()
073                .clientId(this.clientId)
074                .broker(this.brokerUrl)
075                .credentials(this.userAndPassword)
076                .codec(new KuraBinaryPayloadCodec.Builder().build())
077                .namespace(
078                        new KuraNamespace.Builder()
079                                .accountName(this.accountName)
080                                .build())
081                .module(
082                        KuraBirthCertificateModule.newBuilder(this.accountName)
083                                .defaultProviders()
084                                .build());
085
086        if (customizer != null) {
087            customizer.accept(builder);
088        }
089
090        return builder.build();
091    }
092
093    private void validate() {
094        hasString(this.accountName, "accountName");
095        hasString(this.brokerUrl, "brokerUrl");
096        hasString(this.clientId, "clientId");
097    }
098
099    private static void hasString(final String value, final String name) {
100        if (value == null || value.isEmpty()) {
101            throw new IllegalStateException(String.format("'%s' must be set", value));
102        }
103    }
104}