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.kura;
013
014import static java.util.Objects.requireNonNull;
015import static org.eclipse.kapua.gateway.client.Topic.ensureNotSpecial;
016
017import java.util.stream.Collectors;
018import java.util.stream.Stream;
019
020import org.eclipse.kapua.gateway.client.Topic;
021import org.eclipse.kapua.gateway.client.mqtt.MqttNamespace;
022
023public class KuraNamespace implements MqttNamespace {
024
025    public static final class Builder {
026
027        private String accountName;
028
029        public Builder accountName(final String accountName) {
030            requireNonNull(accountName);
031            ensureNotSpecial(accountName);
032
033            this.accountName = accountName;
034            return this;
035        }
036
037        public String accountName() {
038            return this.accountName;
039        }
040
041        public KuraNamespace build() {
042            if (this.accountName == null || this.accountName.isEmpty()) {
043                throw new IllegalArgumentException("'accountName' must be set");
044            }
045
046            return new KuraNamespace(this.accountName);
047        }
048    }
049
050    private final String accountName;
051
052    private KuraNamespace(final String accountName) {
053        this.accountName = accountName;
054    }
055
056    @Override
057    public String dataTopic(final String clientId, final String applicationId, final Topic topic) {
058        ensureNotSpecial(clientId);
059        ensureNotSpecial(applicationId);
060
061        return Stream.concat(
062                Stream.of(
063                        this.accountName,
064                        clientId,
065                        applicationId),
066                topic.stream())
067                .collect(Collectors.joining("/"));
068    }
069
070}