diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib')
9 files changed, 297 insertions, 104 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java index 1f5e20fd0b..bde9d06ae4 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java @@ -20,25 +20,28 @@ package org.openecomp.core.nosqldb.util; +import org.openecomp.core.utilities.file.FileUtils; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.services.YamlUtil; import java.io.FileInputStream; -import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; /** * The type Configuration manager. */ public class ConfigurationManager { - private static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; - private static final String cassandraKey = "cassandraConfig"; + static final String CONFIGURATION_YAML_FILE = "configuration.yaml"; + + private static final String CASSANDRA_KEY = "cassandraConfig"; private static final String DEFAULT_KEYSPACE_NAME = "dox"; private static final String CASSANDRA_ADDRESSES = "cassandra.addresses"; private static final String CASSANDRA_DOX_KEY_STORE = "cassandra.dox.keystore"; @@ -49,14 +52,14 @@ public class ConfigurationManager { private static final String CASSANDRA_SSL = "cassandra.ssl"; private static final String CASSANDRA_TRUSTSTORE = "cassandra.Truststore"; private static final String CASSANDRA_TRUSTSTORE_PASSWORD = "cassandra.TruststorePassword"; - private static final String cassandraHostsKey = "cassandraHosts"; - private static final String cassandraPortKey = "port"; - private static final String cassandraUsernameKey = "username"; - private static final String cassandraPasswordKey = "password"; - private static final String cassandraAuthenticateKey = "authenticate"; - private static final String cassandraSSLKey = "ssl"; - private static final String cassandraTruststorePathKey = "truststorePath"; - private static final String cassandraTruststorePasswordKey = "truststorePassword"; + private static final String CASSANDRA_HOSTS_KEY = "cassandraHosts"; + private static final String CASSANDRA_PORT_KEY = "port"; + private static final String CASSANDRA_USERNAME_KEY = "username"; + private static final String CASSANDRA_PASSWORD_KEY = "password"; + private static final String CASSANDRA_AUTHENTICATE_KEY = "authenticate"; + private static final String CASSANDRA_SSL_KEY = "ssl"; + private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; + private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword"; private static ConfigurationManager instance = null; private final LinkedHashMap<String, Object> cassandraConfiguration; @@ -64,18 +67,24 @@ public class ConfigurationManager { private ConfigurationManager() { - YamlUtil yamlUtil = new YamlUtil(); + String configurationYamlFile = System.getProperty(CONFIGURATION_YAML_FILE); - InputStream yamlAsString; - if (configurationYamlFile != null) { - yamlAsString = getConfigFileIs(configurationYamlFile); - } else { - //Load from resources - yamlAsString = yamlUtil.loadYamlFileIs("/" + CONFIGURATION_YAML_FILE); - } - Map<String, LinkedHashMap<String, Object>> configurationMap = yamlUtil.yamlToMap(yamlAsString); - cassandraConfiguration = configurationMap.get(cassandraKey); + Function<InputStream, Map<String, LinkedHashMap<String, Object>>> reader = (is) -> { + YamlUtil yamlUtil = new YamlUtil(); + return yamlUtil.yamlToMap(is); + }; + + try { + + Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null + ? readFromFile(configurationYamlFile, reader) // load from file + : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource + cassandraConfiguration = configurationMap.get(CASSANDRA_KEY); + + } catch (IOException e) { + throw new RuntimeException("Failed to read configuration", e); + } } /** @@ -101,7 +110,7 @@ public class ConfigurationManager { if (addresses != null) { return addresses.split(","); } - List lsAddresses = (ArrayList) cassandraConfiguration.get(cassandraHostsKey); + List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); String[] addressesArray; addressesArray = (String[]) lsAddresses.toArray(new String[lsAddresses.size()]); return addressesArray; @@ -131,7 +140,7 @@ public class ConfigurationManager { public String getUsername() { String username = System.getProperty(CASSANDRA_USER); if (username == null) { - username = (String) cassandraConfiguration.get(cassandraUsernameKey); + username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); } return username; } @@ -144,7 +153,7 @@ public class ConfigurationManager { public String getPassword() { String password = System.getProperty(CASSANDRA_PASSWORD); if (password == null) { - password = (String) cassandraConfiguration.get(cassandraPasswordKey); + password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); } return password; } @@ -157,7 +166,7 @@ public class ConfigurationManager { public String getTruststorePath() { String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); if (truststorePath == null) { - truststorePath = (String) cassandraConfiguration.get(cassandraTruststorePathKey); + truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); } return truststorePath; } @@ -170,7 +179,7 @@ public class ConfigurationManager { public String getTruststorePassword() { String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); if (truststorePassword == null) { - truststorePassword = (String) cassandraConfiguration.get(cassandraTruststorePasswordKey); + truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); } return truststorePassword; } @@ -184,7 +193,7 @@ public class ConfigurationManager { int port; String sslPort = System.getProperty(CASSANDRA_PORT); if (sslPort == null) { - sslPort = (String) cassandraConfiguration.get(cassandraPortKey); + sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY); if (sslPort == null) { sslPort = "0"; } @@ -200,7 +209,7 @@ public class ConfigurationManager { * @return the boolean */ public boolean isSsl() { - return getBooleanResult(CASSANDRA_SSL, cassandraSSLKey); + return getBooleanResult(CASSANDRA_SSL, CASSANDRA_SSL_KEY); } /** @@ -209,7 +218,7 @@ public class ConfigurationManager { * @return the boolean */ public boolean isAuthenticate() { - return getBooleanResult(CASSANDRA_AUTHENTICATE, cassandraAuthenticateKey); + return getBooleanResult(CASSANDRA_AUTHENTICATE, CASSANDRA_AUTHENTICATE_KEY); } private Boolean getBooleanResult(String property, String key) { @@ -226,13 +235,9 @@ public class ConfigurationManager { return res; } - private InputStream getConfigFileIs(String file) { - InputStream is = null; - try { - is = new FileInputStream(file); - } catch (FileNotFoundException exception) { - log.debug("",exception); + private <T> T readFromFile(String file, Function<InputStream, T> reader) throws IOException { + try (InputStream is = new FileInputStream(file)) { + return reader.apply(is); } - return is; } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java new file mode 100644 index 0000000000..f8d5d2dce7 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/test/java/org/openecomp/core/nosqldb/util/ConfigurationManagerTest.java @@ -0,0 +1,73 @@ +package org.openecomp.core.nosqldb.util; + +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import java.io.Closeable; +import java.io.IOException; +import java.lang.reflect.Field; + +import static org.testng.Assert.*; + +/** + * @author EVITALIY + * @since 22 Oct 17 + */ +public class ConfigurationManagerTest { + + private static final String NON_EXISTENT = "unexistentfile"; + + @BeforeMethod + public void resetInstance() throws NoSuchFieldException, IllegalAccessException { + Field field = ConfigurationManager.class.getDeclaredField("instance"); + field.setAccessible(true); + field.set(null, null); + } + + @Test(expectedExceptions = IOException.class, + expectedExceptionsMessageRegExp = ".*" + NON_EXISTENT + ".*") + public void testGetInstanceSystemProperty() throws Throwable { + + try (ConfigurationSystemPropertyUpdater updater = new ConfigurationSystemPropertyUpdater(NON_EXISTENT)) { + ConfigurationManager.getInstance(); + } catch (RuntimeException e) { + Throwable cause = e.getCause(); + throw cause == null ? e : cause; + } + } + + @Test() + public void testGetInstanceDefault() throws Exception { + + try (ConfigurationSystemPropertyUpdater property = new ConfigurationSystemPropertyUpdater()) { + ConfigurationManager manager = ConfigurationManager.getInstance(); + assertNotNull(manager.getUsername()); + } + } + + + private static class ConfigurationSystemPropertyUpdater implements Closeable { + + private final String oldValue; + + private ConfigurationSystemPropertyUpdater(String value) { + this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, value); + } + + private ConfigurationSystemPropertyUpdater() { + this.oldValue = System.getProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + } + + @Override + public void close() throws IOException { + + if (oldValue == null) { + System.clearProperty(ConfigurationManager.CONFIGURATION_YAML_FILE); + } else { + System.setProperty(ConfigurationManager.CONFIGURATION_YAML_FILE, oldValue); + } + } + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java index 6ff213c34c..6ab3f8b049 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/file/FileUtils.java @@ -28,6 +28,7 @@ import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.tosca.services.YamlUtil; import java.io.ByteArrayInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URL; @@ -54,7 +55,16 @@ public class FileUtils { * @param function logic to be applied to the input stream */ public static <T> T readViaInputStream(String fileName, Function<InputStream, T> function) { - return readViaInputStream(FileUtils.class.getClassLoader().getResource(fileName), function); + + Objects.requireNonNull(fileName); + + // the leading slash doesn't make sense and doesn't work when used with a class loader + URL resource = FileUtils.class.getClassLoader().getResource(fileName.startsWith("/") ? fileName.substring(1) : fileName); + if (resource == null) { + throw new IllegalArgumentException("Resource not found: " + fileName); + } + + return readViaInputStream(resource, function); } /** diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java new file mode 100644 index 0000000000..e32aa39904 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/java/org/openecomp/core/utilities/file/FileUtilsTest.java @@ -0,0 +1,48 @@ +package org.openecomp.core.utilities.file; + +import org.testng.annotations.Test; + +import java.io.IOException; +import java.io.InputStream; +import java.util.function.Function; + +import static org.testng.Assert.assertTrue; + +/** + * @author EVITALIY + * @since 22 Oct 17 + */ +public class FileUtilsTest { + + private static final String TEST_RESOURCE = FileUtilsTest.class.getPackage().getName() + .replace('.', '/') + "/test-resource.txt"; + + private static final Function<InputStream, Integer> TEST_FUNCTION = (s) -> { + + try { + return s.available(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }; + + @Test + public void testReadViaInputStreamWithSlash() throws Exception { + assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0); + } + + @Test + public void testReadViaInputStreamWithoutSlash() throws Exception { + assertTrue(FileUtils.readViaInputStream(TEST_RESOURCE, TEST_FUNCTION) > 0); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testReadViaInputStreamNull() throws Exception { + FileUtils.readViaInputStream((String) null, TEST_FUNCTION); + } + + @Test(expectedExceptions = IllegalArgumentException.class) + public void testReadViaInputStreamNotFound() throws Exception { + FileUtils.readViaInputStream("notfound.txt", TEST_FUNCTION); + } +}
\ No newline at end of file diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt new file mode 100644 index 0000000000..dce400c8a5 --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/test/resources/org/openecomp/core/utilities/file/test-resource.txt @@ -0,0 +1 @@ +CLASSPATH RESOURCE
\ No newline at end of file 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 index fa08ba49a8..deb5ffde35 100644 --- 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 @@ -60,13 +60,12 @@ public class ZusammenConnectorImpl implements ZusammenConnector { @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; + Response<Collection<Item>> response = itemAdaptorFactory.createInterface(context).list(context); + if (!response.isSuccessful()) { + throw new RuntimeException( + "Failed to list Items. message:" + response.getReturnCode().toString()); } + return response.getValue(); } @Override @@ -76,7 +75,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { return response.getValue(); } else { throw new RuntimeException( - "failed to create Item. message:" + response.getReturnCode().getMessage()); + "failed to create Item. message:" + response.getReturnCode().toString()); } } @@ -87,7 +86,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { if (!response.isSuccessful()) { throw new RuntimeException("failed to update Item . ItemId:" + itemId + "" + - " message:" + response.getReturnCode().getMessage()); + " message:" + response.getReturnCode().toString()); } } @@ -98,7 +97,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { if (!versions.isSuccessful()) { logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ITEM_VERSION_RETRIEVAL, versions .getReturnCode()); - throw new RuntimeException(versions.getReturnCode().getMessage()); // TODO: 3/26/2017 + throw new RuntimeException(versions.getReturnCode().toString()); // TODO: 3/26/2017 } return versions.getValue(); } @@ -114,7 +113,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { throw new RuntimeException("failed to create Item Version. ItemId:" + itemId + " based " + "on:" + baseVersionId + " message:" + response - .getReturnCode().getMessage()); + .getReturnCode().toString()); } } @@ -126,7 +125,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { 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())); + itemId.getValue(), versionId.getValue(), response.getReturnCode().toString())); } } @@ -151,7 +150,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { 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())); + response.getReturnCode().toString())); } } @@ -166,7 +165,7 @@ public class ZusammenConnectorImpl implements ZusammenConnector { } else { logErrorMessageToMdc(ItemElementLoggerTargetServiceName.ELEMENT_GET_BY_PROPERTY, elementInfosResponse.getReturnCode()); - throw new RuntimeException(elementInfosResponse.getReturnCode().getMessage()); + throw new RuntimeException(elementInfosResponse.getReturnCode().toString()); } } @@ -187,11 +186,15 @@ public class ZusammenConnectorImpl implements ZusammenConnector { @Override public Optional<Element> saveElement(SessionContext context, ElementContext elementContext, ZusammenElement element, String message) { - Response<Element> saveResponse = elementAdaptorFactory.createInterface(context) + Response<Element> response = elementAdaptorFactory.createInterface(context) .save(context, elementContext, element, message); - return saveResponse.isSuccessful() - ? Optional.of(saveResponse.getValue()) - : Optional.empty(); // TODO: 3/21/2017 error? + if (!response.isSuccessful()) { + throw new RuntimeException(String + .format("Failed to save element %s. ItemId: %s, versionId: %s, message: %s", + element.getElementId().getValue(), elementContext.getItemId().getValue(), + elementContext.getVersionId().getValue(), response.getReturnCode().toString())); + } + return Optional.of(response.getValue()); } private void logErrorMessageToMdc(ItemElementLoggerTargetServiceName diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/ElementCollaborationStore.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/ElementCollaborationStore.java index 76b0a900c1..ac103c0fbf 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/ElementCollaborationStore.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/ElementCollaborationStore.java @@ -11,17 +11,20 @@ import org.openecomp.core.zusammen.plugin.dao.ElementRepository; import org.openecomp.core.zusammen.plugin.dao.ElementRepositoryFactory; import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; +import java.util.ArrayList; import java.util.Collection; -import java.util.HashSet; -import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; +import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getCollaborationElement; import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName; public class ElementCollaborationStore { + private static final String SUB_ELEMENT_NOT_EXIST_ERROR_MSG = + "List sub elements error: item %s, version %s - " + + "element %s, which appears as sub element of element %s, does not exist"; + public Collection<CollaborationElement> listElements(SessionContext context, ElementContext elementContext, Id elementId) { @@ -33,14 +36,26 @@ public class ElementCollaborationStore { } ElementRepository elementRepository = getElementRepository(context); - return elementRepository.get(context, elementEntityContext, new ElementEntity(elementId)) - .map(ElementEntity::getSubElementIds).orElse(new HashSet<>()).stream() - .map(subElementId -> elementRepository - .get(context, elementEntityContext, new ElementEntity(subElementId)).get()) - .filter(Objects::nonNull) - .map(subElement -> ZusammenPluginUtil - .getCollaborationElement(elementEntityContext, subElement)) - .collect(Collectors.toList()); + String elementIdValue = elementId.getValue(); + String versionIdValue = elementContext.getChangeRef() == null + ? elementContext.getVersionId().getValue() + : elementContext.getChangeRef(); + Collection<CollaborationElement> subElements = new ArrayList<>(); + + Optional<ElementEntity> element = + elementRepository.get(context, elementEntityContext, new ElementEntity(elementId)); + if (element.isPresent() && element.get().getSubElementIds() != null) { + for (Id subElementId : element.get().getSubElementIds()) { + ElementEntity subElement = + elementRepository.get(context, elementEntityContext, new ElementEntity(subElementId)) + .orElseThrow( + () -> new IllegalStateException(String.format(SUB_ELEMENT_NOT_EXIST_ERROR_MSG, + elementContext.getItemId().getValue(), versionIdValue, + subElementId.getValue(), elementIdValue))); + subElements.add(getCollaborationElement(elementEntityContext, subElement)); + } + } + return subElements; } public CollaborationElement getElement(SessionContext context, ElementContext elementContext, @@ -49,8 +64,7 @@ public class ElementCollaborationStore { new ElementEntityContext(ZusammenPluginUtil.getPrivateSpaceName(context), elementContext); return getElementRepository(context) .get(context, elementEntityContext, new ElementEntity(elementId)) - .map(elementEntity -> ZusammenPluginUtil - .getCollaborationElement(elementEntityContext, elementEntity)) + .map(elementEntity -> getCollaborationElement(elementEntityContext, elementEntity)) .orElse(null); } @@ -78,7 +92,7 @@ public class ElementCollaborationStore { ZusammenPluginUtil.getElementEntity(element)); } - public boolean checkHealth(SessionContext sessionContext){ + public boolean checkHealth(SessionContext sessionContext) { return getElementRepository(sessionContext).checkHealth(sessionContext); } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/VersionCollaborationStore.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/VersionCollaborationStore.java index ae23b6e8a9..db3066c313 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/VersionCollaborationStore.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/collaboration/VersionCollaborationStore.java @@ -14,7 +14,10 @@ import org.openecomp.core.zusammen.plugin.dao.ElementRepositoryFactory; import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; +import static org.openecomp.core.zusammen.plugin.ZusammenPluginConstants.ROOT_ELEMENTS_PARENT_ID; import static org.openecomp.core.zusammen.plugin.ZusammenPluginUtil.getSpaceName; public class VersionCollaborationStore { @@ -25,46 +28,74 @@ public class VersionCollaborationStore { throw new UnsupportedOperationException( "In this plugin implementation tag is supported only on versionId"); } - copyElements(context, getSpaceName(context, Space.PRIVATE), itemId, versionId, tag.getName()); + String space = getSpaceName(context, Space.PRIVATE); + ElementEntityContext targetContext = new ElementEntityContext(space, itemId, versionId); + targetContext.setChangeRef(tag.getName()); + copyElements(context, new ElementEntityContext(space, itemId, versionId), targetContext, + getElementRepository(context)); } public CollaborationMergeChange resetItemVersionHistory(SessionContext context, Id itemId, Id versionId, String changeRef) { ElementRepository elementRepository = getElementRepository(context); - ElementEntityContext elementContext = - new ElementEntityContext(getSpaceName(context, Space.PRIVATE), itemId, versionId); - CollaborationMergeChange resetChange = new CollaborationMergeChange(); + String spaceName = getSpaceName(context, Space.PRIVATE); + ElementEntityContext versionContext = new ElementEntityContext(spaceName, itemId, versionId); - Collection<ElementEntity> versionElements = elementRepository.list(context, elementContext); - versionElements.stream() - .map(elementEntity -> - convertElementEntityToElementChange(elementEntity, elementContext, Action.DELETE)) - .forEach(resetChange.getChangedElements()::add); + Collection<ElementEntity> deletedElements = + deleteElements(context, versionContext, elementRepository); - elementContext.setChangeRef(changeRef); - Collection<ElementEntity> changeRefElements = elementRepository.list(context, elementContext); - changeRefElements.stream() - .map(elementEntity -> - convertElementEntityToElementChange(elementEntity, elementContext, Action.CREATE)) - .forEach(resetChange.getChangedElements()::add); + ElementEntityContext changeRefContext = new ElementEntityContext(spaceName, itemId, versionId); + changeRefContext.setChangeRef(changeRef); - return resetChange; // TODO: 4/19/2017 version change... + Collection<ElementEntity> createdElements = + copyElements(context, changeRefContext, versionContext, elementRepository); + + // TODO: 4/19/2017 version change... + return createCollaborationMergeChange(versionContext, deletedElements, createdElements); } - private void copyElements(SessionContext context, String space, Id itemId, Id sourceVersionId, - String targetTag) { - ElementRepository elementRepository = getElementRepository(context); - ElementEntityContext elementContext = new ElementEntityContext(space, itemId, sourceVersionId); + private Collection<ElementEntity> deleteElements(SessionContext context, + ElementEntityContext elementContext, + ElementRepository elementRepository) { + Collection<ElementEntity> elements = elementRepository.list(context, elementContext); + elements.forEach(element -> elementRepository + .delete(context, elementContext, new ElementEntity(element.getId()))); + elementRepository.delete(context, elementContext, new ElementEntity(ROOT_ELEMENTS_PARENT_ID)); + return elements; + } - Collection<ElementEntity> versionElements = elementRepository.list(context, elementContext); + private Collection<ElementEntity> copyElements(SessionContext context, + ElementEntityContext sourceElementContext, + ElementEntityContext targetElementContext, + ElementRepository elementRepository) { + Collection<ElementEntity> elements = elementRepository.list(context, sourceElementContext); + elements.forEach(elementEntity -> + elementRepository.create(context, targetElementContext, elementEntity)); + return elements; + } + + private CollaborationMergeChange createCollaborationMergeChange( + ElementEntityContext versionContext, + Collection<ElementEntity> deletedElements, + Collection<ElementEntity> createdElements) { + CollaborationMergeChange mergeChange = new CollaborationMergeChange(); + mergeChange.getChangedElements().addAll( + convertToCollaborationElementChanges(versionContext, deletedElements, Action.DELETE)); + mergeChange.getChangedElements().addAll( + convertToCollaborationElementChanges(versionContext, createdElements, Action.CREATE)); + return mergeChange; + } - elementContext.setChangeRef(targetTag); - versionElements - .forEach(elementEntity -> elementRepository.create(context, elementContext, elementEntity)); + private List<CollaborationElementChange> convertToCollaborationElementChanges( + ElementEntityContext elementContext, Collection<ElementEntity> changedElements, + Action action) { + return changedElements.stream() + .map(element -> convertToCollaborationElementChange(element, elementContext, action)) + .collect(Collectors.toList()); } - private CollaborationElementChange convertElementEntityToElementChange( + private CollaborationElementChange convertToCollaborationElementChange( ElementEntity elementEntity, ElementEntityContext elementContext, Action action) { CollaborationElementChange elementChange = new CollaborationElementChange(); elementChange diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/dao/impl/CassandraElementRepository.java b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/dao/impl/CassandraElementRepository.java index c19e8799d1..6cc1350444 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/dao/impl/CassandraElementRepository.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-zusammen-lib/openecomp-zusammen-plugin/src/main/java/org/openecomp/core/zusammen/plugin/dao/impl/CassandraElementRepository.java @@ -16,12 +16,6 @@ package org.openecomp.core.zusammen.plugin.dao.impl; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.mapping.annotations.Accessor; -import com.datastax.driver.mapping.annotations.Param; -import com.datastax.driver.mapping.annotations.Query; -import com.google.gson.reflect.TypeToken; import com.amdocs.zusammen.datatypes.Id; import com.amdocs.zusammen.datatypes.Namespace; import com.amdocs.zusammen.datatypes.SessionContext; @@ -29,6 +23,12 @@ import com.amdocs.zusammen.datatypes.item.Info; import com.amdocs.zusammen.datatypes.item.Relation; import com.amdocs.zusammen.plugin.statestore.cassandra.dao.types.ElementEntityContext; import com.amdocs.zusammen.utils.fileutils.json.JsonUtil; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.mapping.annotations.Accessor; +import com.datastax.driver.mapping.annotations.Param; +import com.datastax.driver.mapping.annotations.Query; +import com.google.gson.reflect.TypeToken; import org.openecomp.core.zusammen.plugin.dao.ElementRepository; import org.openecomp.core.zusammen.plugin.dao.types.ElementEntity; @@ -38,22 +38,30 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; public class CassandraElementRepository implements ElementRepository { + private static final String VERSION_ELEMENT_NOT_EXIST_ERROR_MSG = + "List version elements error: " + + "element %s, which appears as an element of item %s version %s, does not exist"; + @Override public Collection<ElementEntity> list(SessionContext context, ElementEntityContext elementContext) { Set<String> elementIds = getVersionElementIds(context, elementContext); - return elementIds.stream() - .map(elementId -> get(context, elementContext, new ElementEntity(new Id(elementId))).get()) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + Collection<ElementEntity> elements = new ArrayList<>(); + for (String elementId : elementIds) { + elements.add(get(context, elementContext, new ElementEntity(new Id(elementId))) + .orElseThrow( + () -> new IllegalStateException(String.format(VERSION_ELEMENT_NOT_EXIST_ERROR_MSG, + elementId, elementContext.getItemId().getValue(), + getVersionId(elementContext))))); + } + return elements; } @Override |