diff options
Diffstat (limited to 'models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider')
4 files changed, 0 insertions, 616 deletions
diff --git a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/ConsumerGroupData.java b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/ConsumerGroupData.java deleted file mode 100644 index 3acaf0888..000000000 --- a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/ConsumerGroupData.java +++ /dev/null @@ -1,190 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.sim.dmaap.provider; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Data associated with a consumer group. All consumer instances within a group share the - * same data object. - */ -public class ConsumerGroupData { - private static final Logger logger = LoggerFactory.getLogger(ConsumerGroupData.class); - - /** - * Returned when messages can no longer be read from this consumer group object, - * because it is being removed from the topic. {@link #UNREADABLE_LIST} must not be - * the same list as Collections.emptyList(), thus we wrap it. - */ - public static final List<String> UNREADABLE_LIST = Collections.unmodifiableList(Collections.emptyList()); - - /** - * Returned when there are no messages read. Collections.emptyList() is already - * unmodifiable, thus no need to wrap it. - */ - private static final List<String> EMPTY_LIST = Collections.emptyList(); - - /** - * This is locked while fields other than {@link #messageQueue} are updated. - */ - private final Object lockit = new Object(); - - /** - * Number of sweep cycles that have occurred since a consumer has attempted to read - * from the queue. This consumer group should be removed once this count exceeds - * {@code 1}, provided {@link #nreaders} is zero. - */ - private int nsweeps = 0; - - /** - * Number of consumers that are currently attempting to read from the queue. This - * consumer group should not be removed as long as this is non-zero. - */ - private int nreaders = 0; - - /** - * Message queue. - */ - private final BlockingQueue<String> messageQueue = new LinkedBlockingQueue<>(); - - - /** - * Constructs the object. - * - * @param topicName name of the topic with which this object is associated - * @param groupName name of the consumer group with which this object is associated - */ - public ConsumerGroupData(String topicName, String groupName) { - logger.info("Topic {}: add consumer group: {}", topicName, groupName); - } - - /** - * Determines if this consumer group should be removed. This should be invoked once - * during each sweep cycle. When this returns {@code true}, this consumer group should - * be immediately discarded, as any readers will sit in a spin loop waiting for it to - * be discarded. - * - * @return {@code true} if this consumer group should be removed, {@code false} - * otherwise - */ - public boolean shouldRemove() { - synchronized (lockit) { - return (nreaders == 0 && ++nsweeps > 1); - } - } - - /** - * Reads messages from the queue, blocking if necessary. - * - * @param maxRead maximum number of messages to read - * @param waitMs time to wait, in milliseconds, if the queue is currently empty - * @return a list of messages read from the queue, empty if no messages became - * available before the wait time elapsed, or {@link #UNREADABLE_LIST} if this - * consumer group object is no longer active - * @throws InterruptedException if this thread was interrupted while waiting for the - * first message - */ - public List<String> read(int maxRead, long waitMs) throws InterruptedException { - - synchronized (lockit) { - if (nsweeps > 1 && nreaders == 0) { - // cannot use this consumer group object anymore - return UNREADABLE_LIST; - } - - ++nreaders; - } - - /* - * Note: do EVERYTHING inside of the "try" block, so that the "finally" block can - * update the reader count. - * - * Do NOT hold the lockit while we're polling, as poll() may block for a while. - */ - try { - // always read at least one message - int maxRead2 = Math.max(1, maxRead); - long waitMs2 = Math.max(0, waitMs); - - // perform a blocking read of the queue - String obj = getNextMessage(waitMs2); - if (obj == null) { - return EMPTY_LIST; - } - - /* - * List should hold all messages from the queue PLUS the one we already have. - * Note: it's possible for additional messages to be added to the queue while - * we're reading from it. In that case, the list will grow as needed. - */ - List<String> lst = new ArrayList<>(Math.min(maxRead2, messageQueue.size() + 1)); - lst.add(obj); - - // perform NON-blocking read of subsequent messages - for (var x = 1; x < maxRead2; ++x) { - if ((obj = messageQueue.poll()) == null) { - break; - } - - lst.add(obj); - } - - return lst; - - } finally { - synchronized (lockit) { - --nreaders; - nsweeps = 0; - } - } - } - - /** - * Writes messages to the queue. - * - * @param messages messages to be written to the queue - */ - public void write(List<String> messages) { - messageQueue.addAll(messages); - } - - // the following methods may be overridden by junit tests - - /** - * Gets the next message from the queue. - * - * @param waitMs time to wait, in milliseconds, if the queue is currently empty - * @return the next message, or {@code null} if no messages became available before - * the wait time elapsed - * @throws InterruptedException if this thread was interrupted while waiting for the - * message - */ - protected String getNextMessage(long waitMs) throws InterruptedException { - return messageQueue.poll(waitMs, TimeUnit.MILLISECONDS); - } -} diff --git a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapGetTopicResponse.java b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapGetTopicResponse.java deleted file mode 100644 index 1f05976f7..000000000 --- a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapGetTopicResponse.java +++ /dev/null @@ -1,37 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Bell Canada. All rights reserved. - * ================================================================================ - * Licensed 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.sim.dmaap.provider; - -import java.util.List; -import lombok.Getter; -import lombok.Setter; -import lombok.ToString; - -/** - * Class to capture get topic response from dmaap simulator. - */ -@Getter -@Setter -@ToString -public class DmaapGetTopicResponse { - - private List<String> topics; -} diff --git a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapSimProvider.java b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapSimProvider.java deleted file mode 100644 index afbe10f51..000000000 --- a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/DmaapSimProvider.java +++ /dev/null @@ -1,189 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019, 2023 Nordix Foundation. - * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. - * ================================================================================ - * Licensed 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.sim.dmaap.provider; - -import jakarta.ws.rs.core.Response; -import jakarta.ws.rs.core.Response.Status; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import lombok.Getter; -import lombok.Setter; -import org.onap.policy.common.utils.services.ServiceManagerContainer; -import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Provider to simulate DMaaP. - * - * @author Liam Fallon (liam.fallon@est.tech) - */ -public class DmaapSimProvider extends ServiceManagerContainer { - private static final Logger LOGGER = LoggerFactory.getLogger(DmaapSimProvider.class); - - @Getter - @Setter - private static DmaapSimProvider instance; - - /** - * Maps a topic name to its data. - */ - private final Map<String, TopicData> topic2data = new ConcurrentHashMap<>(); - - /** - * Thread used to remove idle consumers from the topics. - */ - private ScheduledExecutorService timerPool; - - - /** - * Constructs the object. - * - * @param params parameters - */ - public DmaapSimProvider(DmaapSimParameterGroup params) { - addAction("Topic Sweeper", () -> { - timerPool = makeTimerPool(); - timerPool.scheduleWithFixedDelay(new SweeperTask(), params.getTopicSweepSec(), params.getTopicSweepSec(), - TimeUnit.SECONDS); - }, () -> timerPool.shutdown()); - } - - /** - * Process a DMaaP message. - * - * @param topicName the topic name - * @param dmaapMessage the message to process - * @return a response to the message - */ - @SuppressWarnings("unchecked") - public Response processDmaapMessagePut(final String topicName, final Object dmaapMessage) { - LOGGER.debug("Topic: {}, Received DMaaP message(s): {}", topicName, dmaapMessage); - - List<Object> lst; - - if (dmaapMessage instanceof List) { - lst = (List<Object>) dmaapMessage; - } else { - lst = Collections.singletonList(dmaapMessage); - } - - TopicData topic = topic2data.get(topicName); - - /* - * Write all messages and return the count. If the topic doesn't exist yet, then - * there are no subscribers to receive the messages, thus treat it as if all - * messages were published. - */ - int nmessages = (topic != null ? topic.write(lst) : lst.size()); - - Map<String, Object> map = new LinkedHashMap<>(); - map.put("serverTimeMs", 0); - map.put("count", nmessages); - - return Response.status(Response.Status.OK).entity(map).build(); - } - - /** - * Wait for and return a DMaaP message. - * - * @param topicName The topic to wait on - * @param consumerGroup the consumer group that is waiting - * @param consumerId the consumer ID that is waiting - * @param limit the maximum number of messages to get - * @param timeoutMs the length of time to wait for - * @return the DMaaP message or - */ - public Response processDmaapMessageGet(final String topicName, final String consumerGroup, final String consumerId, - final int limit, final long timeoutMs) { - - LOGGER.debug("Topic: {}, Request for DMaaP message: {}: {} with limit={} timeout={}", topicName, consumerGroup, - consumerId, limit, timeoutMs); - - try { - List<String> lst = topic2data.computeIfAbsent(topicName, this::makeTopicData).read(consumerGroup, limit, - timeoutMs); - - LOGGER.debug("Topic: {}, Retrieved {} messages for: {}: {}", topicName, lst.size(), consumerGroup, - consumerId); - return Response.status(Status.OK).entity(lst).build(); - - } catch (InterruptedException e) { - LOGGER.warn("Topic: {}, Request for DMaaP message interrupted: {}: {}", topicName, consumerGroup, - consumerId, e); - Thread.currentThread().interrupt(); - return Response.status(Status.GONE).entity(Collections.emptyList()).build(); - } - } - - /** - * Returns the list of default topics. - * - * @return the topic list - */ - public Response processDmaapTopicsGet() { - - LOGGER.debug("Request for listing DMaaP topics"); - var response = new DmaapGetTopicResponse(); - response.setTopics(List.of("POLICY-PDP-PAP", "POLICY-NOTIFICATION", "unauthenticated.DCAE_CL_OUTPUT", - "POLICY-CL-MGT")); - return Response.status(Status.OK).entity(response).build(); - } - - /** - * Task to remove idle consumers from each topic. - */ - private class SweeperTask implements Runnable { - @Override - public void run() { - topic2data.values().forEach(TopicData::removeIdleConsumers); - } - } - - // the following methods may be overridden by junit tests - - /** - * Makes a new timer pool. - * - * @return a new timer pool - */ - protected ScheduledExecutorService makeTimerPool() { - return Executors.newScheduledThreadPool(1); - } - - /** - * Makes a new topic. - * - * @param topicName topic name - * @return a new topic - */ - protected TopicData makeTopicData(String topicName) { - return new TopicData(topicName); - } -} diff --git a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/TopicData.java b/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/TopicData.java deleted file mode 100644 index 8e5cf24cc..000000000 --- a/models-sim/models-sim-dmaap/src/main/java/org/onap/policy/models/sim/dmaap/provider/TopicData.java +++ /dev/null @@ -1,200 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP Policy Models - * ================================================================================ - * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * Licensed 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. - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.models.sim.dmaap.provider; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Function; -import org.onap.policy.common.utils.coder.Coder; -import org.onap.policy.common.utils.coder.CoderException; -import org.onap.policy.common.utils.coder.StandardCoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Data associated with a topic. - * - * <p/> - * Note: for ease of implementation, this adds a topic when a consumer polls it rather - * than when a publisher writes to it. This is the opposite of how the real DMaaP works. - * As a result, this will never return a topic-not-found message to the consumer. - */ -public class TopicData { - private static final Logger logger = LoggerFactory.getLogger(TopicData.class); - - /** - * Name of the topic with which this data is associated. - */ - private final String topicName; - - /** - * Maps a consumer group name to its associated data. - */ - private final Map<String, ConsumerGroupData> group2data = new ConcurrentHashMap<>(); - - - /** - * Constructs the object. - * - * @param topicName name of the topic with which this object is associated - */ - public TopicData(String topicName) { - logger.info("Topic {}: added", topicName); - this.topicName = topicName; - } - - /** - * Removes idle consumers from the topic. This is typically called once during each - * sweep cycle. - */ - public void removeIdleConsumers() { - Iterator<Entry<String, ConsumerGroupData>> iter = group2data.entrySet().iterator(); - while (iter.hasNext()) { - Entry<String, ConsumerGroupData> ent = iter.next(); - if (ent.getValue().shouldRemove()) { - /* - * We want the minimum amount of time to elapse between invoking - * shouldRemove() and iter.remove(), thus all other statements (e.g., - * logging) should be done AFTER iter.remove(). - */ - iter.remove(); - - logger.info("Topic {}: removed consumer group: {}", topicName, ent.getKey()); - } - } - } - - /** - * Reads from a particular consumer group's queue. - * - * @param consumerGroup name of the consumer group from which to read - * @param maxRead maximum number of messages to read - * @param waitMs time to wait, in milliseconds, if the queue is currently empty - * @return a list of messages read from the queue, empty if no messages became - * available before the wait time elapsed - * @throws InterruptedException if this thread was interrupted while waiting for the - * first message - */ - public List<String> read(String consumerGroup, int maxRead, long waitMs) throws InterruptedException { - /* - * It's possible that this thread may spin several times while waiting for - * removeIdleConsumers() to complete its call to iter.remove(), thus we create - * this closure once, rather than each time through the loop. - */ - Function<String, ConsumerGroupData> maker = this::makeData; - - // loop until we get a readable list - List<String> result; - - // @formatter:off - - do { - result = group2data.computeIfAbsent(consumerGroup, maker).read(maxRead, waitMs); - } while (result == ConsumerGroupData.UNREADABLE_LIST); - - // @formatter:on - - return result; - } - - /** - * Writes messages to the queues of every consumer group. - * - * @param messages messages to be written to the queues - * @return the number of messages enqueued - */ - public int write(List<Object> messages) { - List<String> list = convertMessagesToStrings(messages); - - /* - * We don't care if a consumer group is deleted from the map while we're adding - * messages to it, as those messages will simply be ignored (and discarded by the - * garbage collector). - */ - for (ConsumerGroupData data : group2data.values()) { - data.write(list); - } - - return list.size(); - } - - /** - * Converts a list of message objects to a list of message strings. If a message - * cannot be converted for some reason, then it is not added to the result list, thus - * the result list may be shorted than the original input list. - * - * @param messages objects to be converted - * @return a list of message strings - */ - protected List<String> convertMessagesToStrings(List<Object> messages) { - Coder coder = new StandardCoder(); - List<String> list = new ArrayList<>(messages.size()); - - for (Object msg : messages) { - var str = convertMessageToString(msg, coder); - if (str != null) { - list.add(str); - } - } - - return list; - } - - /** - * Converts a message object to a message string. - * - * @param message message to be converted - * @param coder used to encode the message as a string - * @return the message string, or {@code null} if it cannot be converted - */ - protected String convertMessageToString(Object message, Coder coder) { - if (message == null) { - return null; - } - - if (message instanceof String) { - return message.toString(); - } - - try { - return coder.encode(message); - } catch (CoderException e) { - logger.warn("cannot encode {}", message, e); - return null; - } - } - - // this may be overridden by junit tests - - /** - * Makes data for a consumer group. - * - * @param consumerGroup name of the consumer group to make - * @return new consumer group data - */ - protected ConsumerGroupData makeData(String consumerGroup) { - return new ConsumerGroupData(topicName, consumerGroup); - } -} |