2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.metadata.impl;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
28 import javax.sql.rowset.CachedRowSet;
30 import org.openecomp.appc.cache.MetadataCache;
31 import org.openecomp.appc.cache.impl.MetadataCacheFactory;
32 import org.openecomp.appc.metadata.MetadataService;
33 import org.openecomp.appc.metadata.objects.DependencyModelIdentifier;
35 import java.sql.SQLException;
36 import java.util.ArrayList;
39 public class MetadataServiceImpl implements MetadataService {
41 private DbLibService dbLibService;
43 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
45 private MetadataCache<DependencyModelIdentifier,String> cache;
47 public MetadataServiceImpl(){
51 private void initialize(){
52 cache = MetadataCacheFactory.getInstance().getMetadataCache();
53 // TODO initialze dbLibService
56 public void setDbLibService(DbLibService dbLibService) {
57 this.dbLibService = dbLibService;
61 public String getVnfModel(DependencyModelIdentifier modelIdentifier) {
62 logger.debug("Reading Vnf Model data from cache for vnfType : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
63 String vnfModel = cache.getObject(modelIdentifier);
64 if(vnfModel ==null || vnfModel.length() ==0){
65 logger.debug("Vnf Model not available in cache. Reading from database.");
66 vnfModel = readVnfModel(modelIdentifier);
67 if(vnfModel !=null && vnfModel.length()>0){
68 logger.debug("Adding retrieved Vnf Model to cache.");
69 addVnfModel(modelIdentifier,vnfModel);
75 private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
76 cache.putObject(modelIdentifier,vnfModel);
79 private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
81 logger.debug("Reading Vnf Model data from database for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" and RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
82 StringBuilder query = new StringBuilder();
83 String vnfModel =null;
84 query.append("SELECT ARTIFACT_CONTENT FROM sdnctl.ASDC_ARTIFACTS WHERE RESOURCE_NAME = ? ") ;
85 ArrayList<String> argList = new ArrayList<>();
86 argList.add(modelIdentifier.getVnfType());
88 if (modelIdentifier.getCatalogVersion()==null){
89 query.append(" ORDER BY SUBSTRING_INDEX(RESOURCE_VERSION, '.', 1)*1 DESC , " +
90 "SUBSTRING_INDEX(SUBSTRING_INDEX(RESOURCE_VERSION, '.', 2),'.', -1) *1 DESC , " +
91 "SUBSTRING_INDEX(RESOURCE_VERSION, '.', -1)*1 DESC ;");
93 query.append("AND RESOURCE_VERSION = ? ;");
94 argList.add(modelIdentifier.getCatalogVersion());
97 final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
99 vnfModel = data.getString("ARTIFACT_CONTENT");
100 if (vnfModel == null || vnfModel.isEmpty()) {
101 logger.error("Invalid dependency model for vnf type : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
102 throw new RuntimeException("Invalid or Empty VNF Model");
104 logger.debug("Retrieved Vnf Model : " + vnfModel);
106 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" AND RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
108 } catch (SQLException e) {
109 throw new RuntimeException("Database error occurred");