2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.metadata.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.ccsdk.sli.core.dblib.DbLibService;
31 import javax.sql.rowset.CachedRowSet;
33 import org.onap.appc.cache.MetadataCache;
34 import org.onap.appc.cache.impl.MetadataCacheFactory;
35 import org.onap.appc.metadata.MetadataService;
36 import org.onap.appc.metadata.objects.DependencyModelIdentifier;
38 import java.sql.SQLException;
39 import java.util.ArrayList;
42 public class MetadataServiceImpl implements MetadataService {
44 private DbLibService dbLibService;
46 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
48 private MetadataCache<DependencyModelIdentifier, String> cache;
50 public MetadataServiceImpl() {
54 private void initialize() {
55 cache = MetadataCacheFactory.getInstance().getMetadataCache();
56 // TODO initialze dbLibService
59 public void setDbLibService(DbLibService dbLibService) {
60 this.dbLibService = dbLibService;
63 void setCache(MetadataCache<DependencyModelIdentifier, String> cache) {
68 public String getVnfModel(DependencyModelIdentifier modelIdentifier) {
69 logger.debug("Reading Vnf Model data from cache for vnfType : " + modelIdentifier.getVnfType()
70 + " and catalog version : " + modelIdentifier.getCatalogVersion());
71 String vnfModel = cache.getObject(modelIdentifier);
72 if (vnfModel == null || vnfModel.length() == 0) {
73 logger.debug("Vnf Model not available in cache. Reading from database.");
74 vnfModel = readVnfModel(modelIdentifier);
75 if (vnfModel != null && vnfModel.length() > 0) {
76 logger.debug("Adding retrieved Vnf Model to cache.");
77 addVnfModel(modelIdentifier, vnfModel);
83 private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
84 cache.putObject(modelIdentifier, vnfModel);
87 private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
89 logger.debug("Reading Vnf Model data from database for RESOURCE_NAME : " + modelIdentifier.getVnfType()
90 + " and RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
91 StringBuilder query = new StringBuilder();
92 String vnfModel = null;
93 query.append("SELECT ARTIFACT_CONTENT FROM sdnctl.ASDC_ARTIFACTS WHERE RESOURCE_NAME = ? ");
94 ArrayList<String> argList = new ArrayList<>();
95 argList.add(modelIdentifier.getVnfType());
97 if (modelIdentifier.getCatalogVersion() == null) {
98 query.append(" ORDER BY SUBSTRING_INDEX(RESOURCE_VERSION, '.', 1)*1 DESC , " +
99 "SUBSTRING_INDEX(SUBSTRING_INDEX(RESOURCE_VERSION, '.', 2),'.', -1) *1 DESC , " +
100 "SUBSTRING_INDEX(RESOURCE_VERSION, '.', -1)*1 DESC ;");
102 query.append("AND RESOURCE_VERSION = ? ;");
103 argList.add(modelIdentifier.getCatalogVersion());
106 final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
108 vnfModel = data.getString("ARTIFACT_CONTENT");
109 if (vnfModel == null || vnfModel.isEmpty()) {
110 logger.error("Invalid dependency model for vnf type : " + modelIdentifier.getVnfType()
111 + " and catalog version : " + modelIdentifier.getCatalogVersion());
112 throw new RuntimeException("Invalid or Empty VNF Model");
114 logger.debug("Retrieved Vnf Model : " + vnfModel);
116 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : " + modelIdentifier.getVnfType()
117 + " AND RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
119 } catch (SQLException e) {
120 throw new RuntimeException("Database error occurred");