From f5f13c4f6b6fe3b4d98e349dfd7db59339803436 Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Sun, 19 Feb 2017 12:35:04 +0200 Subject: push addional code Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74 Signed-off-by: Michael Lando --- .../openecomp-sdc-model-core/pom.xml | 65 +++++++ .../sdc/model/impl/AbstractServiceModelDao.java | 191 +++++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/pom.xml create mode 100644 openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/src/main/java/org/openecomp/sdc/model/impl/AbstractServiceModelDao.java (limited to 'openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core') diff --git a/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/pom.xml b/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/pom.xml new file mode 100644 index 0000000000..ba4d404ae7 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/pom.xml @@ -0,0 +1,65 @@ + + 4.0.0 + + + org.openecomp.sdc + openecomp-sdc-lib + 1.0.0-SNAPSHOT + ../.. + + + openecomp-sdc-model-core + openecomp-sdc-model-core + + + + org.slf4j + slf4j-api + 1.7.10 + + + + ch.qos.logback + logback-classic + 1.1.2 + + + + + + org.openecomp.core + openecomp-utilities-lib + ${project.version} + + + org.openecomp.sdc + openecomp-sdc-model-api + ${project.version} + + + org.openecomp.sdc + openecomp-sdc-versioning-core + ${project.version} + + + + org.openecomp.sdc + openecomp-sdc-datatypes-lib + ${project.version} + + + org.mockito + mockito-all + test + 1.10.19 + + + com.google.guava + guava + 19.0 + + + + + \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/src/main/java/org/openecomp/sdc/model/impl/AbstractServiceModelDao.java b/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/src/main/java/org/openecomp/sdc/model/impl/AbstractServiceModelDao.java new file mode 100644 index 0000000000..eb993f6fb1 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-model-lib/openecomp-sdc-model-core/src/main/java/org/openecomp/sdc/model/impl/AbstractServiceModelDao.java @@ -0,0 +1,191 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.model.impl; + +import org.openecomp.core.model.dao.ServiceArtifactDaoInter; +import org.openecomp.core.model.dao.ServiceTemplateDaoInter; +import org.openecomp.core.model.types.ServiceArtifact; +import org.openecomp.core.model.types.ServiceElement; +import org.openecomp.core.model.types.ServiceTemplate; +import org.openecomp.core.utilities.file.FileContentHandler; +import org.openecomp.core.utilities.file.FileUtils; +import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel; +import org.openecomp.sdc.tosca.services.yamlutil.ToscaExtensionYamlUtil; +import org.openecomp.sdc.versioning.dao.VersionableDao; +import org.openecomp.sdc.versioning.dao.types.Version; + +import java.io.InputStream; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class AbstractServiceModelDao implements VersionableDao { + + protected ServiceTemplateDaoInter templateDao; + protected ServiceArtifactDaoInter artifactDao; + + @Override + public void registerVersioning(String versionableEntityType) { + templateDao.registerVersioning(versionableEntityType); + artifactDao.registerVersioning(versionableEntityType); + } + + + /** + * Gets service model. + * + * @param vspId the vsp id + * @param version the version + * @return the service model + */ + public ToscaServiceModel getServiceModel(String vspId, Version version) { + if (vspId == null || version == null) { + //throw new CoreException() + throw new RuntimeException("missing service model key"); + } + + + FileContentHandler artifactFiles = getArtifacts(vspId, version); + Map serviceTemplates = + getTemplates(vspId, version); + String entryDefinitionServiceTemplate = getServiceBase(vspId, version); + return new ToscaServiceModel(artifactFiles, serviceTemplates, entryDefinitionServiceTemplate); + } + + + public void storeExternalArtifact(ServiceArtifact serviceArtifact) { + artifactDao.create(serviceArtifact); + //TODO: update last modification time + } + + + /** + * Store service model. + * + * @param vspId the vsp id + * @param version the version + * @param toscaServiceModel the tosca service model + */ + public void storeServiceModel(String vspId, Version version, + ToscaServiceModel toscaServiceModel) { + ServiceArtifact entityArt; + + for (String fileName : toscaServiceModel.getArtifactFiles().getFileList()) { + entityArt = new ServiceArtifact(); + entityArt.setContentData( + FileUtils.toByteArray(toscaServiceModel.getArtifactFiles().getFileContent(fileName))); + entityArt.setVspId(vspId); + entityArt.setVersion(version); + entityArt.setName(fileName); + + artifactDao.create(entityArt); + } + + ServiceTemplate entityTmp; + String yaml; + for (Map.Entry + entryTemplate : toscaServiceModel + .getServiceTemplates().entrySet()) { + entityTmp = new ServiceTemplate(); + + yaml = new ToscaExtensionYamlUtil().objectToYaml(entryTemplate.getValue()); + entityTmp.setContentData(yaml.getBytes()); + entityTmp.setVspId(vspId); + entityTmp.setVersion(version); + entityTmp.setName(entryTemplate.getKey()); + entityTmp.setBaseName(toscaServiceModel.getEntryDefinitionServiceTemplate()); + + templateDao.create(entityTmp); + } + + //TODO: update last modification time + } + + + /** + * Gets service model info. + * + * @param vspId the vsp id + * @param version the version + * @param name the name + * @return the service model info + */ + public ServiceElement getServiceModelInfo(String vspId, Version version, String name) { + ServiceElement element = templateDao.getTemplateInfo(vspId, version, name); + if (element != null) { + return element; + } + + element = artifactDao.getArtifactInfo(vspId, version, name); + if (element != null) { + return element; + } + return null; + } + + + /** + * Gets service model content names. + * + * @return the service model content names + */ + public List getServiceModelContentNames() { + + + return null; + } + + + private String getServiceBase(String vspId, Version version) { + return templateDao.getBase(vspId, version); + } + + private Map getTemplates( + String vspId, Version version) { + + Collection templates = templateDao.list(vspId, version); + if (templates == null) { + return null; + } + return templates.stream().collect(Collectors.toMap(template -> template.getName(), + template -> getServiceTemplate(template.getContent()))); + } + + private org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate getServiceTemplate( + InputStream content) { + return new ToscaExtensionYamlUtil() + .yamlToObject(content, org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate.class); + } + + private FileContentHandler getArtifacts(String vspId, Version version) { + Collection templates = artifactDao.list(vspId, version); + if (templates == null) { + return null; + } + + FileContentHandler fileContentHandler = new FileContentHandler(); + templates.stream().forEach(serviceArtifact -> fileContentHandler + .addFile(serviceArtifact.getName(), serviceArtifact.getContent())); + + return fileContentHandler; + } +} -- cgit 1.2.3-korg