8976d13329d2edeae912a37f73f1af061e91ef61
[appc.git] / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / main / java / org / onap / appc / licmgr / impl / LicenseDataAccessServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.licmgr.impl;
26
27 import javax.sql.rowset.CachedRowSet;
28
29 import org.onap.appc.licmgr.Constants;
30 import org.onap.appc.licmgr.LicenseDataAccessService;
31 import org.onap.appc.licmgr.exception.DataAccessException;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.onap.ccsdk.sli.core.dblib.DbLibService;
35
36 import static org.onap.appc.licmgr.Constants.SDC_ARTIFACTS_FIELDS;
37
38 import java.sql.SQLException;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Map;
42
43
44 @SuppressWarnings("JavaDoc")
45 public class LicenseDataAccessServiceImpl implements LicenseDataAccessService {
46
47     private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);
48
49     public void setSchema(String schema) {
50         this.schema = schema;
51     }
52
53     private String schema;
54
55     public void setDbLibService(DbLibService dbLibService) {
56         this.dbLibService = dbLibService;
57     }
58
59     private DbLibService dbLibService;
60
61
62     /**
63      * empty constructor
64      */
65     public LicenseDataAccessServiceImpl(){}
66
67     @Override
68     public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
69                     DataAccessException {
70
71         Map<String,String> result = new HashMap<>();
72         if (null == fields || 0 == fields.length) fields = new String[]{SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};
73
74         String queryString = buildQueryStatement();
75
76         ArrayList<String> argList = new ArrayList<>();
77         argList.add(vnfType);
78         argList.add(vnfVersion);
79         argList.add(Constants.VF_LICENSE);
80
81         try {
82
83             final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);
84
85             if (data.first()) {
86                 for (String field : fields) {
87                     result.put(field, data.getString(field));
88                 }
89             } else {
90                 String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.SDC_ARTIFACTS;
91                 logger.info(msg);
92             }
93         } catch (SQLException e) {
94             logger.error("Error Accessing Database " + e);
95             throw new DataAccessException(e);
96         }
97
98         return result;
99     }
100
101     private String buildQueryStatement() {
102         return "select * " + "from " + Constants.SDC_ARTIFACTS + " " +
103             "where " + SDC_ARTIFACTS_FIELDS.RESOURCE_NAME.name() + " = ?" +
104              " AND " + SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.name() + " = ?" +
105              " AND " + SDC_ARTIFACTS_FIELDS.ARTIFACT_TYPE.name() + " = ?";
106     }
107
108 }