package com.dexels.kafka.webapi; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import com.fasterxml.jackson.annotation.JsonProperty; public class TopicStructure { public class Tenant { @JsonProperty private Map deployments = new HashMap<>(); public Tenant() { } public Deployment getDeployment(String name) { Deployment d = deployments.get(name); if(d==null) { final Deployment deployment = new Deployment(); deployments.put(name, deployment); d = deployment; } return d; } } public class Deployment { @JsonProperty private final Map generations = new HashMap<>(); @JsonProperty private final Set nonGenerationalTopics = new HashSet<>(); public Deployment() { } public Set getNonGenerationalTopics() { return Collections.unmodifiableSet(this.nonGenerationalTopics); } @JsonProperty public int nonGenerationaltopicCount() { return this.nonGenerationalTopics.size(); } public void addNonGenerationalTopic(String topic) { nonGenerationalTopics.add(topic); } public Generation getGeneration(String generationName) { Generation g = generations.get(generationName); if(g==null) { g = new Generation(); generations.put(generationName, g); } return g; } public Set getGenerations() { return generations.keySet(); } } public class Generation { @JsonProperty private final Set topics = new HashSet<>(); public void addTopic(String topic) { this.topics.add(topic); } public Set getTopics() { return Collections.unmodifiableSet(this.topics); } @JsonProperty public int topicCount() { return this.topics.size(); } } @JsonProperty private Map tenants = new HashMap<>(); @JsonProperty private Set otherTopics = new HashSet<>(); public Collection tenants() { return tenants.values(); } public Tenant getTenant(String tenantName) { Tenant t = tenants.get(tenantName); if(t==null) { t = new Tenant(); tenants.put(tenantName, t); } return t; } public TopicStructure consumeTopic(String topicOriginal) { String topic; if(topicOriginal.startsWith("highlevel-")) { topic = topicOriginal.substring("highlevel-".length(), topicOriginal.length()); } else if (topicOriginal.startsWith("lowlevel-")) { topic = topicOriginal.substring("lowlevel-".length(), topicOriginal.length()); } else { topic = topicOriginal; } String[] parts = topic.split("-"); if(parts.length<3 || topic.startsWith("NAVAJO-")) { otherTopics.add(topicOriginal); } else { Deployment d = getTenant(parts[0]).getDeployment(parts[1]); if(parts[2].equals("generation")) { d.getGeneration(parts[3]).addTopic(topicOriginal); } else { d.addNonGenerationalTopic(topicOriginal); } } return this; } public Set tenantNames() { return tenants.keySet(); } }