Second part of onap rename
[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     @Override
64     public String getVnfModel(DependencyModelIdentifier modelIdentifier) {
65         logger.debug("Reading Vnf Model data from cache for vnfType : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
66         String vnfModel = cache.getObject(modelIdentifier);
67         if(vnfModel ==null || vnfModel.length() ==0){
68             logger.debug("Vnf Model not available in cache. Reading from database.");
69             vnfModel = readVnfModel(modelIdentifier);
70             if(vnfModel !=null  && vnfModel.length()>0){
71                 logger.debug("Adding retrieved Vnf Model to cache.");
72                 addVnfModel(modelIdentifier,vnfModel);
73             }
74         }
75         return vnfModel;
76     }
77
78     private void addVnfModel(DependencyModelIdentifier modelIdentifier, String vnfModel) {
79         cache.putObject(modelIdentifier,vnfModel);
80     }
81
82     private String readVnfModel(DependencyModelIdentifier modelIdentifier) {
83
84         logger.debug("Reading Vnf Model data from database for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" and RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
85         StringBuilder query = new StringBuilder();
86         String vnfModel =null;
87         query.append("SELECT ARTIFACT_CONTENT FROM sdnctl.ASDC_ARTIFACTS WHERE RESOURCE_NAME = ? ") ;
88         ArrayList<String> argList = new ArrayList<>();
89         argList.add(modelIdentifier.getVnfType());
90
91         if (modelIdentifier.getCatalogVersion()==null){
92             query.append(" ORDER BY  SUBSTRING_INDEX(RESOURCE_VERSION, '.', 1)*1  DESC , " +
93                     "SUBSTRING_INDEX(SUBSTRING_INDEX(RESOURCE_VERSION, '.', 2),'.', -1) *1 DESC , " +
94                     "SUBSTRING_INDEX(RESOURCE_VERSION, '.', -1)*1 DESC ;");
95         }else{
96             query.append("AND RESOURCE_VERSION = ? ;");
97             argList.add(modelIdentifier.getCatalogVersion());
98         }
99         try {
100             final CachedRowSet data = dbLibService.getData(query.toString(), argList, "sdnctl");
101             if (data.first()) {
102                 vnfModel = data.getString("ARTIFACT_CONTENT");
103                 if (vnfModel == null || vnfModel.isEmpty()) {
104                     logger.error("Invalid dependency model for vnf type : "+ modelIdentifier.getVnfType() +" and catalog version : " +modelIdentifier.getCatalogVersion());
105                     throw new RuntimeException("Invalid or Empty VNF Model");
106                 }
107                 logger.debug("Retrieved Vnf Model : " + vnfModel);
108             }else {
109                 logger.warn("VNF Model not found in datastore for RESOURCE_NAME : "+ modelIdentifier.getVnfType() +" AND RESOURCE_VERSION : " +modelIdentifier.getCatalogVersion());
110             }
111         } catch (SQLException e) {
112             throw new RuntimeException("Database error occurred");
113         }
114         return  vnfModel;
115     }
116 }