diff options
author | 2017-12-20 14:30:43 +0200 | |
---|---|---|
committer | 2017-12-21 11:12:33 +0000 | |
commit | 8e9c0653dd6c6862123c9609ae34e1206d86456e (patch) | |
tree | 5eeef00ec0677133baa439ca8d7ffd7aca4804b6 /openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main | |
parent | 785ebcc95de3e064e843bec04ba7a209d854fc7c (diff) |
Add collaboration feature
Issue-ID: SDC-767
Change-Id: I14fb4c1f54086ed03a56a7ff7fab9ecd40381795
Signed-off-by: talig <talig@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main')
17 files changed, 990 insertions, 40 deletions
diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculator.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculator.java new file mode 100644 index 0000000000..dc9ac7b325 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculator.java @@ -0,0 +1,12 @@ +package org.openecomp.sdc.versioning; + +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; + +import java.util.Set; + +public interface VersionCalculator { + String calculate(String baseVersion, VersionCreationMethod creationMethod); + + void injectAdditionalInfo(Version version, Set<String> existingVersions); +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculatorFactory.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculatorFactory.java new file mode 100644 index 0000000000..85f02cd267 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/VersionCalculatorFactory.java @@ -0,0 +1,31 @@ +/*- + * ============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.versioning; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; + +public abstract class VersionCalculatorFactory extends AbstractComponentFactory<VersionCalculator> { + + public static VersionCalculatorFactory getInstance() { + return AbstractFactory.getInstance(VersionCalculatorFactory.class); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/ItemDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/ItemDaoFactoryImpl.java new file mode 100644 index 0000000000..0f9c9065d4 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/ItemDaoFactoryImpl.java @@ -0,0 +1,36 @@ +/*- + * ============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.versioning.dao.impl; + +import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory; +import org.openecomp.sdc.versioning.dao.ItemDao; +import org.openecomp.sdc.versioning.dao.ItemDaoFactory; +import org.openecomp.sdc.versioning.dao.impl.zusammen.ItemZusammenDaoImpl; + +public class ItemDaoFactoryImpl extends ItemDaoFactory { + private static ItemDao INSTANCE = + new ItemZusammenDaoImpl(ZusammenAdaptorFactory.getInstance().createInterface()); + + @Override + public ItemDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionDaoFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionDaoFactoryImpl.java new file mode 100644 index 0000000000..f8b0d71eb7 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionDaoFactoryImpl.java @@ -0,0 +1,36 @@ +/*- + * ============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.versioning.dao.impl; + +import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory; +import org.openecomp.sdc.versioning.dao.VersionDao; +import org.openecomp.sdc.versioning.dao.VersionDaoFactory; +import org.openecomp.sdc.versioning.dao.impl.zusammen.VersionZusammenDaoImpl; + +public class VersionDaoFactoryImpl extends VersionDaoFactory { + private static VersionDao INSTANCE = + new VersionZusammenDaoImpl(ZusammenAdaptorFactory.getInstance().createInterface()); + + @Override + public VersionDao createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoCassandraImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoCassandraImpl.java index 5673da7070..0845beaa2f 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoCassandraImpl.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/VersionableEntityDaoCassandraImpl.java @@ -24,12 +24,12 @@ import com.datastax.driver.core.ColumnDefinitions; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.mapping.UDTMapper; -import org.openecomp.sdc.logging.api.Logger; -import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.core.nosqldb.api.NoSqlDb; import org.openecomp.core.nosqldb.factory.NoSqlDbFactory; import org.openecomp.core.util.UniqueValueUtil; import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.versioning.dao.VersionableEntityDao; import org.openecomp.sdc.versioning.dao.types.Version; import org.openecomp.sdc.versioning.types.UniqueValueMetadata; diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java new file mode 100644 index 0000000000..8fdcf71d59 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/ItemZusammenDaoImpl.java @@ -0,0 +1,104 @@ +package org.openecomp.sdc.versioning.dao.impl.zusammen; + +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.item.Info; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.core.zusammen.api.ZusammenUtil; +import org.openecomp.sdc.versioning.dao.ItemDao; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.Item; + +import java.util.Collection; +import java.util.Map; +import java.util.stream.Collectors; + +public class ItemZusammenDaoImpl implements ItemDao { + + private ZusammenAdaptor zusammenAdaptor; + + public ItemZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) { + this.zusammenAdaptor = zusammenAdaptor; + } + + @Override + public Collection<Item> list() { + return zusammenAdaptor.listItems(ZusammenUtil.createSessionContext()) + .stream().map(this::mapFromZusammenItem) + .collect(Collectors.toList()); + } + + @Override + public Item get(Item item) { + return mapFromZusammenItem( + zusammenAdaptor.getItem(ZusammenUtil.createSessionContext(), new Id(item.getId()))); + } + + @Override + public Item create(Item item) { + Id itemId = zusammenAdaptor + .createItem(ZusammenUtil.createSessionContext(), mapToZusammenItemInfo(item)); + item.setId(itemId.getValue()); + return item; + } + + @Override + public void update(Item item) { + zusammenAdaptor + .updateItem(ZusammenUtil.createSessionContext(), new Id(item.getId()), + mapToZusammenItemInfo(item)); + } + + private Item mapFromZusammenItem(com.amdocs.zusammen.datatypes.item.Item zusammenItem) { + if (zusammenItem == null) { + return null; + } + Item item = new Item(); + item.setId(zusammenItem.getId().getValue()); + item.setName(zusammenItem.getInfo().getName()); + item.setDescription(zusammenItem.getInfo().getDescription()); + + zusammenItem.getInfo().getProperties().entrySet() + .forEach(property -> addPropertyToItem(property.getKey(), property.getValue(), item)); + + item.setCreationTime(zusammenItem.getCreationTime()); + item.setModificationTime(zusammenItem.getModificationTime()); + return item; + } + + private void addPropertyToItem(String propertyKey, Object propertyValue, Item item) { + switch (propertyKey) { + case InfoPropertyName.ITEM_TYPE: + item.setType((String) propertyValue); + break; + case InfoPropertyName.ITEM_VERSIONS_STATUSES: + for (Map.Entry<String, Number> statusCounter : + ((Map<String, Number>) propertyValue).entrySet()) { + item.getVersionStatusCounters().put(VersionStatus.valueOf(statusCounter.getKey()), + statusCounter.getValue().intValue()); + } + break; + default: + item.addProperty(propertyKey, propertyValue); + } + } + + private Info mapToZusammenItemInfo(Item item) { + Info info = new Info(); + info.setName(item.getName()); + info.setDescription(item.getDescription()); + info.addProperty(InfoPropertyName.ITEM_TYPE, item.getType()); + info.addProperty(InfoPropertyName.ITEM_VERSIONS_STATUSES, item.getVersionStatusCounters()); + item.getProperties().entrySet() + .forEach(property -> info.addProperty(property.getKey(), property.getValue())); + return info; + } + + private static final class InfoPropertyName { + private static final String ITEM_TYPE = "item_type"; + private static final String ITEM_VERSIONS_STATUSES = "item_versions_statuses"; + + private InfoPropertyName() { + throw new IllegalStateException("Constants class"); + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/VersionZusammenDaoImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/VersionZusammenDaoImpl.java new file mode 100644 index 0000000000..195973bf5c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/VersionZusammenDaoImpl.java @@ -0,0 +1,165 @@ +package org.openecomp.sdc.versioning.dao.impl.zusammen; + +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.item.ItemVersionStatus; +import com.amdocs.zusammen.datatypes.item.SynchronizationStatus; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; +import org.openecomp.sdc.versioning.dao.VersionDao; +import org.openecomp.sdc.versioning.dao.impl.zusammen.convertor.ItemVersionToVersionConvertor; +import org.openecomp.sdc.versioning.dao.types.Revision; +import org.openecomp.sdc.versioning.dao.types.SynchronizationState; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionState; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +import static org.openecomp.core.zusammen.api.ZusammenUtil.createSessionContext; + +public class VersionZusammenDaoImpl implements VersionDao { + + public static final class ZusammenProperty { + public static final String LABEL = "label"; + public static final String STATUS = "status"; + + private ZusammenProperty() { + throw new IllegalStateException("Constants class"); + } + } + + private ZusammenAdaptor zusammenAdaptor; + + public VersionZusammenDaoImpl(ZusammenAdaptor zusammenAdaptor) { + this.zusammenAdaptor = zusammenAdaptor; + } + + @Override + public List<Version> list(String itemId) { + ItemVersionToVersionConvertor convertor = new ItemVersionToVersionConvertor(); + return zusammenAdaptor.listPublicVersions(createSessionContext(), new Id(itemId)).stream() + .map(convertor::convert) + .collect(Collectors.toList()); + } + + @Override + public void create(String itemId, Version version) { + Id versionId = + zusammenAdaptor.createVersion(createSessionContext(), new Id(itemId), + version.getBaseId() == null ? null : new Id(version.getBaseId()), + mapToZusammenVersion(version)); + + version.setId(versionId.getValue()); + } + + @Override + public void update(String itemId, Version version) { + zusammenAdaptor.updateVersion(createSessionContext(), new Id(itemId), new Id(version.getId()), + mapToZusammenVersion(version)); + } + + @Override + public Optional<Version> get(String itemId, Version version) { + SessionContext context = createSessionContext(); + Id itemIdObj = new Id(itemId); + Id versionId = new Id(version.getId()); + ItemVersion itemVersion = zusammenAdaptor.getVersion(context, itemIdObj, versionId); + + if (itemVersion == null) { + return Optional.empty(); + } + + VersionState versionState = + convertState(zusammenAdaptor.getVersionStatus(context, itemIdObj, versionId)); + updateVersionStatus(context, itemIdObj, versionId, versionState, itemVersion); + + Version result = new ItemVersionToVersionConvertor().convert(itemVersion); + result.setState(versionState); + return Optional.of(result); + } + + @Override + public void delete(String itemId, Version version) { + throw new UnsupportedOperationException("Delete version operation is not yet supported."); + } + + @Override + public void publish(String itemId, Version version, String message) { + zusammenAdaptor + .publishVersion(createSessionContext(), new Id(itemId), new Id(version.getId()), message); + } + + @Override + public void sync(String itemId, Version version) { + zusammenAdaptor + .syncVersion(createSessionContext(), new Id(itemId), new Id(version.getId())); + } + + @Override + public void forceSync(String itemId, Version version) { + zusammenAdaptor + .forceSyncVersion(createSessionContext(), new Id(itemId), new Id(version.getId())); + } + + @Override + public void revert(String itemId, Version version, String revisionId) { + zusammenAdaptor.revert(createSessionContext(), itemId, version.getId(), revisionId); + } + + @Override + public List<Revision> listRevisions(String itemId, Version version) { + return zusammenAdaptor.listRevisions(createSessionContext(), itemId, version.getId()); + } + + private void updateVersionStatus(SessionContext context, Id itemId, Id versionId, + VersionState versionState, ItemVersion itemVersion) { + if (versionState.getSynchronizationState() != SynchronizationState.UpToDate || + versionState.isDirty()) { + String versionStatus = zusammenAdaptor.getPublicVersion(context, itemId, versionId) + .getData().getInfo().getProperty(ZusammenProperty.STATUS); + itemVersion.getData().getInfo().addProperty(ZusammenProperty.STATUS, versionStatus); + } + } + + private ItemVersionData mapToZusammenVersion(Version version) { + Info info = new Info(); + info.addProperty(ZusammenProperty.LABEL, version.toString()); + info.addProperty(ZusammenProperty.STATUS, version.getStatus().name()); + info.setName(version.getName()); + info.setDescription(version.getDescription()); + + ItemVersionData itemVersionData = new ItemVersionData(); + itemVersionData.setInfo(info); + return itemVersionData; + } + + private VersionState convertState(ItemVersionStatus versionStatus) { + VersionState state = new VersionState(); + state.setSynchronizationState(getSyncState(versionStatus.getSynchronizationStatus())); + state.setDirty(versionStatus.isDirty()); + return state; + } + + private SynchronizationState getSyncState(SynchronizationStatus synchronizationStatus) { + switch (synchronizationStatus) { + case UP_TO_DATE: + return SynchronizationState.UpToDate; + case OUT_OF_SYNC: + return SynchronizationState.OutOfSync; + case MERGING: + return SynchronizationState.Merging; + default: + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withCategory(ErrorCategory.APPLICATION) + .withId("UNKNOWN_VERSION_STATE") + .withMessage("Version state is unknown").build()); + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/convertor/ItemVersionToVersionConvertor.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/convertor/ItemVersionToVersionConvertor.java new file mode 100644 index 0000000000..f47b5aa612 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/dao/impl/zusammen/convertor/ItemVersionToVersionConvertor.java @@ -0,0 +1,49 @@ +package org.openecomp.sdc.versioning.dao.impl.zusammen.convertor; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import org.openecomp.convertor.ElementConvertor; +import org.openecomp.sdc.versioning.dao.impl.zusammen.VersionZusammenDaoImpl; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; + +public class ItemVersionToVersionConvertor extends ElementConvertor { + @Override + public Object convert(Element element) { + return null; + } + + @Override + public Object convert(Item item) { + return null; + } + + @Override + public Object convert(ElementInfo elementInfo) { + return null; + } + + @Override + public Version convert(ItemVersion itemVersion) { + if (itemVersion == null) { + return null; + } + Version version = Version.valueOf( + itemVersion.getData().getInfo().getProperty(VersionZusammenDaoImpl.ZusammenProperty.LABEL)); + version.setStatus(VersionStatus.valueOf(itemVersion.getData().getInfo() + .getProperty(VersionZusammenDaoImpl.ZusammenProperty.STATUS))); + version.setName(itemVersion.getData().getInfo().getName()); + version.setDescription(itemVersion.getData().getInfo().getDescription()); + + version.setId(itemVersion.getId().getValue()); + if (itemVersion.getBaseId() != null) { + version.setBaseId(itemVersion.getBaseId().getValue()); + } + version.setCreationTime(itemVersion.getCreationTime()); + version.setModificationTime(itemVersion.getModificationTime()); + return version; + } + +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerFactoryImpl.java new file mode 100644 index 0000000000..ba5bd07d42 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerFactoryImpl.java @@ -0,0 +1,39 @@ +/*- + * ============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.versioning.impl; + +import org.openecomp.sdc.itempermissions.PermissionsServicesFactory; +import org.openecomp.sdc.notification.factories.SubscriptionServiceFactory; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.ItemManagerFactory; +import org.openecomp.sdc.versioning.dao.ItemDaoFactory; + +public class ItemManagerFactoryImpl extends ItemManagerFactory { + private static final ItemManager INSTANCE = + new ItemManagerImpl(ItemDaoFactory.getInstance().createInterface(), + PermissionsServicesFactory.getInstance().createInterface(), + SubscriptionServiceFactory.getInstance().createInterface()); + + @Override + public ItemManager createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerImpl.java new file mode 100644 index 0000000000..79284542fb --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/ItemManagerImpl.java @@ -0,0 +1,68 @@ +package org.openecomp.sdc.versioning.impl; + +import org.openecomp.sdc.common.session.SessionContextProviderFactory; +import org.openecomp.sdc.itempermissions.PermissionsServices; +import org.openecomp.sdc.notification.services.SubscriptionService; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.dao.ItemDao; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.Item; + +import java.util.Collection; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class ItemManagerImpl implements ItemManager { + public static final String CREATE_ITEM = "Create_Item"; + + private ItemDao itemDao; + private PermissionsServices permissionsServices; + private SubscriptionService subscriptionService; + + public ItemManagerImpl(ItemDao itemDao, PermissionsServices permissionsServices, + SubscriptionService subscriptionService) { + this.itemDao = itemDao; + this.permissionsServices = permissionsServices; + this.subscriptionService = subscriptionService; + } + + @Override + public Collection<Item> list(Predicate<Item> predicate) { + return itemDao.list().stream().filter(predicate).collect(Collectors.toList()); + } + + @Override + public Item get(String itemId) { + Item item = new Item(); + item.setId(itemId); + return itemDao.get(item); + } + + @Override + public Item create(Item item) { + Item createdItem = itemDao.create(item); + + String userId = SessionContextProviderFactory.getInstance() + .createInterface().get().getUser().getUserId(); + String itemId = createdItem.getId(); + permissionsServices.execute(itemId, userId, CREATE_ITEM); + subscriptionService.subscribe(userId, itemId); + + return createdItem; + } + + @Override + public void updateVersionStatus(String itemId, VersionStatus addedVersionStatus, + VersionStatus removedVersionStatus) { + Item item = get(itemId); + if (item == null) { + return; + } + + item.addVersionStatus(addedVersionStatus); + if (removedVersionStatus != null) { + item.removeVersionStatus(removedVersionStatus); + } + itemDao.update(item); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorFactoryImpl.java new file mode 100644 index 0000000000..2300f62180 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorFactoryImpl.java @@ -0,0 +1,14 @@ +package org.openecomp.sdc.versioning.impl; + +import org.openecomp.sdc.versioning.VersionCalculator; +import org.openecomp.sdc.versioning.VersionCalculatorFactory; + +public class MajorVersionCalculatorFactoryImpl extends VersionCalculatorFactory { + private static final VersionCalculator INSTANCE = + new MajorVersionCalculatorImpl(); + + @Override + public VersionCalculator createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorImpl.java new file mode 100644 index 0000000000..91c59e0efd --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/MajorVersionCalculatorImpl.java @@ -0,0 +1,53 @@ +package org.openecomp.sdc.versioning.impl; + +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.versioning.VersionCalculator; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; + +import java.util.HashSet; +import java.util.Set; + +public class MajorVersionCalculatorImpl implements VersionCalculator { + private static final String INITIAL_VERSION = "1.0"; + private static final String VERSION_STRING_VIOLATION_MSG = + "Version string must be in the format of: {integer}.{integer}"; + + @Override + public String calculate(String baseVersion, VersionCreationMethod creationMethod) { + + if (baseVersion == null) { + return INITIAL_VERSION; + } + + String[] versionLevels = baseVersion.split("\\."); + if (versionLevels.length != 2) { + throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG); + } + + int index = Integer.parseInt(versionLevels[0]); + index++; + versionLevels[0] = Integer.toString(index); + versionLevels[1] = "0"; + + return CommonMethods.arrayToSeparatedString(versionLevels, '.'); + } + + @Override + public void injectAdditionalInfo(Version version, Set<String> existingVersions) { + String optionalVersion; + Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>(); + if(version.getStatus().equals(VersionStatus.Certified)) { + try { + optionalVersion = calculate(version.getName(), VersionCreationMethod.major); + if (!existingVersions.contains(optionalVersion)) { + optionalCreationMethods.add(VersionCreationMethod.major); + } + } catch (IllegalArgumentException iae) { + //not a valid creation method. + } + } + version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorFactoryImpl.java new file mode 100644 index 0000000000..155aa9c5e6 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorFactoryImpl.java @@ -0,0 +1,34 @@ +/*- + * ============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.versioning.impl; + +import org.openecomp.sdc.versioning.VersionCalculator; +import org.openecomp.sdc.versioning.VersionCalculatorFactory; + +public class VersionCalculatorFactoryImpl extends VersionCalculatorFactory { + private static final VersionCalculator INSTANCE = + new VersionCalculatorImpl(); + + @Override + public VersionCalculator createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorImpl.java new file mode 100644 index 0000000000..b7008de867 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersionCalculatorImpl.java @@ -0,0 +1,155 @@ +package org.openecomp.sdc.versioning.impl; + +import org.openecomp.core.utilities.CommonMethods; +import org.openecomp.sdc.versioning.VersionCalculator; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; + +import java.util.HashSet; +import java.util.Set; + +public class VersionCalculatorImpl implements VersionCalculator { + + private static final String INITIAL_VERSION = "1.0"; + private static final String VERSION_STRING_VIOLATION_MSG = + "Version string must be in the format of: {integer}.{integer}"; + private static final String PARENT_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_TOP_LEVEL = + "Creation of parent level version on top level version is invalid."; + private static final String SUB_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_LOWEST_LEVEL = + "Creation of parent level version on top level version is invalid."; + + private static final String VERSION_CALCULATION_ERROR_MSG = + "Version calculation error."; + + private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method-"; + + + @Override + public String calculate(String baseVersion, VersionCreationMethod creationMethod) { + + if (baseVersion == null) { + return INITIAL_VERSION; + } + + String[] versionLevels = baseVersion.split("\\."); + if (versionLevels.length != 2) { + throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG); + } + + int index; + switch (creationMethod) { + case major: + index = Integer.parseInt(versionLevels[0]); + index++; + versionLevels[0] = Integer.toString(index); + versionLevels[1] = "0"; + break; + case minor: + index = Integer.parseInt(versionLevels[1]); + index++; + versionLevels[1] = Integer.toString(index); + break; + } + return CommonMethods.arrayToSeparatedString(versionLevels, '.'); + } + + + // version calculator when there are no version restrictions + /* @Override + public String calculate(String baseVersion, VersionCreationMethod creationMethod) { + + return calculate(baseVersion,creationMethod,2); + } + + private String calculate(String baseVersion, VersionCreationMethod creationMethod,int + maxVersionLevels) { + if (baseVersion == null) { + return INITIAL_VERSION; + } + + String[] versionLevels = baseVersion.split("\\."); + if (versionLevels.length > maxVersionLevels) { + throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG); + } + + int index; + int versionLevel = calcVersionLevel(versionLevels); + if (versionLevel == -1) { + throw new IllegalArgumentException( + VERSION_STRING_VIOLATION_MSG + " given version:" + baseVersion); + } + int requiredVersionLevelIncrease; + switch (creationMethod) { + case parent_level: + if (versionLevel == 0) { + throw new IllegalArgumentException( + PARENT_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_TOP_LEVEL + " version:" + baseVersion); + + } + requiredVersionLevelIncrease = versionLevel - 1; + versionLevels[versionLevel] = "0"; + index = Integer.getInteger(versionLevels[requiredVersionLevelIncrease]); + index++; + versionLevels[requiredVersionLevelIncrease] = Integer.toString(index); + break; + case same_level: + requiredVersionLevelIncrease = versionLevel; + index = Integer.valueOf(versionLevels[requiredVersionLevelIncrease]); + index++; + versionLevels[requiredVersionLevelIncrease] = Integer.toString(index); + break; + case sub_level: + if (versionLevel == versionLevels.length - 1) { + throw new IllegalArgumentException( + SUB_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_LOWEST_LEVEL + " version:" + baseVersion); + } + requiredVersionLevelIncrease = versionLevel + 1; + if(requiredVersionLevelIncrease>maxVersionLevels){ + throw new IllegalArgumentException(INVALID_CREATION_METHOD_MSG+" max " + + "levels:"+maxVersionLevels + "requested level:"+requiredVersionLevelIncrease); + } + String newVersion = baseVersion + ".1"; + versionLevels = newVersion.split("\\."); + + break; + default: + throw new IllegalArgumentException(VERSION_CALCULATION_ERROR_MSG + " base " + + "version:" + baseVersion + " creation method:" + creationMethod); + + } + + return CommonMethods.arrayToSeparatedString(versionLevels, '.'); + }*/ + + + @Override + public void injectAdditionalInfo(Version version, Set<String> existingVersions) { + String optionalVersion; + Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>(); + if(version.getStatus().equals(VersionStatus.Certified)) { + for (VersionCreationMethod versionCreationMethod : VersionCreationMethod.values()) { + try { + optionalVersion = calculate(version.getName(), versionCreationMethod); + if (!existingVersions.contains(optionalVersion)) { + optionalCreationMethods.add(versionCreationMethod); + } + } catch (IllegalArgumentException iae) { + //not a valid creation method. + } + } + } + version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods); + + } + + + private int calcVersionLevel(String[] versionLevels) { + for (int i = versionLevels.length - 1; i >= 0; i--) { + if (!versionLevels[i].equals("0")) { + return i; + } + } + return -1; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerFactoryImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerFactoryImpl.java index 8ab5c608bf..b4dbcf3a08 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerFactoryImpl.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerFactoryImpl.java @@ -20,16 +20,21 @@ package org.openecomp.sdc.versioning.impl; +import org.openecomp.sdc.versioning.ItemManagerFactory; +import org.openecomp.sdc.versioning.VersionCalculatorFactory; import org.openecomp.sdc.versioning.VersioningManager; import org.openecomp.sdc.versioning.VersioningManagerFactory; +import org.openecomp.sdc.versioning.dao.VersionDaoFactory; import org.openecomp.sdc.versioning.dao.VersionInfoDaoFactory; import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDaoFactory; public class VersioningManagerFactoryImpl extends VersioningManagerFactory { private static final VersioningManager INSTANCE = new VersioningManagerImpl( VersionInfoDaoFactory.getInstance().createInterface(), - VersionInfoDeletedDaoFactory.getInstance().createInterface() - ); + VersionInfoDeletedDaoFactory.getInstance().createInterface(), + VersionDaoFactory.getInstance().createInterface(), + VersionCalculatorFactory.getInstance().createInterface(), + ItemManagerFactory.getInstance().createInterface()); @Override public VersioningManager createInterface() { diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerImpl.java b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerImpl.java index 4a49d5391f..7f983477cf 100644 --- a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerImpl.java +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/java/org/openecomp/sdc/versioning/impl/VersioningManagerImpl.java @@ -21,25 +21,31 @@ package org.openecomp.sdc.versioning.impl; import org.openecomp.sdc.common.errors.CoreException; +import org.openecomp.sdc.common.errors.ErrorCategory; +import org.openecomp.sdc.common.errors.ErrorCode; import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage; import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; import org.openecomp.sdc.logging.types.LoggerConstants; import org.openecomp.sdc.logging.types.LoggerErrorCode; import org.openecomp.sdc.logging.types.LoggerErrorDescription; import org.openecomp.sdc.logging.types.LoggerTragetServiceName; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.VersionCalculator; import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.dao.VersionDao; import org.openecomp.sdc.versioning.dao.VersionInfoDao; import org.openecomp.sdc.versioning.dao.VersionInfoDeletedDao; import org.openecomp.sdc.versioning.dao.VersionableEntityDaoFactory; +import org.openecomp.sdc.versioning.dao.types.Revision; +import org.openecomp.sdc.versioning.dao.types.SynchronizationState; import org.openecomp.sdc.versioning.dao.types.UserCandidateVersion; import org.openecomp.sdc.versioning.dao.types.Version; -import org.openecomp.sdc.versioning.dao.types.VersionHistoryEntity; import org.openecomp.sdc.versioning.dao.types.VersionInfoDeletedEntity; import org.openecomp.sdc.versioning.dao.types.VersionInfoEntity; import org.openecomp.sdc.versioning.dao.types.VersionStatus; -import org.openecomp.sdc.versioning.dao.types.VersionType; -import org.openecomp.sdc.versioning.dao.types.VersionableEntityId; import org.openecomp.sdc.versioning.errors.CheckinOnEntityLockedByOtherErrorBuilder; import org.openecomp.sdc.versioning.errors.CheckinOnUnlockedEntityErrorBuilder; import org.openecomp.sdc.versioning.errors.CheckoutOnLockedEntityErrorBuilder; @@ -52,6 +58,7 @@ import org.openecomp.sdc.versioning.errors.EntityNotExistErrorBuilder; import org.openecomp.sdc.versioning.errors.SubmitLockedEntityNotAllowedErrorBuilder; import org.openecomp.sdc.versioning.errors.UndoCheckoutOnEntityLockedByOtherErrorBuilder; import org.openecomp.sdc.versioning.errors.UndoCheckoutOnUnlockedEntityErrorBuilder; +import org.openecomp.sdc.versioning.types.VersionCreationMethod; import org.openecomp.sdc.versioning.types.VersionInfo; import org.openecomp.sdc.versioning.types.VersionableEntityAction; import org.openecomp.sdc.versioning.types.VersionableEntityMetadata; @@ -66,18 +73,27 @@ import java.util.Set; import java.util.stream.Collectors; public class VersioningManagerImpl implements VersioningManager { - + private static final Logger LOGGER = LoggerFactory.getLogger(VersioningManagerImpl.class); private static final Version INITIAL_ACTIVE_VERSION = new Version(0, 0); private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage(); private static Map<String, Set<VersionableEntityMetadata>> versionableEntities = new HashMap<>(); - private VersionInfoDao versionInfoDao; - private VersionInfoDeletedDao versionInfoDeletedDao; + private final VersionInfoDao versionInfoDao; + private final VersionInfoDeletedDao versionInfoDeletedDao; + private VersionDao versionDao; + private VersionCalculator versionCalculator; + private ItemManager itemManager; public VersioningManagerImpl(VersionInfoDao versionInfoDao, - VersionInfoDeletedDao versionInfoDeletedDao) { + VersionInfoDeletedDao versionInfoDeletedDao, + VersionDao versionDao, + VersionCalculator versionCalculator, + ItemManager itemManager) { this.versionInfoDao = versionInfoDao; this.versionInfoDeletedDao = versionInfoDeletedDao; + this.versionDao = versionDao; + this.versionCalculator = versionCalculator; + this.itemManager = itemManager; } private static VersionInfo getVersionInfo(VersionInfoEntity versionInfoEntity, String user, @@ -146,8 +162,12 @@ public class VersioningManagerImpl implements VersioningManager { VersionInfo versionInfo = new VersionInfo(); versionInfo.setStatus(status); activeVersion.setStatus(status); - if(latestFinalVersion!= null) latestFinalVersion.setStatus(status); - if(viewableVersions != null) viewableVersions.forEach(version->version.setStatus(status)); + if (latestFinalVersion != null) { + latestFinalVersion.setStatus(status); + } + if (viewableVersions != null) { + viewableVersions.forEach(version -> version.setStatus(status)); + } versionInfo.setActiveVersion(activeVersion); versionInfo.setLatestFinalVersion(latestFinalVersion); versionInfo.setViewableVersions(toSortedList(viewableVersions)); @@ -166,11 +186,9 @@ public class VersioningManagerImpl implements VersioningManager { private static List<Version> toSortedList( Set<Version> versions) { // changing the Set to List in DB will require migration... - return versions.stream().sorted((o1, o2) -> { - return o1.getMajor() > o2.getMajor() ? 1 - : o1.getMajor() == o2.getMajor() ? (o1.getMinor() > o2.getMinor() ? 1 - : o1.getMinor() == o2.getMinor() ? 0 : -1) : -1; - }).collect(Collectors.toList()); + return versions.stream().sorted((o1, o2) -> o1.getMajor() > o2.getMajor() ? 1 + : o1.getMajor() == o2.getMajor() ? (o1.getMinor() > o2.getMinor() ? 1 + : o1.getMinor() == o2.getMinor() ? 0 : -1) : -1).collect(Collectors.toList()); } private static List<Version> getFinalVersions(Set<Version> versions) { @@ -263,8 +281,8 @@ public class VersioningManagerImpl implements VersioningManager { LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't checkout versionable entity"); throw new CoreException(new CheckoutOnLockedEntityErrorBuilder(entityType, entityId, versionInfoEntity.getCandidate().getUser()).build()); - case Final: - case Available: + case Certified: + case Draft: checkoutVersion = doCheckout(versionInfoEntity, user); break; default: @@ -300,8 +318,8 @@ public class VersioningManagerImpl implements VersioningManager { } activeVersion = undoCheckout(versionInfoEntity); break; - case Final: - case Available: + case Certified: + case Draft: MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.UNDO_CHECKOUT_ENTITY, ErrorLevel.ERROR.name(), LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), @@ -320,8 +338,9 @@ public class VersioningManagerImpl implements VersioningManager { deleteVersionFromEntity(versionInfoEntity.getEntityType(), versionInfoEntity.getEntityId(), versionInfoEntity.getCandidate().getVersion(), versionInfoEntity.getActiveVersion()); - versionInfoEntity.setStatus(versionInfoEntity.getActiveVersion().isFinal() ? VersionStatus.Final - : VersionStatus.Available); + versionInfoEntity + .setStatus(versionInfoEntity.getActiveVersion().isFinal() ? VersionStatus.Certified + : VersionStatus.Draft); versionInfoEntity.setCandidate(null); versionInfoDao.update(versionInfoEntity); return versionInfoEntity.getActiveVersion(); @@ -341,8 +360,8 @@ public class VersioningManagerImpl implements VersioningManager { Version checkedInVersion = null; switch (versionInfoEntity.getStatus()) { - case Available: - case Final: + case Draft: + case Certified: MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.CHECKIN_ENTITY, ErrorLevel.ERROR.name(), LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't checkin versionable entity"); @@ -379,7 +398,7 @@ public class VersioningManagerImpl implements VersioningManager { Version submitVersion = null; switch (versionInfoEntity.getStatus()) { - case Final: + case Certified: MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(), LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't submit versionable entity"); @@ -391,7 +410,7 @@ public class VersioningManagerImpl implements VersioningManager { LoggerErrorCode.PERMISSION_ERROR.getErrorCode(), "Can't submit versionable entity"); throw new CoreException(new SubmitLockedEntityNotAllowedErrorBuilder(entityType, entityId, versionInfoEntity.getCandidate().getUser()).build()); - case Available: + case Draft: submitVersion = doSubmit(versionInfoEntity, user, submitDescription); break; default: @@ -444,6 +463,143 @@ public class VersioningManagerImpl implements VersioningManager { return activeVersions; } + @Override + public List<Version> list(String itemId) { + + List<Version> versions = versionDao.list(itemId); + Set<String> versionsNames = versions.stream().map(Version::getName).collect(Collectors.toSet()); + versions.forEach(version -> { + version.setAdditionalInfo(new HashMap<>()); + versionCalculator.injectAdditionalInfo(version, versionsNames); + }); + return versions; + } + + @Override + public Version get(String itemId, Version version) { + return versionDao.get(itemId, version) + .map(retrievedVersion -> getUpdateRetrievedVersion(itemId, retrievedVersion)) + .orElseGet(() -> getSyncedVersion(itemId, version)); + } + + private Version getUpdateRetrievedVersion(String itemId, Version version) { + if (version.getStatus() == VersionStatus.Certified && + (version.getState().getSynchronizationState() == SynchronizationState.OutOfSync || + version.getState().isDirty())) { + forceSync(itemId, version); + LOGGER.info("Item Id {}, version Id {}: Force sync is done", itemId, version.getId()); + version = versionDao.get(itemId, version) + .orElseThrow(() -> new IllegalStateException( + "Get version after a successful force sync must return the version")); + } + return version; + } + + private Version getSyncedVersion(String itemId, Version version) { + sync(itemId, version); + LOGGER.info("Item Id {}, version Id {}: First time sync is done", itemId, version.getId()); + return versionDao.get(itemId, version) + .orElseThrow(() -> new IllegalStateException( + "Get version after a successful sync must return the version")); + } + + @Override + public Version create(String itemId, Version version, VersionCreationMethod creationMethod) { + String baseVersionName = null; + if (version.getBaseId() == null) { + version.setDescription("Initial version"); + } else { + baseVersionName = get(itemId, new Version(version.getBaseId())).getName(); + } + String versionName = versionCalculator.calculate(baseVersionName, creationMethod); + validateVersionName(itemId, versionName); + version.setName(versionName); + + versionDao.create(itemId, version); + itemManager.updateVersionStatus(itemId, VersionStatus.Draft, null); + + publish(itemId, version, String.format("Initial version: %s ", version.getName())); + return version; + } + + private void validateVersionName(String itemId, String versionName) { + if (versionDao.list(itemId).stream() + .anyMatch(version -> versionName.equals(version.getName()))) { + String errorDescription = String + .format("Item %s: create version failed, a version with the name %s already exist", + itemId, versionName); + + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY, + LoggerTragetServiceName.CREATE_VERSION, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription); + + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withCategory(ErrorCategory.APPLICATION) + .withId("VERSION_NAME_ALREADY_EXIST") + .withMessage(errorDescription) + .build()); + } + } + + @Override + public void submit(String itemId, Version version, String submitDescription) { + version = get(itemId, version); + + validateSubmit(itemId, version); + + version.setStatus(VersionStatus.Certified); + versionDao.update(itemId, version); + + publish(itemId, version, submitDescription); + + itemManager.updateVersionStatus(itemId, VersionStatus.Certified, VersionStatus.Draft); + } + + private void validateSubmit(String itemId, Version version) { + if (version.getStatus() == VersionStatus.Certified) { + String errorDescription = String + .format("Item %s: submit version failed, version %s is already Certified", itemId, + version.getId()); + + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + LoggerTragetServiceName.SUBMIT_ENTITY, ErrorLevel.ERROR.name(), + LoggerErrorCode.DATA_ERROR.getErrorCode(), errorDescription); + + throw new CoreException(new ErrorCode.ErrorCodeBuilder() + .withCategory(ErrorCategory.APPLICATION) + .withId("VERSION_ALREADY_CERTIFIED") + .withMessage(errorDescription) + .build()); + } + } + + @Override + public void publish(String itemId, Version version, String message) { + versionDao.publish(itemId, version, message); + } + + + @Override + public void sync(String itemId, Version version) { + versionDao.sync(itemId, version); + } + + @Override + public void forceSync(String itemId, Version version) { + versionDao.forceSync(itemId, version); + } + + @Override + public void revert(String itemId, Version version, String revisionId) { + versionDao.revert(itemId, version, revisionId); + } + + @Override + public List<Revision> listRevisions(String itemId, Version version) { + return versionDao.listRevisions(itemId, version); + + } + private void markAsCheckedOut(VersionInfoEntity versionInfoEntity, String checkingOutUser) { versionInfoEntity.setStatus(VersionStatus.Locked); versionInfoEntity.setCandidate(new UserCandidateVersion(checkingOutUser, @@ -491,7 +647,7 @@ public class VersioningManagerImpl implements VersioningManager { versionInfoEntity.setCandidate(null); versionInfoEntity.setActiveVersion(userCandidateVersion.getVersion()); versionInfoEntity.getViewableVersions().add(versionInfoEntity.getActiveVersion()); - versionInfoEntity.setStatus(VersionStatus.Available); + versionInfoEntity.setStatus(VersionStatus.Draft); closeVersionOnEntity(versionInfoEntity.getEntityType(), versionInfoEntity.getEntityId(), versionInfoEntity.getActiveVersion()); @@ -519,22 +675,12 @@ public class VersioningManagerImpl implements VersioningManager { versionInfoEntity.setViewableVersions(viewableVersions); versionInfoEntity.setActiveVersion(finalVersion); versionInfoEntity.setLatestFinalVersion(finalVersion); - versionInfoEntity.setStatus(VersionStatus.Final); + versionInfoEntity.setStatus(VersionStatus.Certified); versionInfoDao.update(versionInfoEntity); return finalVersion; } - private void createVersionHistory(VersionableEntityId entityId, Version version, String user, - String description, VersionType type) { - VersionHistoryEntity versionHistory = new VersionHistoryEntity(entityId); - versionHistory.setVersion(version); - versionHistory.setUser(user); - versionHistory.setDescription(description); - versionHistory.setType(type); - //versionHistoryDao.create(versionHistory); - } - private void initVersionOnEntity(String entityType, String entityId, Version baseVersion, Version newVersion) { Set<VersionableEntityMetadata> entityMetadatas = versionableEntities.get(entityType); diff --git a/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..d2d16eff37 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-core/src/main/resources/factoryConfiguration.json @@ -0,0 +1,3 @@ +{ + "org.openecomp.sdc.versioning.VersionCalculatorFactory": "org.openecomp.sdc.versioning.impl.MajorVersionCalculatorFactoryImpl" +}
\ No newline at end of file |