Merge "Updated champ-lib to use the correct logger"
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / graph / impl / ChampAPIImpl.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.champcore.graph.impl;
22
23 import java.util.Map.Entry;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.atomic.AtomicBoolean;
26
27 import org.onap.aai.champcore.ChampAPI;
28 import org.onap.aai.champcore.ChampCoreMsgs;
29 import org.onap.aai.champcore.ChampGraph;
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32
33
34 public class ChampAPIImpl implements ChampAPI {
35
36         private static final Logger LOGGER = LoggerFactory.getInstance().getLogger(ChampAPIImpl.class);
37
38         private final AtomicBoolean shutdown;
39         private final String type;
40         private final ConcurrentHashMap<String, ChampGraph> graphs;
41
42         public ChampAPIImpl(String type) {
43                 this.type = type;
44                 this.graphs = new ConcurrentHashMap<String, ChampGraph> ();
45                 this.shutdown = new AtomicBoolean(false);
46         }
47
48         private ConcurrentHashMap<String, ChampGraph> getGraphs() {
49           return graphs;
50         }
51
52         @Override
53         public ChampGraph getGraph(String graphName) {
54                 if (shutdown.get()) {
55                         throw new IllegalStateException("Cannot call getGraph() after shutdown() has been initiated");
56                 }
57
58                 if (getGraphs().containsKey(graphName)) {
59                         return getGraphs().get(graphName);
60                 }
61
62                 // At this point, we know a graph with this name doesn't exist. Create and return it.
63                 final ChampGraph graph = new InMemoryChampGraphImpl.Builder().build();
64                 graphs.put(graphName, graph);
65                 return graph;
66         }
67
68         @Override
69         public void shutdown() {
70                 if (shutdown.compareAndSet(false, true)) {
71                         for (Entry<String, ChampGraph> graphEntry : graphs.entrySet()) {
72                                 LOGGER.info(ChampCoreMsgs.CHAMPCORE_CHAMP_API_IMPL_INFO, 
73                                     String.format("Shutting down graph %s", graphEntry.getKey()));
74                                 
75                                 try {
76                                         graphEntry.getValue().shutdown();
77                                         LOGGER.info(ChampCoreMsgs.CHAMPCORE_CHAMP_API_IMPL_INFO,
78                                             String.format("Graph %s shutdown successfully", graphEntry.getKey()));
79                                 } catch (Throwable t) {
80                                         LOGGER.warn(ChampCoreMsgs.CHAMPCORE_CHAMP_API_IMPL_WARN,
81                                             String.format("Caught exception while shutting down graph %s: %s", graphEntry.getKey(), t.getMessage()));
82                                 }
83                         }
84                 }
85         }
86
87         @Override
88         public String getType() {
89                 return type;
90         }
91
92 }