Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / dal / cache / InMemoryEntityCache.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 package org.openecomp.sparky.dal.cache;
27
28 import java.util.concurrent.ConcurrentHashMap;
29
30 import org.openecomp.cl.api.Logger;
31 import org.openecomp.cl.eelf.LoggerFactory;
32 import org.openecomp.sparky.dal.rest.OperationResult;
33 import org.openecomp.sparky.logging.AaiUiMsgs;
34
35 /**
36  * The Class InMemoryEntityCache.
37  *
38  * @author davea.
39  */
40 public class InMemoryEntityCache implements EntityCache {
41
42   private ConcurrentHashMap<String, OperationResult> cachedEntityData;
43   private static final Logger LOG =
44       LoggerFactory.getInstance().getLogger(InMemoryEntityCache.class);
45
46   /**
47    * Instantiates a new in memory entity cache.
48    */
49   public InMemoryEntityCache() {
50     cachedEntityData = new ConcurrentHashMap<String, OperationResult>();
51   }
52
53   /* (non-Javadoc)
54    * @see org.openecomp.sparky.dal.cache.EntityCache#put(java.lang.String, org.openecomp.sparky.dal.rest.OperationResult)
55    */
56   @Override
57   public void put(String key, OperationResult data) {
58     if (data == null) {
59       return;
60     }
61
62     if (cachedEntityData.putIfAbsent(key, data) != null) {
63       if (LOG.isDebugEnabled()) {
64         LOG.debug(AaiUiMsgs.DATA_CACHE_SUCCESS, key);
65       }
66     }
67
68   }
69
70   /* (non-Javadoc)
71    * @see org.openecomp.sparky.dal.cache.EntityCache#get(java.lang.String, java.lang.String)
72    */
73   @Override
74   public OperationResult get(String entityKey, String link) {
75
76     if (link != null) {
77       return cachedEntityData.get(link);
78     }
79
80     return null;
81   }
82
83   /* (non-Javadoc)
84    * @see org.openecomp.sparky.dal.cache.EntityCache#shutdown()
85    */
86   @Override
87   public void shutdown() {
88     // TODO Auto-generated method stub
89     // nothing to do
90
91   }
92
93   /* (non-Javadoc)
94    * @see org.openecomp.sparky.dal.cache.EntityCache#clear()
95    */
96   @Override
97   public void clear() {
98     cachedEntityData.clear();
99   }
100
101 }