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