Merge "CassaLockStore.java - Sonar Fixes"
[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  * ===================================================================
7  *  Modifications Copyright (c) 2019 Samsung
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  *
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.datastore;
26
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.onap.music.eelf.logging.EELFLoggerDelegate;
31 import org.onap.music.exceptions.MusicServiceException;
32 import org.onap.music.main.MusicUtil;
33
34 import com.datastax.driver.core.ResultSet;
35 import com.datastax.driver.core.TableMetadata;
36
37 public class MusicDataStoreHandle {
38
39     private MusicDataStoreHandle(){
40         throw new IllegalStateException("Utility class");
41     }
42
43     public static MusicDataStore mDstoreHandle = null;
44     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicDataStoreHandle.class);
45     
46     /**
47     *
48     * @param remoteIp
49     * @return
50     */
51    public static MusicDataStore getDSHandle(String remoteIp) {
52        logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
53        long start = System.currentTimeMillis();
54        if (mDstoreHandle == null) {
55            mDstoreHandle = new MusicDataStore(remoteIp);
56        }
57        long end = System.currentTimeMillis();
58        logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
59        return mDstoreHandle;
60    }
61
62    /**
63     *
64     * @return
65     * @throws MusicServiceException
66     */
67    public static MusicDataStore getDSHandle() throws MusicServiceException {
68        logger.info(EELFLoggerDelegate.metricsLogger,"Acquiring data store handle");
69        long start = System.currentTimeMillis();
70        if (mDstoreHandle == null) {
71            // Quick Fix - Best to put this into every call to getDSHandle?
72            if (! MusicUtil.getMyCassaHost().equals("localhost") ) {
73                mDstoreHandle = new MusicDataStore(MusicUtil.getMyCassaHost());
74            } else {
75                mDstoreHandle = new MusicDataStore();
76            }
77        }
78        if(mDstoreHandle.getSession() == null) {
79            String message = "Connection to Cassandra has not been enstablished."
80                    + " Please check connection properites and reboot.";
81            logger.info(EELFLoggerDelegate.applicationLogger, message);
82            throw new MusicServiceException(message);
83        }
84        long end = System.currentTimeMillis();
85        logger.info(EELFLoggerDelegate.metricsLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
86        return mDstoreHandle;
87    }
88    
89    
90    /**
91    *
92    * @param keyspace
93    * @param tablename
94    * @return
95    * @throws MusicServiceException
96    */
97   public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException {
98       return getDSHandle().returnColumnMetadata(keyspace, tablename);
99   }
100   
101   /**
102   *
103   * @param results
104   * @return
105   * @throws MusicServiceException
106   */
107  public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException {
108      return getDSHandle().marshalData(results);
109  }
110
111 }