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 fj.data.Either;
24 import org.openecomp.sdc.be.config.BeEcompErrorManager;
25 import org.openecomp.sdc.be.config.ConfigurationManager;
26 import org.openecomp.sdc.be.dao.api.ICatalogDAO;
27 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
28 import org.openecomp.sdc.be.resources.api.IResourceUploader;
29 import org.openecomp.sdc.be.resources.data.ESArtifactData;
30 import org.openecomp.sdc.be.resources.exception.ResourceDAOException;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.springframework.stereotype.Component;
34 import javax.annotation.PostConstruct;
35 import javax.annotation.Resource;
37 @Component("resource-upload")
38 public class ResourceUploader implements IResourceUploader {
40 private static final String DEFAULT_ARTIFACT_INDEX_NAME = "resources";
43 private ICatalogDAO resourceDAO;
44 private static Logger log = Logger.getLogger(ResourceUploader.class.getName());
48 ConfigurationManager configMgr = ConfigurationManager.getConfigurationManager();
49 String artifactsIndex = null;
50 artifactsIndex = configMgr.getConfiguration().getArtifactsIndex();
51 if (artifactsIndex == null || artifactsIndex.isEmpty()) {
52 artifactsIndex = DEFAULT_ARTIFACT_INDEX_NAME;
54 resourceDAO.addToIndicesMap(ESArtifactData.class.getSimpleName().toLowerCase(), artifactsIndex);
57 public ResourceUploader() {
61 public ResourceUploader(ICatalogDAO resourcetDAO) {
63 this.resourceDAO = resourcetDAO;
66 public ICatalogDAO getResourceDAO() {
70 public void setResourceDAO(ICatalogDAO resourceDAO) {
71 this.resourceDAO = resourceDAO;
75 public ResourceUploadStatus saveArtifact(ESArtifactData artifactData, boolean isReload) {
76 ResourceUploadStatus status = ResourceUploadStatus.OK;
77 if (resourceDAO == null) {
78 BeEcompErrorManager.getInstance()
79 .logBeInitializationError("Save Artifact - internal object not initialized");
80 log.debug("update artifact failed - resourceDAO is null");
81 return ResourceUploadStatus.ERROR;
84 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = getArtifact(artifactData.getId());
85 if (getArtifactStatus.isLeft()) {
86 status = ResourceUploadStatus.ALREADY_EXIST;
87 log.debug("ResourceUploadStatus:saveArtifact artifact with id {} already exist.", artifactData.getId());
89 status = updateArtifact(artifactData, getArtifactStatus.left().value());
94 resourceDAO.writeArtifact(artifactData);
95 status = ResourceUploadStatus.OK;
97 } catch (ResourceDAOException e) {
98 status = ResourceUploadStatus.ERROR;
99 BeEcompErrorManager.getInstance().logBeDaoSystemError("Save Artifact to database");
100 log.debug("ResourceUploadStatus:saveArtifact failed with exception ", e);
109 public ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData) {
110 ResourceUploadStatus status = ResourceUploadStatus.OK;
111 if (resourceDAO == null)
112 return ResourceUploadStatus.ERROR;
114 Either<ESArtifactData, ResourceUploadStatus> getArtifactStatus = getArtifact(artifactUpdateData.getId());
115 if (getArtifactStatus.isRight()) {
116 status = getArtifactStatus.right().value();
117 log.debug("ResourceUploadStatus:updateArtifactt artifact with id {}", artifactUpdateData.getId()
120 if (getArtifactStatus.isLeft()) {
121 status = updateArtifact(artifactUpdateData, getArtifactStatus.left().value());
128 public Either<ESArtifactData, ResourceUploadStatus> getArtifact(String id) {
129 if (resourceDAO == null)
130 return Either.right(ResourceUploadStatus.ERROR);
132 return resourceDAO.getArtifact(id);
136 public void deleteArtifact(String id) {
137 if (resourceDAO != null) {
138 resourceDAO.deleteArtifact(id);
143 private ResourceUploadStatus updateArtifact(ESArtifactData artifactUpdateData, ESArtifactData existData) {
144 ResourceUploadStatus status;
146 updateData(artifactUpdateData, existData);
149 resourceDAO.writeArtifact(artifactUpdateData);
150 status = ResourceUploadStatus.OK;
152 } catch (ResourceDAOException e) {
153 status = ResourceUploadStatus.ERROR;
154 log.debug("ResourceUploadStatus:updateArtifact failed with exception ", e);
159 private void updateData(ESArtifactData artifactUpdateData, ESArtifactData existData) {
161 if (artifactUpdateData.getData() == null) {
162 artifactUpdateData.setData(existData.getData());
168 public void deleteAllArtifacts() {
169 resourceDAO.deleteAllArtifacts();