2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.metadata.impl;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
29 import javax.sql.rowset.CachedRowSet;
31 import org.openecomp.appc.cache.MetadataCache;
32 import org.openecomp.appc.cache.impl.MetadataCacheFactory;
33 import org.openecomp.appc.metadata.MetadataService;
34 import org.openecomp.appc.metadata.objects.DependencyModelIdentifier;
36 import java.sql.SQLException;
37 import java.util.ArrayList;
40 public class MetadataServiceImpl implements MetadataService {
42 private DbLibService dbLibService;
44 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
46 private MetadataCache<DependencyModelIdentifier,String> cache;
48 public MetadataServiceImpl(){
52 private void initialize(){
53 cache = MetadataCacheFactory.getInstance().getMetadataCache();
54 // TODO initialze dbLibService
57 public void setDbLibService(DbLibService dbLibService) {
58 this.dbLibService = dbLibService;
62 public String getVnfModel(DependencyModelIdentifier modelIdentifier) {
63 logger.debug("Reading Vnf Model data from cache for vnfType : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
64 String vnfModel = cache.getObject(modelIdentifier);
65 if(vnfModel ==null || vnfModel.length() ==0){
66 logger.debug("Vnf Model not available in cache. Reading from database.");
67 vnfModel = readVnfModel(modelIdentifier);
68 if(vnfModel !=null && vnfModel.length()>0){
69 logger.debug("Adding retrieved Vnf Model to cache.");
70 addVnfModel(modelIdentifier,vnfModel);
76 private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
77 cache.putObject(modelIdentifier,vnfModel);
80 private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
82 logger.debug("Reading Vnf Model data from database for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" and RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
83 StringBuilder query = new StringBuilder();
84 String vnfModel =null;
85 query.append("SELECT ARTIFACT_CONTENT FROM sdnctl.ASDC_ARTIFACTS WHERE RESOURCE_NAME = ? ") ;
86 ArrayList<String> argList = new ArrayList<>();
87 argList.add(modelIdentifier.getVnfType());
89 if (modelIdentifier.getCatalogVersion()==null){
90 query.append(" ORDER BY SUBSTRING_INDEX(RESOURCE_VERSION, '.', 1)*1 DESC , " +
91 "SUBSTRING_INDEX(SUBSTRING_INDEX(RESOURCE_VERSION, '.', 2),'.', -1) *1 DESC , " +
92 "SUBSTRING_INDEX(RESOURCE_VERSION, '.', -1)*1 DESC ;");
94 query.append("AND RESOURCE_VERSION = ? ;");
95 argList.add(modelIdentifier.getCatalogVersion());
98 final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
100 vnfModel = data.getString("ARTIFACT_CONTENT");
101 if (vnfModel == null || vnfModel.isEmpty()) {
102 logger.error("Invalid dependency model for vnf type : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
103 throw new RuntimeException("Invalid or Empty VNF Model");
105 logger.debug("Retrieved Vnf Model : " + vnfModel);
107 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" AND RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
109 } catch (SQLException e) {
110 throw new RuntimeException("Database error occurred");