Update license date and text
[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.ChampGraph;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class ChampAPIImpl implements ChampAPI {
33
34         private static final Logger LOGGER = LoggerFactory.getLogger(ChampAPIImpl.class);
35
36         private final AtomicBoolean shutdown;
37         private final String type;
38         private final ConcurrentHashMap<String, ChampGraph> graphs;
39
40         public ChampAPIImpl(String type) {
41                 this.type = type;
42                 this.graphs = new ConcurrentHashMap<String, ChampGraph> ();
43                 this.shutdown = new AtomicBoolean(false);
44         }
45
46         private ConcurrentHashMap<String, ChampGraph> getGraphs() {
47           return graphs;
48         }
49
50         @Override
51         public ChampGraph getGraph(String graphName) {
52                 if (shutdown.get()) {
53                         throw new IllegalStateException("Cannot call getGraph() after shutdown() has been initiated");
54                 }
55
56                 if (getGraphs().containsKey(graphName)) {
57                         return getGraphs().get(graphName);
58                 }
59
60                 // At this point, we know a graph with this name doesn't exist. Create and return it.
61                 final ChampGraph graph = new InMemoryChampGraphImpl.Builder().build();
62                 graphs.put(graphName, graph);
63                 return graph;
64         }
65
66         @Override
67         public void shutdown() {
68                 if (shutdown.compareAndSet(false, true)) {
69                         for (Entry<String, ChampGraph> graphEntry : graphs.entrySet()) {
70                                 LOGGER.info("Shutting down graph {}", graphEntry.getKey());
71                                 
72                                 try {
73                                         graphEntry.getValue().shutdown();
74                                         LOGGER.info("Graph {} shutdown successfully", graphEntry.getKey());
75                                 } catch (Throwable t) {
76                                         LOGGER.warn("Caught exception while shutting down graph " + graphEntry.getKey(), t);
77                                 }
78                         }
79                 }
80         }
81
82         @Override
83         public String getType() {
84                 return type;
85         }
86
87 }