2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.sdc.artifacts.helper;
27 import org.apache.commons.lang.StringUtils;
28 import org.openecomp.appc.exceptions.APPCException;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import org.openecomp.appc.sdc.artifacts.object.SDCReference;
32 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
33 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
38 import javax.sql.rowset.CachedRowSet;
39 import java.sql.SQLException;
40 import java.util.ArrayList;
42 import static org.openecomp.appc.sdc.artifacts.helper.Constants.COMMA;
43 import static org.openecomp.appc.sdc.artifacts.helper.Constants.AND;
46 * Provides methods for storing sdc artifacts into app-c database
48 public class ArtifactStorageService {
50 private DbLibService dbLibService;
52 private static final String SCHEMA = "sdnctl";
54 private static final String SELECT_QUERY = Constants.SELECT_FROM + Constants.SDC_ARTIFACTS +
55 Constants.WHERE + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + Constants.QUERY_PLACEHOLDER +
56 AND + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + Constants.QUERY_PLACEHOLDER +
57 AND + Constants.ARTIFACT_TYPE + Constants.QUERY_PLACEHOLDER;
59 private static final String SELECT_QUERY_SDC_REFERENCE = Constants.SELECT_FROM + Constants.SDC_REFERENCE +
60 Constants.WHERE + Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + Constants.QUERY_PLACEHOLDER +
61 AND + Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY + Constants.QUERY_PLACEHOLDER ;
63 private static final String INSERT_QUERY = Constants.INSERT + Constants.SDC_ARTIFACTS +
64 " ( " + Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID + COMMA +
65 Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID + COMMA +
66 Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME + COMMA +
67 Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION + COMMA +
68 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID + COMMA +
69 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME + COMMA +
70 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + COMMA +
71 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + COMMA +
72 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE + COMMA +
73 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID + COMMA +
74 Constants.ARTIFACT_TYPE + COMMA +
75 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION + COMMA +
76 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION + COMMA +
77 Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE + COMMA +
78 Constants.ARTIFACT_NAME +COMMA +
79 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
81 private static final String INSERT_QUERY_WITH_INT_VER = Constants.INSERT + Constants.SDC_ARTIFACTS +
82 " ( " + Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID + COMMA +
83 Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID + COMMA +
84 Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME + COMMA +
85 Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION + COMMA +
86 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID + COMMA +
87 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME + COMMA +
88 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + COMMA +
89 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + COMMA +
90 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE + COMMA +
91 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID + COMMA +
92 Constants.ARTIFACT_TYPE + COMMA +
93 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION + COMMA +
94 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION + COMMA +
95 Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE + COMMA +
96 Constants.ARTIFACT_NAME + COMMA +
97 Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT + COMMA +
98 Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
100 private static final String SDC_REF_INSERT_QUERY = Constants.INSERT + Constants.SDC_REFERENCE +
101 "( "+ Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + COMMA +
102 Constants.SDC_REFERENCE_FIELDS.VNFC_TYPE+ COMMA +
103 Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY +COMMA +
104 Constants.SDC_REFERENCE_FIELDS.ACTION +COMMA +
105 Constants.ARTIFACT_TYPE + COMMA +
106 Constants.ARTIFACT_NAME + " ) values (?,?,?,?,?,?)";
108 private static final String SELECT_MAX_INT_VERSION = "SELECT coalesce(max(" + Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION + ")+1,1) as " + Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION +
109 " FROM " + Constants.SDC_ARTIFACTS + Constants.WHERE + Constants.ARTIFACT_NAME + Constants.QUERY_PLACEHOLDER;
112 private final EELFLogger logger = EELFManager.getInstance().getLogger(ArtifactStorageService.class);
115 * Stores Artifact received from SDC into APP-C database
116 * @param artifact - SDC Artifact object
117 * @throws APPCException
119 public void storeSDCArtifact(SDCArtifact artifact) throws APPCException {
120 if(logger.isDebugEnabled()){
121 logger.debug("Entering storeSDCArtifact with : " + artifact.toString());
124 initializeDBLibService();
125 ArrayList<String> arguments = prepareArguments(artifact);
126 dbLibService.writeData(INSERT_QUERY,arguments,SCHEMA);
127 } catch (SQLException e) {
128 logger.error("Error storing artifact in database : " +artifact.toString(),e);
129 throw new APPCException(e.getMessage(),e);
131 if(logger.isDebugEnabled()){
132 logger.debug("Exiting storeSDCArtifact");
137 * Stores Artifact received from SDC and its Reference into APP-C database if it does not exist
138 * @param artifact - SDC Artifact object
139 * @param reference - SDC reference object
140 * @throws APPCException
142 public void storeSDCArtifactWithReference(SDCArtifact artifact , SDCReference reference) throws APPCException {
143 if(logger.isDebugEnabled()){
144 logger.debug("Entering storeSDCArtifactWithReference with : " + artifact.toString());
147 initializeDBLibService();
148 SDCArtifact existingArtifact = retrieveSDCArtifact(artifact.getResourceName(), artifact.getResourceVersion(),artifact.getArtifactType());
149 if (existingArtifact ==null) { // new resource
150 logger.debug(String.format("Artifact not found for vnfType = %s, version = %s and artifactType = %s. Inserting data." ,
151 artifact.getResourceName(),artifact.getResourceVersion() ,artifact.getArtifactType()));
152 ArrayList<String> arguments = prepareArguments(artifact);
153 Integer version = getNextInternalVersion(artifact.getArtifactName());
154 arguments.add(version.toString());
155 dbLibService.writeData(INSERT_QUERY_WITH_INT_VER,arguments,SCHEMA);
156 } else { // duplicate
157 logger.debug(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'",
158 artifact.getArtifactType() , artifact.getResourceName() , artifact.getResourceVersion()));
161 SDCReference existingReference = retrieveSDCReference(reference.getVnfType(),reference.getFileCategory());
162 if(existingReference == null){
163 logger.debug("Inserting SDC Reference data: " +reference.toString());
164 ArrayList<String> arguments = prepareReferenceArguments(reference);
165 dbLibService.writeData(SDC_REF_INSERT_QUERY,arguments,SCHEMA);
167 logger.debug("Artifact reference already exists for: " +reference.toString());
169 } catch (SQLException e) {
170 logger.error("Error storing artifact to database: " + artifact.toString(),e);
171 throw new APPCException(e.getMessage(),e);
173 if(logger.isDebugEnabled()){
174 logger.debug("Exiting storeSDCArtifactWithReference");
178 private Integer getNextInternalVersion(String artifactName) throws APPCException {
179 if (logger.isDebugEnabled()) {
180 logger.debug("Entering getNextInternalVersion with artifactName:" + artifactName);
184 initializeDBLibService();
185 ArrayList<String> arguments = new ArrayList<>();
186 arguments.add(artifactName);
187 CachedRowSet rowSet = dbLibService.getData(SELECT_MAX_INT_VERSION, arguments, SCHEMA);
188 if (rowSet.first()) {
189 version = rowSet.getInt(Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION .toString());
191 }catch (SQLException e) {
192 logger.error("Error getting internal version for artifact name " + artifactName , e);
193 throw new APPCException(e);
195 if (logger.isDebugEnabled()) {
196 logger.debug("Exiting getNextInternalVersion with retrieved version:" + version.toString());
201 private void initializeDBLibService() {
202 if(dbLibService == null){
203 BundleContext context = FrameworkUtil.getBundle(DbLibService.class).getBundleContext();
204 ServiceReference serviceReference = context.getServiceReference(DbLibService.class.getName());
205 dbLibService = (DbLibService)context.getService(serviceReference);
209 private ArrayList<String> prepareReferenceArguments(SDCReference reference) {
210 ArrayList<String> arguments = new ArrayList<>();
211 arguments.add(reference.getVnfType());
212 arguments.add(reference.getVnfcType());
213 arguments.add(reference.getFileCategory());
214 arguments.add(reference.getAction());
215 arguments.add(reference.getArtifactType());
216 arguments.add(reference.getArtifactName());
220 private ArrayList<String> prepareArguments(SDCArtifact artifact) {
221 ArrayList<String> arguments = new ArrayList<>();
222 arguments.add(artifact.getServiceUUID());
223 arguments.add(artifact.getDistributionId());
224 arguments.add(artifact.getServiceName());
225 arguments.add(truncateServiceDescription(artifact.getServiceDescription()));
226 arguments.add(artifact.getResourceUUID());
227 arguments.add(artifact.getResourceInstanceName());
228 arguments.add(artifact.getResourceName());
229 arguments.add(artifact.getResourceVersion());
230 arguments.add(artifact.getResourceType());
231 arguments.add(artifact.getArtifactUUID());
232 arguments.add(artifact.getArtifactType());
233 arguments.add(artifact.getArtifactVersion());
234 arguments.add(artifact.getArtifactDescription());
235 arguments.add(artifact.getCreationDate());
236 arguments.add(artifact.getArtifactName());
237 arguments.add(artifact.getArtifactContent());
241 private String truncateServiceDescription(String serviceDescription){
242 if (!StringUtils.isBlank(serviceDescription) && serviceDescription.length()>255){
243 logger.info("Truncating the SERVICE_DESCRIPTION to 255 characters");
244 serviceDescription=serviceDescription.substring(0,255);
246 return serviceDescription;
250 * Reads the SDC artifact from APP-C database
251 * @param resourceName - resource Name from SDC Artifact
252 * @param resourceVersion - resource version from SDC Artifact
253 * @param artifactType artifact type from SDC Artifact
254 * @return - SDC_ARTIFACT record if data exists
255 * @throws APPCException
257 public SDCArtifact retrieveSDCArtifact(String resourceName, String resourceVersion, String artifactType) throws APPCException {
258 SDCArtifact artifact = null;
260 initializeDBLibService();
261 ArrayList<String> arguments = new ArrayList<>();
262 arguments.add(resourceName);
263 arguments.add(resourceVersion);
264 arguments.add(artifactType);
265 CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY, arguments, SCHEMA);
266 if (rowSet.first()) {
267 artifact = new SDCArtifact();
268 artifact.setArtifactUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID.toString()));
269 artifact.setArtifactName(rowSet.getString(Constants.ARTIFACT_NAME));
270 artifact.setArtifactType(rowSet.getString(Constants.ARTIFACT_TYPE));
271 artifact.setArtifactVersion(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION.toString()));
272 artifact.setArtifactDescription(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION.toString()));
273 artifact.setArtifactContent(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.toString()));
275 artifact.setResourceUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID.toString()));
276 artifact.setResourceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME.toString()));
277 artifact.setResourceType(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE.toString()));
278 artifact.setResourceVersion(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.toString()));
279 artifact.setResourceInstanceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME.toString()));
281 artifact.setServiceUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID.toString()));
282 artifact.setServiceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME.toString()));
283 artifact.setServiceDescription(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION.toString()));
285 artifact.setCreationDate(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE.toString()));
286 artifact.setDistributionId(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID.toString()));
289 } catch (SQLException e) {
290 logger.error("Error query artifact for " + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + " = " + resourceName +
291 Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + " = " + resourceVersion +
292 Constants.ARTIFACT_TYPE + " = " + artifactType, e);
293 throw new APPCException(e);
299 * Reads the SDC reference from APP-C database
300 * @param vnfType - vnf Type from SDC reference
301 * @param fileCategory - file category from SDC reference
302 * @return - SDC_ARTIFACT record if data exists
303 * @throws APPCException
305 public SDCReference retrieveSDCReference(String vnfType, String fileCategory) throws APPCException {
306 SDCReference reference = null;
308 initializeDBLibService();
309 ArrayList<String> arguments = new ArrayList<>();
310 arguments.add(vnfType);
311 arguments.add(fileCategory);
312 CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY_SDC_REFERENCE, arguments, SCHEMA);
313 if (rowSet.first()) {
314 reference = new SDCReference();
315 reference.setVnfType(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.VNF_TYPE.toString()));
316 reference.setVnfcType(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.VNFC_TYPE.toString()));
317 reference.setFileCategory(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY.toString()));
318 reference.setAction(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.ACTION.toString()));
319 reference.setArtifactType(rowSet.getString(Constants.ARTIFACT_TYPE));
320 reference.setArtifactName(rowSet.getString(Constants.ARTIFACT_NAME));
322 } catch (SQLException e) {
323 logger.error("Error querying SDC_REFERENCE for " + Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + " = " + vnfType +
324 Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY + " = " + fileCategory , e);
325 throw new APPCException(e);