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