2 * ============LICENSE_START=======================================================
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
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 * ============LICENSE_END=========================================================
24 package org.onap.appc.metadata.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import org.onap.ccsdk.sli.core.dblib.DbLibService;
30 import javax.sql.rowset.CachedRowSet;
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;
37 import java.sql.SQLException;
38 import java.util.ArrayList;
41 public class MetadataServiceImpl implements MetadataService {
43 private DbLibService dbLibService;
45 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
47 private MetadataCache<DependencyModelIdentifier, String> cache;
49 public MetadataServiceImpl() {
53 private void initialize() {
54 cache = MetadataCacheFactory.getInstance().getMetadataCache();
55 // TODO initialze dbLibService
58 public void setDbLibService(DbLibService dbLibService) {
59 this.dbLibService = dbLibService;
62 void setCache(MetadataCache<DependencyModelIdentifier, String> cache) {
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);
82 private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
83 cache.putObject(modelIdentifier, vnfModel);
86 private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
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());
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 ;");
101 query.append("AND RESOURCE_VERSION = ? ;");
102 argList.add(modelIdentifier.getCatalogVersion());
105 final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
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");
113 logger.debug("Retrieved Vnf Model : " + vnfModel);
115 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : " + modelIdentifier.getVnfType()
116 + " AND RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
118 } catch (SQLException e) {
119 throw new RuntimeException("Database error occurred");