CADI and a few small updates.
[music.git] / src / main / java / org / onap / music / datastore / MusicDataStoreHandle.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *  Modifications Copyright (C) 2019 IBM.
7  * ===================================================================
8  *  Modifications Copyright (c) 2019 Samsung
9  * ===================================================================
10  *  Licensed under the Apache License, Version 2.0 (the "License");
11  *  you may not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS,
18  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  *
22  * ============LICENSE_END=============================================
23  * ====================================================================
24  */
25
26 package org.onap.music.datastore;
27
28 import java.util.HashMap;
29 import java.util.Map;
30
31 import org.onap.music.eelf.logging.EELFLoggerDelegate;
32 import org.onap.music.exceptions.MusicServiceException;
33 import org.onap.music.main.MusicUtil;
34
35 import com.datastax.driver.core.KeyspaceMetadata;
36 import com.datastax.driver.core.ResultSet;
37 import com.datastax.driver.core.TableMetadata;
38
39 public class MusicDataStoreHandle {
40
41     private MusicDataStoreHandle(){
42         throw new IllegalStateException("Utility class");
43     }
44
45     private static MusicDataStore mDstoreHandle = null;
46     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class);
47     
48     /**
49     *
50     * @param remoteIp
51     * @return
52     */
53     public static MusicDataStore getDSHandle(String remoteIp) {
54         logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
55         long start = System.currentTimeMillis();
56         if (mDstoreHandle == null) {
57             mDstoreHandle = new MusicDataStore(remoteIp);
58         }
59         long end = System.currentTimeMillis();
60         logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
61         return mDstoreHandle;
62     }
63
64    /**
65     *
66     * @return
67     * @throws MusicServiceException
68     */
69     public static MusicDataStore getDSHandle() throws MusicServiceException {
70         logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
71         long start = System.currentTimeMillis();
72         if (mDstoreHandle == null) {
73             // Quick Fix - Best to put this into every call to getDSHandle?
74            if (!"localhost".equals(MusicUtil.getMyCassaHost())) {
75                 mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
76             } else {
77                 mDstoreHandle = new MusicDataStore();
78             }
79         }
80         if(mDstoreHandle.getSession() == null) {
81             String message = "Connection to Cassandra has not been enstablished."
82                 + " Please check connection properites and reboot.";
83             logger.info(EELFLoggerDelegate.applicationLogger, message);
84             throw new MusicServiceException(message);
85         }
86         long end = System.currentTimeMillis();
87         logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
88         return mDstoreHandle;
89     }
90
91
92    /**
93    *
94    * @param keyspace
95    * @param tablename
96    * @return
97    * @throws MusicServiceException
98    */
99     public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException {
100         return getDSHandle().returnColumnMetadata(keyspace, tablename);
101     }
102     
103     /**
104     *
105     * @param keyspace
106     * @param tablename
107     * @return
108     * @throws MusicServiceException
109     */
110      public static KeyspaceMetadata returnkeyspaceMetadata(String keyspace) throws MusicServiceException {
111          return getDSHandle().returnKeyspaceMetadata(keyspace);
112      }
113
114   /**
115   *
116   * @param results
117   * @return
118   * @throws MusicServiceException
119   */
120     public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException {
121         return getDSHandle().marshalData(results);
122     }
123
124 }