Revert "remove redundant code under appc-common-bundle/java"
[appc.git] / appc-core / appc-common-bundle / java / org / onap / appc / metadata / impl / MetadataServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.metadata.impl;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.onap.ccsdk.sli.core.dblib.DbLibService;
29
30 import javax.sql.rowset.CachedRowSet;
31
32 import org.onap.appc.cache.MetadataCache;
33 import org.onap.appc.cache.impl.MetadataCacheFactory;
34 import org.onap.appc.metadata.MetadataService;
35 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
36
37 import java.sql.SQLException;
38 import java.util.ArrayList;
39
40
41 public class MetadataServiceImpl implements MetadataService {
42
43     private DbLibService dbLibService;
44
45     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
46
47     private MetadataCache<DependencyModelIdentifier, String> cache;
48
49     public MetadataServiceImpl() {
50         initialize();
51     }
52
53     private void initialize() {
54         cache = MetadataCacheFactory.getInstance().getMetadataCache();
55         // TODO initialze dbLibService
56     }
57
58     public void setDbLibService(DbLibService dbLibService) {
59         this.dbLibService = dbLibService;
60     }
61
62     void setCache(MetadataCache<DependencyModelIdentifier, String> cache) {
63         this.cache = cache;
64     }
65
66     @Override
67     public String getVnfModel(DependencyModelIdentifier modelIdentifier) {
68         logger.debug("Reading Vnf Model data from cache for vnfType : " + modelIdentifier.getVnfType()
69             + " and catalog version : " + modelIdentifier.getCatalogVersion());
70         String vnfModel = cache.getObject(modelIdentifier);
71         if (vnfModel == null || vnfModel.length() == 0) {
72             logger.debug("Vnf Model not available in cache. Reading from database.");
73             vnfModel = readVnfModel(modelIdentifier);
74             if (vnfModel != null && vnfModel.length() > 0) {
75                 logger.debug("Adding retrieved Vnf Model to cache.");
76                 addVnfModel(modelIdentifier, vnfModel);
77             }
78         }
79         return vnfModel;
80     }
81
82     private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
83         cache.putObject(modelIdentifier, vnfModel);
84     }
85
86     private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
87
88         logger.debug("Reading Vnf Model data from database for RESOURCE_NAME : " + modelIdentifier.getVnfType()
89             + " and RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
90         StringBuilder query = new StringBuilder();
91         String vnfModel = null;
92         query.append("SELECT ARTIFACT_CONTENT FROM sdnctl.ASDC_ARTIFACTS WHERE RESOURCE_NAME = ? ");
93         ArrayList<String> argList = new ArrayList<>();
94         argList.add(modelIdentifier.getVnfType());
95
96         if (modelIdentifier.getCatalogVersion() == null) {
97             query.append(" ORDER BY  SUBSTRING_INDEX(RESOURCE_VERSION, '.', 1)*1  DESC , " +
98                 "SUBSTRING_INDEX(SUBSTRING_INDEX(RESOURCE_VERSION, '.', 2),'.', -1) *1 DESC , " +
99                 "SUBSTRING_INDEX(RESOURCE_VERSION, '.', -1)*1 DESC ;");
100         } else {
101             query.append("AND RESOURCE_VERSION = ? ;");
102             argList.add(modelIdentifier.getCatalogVersion());
103         }
104         try {
105             final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
106             if (data.first()) {
107                 vnfModel = data.getString("ARTIFACT_CONTENT");
108                 if (vnfModel == null || vnfModel.isEmpty()) {
109                     logger.error("Invalid dependency model for vnf type : " + modelIdentifier.getVnfType()
110                         + " and catalog version : " + modelIdentifier.getCatalogVersion());
111                     throw new RuntimeException("Invalid or Empty VNF Model");
112                 }
113                 logger.debug("Retrieved Vnf Model : " + vnfModel);
114             } else {
115                 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : " + modelIdentifier.getVnfType()
116                     + " AND RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
117             }
118         } catch (SQLException e) {
119             throw new RuntimeException("Database error occurred");
120         }
121         return vnfModel;
122     }
123 }