79d0ffac2ab3f4dd2e775aa9bedd020cf8718b57
[appc.git] / appc-common / src / main / java / org / onap / appc / metadata / impl / MetadataServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.metadata.impl;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.ccsdk.sli.core.dblib.DbLibService;
30
31 import javax.sql.rowset.CachedRowSet;
32
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;
37
38 import java.sql.SQLException;
39 import java.util.ArrayList;
40
41
42 public class MetadataServiceImpl implements MetadataService {
43
44     private DbLibService dbLibService;
45
46     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
47
48     private MetadataCache<DependencyModelIdentifier, String> cache;
49
50     public MetadataServiceImpl() {
51         initialize();
52     }
53
54     private void initialize() {
55         cache = MetadataCacheFactory.getInstance().getMetadataCache();
56         // TODO initialze dbLibService
57     }
58
59     public void setDbLibService(DbLibService dbLibService) {
60         this.dbLibService = dbLibService;
61     }
62
63     void setCache(MetadataCache<DependencyModelIdentifier, String> cache) {
64         this.cache = cache;
65     }
66
67     @Override
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);
78             }
79         }
80         return vnfModel;
81     }
82
83     private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
84         cache.putObject(modelIdentifier, vnfModel);
85     }
86
87     private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
88
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());
96
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 ;");
101         } else {
102             query.append("AND RESOURCE_VERSION = ? ;");
103             argList.add(modelIdentifier.getCatalogVersion());
104         }
105         try {
106             final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
107             if (data.first()) {
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");
113                 }
114                 logger.debug("Retrieved Vnf Model : " + vnfModel);
115             } else {
116                 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : " + modelIdentifier.getVnfType()
117                     + " AND RESOURCE_VERSION : " + modelIdentifier.getCatalogVersion());
118             }
119         } catch (SQLException e) {
120             throw new RuntimeException("Database error occurred");
121         }
122         return vnfModel;
123     }
124 }