2 * ================================================================================
3 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
20 package org.onap.dcae.analytics.tca.web.abatement.simple;
22 import static org.onap.dcae.analytics.model.TcaModelConstants.TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT;
23 import static org.onap.dcae.analytics.model.TcaModelConstants.TCA_ABATEMENT_SIMPLE_REPOSITORY_REMOVE_ENTITY_COUNT;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentLinkedQueue;
30 import org.onap.dcae.analytics.tca.core.service.TcaAbatementEntity;
31 import org.onap.dcae.analytics.tca.core.service.TcaAbatementRepository;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * A simple implementation for {@link TcaAbatementRepository} which should be used for testing and development
39 * @author Rajiv Singla
41 public class SimpleAbatementRepository implements TcaAbatementRepository {
43 private static final Logger logger = LoggerFactory.getLogger(SimpleAbatementRepository.class);
45 private final ConcurrentLinkedQueue<String> lookupKeysQueue;
46 private final ConcurrentHashMap<String, SimpleAbatementEntity> repository;
48 public SimpleAbatementRepository() {
49 this.lookupKeysQueue = new ConcurrentLinkedQueue<>();
50 this.repository = new ConcurrentHashMap<>(TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT);
55 public synchronized void save(final TcaAbatementEntity tcaAbatementEntity) {
57 // remove entries from repository if required
58 if (lookupKeysQueue.size() == TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT) {
60 logger.warn("Simple Abatement Repository reached its max allowed size: {}, " +
61 "Removing last inserted {} entries",
62 TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT,
63 TCA_ABATEMENT_SIMPLE_REPOSITORY_REMOVE_ENTITY_COUNT);
65 for (int i = 0; i < TCA_ABATEMENT_SIMPLE_REPOSITORY_REMOVE_ENTITY_COUNT; i++) {
66 final String lookupKey = lookupKeysQueue.poll();
67 logger.warn("Removing Abatement Lookup key: {} from Simple Abatement Repository", lookupKey);
68 repository.remove(lookupKey);
73 lookupKeysQueue.add(tcaAbatementEntity.getLookupKey());
74 final SimpleAbatementEntity simpleAbatementEntity =
75 new SimpleAbatementEntity(tcaAbatementEntity.getLookupKey(), tcaAbatementEntity.getRequestId(),
76 tcaAbatementEntity.isAbatementAlertSent());
77 repository.put(tcaAbatementEntity.getLookupKey(), simpleAbatementEntity);
82 public List<TcaAbatementEntity> findByLookupKey(final String lookUpKey) {
83 final SimpleAbatementEntity repoEntity = repository.getOrDefault(lookUpKey, null);
84 if (repoEntity == null) {
85 return Collections.emptyList();
87 return Collections.singletonList(repoEntity);