summaryrefslogtreecommitdiffstats
path: root/appc-sdc-listener/appc-sdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/artifacts/helper/ArtifactStorageService.java
blob: db4da9fef305cd86e1e692bb16295db0582acf14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.sdc.artifacts.helper;

import org.apache.commons.lang.StringUtils;
import org.openecomp.appc.exceptions.APPCException;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.openecomp.appc.sdc.artifacts.object.SDCReference;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.util.ArrayList;

import static org.openecomp.appc.sdc.artifacts.helper.Constants.COMMA;
import static org.openecomp.appc.sdc.artifacts.helper.Constants.AND;

/**
 * Provides methods for storing sdc artifacts into app-c database
 */
public class ArtifactStorageService {

    private DbLibService dbLibService;

    private static final String SCHEMA =  "sdnctl";

    private static final String SELECT_QUERY = Constants.SELECT_FROM + Constants.SDC_ARTIFACTS +
            Constants.WHERE + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + Constants.QUERY_PLACEHOLDER +
            AND + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + Constants.QUERY_PLACEHOLDER +
            AND + Constants.ARTIFACT_TYPE + Constants.QUERY_PLACEHOLDER;

    private static final String SELECT_QUERY_SDC_REFERENCE = Constants.SELECT_FROM + Constants.SDC_REFERENCE +
            Constants.WHERE + Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + Constants.QUERY_PLACEHOLDER +
            AND + Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY + Constants.QUERY_PLACEHOLDER ;

    private static final String INSERT_QUERY = Constants.INSERT + Constants.SDC_ARTIFACTS +
            " ( " + Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID + COMMA +
            Constants.ARTIFACT_TYPE + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE + COMMA +
            Constants.ARTIFACT_NAME  +COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    private static final String INSERT_QUERY_WITH_INT_VER = Constants.INSERT + Constants.SDC_ARTIFACTS +
            " ( " + Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID + COMMA +
            Constants.ARTIFACT_TYPE + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE + COMMA +
            Constants.ARTIFACT_NAME + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT + COMMA +
            Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    private static final String SDC_REF_INSERT_QUERY = Constants.INSERT + Constants.SDC_REFERENCE +
            "( "+ Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + COMMA +
            Constants.SDC_REFERENCE_FIELDS.VNFC_TYPE+ COMMA +
            Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY +COMMA +
            Constants.SDC_REFERENCE_FIELDS.ACTION +COMMA +
            Constants.ARTIFACT_TYPE + COMMA +
            Constants.ARTIFACT_NAME  + " ) values (?,?,?,?,?,?)";

    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  +
            " FROM " + Constants.SDC_ARTIFACTS + Constants.WHERE + Constants.ARTIFACT_NAME  + Constants.QUERY_PLACEHOLDER;


    private final EELFLogger logger = EELFManager.getInstance().getLogger(ArtifactStorageService.class);

    /**
     * Stores Artifact received from SDC into APP-C database
     * @param artifact - SDC Artifact object
     * @throws APPCException
     */
    public void storeSDCArtifact(SDCArtifact artifact) throws APPCException {
        if(logger.isDebugEnabled()){
            logger.debug("Entering storeSDCArtifact with : " + artifact.toString());
        }
        try {
            initializeDBLibService();
            ArrayList<String> arguments = prepareArguments(artifact);
            dbLibService.writeData(INSERT_QUERY,arguments,SCHEMA);
        } catch (SQLException e) {
            logger.error("Error storing artifact in database : " +artifact.toString(),e);
            throw new APPCException(e.getMessage(),e);
        }
        if(logger.isDebugEnabled()){
            logger.debug("Exiting storeSDCArtifact");
        }
    }

