Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(all): refactor and add new endpoints for topologies
This commit adds new REST endpoints to query available topologies: - GET /api/v1/topologies - GET /api/v1/topologies/:type/versions - GET /api/v1/topologies/:type/versions/:version
- Loading branch information
1 parent
acb82f5
commit 4f67fc6
Showing
11 changed files
with
379 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
azkarra-api/src/main/java/io/streamthoughts/azkarra/api/model/TopologyAndAliases.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2019-2020 StreamThoughts. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.streamthoughts.azkarra.api.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public class TopologyAndAliases { | ||
|
||
private String type; | ||
private Set<String> aliases; | ||
|
||
public TopologyAndAliases(final String type, | ||
final Set<String> aliases) { | ||
this.type = Objects.requireNonNull(type, "type cannot be null"); | ||
this.aliases = Objects.requireNonNull(aliases, "aliases cannot be null");; | ||
} | ||
|
||
@JsonProperty("type") | ||
public String type() { | ||
return type; | ||
} | ||
|
||
@JsonProperty("aliases") | ||
public Set<String> aliases() { | ||
return aliases; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof TopologyAndAliases)) return false; | ||
TopologyAndAliases that = (TopologyAndAliases) o; | ||
return Objects.equals(type, that.type) && | ||
Objects.equals(aliases, that.aliases); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(type, aliases); | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
...rc/main/java/io/streamthoughts/azkarra/runtime/service/AbstractAzkarraStreamsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright 2019-2020 StreamThoughts. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.streamthoughts.azkarra.runtime.service; | ||
|
||
import io.streamthoughts.azkarra.api.AzkarraContext; | ||
import io.streamthoughts.azkarra.api.AzkarraStreamsService; | ||
import io.streamthoughts.azkarra.api.components.ComponentDescriptor; | ||
import io.streamthoughts.azkarra.api.components.ComponentFactory; | ||
import io.streamthoughts.azkarra.api.components.Qualifier; | ||
import io.streamthoughts.azkarra.api.components.qualifier.Qualifiers; | ||
import io.streamthoughts.azkarra.api.errors.NoSuchComponentException; | ||
import io.streamthoughts.azkarra.api.model.TopologyAndAliases; | ||
import io.streamthoughts.azkarra.api.providers.TopologyDescriptor; | ||
import io.streamthoughts.azkarra.api.streams.TopologyProvider; | ||
import io.streamthoughts.azkarra.api.util.Version; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class AbstractAzkarraStreamsService implements AzkarraStreamsService { | ||
|
||
protected final AzkarraContext context; | ||
/** | ||
* Creates a new {@link AbstractAzkarraStreamsService} instance. | ||
* @param context the {@link AzkarraContext} instance. | ||
*/ | ||
AbstractAzkarraStreamsService(final AzkarraContext context) { | ||
this.context = Objects.requireNonNull(context, "context cannot be null"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Set<TopologyDescriptor> getTopologyProviders() { | ||
return context.topologyProviders(); | ||
} | ||
|
||
public List<TopologyAndAliases> getAllTopologies() { | ||
Map<String, Set<String>> topologies = new HashMap<>(); | ||
var factory = context.getComponentFactory(); | ||
var descriptors = factory.findAllDescriptorsByClass(TopologyProvider.class); | ||
|
||
for (ComponentDescriptor<TopologyProvider> descriptor : descriptors) { | ||
TopologyDescriptor<?> topology = new TopologyDescriptor<>(descriptor); | ||
Set<String> aliases = topologies.get(topology.className()); | ||
if (aliases == null) { | ||
aliases = topology.aliases(); | ||
} else { | ||
aliases.retainAll(topology.aliases()); | ||
} | ||
topologies.put(topology.className(), aliases); | ||
} | ||
|
||
return topologies.entrySet() | ||
.stream() | ||
.map(entry -> new TopologyAndAliases(entry.getKey(), entry.getValue())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public TopologyDescriptor getTopologyByAliasAndVersion(final String alias, | ||
final String version) { | ||
Objects.requireNonNull(alias, "alias cannot be null"); | ||
Objects.requireNonNull(version, "version cannot be null"); | ||
|
||
Qualifier<? extends TopologyProvider> qualifier = version.equalsIgnoreCase("latest") | ||
? Qualifiers.byLatestVersion() : Qualifiers.byVersion(version); | ||
return getTopologyByAliasAndQualifiers(alias, qualifier); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
@SuppressWarnings("unchecked") | ||
public TopologyDescriptor getTopologyByAliasAndQualifiers(final String alias, | ||
final Qualifier<? extends TopologyProvider> qualifier) { | ||
ComponentFactory factory = context.getComponentFactory(); | ||
var optional = factory.findDescriptorByAlias(alias, qualifier); | ||
if (optional.isEmpty()) { | ||
throw NoSuchComponentException.forAliasAndQualifier(alias, qualifier); | ||
} | ||
|
||
var descriptor = optional.get(); | ||
if (! TopologyProvider.class.isAssignableFrom(descriptor.type())) { | ||
throw new NoSuchComponentException( | ||
"Component for alias '"+ alias + "' and '" + qualifier + "' is not a sub-type of TopologyProvider"); | ||
} | ||
return new TopologyDescriptor(descriptor); | ||
} | ||
|
||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public List<Version> getTopologyVersionsByAlias(final String alias) { | ||
Objects.requireNonNull(alias, "alias cannot be null"); | ||
ComponentFactory factory = context.getComponentFactory(); | ||
Collection<ComponentDescriptor<Object>> descriptors = factory.findAllDescriptorsByAlias(alias); | ||
if (descriptors.isEmpty()) { | ||
throw NoSuchComponentException.forAlias(alias); | ||
} | ||
|
||
return descriptors | ||
.stream() | ||
.map(ComponentDescriptor::version) | ||
.sorted() | ||
.collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.