2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.resources.impl;
23 import javax.annotation.PostConstruct;
24 import javax.annotation.Resource;
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;
38 import fj.data.Either;
40 @Component("resource-upload")
41 public class ResourceUploader implements IResourceUploader {
43 private static final String DEFAULT_ARTIFACT_INDEX_NAME = "resources";
46 private ICatalogDAO resourceDAO;
47 private static Logger log = LoggerFactory.getLogger(ResourceUploader.class.getName());
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;
57 resourceDAO.addToIndicesMap(ESArtifactData.class.getSimpleName().toLowerCase(), artifactsIndex);
60 public ResourceUploader() {
64 public ResourceUploader(ICatalogDAO resourcetDAO) {
66 this.resourceDAO = resourcetDAO;
69 public ICatalogDAO getResourceDAO() {
73 public void setResourceDAO(ICatalogDAO resourceDAO) {
74 this.resourceDAO = resourceDAO;
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;
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());
92 status = updateArtifact(artifactData, getArtifactStatus.left().value());
97 resourceDAO.writeArtifact(artifactData);
98 status = ResourceUploadStatus.OK;
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);
112 public ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData) {
113 ResourceUploadStatus status = ResourceUploadStatus.OK;
114 if (resourceDAO == null)
115 return ResourceUploadStatus.ERROR;
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()
123 if (getArtifactStatus.isLeft()) {
124 status = updateArtifact(artifactUpdateData, getArtifactStatus.left().value());
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());
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()); }
151 public Either<ESArtifactData, ResourceUploadStatus> getArtifact(String id) {
152 if (resourceDAO == null)
153 return Either.right(ResourceUploadStatus.ERROR);
155 return resourceDAO.getArtifact(id);
159 * @Override public Either<ServiceArtifactData, ResourceUploadStatus>
160 * getServiceArtifact(String id) { if(resourceDAO == null) return
161 * Either.right(ResourceUploadStatus.ERROR);
163 * return resourceDAO.getServiceArtifact(id); }
166 public void deleteArtifact(String id) {
167 if (resourceDAO != null) {
168 resourceDAO.deleteArtifact(id);
173 private ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData, ESArtifactData existData) {
174 ResourceUploadStatus status;
176 updateData(artifactUpdateData, existData);
179 resourceDAO.writeArtifact(artifactUpdateData);
180 status = ResourceUploadStatus.OK;
182 } catch (ResourceDAOException e) {
183 status = ResourceUploadStatus.ERROR;
184 log.debug("ResourceUploadStatus:updateArtifact failed with exception ", e);
189 private void updateData(ESArtifactData artifactUpdateData, ESArtifactData existData) {
191 if (artifactUpdateData.getData() == null) {
192 artifactUpdateData.setData(existData.getData());
198 public void deleteAllArtifacts() {
199 resourceDAO.deleteAllArtifacts();