    /**
     * Stores Artifact received from SDC and its Reference into APP-C database if it does not exist
     * @param artifact - SDC Artifact object
     * @param reference - SDC reference object
     * @throws APPCException
     */
    public void storeSDCArtifactWithReference(SDCArtifact artifact , SDCReference reference) throws APPCException {
        if(logger.isDebugEnabled()){
            logger.debug("Entering storeSDCArtifactWithReference with : " + artifact.toString());
        }
        try {
            initializeDBLibService();
            SDCArtifact existingArtifact = retrieveSDCArtifact(artifact.getResourceName(), artifact.getResourceVersion(),artifact.getArtifactType());
            if (existingArtifact ==null) { // new resource
                logger.debug(String.format("Artifact not found for vnfType = %s, version = %s and artifactType = %s. Inserting data." ,
                        artifact.getResourceName(),artifact.getResourceVersion() ,artifact.getArtifactType()));
                ArrayList<String> arguments = prepareArguments(artifact);
                Integer version = getNextInternalVersion(artifact.getArtifactName());
                arguments.add(version.toString());
                dbLibService.writeData(INSERT_QUERY_WITH_INT_VER,arguments,SCHEMA);
            } else { // duplicate
                logger.debug(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'",
                        artifact.getArtifactType() , artifact.getResourceName() , artifact.getResourceVersion()));
            }

            SDCReference existingReference = retrieveSDCReference(reference.getVnfType(),reference.getFileCategory());
            if(existingReference == null){
                logger.debug("Inserting SDC Reference data: " +reference.toString());
                ArrayList<String> arguments = prepareReferenceArguments(reference);
                dbLibService.writeData(SDC_REF_INSERT_QUERY,arguments,SCHEMA);
            }else{
                logger.debug("Artifact reference already exists for: " +reference.toString());
            }
        } catch (SQLException e) {
            logger.error("Error storing artifact to database: " + artifact.toString(),e);
            throw new APPCException(e.getMessage(),e);
        }
        if(logger.isDebugEnabled()){
            logger.debug("Exiting storeSDCArtifactWithReference");
        }
    }

    private Integer getNextInternalVersion(String artifactName) throws APPCException {
        if (logger.isDebugEnabled()) {
            logger.debug("Entering getNextInternalVersion with artifactName:" + artifactName);
        }
        Integer version = 1;
        try {
            initializeDBLibService();
            ArrayList<String> arguments = new ArrayList<>();
            arguments.add(artifactName);
            CachedRowSet rowSet = dbLibService.getData(SELECT_MAX_INT_VERSION, arguments, SCHEMA);
            if (rowSet.first()) {
                version = rowSet.getInt(Constants.SDC_ARTIFACTS_FIELDS.INTERNAL_VERSION .toString());
            }
        }catch (SQLException e) {
            logger.error("Error getting internal version for artifact name " + artifactName , e);
            throw new APPCException(e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Exiting getNextInternalVersion with retrieved version:" + version.toString());
        }
        return version;
    }

    private void initializeDBLibService() {
        if(dbLibService == null){
            BundleContext context = FrameworkUtil.getBundle(DbLibService.class).getBundleContext();
            ServiceReference serviceReference = context.getServiceReference(DbLibService.class.getName());
            dbLibService  = (DbLibService)context.getService(serviceReference);
        }
    }

    private ArrayList<String> prepareReferenceArguments(SDCReference reference) {
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add(reference.getVnfType());
        arguments.add(reference.getVnfcType());
        arguments.add(reference.getFileCategory());
        arguments.add(reference.getAction());
        arguments.add(reference.getArtifactType());
        arguments.add(reference.getArtifactName());
        return arguments;
    }

    private ArrayList<String> prepareArguments(SDCArtifact artifact) {
        ArrayList<String> arguments = new ArrayList<>();
        arguments.add(artifact.getServiceUUID());
        arguments.add(artifact.getDistributionId());
        arguments.add(artifact.getServiceName());
        arguments.add(truncateServiceDescription(artifact.getServiceDescription()));
        arguments.add(artifact.getResourceUUID());
        arguments.add(artifact.getResourceInstanceName());
        arguments.add(artifact.getResourceName());
        arguments.add(artifact.getResourceVersion());
        arguments.add(artifact.getResourceType());
        arguments.add(artifact.getArtifactUUID());
        arguments.add(artifact.getArtifactType());
        arguments.add(artifact.getArtifactVersion());
        arguments.add(artifact.getArtifactDescription());
        arguments.add(artifact.getCreationDate());
        arguments.add(artifact.getArtifactName());
        arguments.add(artifact.getArtifactContent());
        return arguments;
    }

    private String truncateServiceDescription(String serviceDescription){
        if (!StringUtils.isBlank(serviceDescription) && serviceDescription.length()>255){
            logger.info("Truncating the SERVICE_DESCRIPTION to 255 characters");
            serviceDescription=serviceDescription.substring(0,255);
        }
        return serviceDescription;
    }

    /**
     * Reads the SDC artifact from APP-C database
     * @param resourceName  - resource Name from SDC Artifact
     * @param resourceVersion - resource version from SDC Artifact
     * @param artifactType artifact type from SDC Artifact
     * @return - SDC_ARTIFACT record if data exists
     * @throws APPCException
     */
    public SDCArtifact retrieveSDCArtifact(String resourceName, String resourceVersion, String artifactType) throws APPCException {
        SDCArtifact artifact = null;
        try {
            initializeDBLibService();
            ArrayList<String> arguments = new ArrayList<>();
            arguments.add(resourceName);
            arguments.add(resourceVersion);
            arguments.add(artifactType);
            CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY, arguments, SCHEMA);
            if (rowSet.first()) {
                artifact = new SDCArtifact();
                artifact.setArtifactUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_UUID.toString()));
                artifact.setArtifactName(rowSet.getString(Constants.ARTIFACT_NAME));
                artifact.setArtifactType(rowSet.getString(Constants.ARTIFACT_TYPE));
                artifact.setArtifactVersion(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_VERSION.toString()));
                artifact.setArtifactDescription(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_DESCRIPTION.toString()));
                artifact.setArtifactContent(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.toString()));

                artifact.setResourceUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_UUID.toString()));
                artifact.setResourceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME.toString()));
                artifact.setResourceType(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_TYPE.toString()));
                artifact.setResourceVersion(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.toString()));
                artifact.setResourceInstanceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_INSTANCE_NAME.toString()));

                artifact.setServiceUUID(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_UUID.toString()));
                artifact.setServiceName(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_NAME.toString()));
                artifact.setServiceDescription(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.SERVICE_DESCRIPTION.toString()));

                artifact.setCreationDate(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.CREATION_DATE.toString()));
                artifact.setDistributionId(rowSet.getString(Constants.SDC_ARTIFACTS_FIELDS.DISTRIBUTION_ID.toString()));
            }

        } catch (SQLException e) {
            logger.error("Error query artifact for " + Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_NAME + " = " + resourceName +
                    Constants.SDC_ARTIFACTS_FIELDS.RESOURCE_VERSION + " = " + resourceVersion +
                    Constants.ARTIFACT_TYPE + " = " + artifactType, e);
            throw new APPCException(e);
        }
        return artifact;
    }

    /**
     * Reads the SDC reference from APP-C database
     * @param vnfType  - vnf Type from SDC reference
     * @param fileCategory - file category from SDC reference
     * @return - SDC_ARTIFACT record if data exists
     * @throws APPCException
     */
    public SDCReference retrieveSDCReference(String vnfType, String fileCategory) throws APPCException {
        SDCReference reference = null;
        try {
            initializeDBLibService();
            ArrayList<String> arguments = new ArrayList<>();
            arguments.add(vnfType);
            arguments.add(fileCategory);
            CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY_SDC_REFERENCE, arguments, SCHEMA);
            if (rowSet.first()) {
                reference = new SDCReference();
                reference.setVnfType(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.VNF_TYPE.toString()));
                reference.setVnfcType(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.VNFC_TYPE.toString()));
                reference.setFileCategory(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY.toString()));
                reference.setAction(rowSet.getString(Constants.SDC_REFERENCE_FIELDS.ACTION.toString()));
                reference.setArtifactType(rowSet.getString(Constants.ARTIFACT_TYPE));
                reference.setArtifactName(rowSet.getString(Constants.ARTIFACT_NAME));
            }
        } catch (SQLException e) {
            logger.error("Error querying SDC_REFERENCE for " + Constants.SDC_REFERENCE_FIELDS.VNF_TYPE + " = " + vnfType +
                    Constants.SDC_REFERENCE_FIELDS.FILE_CATEGORY + " = " + fileCategory , e);
            throw new APPCException(e);
        }
        return reference;
    }
}