Standalone TCA with EELF Logger
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-tca-web / src / main / java / org / onap / dcae / analytics / tca / web / abatement / simple / SimpleAbatementRepository.java
1 /*
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  *
18  */
19
20 package org.onap.dcae.analytics.tca.web.abatement.simple;
21
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;
24
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.concurrent.ConcurrentHashMap;
28 import java.util.concurrent.ConcurrentLinkedQueue;
29
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;
34
35 /**
36  * A simple implementation for {@link TcaAbatementRepository} which should be used for testing and development
37  * purposes only.
38  *
39  * @author Rajiv Singla
40  */
41 public class SimpleAbatementRepository implements TcaAbatementRepository {
42
43     private static final Logger logger = LoggerFactory.getLogger(SimpleAbatementRepository.class);
44
45     private final ConcurrentLinkedQueue<String> lookupKeysQueue;
46     private final ConcurrentHashMap<String, SimpleAbatementEntity> repository;
47
48     public SimpleAbatementRepository() {
49         this.lookupKeysQueue = new ConcurrentLinkedQueue<>();
50         this.repository = new ConcurrentHashMap<>(TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT);
51     }
52
53
54     @Override
55     public synchronized void save(final TcaAbatementEntity tcaAbatementEntity) {
56
57         // remove entries from repository if required
58         if (lookupKeysQueue.size() == TCA_ABATEMENT_SIMPLE_REPOSITORY_MAX_ENTITY_COUNT) {
59
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);
64
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);
69             }
70
71         }
72
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);
78
79     }
80
81     @Override
82     public List<TcaAbatementEntity> findByLookupKey(final String lookUpKey) {
83         final SimpleAbatementEntity repoEntity = repository.getOrDefault(lookUpKey, null);
84         if (repoEntity == null) {
85             return Collections.emptyList();
86         }
87         return Collections.singletonList(repoEntity);
88     }
89 }