Merge of new rebased code
[appc.git] / appc-common / src / main / java / org / openecomp / appc / metadata / impl / MetadataServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.metadata.impl;
23
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
27
28 import javax.sql.rowset.CachedRowSet;
29
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;
34
35 import java.sql.SQLException;
36 import java.util.ArrayList;
37
38
39 public class MetadataServiceImpl implements MetadataService {
40
41     private DbLibService dbLibService;
42
43     private static final EELFLogger logger = EELFManager.getInstance().getLogger(MetadataServiceImpl.class);
44
45     private MetadataCache<DependencyModelIdentifier,String> cache;
46
47     public MetadataServiceImpl(){
48         initialize();
49     }
50
51     private void initialize(){
52         cache = MetadataCacheFactory.getInstance().getMetadataCache();
53         // TODO initialze dbLibService
54     }
55
56     public void setDbLibService(DbLibService dbLibService) {
57         this.dbLibService = dbLibService;
58     }
59
60     @Override
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);
70             }
71         }
72         return vnfModel;
73     }
74
75     private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
76         cache.putObject(modelIdentifier,vnfModel);
77     }
78
79     private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
80
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());
87
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 ;");
92         }else{
93             query.append("AND RESOURCE_VERSION = ? ;");
94             argList.add(modelIdentifier.getCatalogVersion());
95         }
96         try {
97             final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
98             if (data.first()) {
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");
103                 }
104                 logger.debug("Retrieved Vnf Model : " + vnfModel);
105             }else {
106                 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" AND RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
107             }
108         } catch (SQLException e) {
109             throw new RuntimeException("Database error occurred");
110         }
111         return  vnfModel;
112     }
113 }