Merging in bug fixes
[appc.git] / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / artifacts / helper / ArtifactStorageService.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.openecomp.appc.sdc.artifacts.helper;
26
27 import org.openecomp.appc.exceptions.APPCException;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
30 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
31 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
32 import org.osgi.framework.BundleContext;
33 import org.osgi.framework.FrameworkUtil;
34 import org.osgi.framework.ServiceReference;
35
36 import javax.sql.rowset.CachedRowSet;
37 import java.sql.SQLException;
38 import java.util.ArrayList;
39
40 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS;
41 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS.*;
42
43 /**
44  * Provides methods for storing sdc artifacts into app-c database
45  */
46 public class ArtifactStorageService {
47
48     private DbLibService dbLibService;
49
50     private static final String COMMA = " , ";
51     private static final String QUERY_PLACEHOLDER = " = ? ";
52
53     private static final String SCHEMA =  "sdnctl";
54
55     private static final String SELECT_QUERY = "SELECT * FROM " + ASDC_ARTIFACTS +
56                                                 " WHERE " + RESOURCE_NAME + QUERY_PLACEHOLDER +
57                                                 " AND " + RESOURCE_VERSION + QUERY_PLACEHOLDER +
58                                                 " AND " + ARTIFACT_TYPE + QUERY_PLACEHOLDER;
59
60     private static final String INSERT_QUERY = "INSERT INTO " + ASDC_ARTIFACTS +
61             " ( " + SERVICE_UUID + COMMA +
62                     DISTRIBUTION_ID + COMMA +
63                     SERVICE_NAME + COMMA +
64                     SERVICE_DESCRIPTION + COMMA +
65                     RESOURCE_UUID + COMMA +
66                     RESOURCE_INSTANCE_NAME + COMMA +
67                     RESOURCE_NAME + COMMA +
68                     RESOURCE_VERSION + COMMA +
69                     RESOURCE_TYPE + COMMA +
70                     ARTIFACT_UUID + COMMA +
71                     ARTIFACT_TYPE + COMMA +
72                     ARTIFACT_VERSION + COMMA +
73                     ARTIFACT_DESCRIPTION + COMMA +
74                     CREATION_DATE + COMMA +
75                     ARTIFACT_NAME  +COMMA +
76                     ARTIFACT_CONTENT + " ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
77
78
79     private final EELFLogger logger = EELFManager.getInstance().getLogger(ArtifactStorageService.class);
80
81     /**
82      * Stores Artifact received from SDC into APP-C database
83      * @param artifact
84      * @throws APPCException
85      */
86     public void storeASDCArtifact(SDCArtifact artifact) throws APPCException {
87         if(logger.isDebugEnabled()){
88             logger.debug("Entering storeASDCArtifact with : " + artifact.toString());
89         }
90         try {
91             initializeDBLibService();
92             ArrayList<String> arguments = prepareArguments(artifact);
93             dbLibService.writeData(INSERT_QUERY,arguments,SCHEMA);
94         } catch (SQLException e) {
95             logger.error("Error storing artifact in database : " +artifact.toString(),e);
96             throw new APPCException(e.getMessage(),e);
97         }
98         if(logger.isDebugEnabled()){
99             logger.debug("Exiting storeASDCArtifact");
100         }
101     }
102
103     private void initializeDBLibService() {
104         if(dbLibService == null){
105             BundleContext context = FrameworkUtil.getBundle(DbLibService.class).getBundleContext();
106             ServiceReference serviceReference = context.getServiceReference(DbLibService.class.getName());
107             dbLibService  = (DbLibService)context.getService(serviceReference);
108         }
109     }
110
111     private ArrayList<String> prepareArguments(SDCArtifact artifact) {
112         ArrayList<String> arguments = new ArrayList<>();
113         arguments.add(artifact.getServiceUUID());
114         arguments.add(artifact.getDistributionId());
115         arguments.add(artifact.getServiceName());
116         arguments.add(artifact.getServiceDescription());
117         arguments.add(artifact.getResourceUUID());
118         arguments.add(artifact.getResourceInstanceName());
119         arguments.add(artifact.getResourceName());
120         arguments.add(artifact.getResourceVersion());
121         arguments.add(artifact.getResourceType());
122         arguments.add(artifact.getArtifactUUID());
123         arguments.add(artifact.getArtifactType());
124         arguments.add(artifact.getArtifactVersion());
125         arguments.add(artifact.getArtifactDescription());
126         arguments.add(artifact.getCreationDate());
127         arguments.add(artifact.getArtifactName());
128         arguments.add(artifact.getArtifactContent());
129         return arguments;
130     }
131
132     /**
133      * reads the SDC artifact from APP-C database
134      * @param resourceName
135      * @param resourceVersion
136      * @param artifactType
137      * @return
138      * @throws APPCException
139      */
140     public SDCArtifact retrieveSDCArtifact(String resourceName, String resourceVersion, String artifactType) throws APPCException {
141         SDCArtifact artifact = null;
142         try {
143             initializeDBLibService();
144             ArrayList<String> arguments = new ArrayList<>();
145             arguments.add(resourceName);
146             arguments.add(resourceVersion);
147             arguments.add(artifactType);
148             CachedRowSet rowSet = dbLibService.getData(SELECT_QUERY, arguments, SCHEMA);
149             if (rowSet.first()) {
150                 artifact = new SDCArtifact();
151                 artifact.setArtifactUUID(rowSet.getString(ARTIFACT_UUID.toString()));
152                 artifact.setArtifactName(rowSet.getString(ARTIFACT_NAME.toString()));
153                 artifact.setArtifactType(rowSet.getString(ARTIFACT_TYPE.toString()));
154                 artifact.setArtifactVersion(rowSet.getString(ARTIFACT_VERSION.toString()));
155                 artifact.setArtifactDescription(rowSet.getString(ARTIFACT_DESCRIPTION.toString()));
156                 artifact.setArtifactContent(rowSet.getString(ARTIFACT_CONTENT.toString()));
157
158                 artifact.setResourceUUID(rowSet.getString(RESOURCE_UUID.toString()));
159                 artifact.setResourceName(rowSet.getString(RESOURCE_NAME.toString()));
160                 artifact.setResourceType(rowSet.getString(RESOURCE_TYPE.toString()));
161                 artifact.setResourceVersion(rowSet.getString(RESOURCE_VERSION.toString()));
162                 artifact.setResourceInstanceName(rowSet.getString(RESOURCE_INSTANCE_NAME.toString()));
163
164                 artifact.setServiceUUID(rowSet.getString(SERVICE_UUID.toString()));
165                 artifact.setServiceName(rowSet.getString(SERVICE_NAME.toString()));
166                 artifact.setServiceDescription(rowSet.getString(SERVICE_DESCRIPTION.toString()));
167
168                 artifact.setCreationDate(rowSet.getString(CREATION_DATE.toString()));
169                 artifact.setDistributionId(rowSet.getString(DISTRIBUTION_ID.toString()));
170             }
171
172         } catch (SQLException e) {
173             logger.error("Error query artifact for " + RESOURCE_NAME + " = " + resourceName +
174                     RESOURCE_VERSION + " = " + resourceVersion +
175                     ARTIFACT_TYPE + " = " + artifactType, e);
176             throw new APPCException(e);
177         }
178         return artifact;
179     }
180 }