Sync Integ to Master
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / impl / ResourceUploader.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.resources.impl;
22
23 import javax.annotation.PostConstruct;
24 import javax.annotation.Resource;
25
26 import org.openecomp.sdc.be.config.BeEcompErrorManager;
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.api.ICatalogDAO;
29 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
30 import org.openecomp.sdc.be.resources.api.IResourceUploader;
31 import org.openecomp.sdc.be.resources.data.ESArtifactData;
32 import org.openecomp.sdc.be.resources.exception.ResourceDAOException;
33 import org.openecomp.sdc.common.config.EcompErrorName;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.stereotype.Component;
37
38 import fj.data.Either;
39
40 @Component("resource-upload")
41 public class ResourceUploader implements IResourceUploader {
42
43         private static final String DEFAULT_ARTIFACT_INDEX_NAME = "resources";
44
45         @Resource
46         private ICatalogDAO resourceDAO;
47         private static Logger log = LoggerFactory.getLogger(ResourceUploader.class.getName());
48
49         @PostConstruct
50         public void init() {
51                 ConfigurationManager configMgr = ConfigurationManager.getConfigurationManager();
52                 String artifactsIndex = null;
53                 artifactsIndex = configMgr.getConfiguration().getArtifactsIndex();
54                 if (artifactsIndex == null || artifactsIndex.isEmpty()) {
55                         artifactsIndex = DEFAULT_ARTIFACT_INDEX_NAME;
56                 }
57                 resourceDAO.addToIndicesMap(ESArtifactData.class.getSimpleName().toLowerCase(), artifactsIndex);
58         }
59
60         public ResourceUploader() {
61                 super();
62         }
63
64         public ResourceUploader(ICatalogDAO resourcetDAO) {
65                 super();
66                 this.resourceDAO = resourcetDAO;
67         }
68
69         public ICatalogDAO getResourceDAO() {
70                 return resourceDAO;
71         }
72
73         public void setResourceDAO(ICatalogDAO resourceDAO) {
74                 this.resourceDAO = resourceDAO;
75         }
76
77         @Override
78         public ResourceUploadStatus saveArtifact(ESArtifactData artifactData, boolean isReload) {
79                 ResourceUploadStatus status = ResourceUploadStatus.OK;
80                 if (resourceDAO == null) {
81                         BeEcompErrorManager.getInstance()
82                                         .logBeInitializationError("Save Artifact - internal object not initialized");
83                         log.debug("update artifact failed - resourceDAO is null");
84                         return ResourceUploadStatus.ERROR;
85                 }
86
87                 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = getArtifact(artifactData.getId());
88                 if (getArtifactStatus.isLeft()) {
89                         status = ResourceUploadStatus.ALREADY_EXIST;
90                         log.debug("ResourceUploadStatus:saveArtifact artifact with id {} already exist.", artifactData.getId());
91                         if (isReload) {
92                                 status = updateArtifact(artifactData, getArtifactStatus.left().value());
93                         }
94                 } else {
95                         try {
96
97                                 resourceDAO.writeArtifact(artifactData);
98                                 status = ResourceUploadStatus.OK;
99
100                         } catch (ResourceDAOException e) {
101                                 status = ResourceUploadStatus.ERROR;
102                                 BeEcompErrorManager.getInstance().logBeDaoSystemError("Save Artifact to database");
103                                 log.debug("ResourceUploadStatus:saveArtifact failed with exception ", e);
104                         }
105
106                 }
107
108                 return status;
109         }
110
111         @Override
112         public ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData) {
113                 ResourceUploadStatus status = ResourceUploadStatus.OK;
114                 if (resourceDAO == null)
115                         return ResourceUploadStatus.ERROR;
116
117                 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = getArtifact(artifactUpdateData.getId());
118                 if (getArtifactStatus.isRight()) {
119                         status = getArtifactStatus.right().value();
120                         log.debug("ResourceUploadStatus:updateArtifactt artifact with id {}", artifactUpdateData.getId()
121                                         + " not exist.");
122                 }
123                 if (getArtifactStatus.isLeft()) {
124                         status = updateArtifact(artifactUpdateData, getArtifactStatus.left().value());
125                 }
126
127                 return status;
128         }
129
130         /*
131          * @Override public ResourceUploadStatus
132          * updateServiceArtifact(ServiceArtifactData artifactUpdateData) {
133          * ResourceUploadStatus status = ResourceUploadStatus.OK; if(resourceDAO ==
134          * null) return ResourceUploadStatus.ERROR; Either<ServiceArtifactData,
135          * ResourceUploadStatus> getServiceArtifactStatus =
136          * getServiceArtifact(artifactUpdateData.getId());
137          * 
138          * if(getServiceArtifactStatus.isRight()){
139          * log.debug("ResourceUploadStatus:updateArtifactt artifact with id " +
140          * artifactUpdateData.getId() + " not exist."); status =
141          * getServiceArtifactStatus.right().value(); }
142          * if(getServiceArtifactStatus.isLeft()){ status =
143          * updateServiceArtifact(artifactUpdateData,
144          * getServiceArtifactStatus.left().value()); }
145          * 
146          * return status; }
147          * 
148          */
149
150         @Override
151         public Either<ESArtifactData, ResourceUploadStatus> getArtifact(String id) {
152                 if (resourceDAO == null)
153                         return Either.right(ResourceUploadStatus.ERROR);
154
155                 return resourceDAO.getArtifact(id);
156         }
157
158         /*
159          * @Override public Either<ServiceArtifactData, ResourceUploadStatus>
160          * getServiceArtifact(String id) { if(resourceDAO == null) return
161          * Either.right(ResourceUploadStatus.ERROR);
162          * 
163          * return resourceDAO.getServiceArtifact(id); }
164          */
165         @Override
166         public void deleteArtifact(String id) {
167                 if (resourceDAO != null) {
168                         resourceDAO.deleteArtifact(id);
169                 }
170
171         }
172
173         private ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData, ESArtifactData existData) {
174                 ResourceUploadStatus status;
175
176                 updateData(artifactUpdateData, existData);
177
178                 try {
179                         resourceDAO.writeArtifact(artifactUpdateData);
180                         status = ResourceUploadStatus.OK;
181
182                 } catch (ResourceDAOException e) {
183                         status = ResourceUploadStatus.ERROR;
184                         log.debug("ResourceUploadStatus:updateArtifact failed with exception ", e);
185                 }
186                 return status;
187         }
188
189         private void updateData(ESArtifactData artifactUpdateData, ESArtifactData existData) {
190
191                 if (artifactUpdateData.getData() == null) {
192                         artifactUpdateData.setData(existData.getData());
193                 }
194
195         }
196
197         @Override
198         public void deleteAllArtifacts() {
199                 resourceDAO.deleteAllArtifacts();
200         }
201
202 }