2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.openecomp.aai.champ.graph.impl;
24 import java.util.Map.Entry;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.openecomp.aai.champ.ChampAPI;
29 import org.openecomp.aai.champ.ChampGraph;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ChampAPIImpl implements ChampAPI {
35 private static final Logger LOGGER = LoggerFactory.getLogger(ChampAPIImpl.class);
37 private final AtomicBoolean shutdown;
38 private final ChampGraph.Type type;
39 private final ConcurrentHashMap<String, ChampGraph> graphs;
41 public ChampAPIImpl(ChampGraph.Type type) {
43 this.graphs = new ConcurrentHashMap<String, ChampGraph> ();
44 this.shutdown = new AtomicBoolean(false);
47 private ConcurrentHashMap<String, ChampGraph> getGraphs() {
52 public ChampGraph getGraph(String graphName) {
53 if (shutdown.get()) throw new IllegalStateException("Cannot call getGraph() after shutdown() has been initiated");
55 if (getGraphs().containsKey(graphName)) return getGraphs().get(graphName);
57 final ChampGraph graph = ChampGraph.Factory.newInstance(type, graphName);
59 final ChampGraph existingGraph = getGraphs().putIfAbsent(graphName, graph);
61 if (existingGraph == null) return graph;
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());
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);
83 public ChampGraph.Type getType() {