diff options
author | 2017-06-09 02:39:56 +0300 | |
---|---|---|
committer | 2017-06-09 02:39:56 +0300 | |
commit | 280f8015d06af1f41a3ef12e8300801c7a5e0d54 (patch) | |
tree | 9c1d3978c04cd28068f02073038c936bb49ca9e0 /openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core | |
parent | fd3821dad11780d33c5373d74c957c442489945e (diff) |
[SDC-29] Amdocs OnBoard 1707 initial commit.
Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370
Signed-off-by: AviZi <avi.ziv@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core')
8 files changed, 670 insertions, 0 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java new file mode 100644 index 0000000000..f746d706e1 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnector.java @@ -0,0 +1,51 @@ +package org.openecomp.core.zusammen.db; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.itemversion.Tag; +import com.amdocs.zusammen.datatypes.response.Response; + +import java.util.Collection; +import java.util.Optional; + +public interface ZusammenConnector { + + Collection<Item> listItems(SessionContext context); + + Id createItem(SessionContext context, Info info); + + void updateItem(SessionContext context, Id itemId, Info info); + + + Collection<ItemVersion> listVersions(SessionContext context, Id itemId); + + Id createVersion(SessionContext context, Id itemId, Id baseVersionId, + ItemVersionData itemVersionData); + + void updateVersion(SessionContext context, Id itemId, Id versionId, + ItemVersionData itemVersionData); + + void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag); + + void resetVersionHistory(SessionContext context, Id itemId, Id versionId, String changeRef); + + + Collection<ElementInfo> listElements(SessionContext context, ElementContext elementContext, + Id parentElementId); + + Response<ElementInfo> getElementInfo(SessionContext context, ElementContext elementContext, Id + elementId); + + Response<Element> getElement(SessionContext context, ElementContext elementContext, Id elementId); + + Optional<Element> saveElement(SessionContext context, ElementContext elementContext, + ZusammenElement element, String message); +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnectorFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnectorFactory.java new file mode 100644 index 0000000000..31a8bf598d --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/ZusammenConnectorFactory.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.core.zusammen.db; + +import org.openecomp.core.factory.api.AbstractComponentFactory; +import org.openecomp.core.factory.api.AbstractFactory; + +public abstract class ZusammenConnectorFactory extends AbstractComponentFactory<ZusammenConnector> { + + public static ZusammenConnectorFactory getInstance() { + return AbstractFactory.getInstance(ZusammenConnectorFactory.class); + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorFactoryImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorFactoryImpl.java new file mode 100644 index 0000000000..f3916521dd --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorFactoryImpl.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.core.zusammen.db.impl; + +import com.amdocs.zusammen.adaptor.inbound.api.item.ElementAdaptorFactory; +import com.amdocs.zusammen.adaptor.inbound.api.item.ItemAdaptorFactory; +import com.amdocs.zusammen.adaptor.inbound.api.item.ItemVersionAdaptorFactory; +import org.openecomp.core.zusammen.db.ZusammenConnector; +import org.openecomp.core.zusammen.db.ZusammenConnectorFactory; + +public class ZusammenConnectorFactoryImpl extends ZusammenConnectorFactory { + private static final ZusammenConnector INSTANCE = + new ZusammenConnectorImpl(ItemAdaptorFactory.getInstance(), + ItemVersionAdaptorFactory.getInstance(), + ElementAdaptorFactory.getInstance()); + + @Override + public ZusammenConnector createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java new file mode 100644 index 0000000000..8aa8e129df --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/db/impl/ZusammenConnectorImpl.java @@ -0,0 +1,200 @@ +package org.openecomp.core.zusammen.db.impl; + +import com.amdocs.zusammen.adaptor.inbound.api.item.ElementAdaptorFactory; +import com.amdocs.zusammen.adaptor.inbound.api.item.ItemAdaptorFactory; +import com.amdocs.zusammen.adaptor.inbound.api.item.ItemVersionAdaptorFactory; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.Space; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.itemversion.Tag; +import com.amdocs.zusammen.datatypes.response.Response; +import com.amdocs.zusammen.datatypes.response.ReturnCode; +import org.openecomp.core.zusammen.db.ZusammenConnector; +import org.openecomp.core.zusammen.impl.CassandraConnectionInitializer; +import org.openecomp.core.zusammen.impl.ItemElementLoggerTargetServiceName; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; +import org.openecomp.sdc.logging.types.LoggerConstants; +import org.openecomp.sdc.logging.types.LoggerErrorCode; + +import java.util.Collection; +import java.util.Optional; + +public class ZusammenConnectorImpl implements ZusammenConnector { + + private ItemAdaptorFactory itemAdaptorFactory; + private ItemVersionAdaptorFactory versionAdaptorFactory; + private ElementAdaptorFactory elementAdaptorFactory; + + public ZusammenConnectorImpl( + ItemAdaptorFactory itemAdaptorFactory, + ItemVersionAdaptorFactory versionAdaptorFactory, + ElementAdaptorFactory elementAdaptorFactory) { + this.itemAdaptorFactory = itemAdaptorFactory; + this.versionAdaptorFactory = versionAdaptorFactory; + this.elementAdaptorFactory = elementAdaptorFactory; + CassandraConnectionInitializer.setCassandraConnectionPropertiesToSystem(); + } + + @Override + public Collection<Item> listItems(SessionContext context) { + Response<Collection<Item>> response = + itemAdaptorFactory.createInterface(context).list(context); + if (response.isSuccessful()) { + return response.getValue(); + } else { + return null; + } + } + + @Override + public Id createItem(SessionContext context, Info info) { + Response<Id> response = itemAdaptorFactory.createInterface(context).create(context, info); + if (response.isSuccessful()) { + return response.getValue(); + } else { + throw new RuntimeException( + "failed to create Item. message:" + response.getReturnCode().getMessage()); + } + } + + @Override + public void updateItem(SessionContext context, Id itemId, Info info) { + Response<Void> response = + itemAdaptorFactory.createInterface(context).update(context, itemId, info); + + if (!response.isSuccessful()) { + throw new RuntimeException("failed to update Item . ItemId:" + itemId + "" + + " message:" + response.getReturnCode().getMessage()); + } + } + + @Override + public Collection<ItemVersion> listVersions(SessionContext context, Id itemId) { + Response<Collection<ItemVersion>> versions = + versionAdaptorFactory.createInterface(context).list(context, Space.PRIVATE, itemId); + if (!versions.isSuccessful()) { + logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ITEM_VERSION_RETRIEVAL, versions + .getReturnCode()); + throw new RuntimeException(versions.getReturnCode().getMessage()); // TODO: 3/26/2017 + } + return versions.getValue(); + } + + @Override + public Id createVersion(SessionContext context, Id itemId, Id baseVersionId, + ItemVersionData itemVersionData) { + Response<Id> response = versionAdaptorFactory.createInterface(context).create(context, itemId, + baseVersionId, itemVersionData); + if (response.isSuccessful()) { + return response.getValue(); + } else { + throw new RuntimeException("failed to create Item Version. ItemId:" + itemId + " based " + + "on:" + baseVersionId + + " message:" + response + .getReturnCode().getMessage()); + } + } + + @Override + public void updateVersion(SessionContext context, Id itemId, Id versionId, + ItemVersionData itemVersionData) { + Response<Void> response = versionAdaptorFactory.createInterface(context) + .update(context, itemId, versionId, itemVersionData); + if (!response.isSuccessful()) { + throw new RuntimeException( + String.format("failed to create Item Version. ItemId: %s, versionId: %s, message: %s", + itemId.getValue(), versionId.getValue(), response.getReturnCode().getMessage())); + } + } + + @Override + public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) { + Response<Void> response = versionAdaptorFactory.createInterface(context) + .tag(context, itemId, versionId, null, tag); + if (!response.isSuccessful()) { + throw new RuntimeException(String.format( + "failed to tag Item Version with tag %s. ItemId: %s, versionId: %s, message: %s", + tag.getName(), itemId.getValue(), versionId.getValue(), response.getReturnCode() + .getMessage())); + } + } + + @Override + public void resetVersionHistory(SessionContext context, Id itemId, Id versionId, + String changeRef) { + Response<Void> response = versionAdaptorFactory.createInterface(context) + .resetHistory(context, itemId, versionId, changeRef); + if (!response.isSuccessful()) { + throw new RuntimeException(String.format( + "failed to reset Item Version back to %s. ItemId: %s, versionId: %s, message: %s", + changeRef, itemId.getValue(), versionId.getValue(), + response.getReturnCode().getMessage())); + } + } + + @Override + public Collection<ElementInfo> listElements(SessionContext context, + ElementContext elementContext, + Id parentElementId) { + Response<Collection<ElementInfo>> elementInfosResponse = elementAdaptorFactory + .createInterface(context).list(context, elementContext, parentElementId); + if (elementInfosResponse.isSuccessful()) { + return elementInfosResponse.getValue(); + } else { + logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ELEMENT_GET_BY_PROPERTY, + elementInfosResponse.getReturnCode()); + throw new RuntimeException(elementInfosResponse.getReturnCode().getMessage()); + } + } + + @Override + public Response<ElementInfo> getElementInfo(SessionContext context, ElementContext elementContext, + Id elementId) { + return elementAdaptorFactory.createInterface(context) + .getInfo(context, elementContext, elementId); + } + + + @Override + public Response<Element> getElement(SessionContext context, ElementContext elementContext, + Id elementId) { + return elementAdaptorFactory.createInterface(context).get(context, elementContext, elementId); + } + + @Override + public Optional<Element> saveElement(SessionContext context, ElementContext elementContext, + ZusammenElement element, String message) { + Response<Element> saveResponse = elementAdaptorFactory.createInterface(context) + .save(context, elementContext, element, message); + return saveResponse.isSuccessful() + ? Optional.of(saveResponse.getValue()) + : Optional.empty(); // TODO: 3/21/2017 error? + } + + private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName + itemElementLoggerTargetServiceName, + ReturnCode returnCode) { + logErrorMessageToMdc(itemElementLoggerTargetServiceName, returnCode.toString()); + } + + private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName + itemElementLoggerTargetServiceName, + String message) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + itemElementLoggerTargetServiceName.getDescription(), + ErrorLevel.ERROR.name(), + LoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode(), + message); + } + + +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java new file mode 100644 index 0000000000..47cb57c5f7 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/CassandraConnectionInitializer.java @@ -0,0 +1,59 @@ +package org.openecomp.core.zusammen.impl; + + +import org.apache.commons.lang3.StringUtils; +import org.openecomp.core.nosqldb.util.CassandraUtils; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +/** + * @author Avrahamg + * @since April 25, 2017 + */ + +public class CassandraConnectionInitializer implements ServletContextListener { + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + setCassandraConnectionPropertiesToSystem(); + } + + public static void setCassandraConnectionPropertiesToSystem() { + if (!System.getProperties().containsKey("cassandra.nodes")) { + System.setProperty("cassandra.nodes", StringUtils.join(CassandraUtils.getAddresses(), ',')); + } + if (!System.getProperties().containsKey("cassandra.authenticate")) { + System + .setProperty("cassandra.authenticate", + CassandraUtils.isAuthenticate() ? "true" : "false"); + } + if (!System.getProperties().containsKey("cassandra.ssl")) { + System.setProperty("cassandra.ssl", + CassandraUtils.isSsl() ? "true" : "false"); + } + if (!System.getProperties().containsKey("cassandra.truststore")) { + System.setProperty("cassandra.truststore", CassandraUtils.getTruststore()); + } + if (!System.getProperties().containsKey("cassandra.truststore.password")) { + System.setProperty("cassandra.truststore.password", CassandraUtils.getTruststorePassword()); + } + if (!System.getProperties().containsKey("cassandra.user")) { + System.setProperty("cassandra.user", CassandraUtils.getUser()); + } + if (!System.getProperties().containsKey("cassandra.password")) { + System.setProperty("cassandra.password", CassandraUtils.getPassword()); + } + if (!System.getProperties().containsKey("cassandra.keyspace")) { + System.setProperty("cassandra.keyspace", "zusammen"); + } + } + + // -Dcassandra.nodes=10.147.97.145 -Dcassandra.keyspace=zusammen -Dcassandra.authenticate=true -Dcassandra.ssl=true + // -Dcassandra.truststore=/apps/jetty/base/be/config/.truststore -Dcassandra.truststore.password=Aa123456 + // -Dcassandra.user=asdc_user -Dcassandra.password=Aa1234%^! + + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ItemElementLoggerTargetServiceName.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ItemElementLoggerTargetServiceName.java new file mode 100644 index 0000000000..1a4466a3c5 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ItemElementLoggerTargetServiceName.java @@ -0,0 +1,27 @@ +package org.openecomp.core.zusammen.impl; + +/** + * @author Avrahamg. + * @since February 19, 2017 + */ +public enum ItemElementLoggerTargetServiceName { + ITEM_CREATION("Item Creation"), + ITEM_VERSION_CREATION("Item Version Creation"), + ELEMENT_CREATION("Element Creation"), + ELEMENT_UPDATE("Element Update"), + ELEMENT_LIST("Element List"), + ELEMENT_GET("Element Get"), + ELEMENT_GET_BY_PROPERTY("Element Get By Property"), + ITEM_RETRIEVAL("Item retrieval"), + ITEM_VERSION_RETRIEVAL("Item version retrieval)"); + + private final String description; + + public String getDescription() { + return description; + } + + ItemElementLoggerTargetServiceName(String description) { + this.description = description; + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorFactoryImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorFactoryImpl.java new file mode 100644 index 0000000000..edc1abf108 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorFactoryImpl.java @@ -0,0 +1,35 @@ +/*- + * ============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.core.zusammen.impl; + +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.core.zusammen.api.ZusammenAdaptorFactory; +import org.openecomp.core.zusammen.db.ZusammenConnectorFactory; + +public class ZusammenAdaptorFactoryImpl extends ZusammenAdaptorFactory { + private static final ZusammenAdaptor INSTANCE = + new ZusammenAdaptorImpl(ZusammenConnectorFactory.getInstance().createInterface()); + + @Override + public ZusammenAdaptor createInterface() { + return INSTANCE; + } +} diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java new file mode 100644 index 0000000000..ad4839bd00 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-core/src/main/java/org/openecomp/core/zusammen/impl/ZusammenAdaptorImpl.java @@ -0,0 +1,228 @@ +package org.openecomp.core.zusammen.impl; + +import com.amdocs.zusammen.adaptor.inbound.api.types.item.Element; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ElementInfo; +import com.amdocs.zusammen.adaptor.inbound.api.types.item.ZusammenElement; +import com.amdocs.zusammen.datatypes.Id; +import com.amdocs.zusammen.datatypes.SessionContext; +import com.amdocs.zusammen.datatypes.item.Action; +import com.amdocs.zusammen.datatypes.item.ElementContext; +import com.amdocs.zusammen.datatypes.item.Info; +import com.amdocs.zusammen.datatypes.item.Item; +import com.amdocs.zusammen.datatypes.item.ItemVersion; +import com.amdocs.zusammen.datatypes.item.ItemVersionData; +import com.amdocs.zusammen.datatypes.itemversion.Tag; +import com.amdocs.zusammen.datatypes.response.Response; +import com.amdocs.zusammen.datatypes.response.ReturnCode; +import org.openecomp.core.zusammen.api.ZusammenAdaptor; +import org.openecomp.core.zusammen.db.ZusammenConnector; +import org.openecomp.sdc.datatypes.error.ErrorLevel; +import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage; +import org.openecomp.sdc.logging.types.LoggerConstants; +import org.openecomp.sdc.logging.types.LoggerErrorCode; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; + +public class ZusammenAdaptorImpl implements ZusammenAdaptor { + + private ZusammenConnector connector; + + public ZusammenAdaptorImpl(ZusammenConnector connector) { + this.connector = connector; + } + + @Override + public Optional<ItemVersion> getFirstVersion(SessionContext context, Id itemId) { + Collection<ItemVersion> versions = connector.listVersions(context, itemId); + if(versions == null || versions.size()==0) { + return Optional.empty(); + } + List<ItemVersion> itemVersions = new ArrayList<>(versions); + sortItemVersionListByModificationTimeDescOrder(itemVersions); + ItemVersion itemVersion = itemVersions.iterator().next(); + + return Optional.ofNullable(itemVersion); + } + + @Override + public Optional<ElementInfo> getElementInfo(SessionContext context, ElementContext elementContext, + Id elementId) { + Response<ElementInfo> response = connector.getElementInfo(context, elementContext, elementId); + return response.isSuccessful() ? Optional.ofNullable(response.getValue()) : Optional.empty(); + } + + @Override + public Optional<Element> getElement(SessionContext context, ElementContext elementContext, + String elementId) { + Response<Element> response = connector.getElement(context, elementContext, new Id(elementId)); + return response.isSuccessful() ? Optional.ofNullable(response.getValue()) : Optional.empty(); + } + + @Override + public Optional<Element> getElementByName( + SessionContext context, ElementContext elementContext, Id parentElementId, + String elementName) { + Collection<ElementInfo> elementInfos = + connector.listElements(context, elementContext, parentElementId); + Predicate<ElementInfo> elementInfoPredicate = elementInfo -> elementInfo.getInfo() != null + && elementName.equals(elementInfo.getInfo().getName()); + return getFirstElementInfo(elementInfos, elementInfoPredicate) + .map(elementInfo -> getElement(context, elementContext, elementInfo.getId().getValue())) + .orElse(Optional.empty()); + } + + @Override + public Collection<ElementInfo> listElements(SessionContext context, ElementContext elementContext, + Id parentElementId) { + return connector.listElements(context, elementContext, parentElementId); + } + + @Override + public Collection<Element> listElementData(SessionContext context, + ElementContext elementContext, + Id parentElementId) { + + Collection<ElementInfo> elementInfoList = connector.listElements(context, elementContext, + parentElementId); + if (elementInfoList != null) { + return elementInfoList.stream().map(elementInfo -> connector.getElement(context, + elementContext, elementInfo.getId()).getValue()).collect(Collectors.toList()); + } + + return new ArrayList<>(); + + } + + + @Override + public Collection<ElementInfo> listElementsByName( + SessionContext context, ElementContext elementContext, Id parentElementId, + String elementName) { + Optional<ElementInfo> elementInfoByName = + getElementInfoByName(context, elementContext, parentElementId, elementName); + + return elementInfoByName.isPresent() + ? connector.listElements(context, elementContext, elementInfoByName.get().getId()) + : new ArrayList<>(); + } + + @Override + public Optional<ElementInfo> getElementInfoByName( + SessionContext context, ElementContext elementContext, Id parentElementId, + String elementName) { + Collection<ElementInfo> elementInfos = + connector.listElements(context, elementContext, parentElementId); + return getFirstElementInfo(elementInfos, + elementInfo -> elementInfo.getInfo() != null && + elementName.equals(elementInfo.getInfo().getName())); + } + + @Override + public Optional<Element> saveElement(SessionContext context, ElementContext elementContext, + ZusammenElement element, String message) { + enrichElementHierarchyRec(context, elementContext, null, element); + return connector.saveElement(context, elementContext, element, message); + } + + private void enrichElementHierarchyRec(SessionContext context, ElementContext + elementContext, Id parentElementId, ZusammenElement element) { + if (element.getAction() == Action.CREATE) { + return; + } + locateElementAndUpdateAction(context, elementContext, parentElementId, element); + element.getSubElements().forEach(subElement -> enrichElementHierarchyRec( + context, elementContext, element.getElementId(), (ZusammenElement) subElement)); + } + + // should be applied only for structural elements + private void locateElementAndUpdateAction(SessionContext context, ElementContext elementContext, + Id parentElementId, ZusammenElement element) { + if (element.getElementId() != null) { + return; + } + Optional<ElementInfo> elementInfo = + getElementInfoByName(context, elementContext, parentElementId, element.getInfo().getName()); + if (elementInfo.isPresent()) { + element.setElementId(elementInfo.get().getId()); + if (element.getAction() == null) { + element.setAction(Action.IGNORE); + } + } else { + element.setAction(Action.CREATE); + } + } + + private Optional<ElementInfo> getFirstElementInfo(Collection<ElementInfo> elementInfos, + Predicate<ElementInfo> elementInfoPredicate) { + return elementInfos.stream() + .filter(elementInfoPredicate) + .findFirst(); + } + + private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName + itemElementLoggerTargetServiceName, + ReturnCode returnCode) { + logErrorMessageToMdc(itemElementLoggerTargetServiceName, returnCode.toString()); + } + + private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName + itemElementLoggerTargetServiceName, + String message) { + MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB, + itemElementLoggerTargetServiceName.getDescription(), + ErrorLevel.ERROR.name(), + LoggerErrorCode.BUSINESS_PROCESS_ERROR.getErrorCode(), + message); + } + + @Override + public Id createItem(SessionContext context, Info info) { + return connector.createItem(context, info); + } + + @Override + public Id createVersion(SessionContext context, Id itemId, Id baseVersionId, ItemVersionData + itemVersionData) { + return connector.createVersion(context, itemId, baseVersionId, itemVersionData); + + } + + @Override + public Collection<Item> listItems(SessionContext context) { + return connector.listItems(context); + } + + @Override + public void updateVersion(SessionContext context, Id itemId, Id versionId, + ItemVersionData itemVersionData) { + connector.updateVersion(context, itemId, versionId, itemVersionData); + } + + @Override + public void tagVersion(SessionContext context, Id itemId, Id versionId, Tag tag) { + connector.tagVersion(context, itemId, versionId, tag); + } + + @Override + public void resetVersionHistory(SessionContext context, Id itemId, Id versionId, + String changeRef) { + connector.resetVersionHistory(context, itemId, versionId, changeRef); + } + + @Override + public void updateItem(SessionContext context, Id itemId, Info info) { + connector.updateItem(context, itemId, info); + } + + private static void sortItemVersionListByModificationTimeDescOrder( + List<ItemVersion> itemVersions) { + itemVersions.sort((o1, o2) -> ((Integer)o2.getId().getValue().length()) + .compareTo( (o1.getId().getValue().length()))); + } + +} |