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