diff options
38 files changed, 4 insertions, 4268 deletions
diff --git a/asdctool/src/main/resources/scripts/populateComponentCache.sh b/asdctool/src/main/resources/scripts/populateComponentCache.sh deleted file mode 100644 index 79ddbf822e..0000000000 --- a/asdctool/src/main/resources/scripts/populateComponentCache.sh +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/bash - -############################## -# Groups Alignment -############################## - -CURRENT_DIR=`pwd` -BASEDIR=$(dirname $0) - -if [ ${BASEDIR:0:1} = "/" ] -then - FULL_PATH=$BASEDIR -else - FULL_PATH=$CURRENT_DIR/$BASEDIR -fi - -source ${FULL_PATH}/baseOperation.sh - -mainClass="org.openecomp.sdc.asdctool.main.MigrationMenu" - -command="java $JVM_LOG_FILE -cp $JARS $mainClass populate-component-cache $@" -echo $command - -$command -result=$? - -echo "***********************************" -echo "***** $result *********************" -echo "***********************************" - -exit $result - - diff --git a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/DataMigrationTest.java b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/DataMigrationTest.java index 487054092e..d99a831903 100644 --- a/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/DataMigrationTest.java +++ b/asdctool/src/test/java/org/openecomp/sdc/asdctool/impl/DataMigrationTest.java @@ -186,12 +186,6 @@ public class DataMigrationTest { } @Test - public void createEventForNoneAuditTable() { - assertThat(dataMigration.createAuditEvent(dataMap, Table.COMPONENT_CACHE)).isNull(); - - } - - @Test public void createEventWhenSomeFieldValuesNotSet() { dataMap.clear(); dataMap.put(AUDIT_ACTION, AuditingActionEnum.AUTH_REQUEST.getName()); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CassandraHealthCheck.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CassandraHealthCheck.java index c77877fac4..56cad89569 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CassandraHealthCheck.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CassandraHealthCheck.java @@ -41,7 +41,6 @@ import java.util.*; @Component("cassandra-health-check") public class CassandraHealthCheck { - private static final Logger log = Logger.getLogger(CassandraHealthCheck.class.getName()); private String localDataCenterName = null; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java index 18d8688036..ccccd58ce7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java @@ -66,7 +66,6 @@ import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.cache.ComponentCache; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.resources.data.ComponentMetadataData; @@ -89,9 +88,6 @@ public abstract class ComponentBusinessLogic extends BaseBusinessLogic { protected ArtifactsBusinessLogic artifactsBusinessLogic; @Autowired - protected ComponentCache componentCache; - - @Autowired private GenericTypeBusinessLogic genericTypeBusinessLogic; @Autowired diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ProductBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ProductBusinessLogic.java index ff532b237d..81c4af8b98 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ProductBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ProductBusinessLogic.java @@ -24,7 +24,6 @@ package org.openecomp.sdc.be.components.impl; import fj.data.Either; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; -import org.openecomp.sdc.be.components.impl.exceptions.ComponentException; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datamodel.api.CategoryTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; @@ -36,7 +35,6 @@ import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.GroupingDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; @@ -66,9 +64,6 @@ public class ProductBusinessLogic extends ComponentBusinessLogic { @Autowired private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; - @Autowired - private ICacheMangerOperation cacheManagerOperation; - public ProductBusinessLogic() { creationRoles = new ArrayList<>(); updateRoles = new ArrayList<>(); @@ -803,14 +798,6 @@ public class ProductBusinessLogic extends ComponentBusinessLogic { return null; } - public ICacheMangerOperation getCacheManagerOperation() { - return cacheManagerOperation; - } - - public void setCacheManagerOperation(ICacheMangerOperation cacheManagerOperation) { - this.cacheManagerOperation = cacheManagerOperation; - } - @Override public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String componentId, List<String> dataParamsToReturn) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 1fa9051a9a..0118a7943d 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -129,7 +129,6 @@ import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.StorageException; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; @@ -202,9 +201,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { private CompositionBusinessLogic compositionBusinessLogic; @Autowired - private ICacheMangerOperation cacheManagerOperation; - - @Autowired private ResourceDataMergeBusinessLogic resourceDataMergeBusinessLogic; @Autowired @@ -1862,10 +1858,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { componentsUtils.auditResource(responseFormat, csarInfo.getModifier(), resource, AuditingActionEnum.IMPORT_RESOURCE); throw e; } - // add the created node types to the cache although they are not in the - // graph. - csarInfo.getCreatedNodes().values().stream() - .forEach(p -> cacheManagerOperation.storeComponentInCache(p, NodeTypeEnum.Resource)); } private Either<Resource, ResponseFormat> handleVfCsarArtifacts(Resource resource, CsarInfo csarInfo, @@ -5221,14 +5213,6 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { } } - public ICacheMangerOperation getCacheManagerOperation() { - return cacheManagerOperation; - } - - public void setCacheManagerOperation(ICacheMangerOperation cacheManagerOperation) { - this.cacheManagerOperation = cacheManagerOperation; - } - @Override public Either<UiComponentDataTransfer, ResponseFormat> getUiComponentDataTransferByComponentId(String resourceId, List<String> dataParamsToReturn) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java index df56ccc1e4..7f4ec46714 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java @@ -120,7 +120,6 @@ import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; @@ -159,14 +158,15 @@ import javax.servlet.http.HttpServletRequest; @org.springframework.stereotype.Component("serviceBusinessLogic") public class ServiceBusinessLogic extends ComponentBusinessLogic { - private static final String CHANGE_SERVICE_DISTRIBUTION = "Change Service Distribution"; + private static final String CHANGE_SERVICE_DISTRIBUTION = "Change Service Distribution"; private static final String THE_SERVICE_WITH_SYSTEM_NAME_LOCKED = "The service with system name {} locked. "; private static final String FAILED_TO_LOCK_SERVICE_RESPONSE_IS = "Failed to lock service {}. Response is {}. "; private static final String AUDIT_BEFORE_SENDING_RESPONSE = "audit before sending response"; private static final Logger log = Logger.getLogger(ServiceBusinessLogic.class); - private static final String INITIAL_VERSION = "0.1"; - private static final String STATUS_SUCCESS_200 = "200"; + private static final String INITIAL_VERSION = "0.1"; + private static final String STATUS_SUCCESS_200 = "200"; private static final String STATUS_DEPLOYED = "DEPLOYED"; + @Autowired private IDistributionEngine distributionEngine; @Autowired @@ -174,8 +174,6 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { @Autowired private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; @Autowired - private ICacheMangerOperation cacheManagerOperation; - @Autowired private ServiceDistributionValidation serviceDistributionValidation; @Autowired @@ -2440,14 +2438,6 @@ public class ServiceBusinessLogic extends ComponentBusinessLogic { return Either.left(componentInstances); } - public ICacheMangerOperation getCacheManagerOperation() { - return cacheManagerOperation; - } - - public void setCacheManagerOperation(ICacheMangerOperation cacheManagerOperation) { - this.cacheManagerOperation = cacheManagerOperation; - } - public void setForwardingPathOperation(ForwardingPathOperation forwardingPathOperation) { this.forwardingPathOperation = forwardingPathOperation; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/LifecycleBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/LifecycleBusinessLogic.java index befca2480a..c7376d352b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/LifecycleBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/LifecycleBusinessLogic.java @@ -41,7 +41,6 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperatio import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation; @@ -102,9 +101,6 @@ public class LifecycleBusinessLogic { private ToscaExportHandler toscaExportUtils; @Autowired - ICacheMangerOperation cacheManagerOperation; - - @Autowired ToscaOperationFacade toscaOperationFacade; @Autowired diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/BaseServiceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/BaseServiceBusinessLogicTest.java index 49455c2b66..e569499f82 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/BaseServiceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/BaseServiceBusinessLogicTest.java @@ -46,7 +46,6 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ForwardingPathOperat import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation; import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; import org.openecomp.sdc.be.resources.data.auditing.ResourceAdminEvent; import org.openecomp.sdc.be.user.Role; @@ -83,7 +82,6 @@ public abstract class BaseServiceBusinessLogicTest { GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class); ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); - CacheMangerOperation cacheManager = Mockito.mock(CacheMangerOperation.class); GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class); ForwardingPathOperation forwardingPathOperation = Mockito.mock(ForwardingPathOperation.class); @@ -162,7 +160,6 @@ public abstract class BaseServiceBusinessLogicTest { bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic); bl.setComponentsUtils(componentsUtils); bl.setCassandraAuditingDao(auditingDao); - bl.setCacheManagerOperation(cacheManager); bl.setForwardingPathOperation(forwardingPathOperation); bl.setToscaOperationFacade(toscaOperationFacade); mockAuditingDaoLogic(); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java index 5844047784..06f74c10e8 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogicTest.java @@ -70,7 +70,6 @@ import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation; import org.openecomp.sdc.be.model.operations.impl.CsarOperation; import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; @@ -175,7 +174,6 @@ public class ResourceBusinessLogicTest { CsarBusinessLogic csarBusinessLogic = new CsarBusinessLogic(); Map<String, DataTypeDefinition> emptyDataTypes = new HashMap<>(); private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class); - CacheMangerOperation cacheManager = Mockito.mock(CacheMangerOperation.class); List<Resource> reslist; public ResourceBusinessLogicTest() { @@ -257,7 +255,6 @@ public class ResourceBusinessLogicTest { bl.setPropertyOperation(propertyOperation); bl.setJanusGraphGenericDao(mockJanusGraphDao); bl.setApplicationDataTypeCache(applicationDataTypeCache); - bl.setCacheManagerOperation(cacheManager); bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic); toscaOperationFacade.setNodeTypeOperation(nodeTypeOperation); toscaOperationFacade.setTopologyTemplateOperation(topologyTemplateOperation); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java index c8e3fd9a22..05ca32cbbc 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogicTest.java @@ -47,7 +47,6 @@ import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation; import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation; import org.openecomp.sdc.be.resources.data.auditing.DistributionDeployEvent; import org.openecomp.sdc.be.resources.data.auditing.DistributionNotificationEvent; @@ -93,7 +92,6 @@ public class ServiceBusinessLogicTest { private GraphLockOperation graphLockOperation = Mockito.mock(GraphLockOperation.class); private JanusGraphDao mockJanusGraphDao = Mockito.mock(JanusGraphDao.class); private ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); - private CacheMangerOperation cacheManager = Mockito.mock(CacheMangerOperation.class); private GenericTypeBusinessLogic genericTypeBusinessLogic = Mockito.mock(GenericTypeBusinessLogic.class); private UserValidations userValidations = Mockito.mock(UserValidations.class); private ResourceAdminEvent auditArchive1 = Mockito.mock(ResourceAdminEvent.class); @@ -171,7 +169,6 @@ public class ServiceBusinessLogicTest { bl.setGenericTypeBusinessLogic(genericTypeBusinessLogic); bl.setComponentsUtils(componentsUtils); bl.setCassandraAuditingDao(auditingDao); - bl.setCacheManagerOperation(cacheManager); bl.setUserValidations(userValidations); mockAuditingDaoLogic(); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/path/beans/ComponentCassandraDaoMock.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/path/beans/ComponentCassandraDaoMock.java deleted file mode 100644 index cbe341230d..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/path/beans/ComponentCassandraDaoMock.java +++ /dev/null @@ -1,89 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.components.path.beans; - -import fj.data.Either; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.cassandra.CassandraClient; -import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; -import org.openecomp.sdc.be.dao.cassandra.ComponentCacheAccessor; -import org.openecomp.sdc.be.dao.cassandra.ComponentCassandraDao; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.util.List; -import java.util.Map; -import java.util.Set; - -@Component("component-cassandra-dao") -public class ComponentCassandraDaoMock extends ComponentCassandraDao { - - public static Integer DEFAULT_FETCH_SIZE = 500; - private ComponentCacheAccessor componentCacheAccessor; - - public ComponentCassandraDaoMock(CassandraClient cassandraClient) { - super(cassandraClient); - } - - @PostConstruct - public void init() { - - } - - public Either<List<ComponentCacheData>, ActionStatus> getComponents(List<String> ids) { - - return null; - } - - public Either<List<ComponentCacheData>, ActionStatus> getAllComponentIdTimeAndType() { - return null; - } - - - public Either<ComponentCacheData, ActionStatus> getComponent(String id) { - - return null; - } - - public CassandraOperationStatus saveComponent(ComponentCacheData componentCacheData) { - return null; - } - - - public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) { - return null; - } - - - public Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> getComponents( - Map<String, Long> idToTimestampMap) { - - return null; - } - - public CassandraOperationStatus deleteComponent(String id) { - return null; - } - - } - diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java index 96eeaaf464..8036213f1e 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/externalapi/servlet/ExternalRefServletTest.java @@ -54,7 +54,6 @@ import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.*; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.IdMapper; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.impl.OperationUtils; import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper; @@ -111,7 +110,6 @@ public class ExternalRefServletTest extends JerseyTest { private static final AccessValidations accessValidationsMock = Mockito.mock(AccessValidations.class); private static final ComponentLocker componentLocker = Mockito.mock(ComponentLocker.class); private static final HealingJanusGraphGenericDao janusGraphGenericDao = Mockito.mock(HealingJanusGraphGenericDao.class); - private static final ICacheMangerOperation cacheManagerOperation = Mockito.mock(ICacheMangerOperation.class); private static final IGraphLockOperation graphLockOperation = Mockito.mock(IGraphLockOperation.class); private static final String COMPONENT_ID = "ci-MyComponentName"; @@ -226,11 +224,6 @@ public class ExternalRefServletTest extends JerseyTest { } @Bean - ICacheMangerOperation cacheMangerOperation() { - return cacheManagerOperation; - } - - @Bean HealingJanusGraphDao janusGraphDao() { this.janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(),janusGraphClient()); return janusGraphDao; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java index fc7ee951b8..50cd2570b9 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/servlets/ArchiveEndpointTest.java @@ -82,7 +82,6 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperatio import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper; @@ -124,7 +123,6 @@ public class ArchiveEndpointTest extends JerseyTest { private static final HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); private static final HealingPipelineDao HEALING_PIPELINE_DAO = mock(HealingPipelineDao.class); - private static final ICacheMangerOperation cacheManagerOperation = mock(ICacheMangerOperation.class); private static GraphVertex serviceVertex; private static GraphVertex resourceVertex; private static GraphVertex resourceVertexVspArchived; @@ -206,11 +204,6 @@ public class ArchiveEndpointTest extends JerseyTest { } @Bean - ICacheMangerOperation cacheMangerOperation() { - return cacheManagerOperation; - } - - @Bean HealingJanusGraphDao janusGraphDao() { janusGraphDao = new HealingJanusGraphDao(healingPipelineDao(), janusGraphClient()); return janusGraphDao; diff --git a/catalog-be/src/test/resources/paths/path-context.xml b/catalog-be/src/test/resources/paths/path-context.xml index 8f6dea21a7..b0312f8b92 100644 --- a/catalog-be/src/test/resources/paths/path-context.xml +++ b/catalog-be/src/test/resources/paths/path-context.xml @@ -81,7 +81,6 @@ Modifications copyright (c) 2018 Nokia <bean id="asset-metadata-utils" class="org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter" /> <bean id="janusgraph-client" class="org.openecomp.sdc.be.components.path.beans.InMemoryJanusGraphClient" /> <bean class="org.openecomp.sdc.be.dao.config.DAOSpringConfig"/> - <bean id="component-cassandra-dao" class="org.openecomp.sdc.be.components.path.beans.ComponentCassandraDaoMock" /> <bean id="cassandra-client" class="org.openecomp.sdc.be.components.path.beans.CassandraClientMock" /> <bean id="audit-cassandra-dao" class="org.openecomp.sdc.be.components.path.beans.AuditCassandraDaoMock" /> <bean id="artifact-cassandra-dao" class="org.openecomp.sdc.be.components.path.beans.ArtifactCassandraDaoMock" /> diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCacheAccessor.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCacheAccessor.java deleted file mode 100644 index dcdd390485..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCacheAccessor.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============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.be.dao.cassandra; - -import com.datastax.driver.mapping.Result; -import com.datastax.driver.mapping.annotations.Accessor; -import com.datastax.driver.mapping.annotations.Param; -import com.datastax.driver.mapping.annotations.Query; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; - -import java.util.List; - -@Accessor -public interface ComponentCacheAccessor { - - @Query("SELECT * FROM sdccomponent.componentcache WHERE id IN :ids ALLOW FILTERING") - Result<ComponentCacheData> getComponents(@Param("ids") List<String> ids); - - @Query("SELECT * FROM sdccomponent.componentcache WHERE id = :id ALLOW FILTERING") - Result<ComponentCacheData> getComponent(@Param("id") String id); - - @Query("SELECT id,modification_time,type FROM sdccomponent.componentcache ALLOW FILTERING") - Result<ComponentCacheData> getAllComponentIdTimeAndType(); - - // @Query("SELECT * FROM sdcartifact.resources LIMIT 2000") - // Result<ESArtifactData> getListOfResources(); - - // Result<ESArtifactData> getListOfResources(List<String> dids); -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDao.java deleted file mode 100644 index 7b23a9db41..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDao.java +++ /dev/null @@ -1,254 +0,0 @@ -/*- - * ============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.be.dao.cassandra; - -import com.datastax.driver.core.Session; -import com.datastax.driver.mapping.MappingManager; -import com.datastax.driver.mapping.Result; -import fj.data.Either; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.config.BeEcompErrorManager; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; -import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; -import org.openecomp.sdc.common.log.wrappers.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.util.*; -import java.util.stream.Collectors; - -@Component("component-cassandra-dao") -public class ComponentCassandraDao extends CassandraDao { - - private static Logger logger = Logger.getLogger(ComponentCassandraDao.class.getName()); - - public final static Integer DEFAULT_FETCH_SIZE = 500; - - private ComponentCacheAccessor componentCacheAccessor; - - - @Autowired - public ComponentCassandraDao(CassandraClient cassandraClient) { - super(cassandraClient); - } - - @PostConstruct - public void init() { - String keyspace = AuditingTypesConstants.COMPONENT_KEYSPACE; - if (client.isConnected()) { - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> result = client.connect(keyspace); - if (result.isLeft()) { - session = result.left().value().left; - manager = result.left().value().right; - componentCacheAccessor = manager.createAccessor(ComponentCacheAccessor.class); - logger.info("** ComponentCassandraDao created"); - } else { - logger.info("** ComponentCassandraDao failed"); - throw new RuntimeException("Artifact keyspace [" + keyspace + "] failed to connect with error : " - + result.right().value()); - } - } else { - logger.info("** Cassandra client isn't connected"); - logger.info("** ComponentCassandraDao created, but not connected"); - } - } - - /** - * - * @param ids - * - list of components unique id - * @return - */ - public Either<List<ComponentCacheData>, ActionStatus> getComponents(List<String> ids) { - - List<ComponentCacheData> components = new ArrayList<>(); - if (ids == null || ids.isEmpty()) { - return Either.left(components); - } - - try { - - Result<ComponentCacheData> events = componentCacheAccessor.getComponents(ids); - if (events == null) { - logger.trace("not found component for ids list of in size {}", ids.size()); - return Either.left(components); - } - events.all().forEach(event -> { - components.add(event); - logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty()); - }); - - logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(), - components.size()); - - return Either.left(components); - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache"); - - logger.debug("failed to get components from cache", e); - return Either.right(ActionStatus.GENERAL_ERROR); - } - } - - public Either<List<ComponentCacheData>, ActionStatus> getAllComponentIdTimeAndType() { - try { - List<ComponentCacheData> components = new ArrayList<>(); - Result<ComponentCacheData> events = componentCacheAccessor.getAllComponentIdTimeAndType(); - if (events == null) { - logger.trace("no component found "); - return Either.left(components); - } - events.all().forEach(event -> { - components.add(event); - logger.trace("Fetch component uid = {} isDirty = {}", event.getId(), event.getIsDirty()); - }); - - logger.debug("Number of components fetched was {}.", components.size()); - - return Either.left(components); - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache"); - - logger.debug("failed to get components from cache", e); - return Either.right(ActionStatus.GENERAL_ERROR); - } - } - - /** - * - * @param id - * - component unique id - * @return - */ - public Either<ComponentCacheData, ActionStatus> getComponent(String id) { - - if (id == null) { - return Either.right(ActionStatus.INVALID_CONTENT); - } - - try { - - Result<ComponentCacheData> events = componentCacheAccessor.getComponent(id); - if (events == null) { - logger.trace("not found component for id {}", id); - return Either.right(ActionStatus.RESOURCE_NOT_FOUND); - } - - ComponentCacheData componentCacheData = events.one(); - if (componentCacheData != null) { - logger.debug("Component with id {} was found. isDirty={}.", componentCacheData.getId(), - componentCacheData.getIsDirty()); - } else { - return Either.right(ActionStatus.RESOURCE_NOT_FOUND); - } - return Either.left(componentCacheData); - - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentFromCache"); - - logger.trace("Failed to get component from cache", e); - return Either.right(ActionStatus.GENERAL_ERROR); - } - } - - public CassandraOperationStatus saveComponent(ComponentCacheData componentCacheData) { - return client.save(componentCacheData, ComponentCacheData.class, manager); - } - - /** - * the method checks if the given table is empty in the artifact keyspace - * - * @param tableName - * the name of the table we want to check - * @return true if the table is empty - */ - public Either<Boolean, CassandraOperationStatus> isTableEmpty(String tableName) { - return super.isTableEmpty(tableName); - } - - /** - * - * @param idToTimestampMap - * - list of components unique id - * @return - */ - public Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> getComponents( - Map<String, Long> idToTimestampMap) { - - List<ComponentCacheData> components = new ArrayList<>(); - Set<String> notFetchedFromCache = new HashSet<>(); - ImmutablePair<List<ComponentCacheData>, Set<String>> result = new ImmutablePair<>( - components, notFetchedFromCache); - - if (idToTimestampMap == null || idToTimestampMap.isEmpty()) { - return Either.left(result); - } - - try { - - Set<String> keySet = idToTimestampMap.keySet(); - List<String> ids = new ArrayList<>(); - ids.addAll(keySet); - Result<ComponentCacheData> events = componentCacheAccessor.getComponents(ids); - if (events == null) { - logger.trace("not found component for ids list of in size {}", ids.size()); - notFetchedFromCache.addAll(idToTimestampMap.keySet()); - return Either.left(result); - } - events.all().forEach(event -> { - long timeFromCache = event.getModificationTime().getTime(); - long timeRequested = idToTimestampMap.get(event.getId()); - if (timeFromCache == timeRequested) { - logger.trace("Fetch component uid = {} from cache", event.getId()); - components.add(event); - } else { - logger.trace( - "Fetch and ignore component uid = {} from cache. Time requested is {} while timestamp in cache is {}", - event.getId(), timeRequested, timeFromCache); - } - }); - - logger.debug("Number of components to fetch was {}. Actually, {} components fetched", ids.size(), - components.size()); - List<String> foundComponents = components.stream().map(ComponentCacheData::getId).collect(Collectors.toList()); - // fetch all ids which was not found in cache/found in cache and not - // updated. - Set<String> notFoundComponents = idToTimestampMap.keySet().stream() - .filter(p -> !foundComponents.contains(p)).collect(Collectors.toSet()); - - notFetchedFromCache.addAll(notFoundComponents); - - return Either.left(result); - } catch (Exception e) { - BeEcompErrorManager.getInstance().logBeDaoSystemError("GetComponentsFromCache"); - - logger.debug("failed to get components from cache", e); - return Either.right(ActionStatus.GENERAL_ERROR); - } - } - - public CassandraOperationStatus deleteComponent(String id) { - return client.delete(id, ComponentCacheData.class, manager); - } - -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java index 2e4a6329aa..b0209d2a13 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/Table.java @@ -40,7 +40,6 @@ public enum Table { GET_USERS_LIST_EVENT(new GetUsersListEventTableDesc()), GET_CATEGORY_HIERARCHY_EVENT(new GetCatHierEventTableDesc()), EXTERNAL_API_EVENT(new ExternalApiEventTableDesc()), - COMPONENT_CACHE(new ComponentCacheTableDescription()), SDC_SCHEMA_FILES(new SdcSchemaFilesTableDescription()), SDC_REPO(new MigrationTasksTableDescription()), SDC_OPERATIONAL_ENVIRONMENT(new OperationalEnvironmentsTableDescription()), diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescription.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescription.java deleted file mode 100644 index cb13e0ea15..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescription.java +++ /dev/null @@ -1,97 +0,0 @@ -/*- - * ============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.be.dao.cassandra.schema.tables; - -import com.datastax.driver.core.DataType; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.dao.cassandra.schema.ITableDescription; -import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class ComponentCacheTableDescription implements ITableDescription { - - @Override - public List<ImmutablePair<String, DataType>> primaryKeys() { - List<ImmutablePair<String, DataType>> keys = new ArrayList<>(); - keys.add(new ImmutablePair<>(ID_FIELD, DataType.varchar())); - return keys; - } - - @Override - public Map<String, ImmutablePair<DataType, Boolean>> getColumnDescription() { - Map<String, ImmutablePair<DataType, Boolean>> columns = new HashMap<>(); - - for (PartialComponentFieldsDescription field : PartialComponentFieldsDescription.values()) { - columns.put(field.getName(), new ImmutablePair<>(field.type, field.indexed)); - } - - return columns; - } - - @Override - public String getKeyspace() { - return AuditingTypesConstants.COMPONENT_KEYSPACE; - } - - @Override - public String getTableName() { - return "componentcache"; - } - - enum PartialComponentFieldsDescription { - DATA("data", DataType.blob(), false), - MODIFICATION_TIME("modification_time", DataType.timestamp(), false), - TYPE("type", DataType.varchar(), false), - IS_DIRTY("is_dirty", DataType.cboolean(), false), - IS_ZIPPED("is_zipped", DataType.cboolean(), false),; - - private String name; - private DataType type; - private boolean indexed; - - PartialComponentFieldsDescription(String name, DataType type, boolean indexed) { - this.name = name; - this.type = type; - this.indexed = indexed; - } - - public String getName() { - return name; - } - - public DataType getType() { - return type; - } - - public boolean isIndexed() { - return indexed; - } - } - - @Override - public List<ImmutablePair<String, DataType>> clusteringKeys() { - return null; - } -} diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/ComponentCacheData.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/ComponentCacheData.java deleted file mode 100644 index cb7af866be..0000000000 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/resources/data/ComponentCacheData.java +++ /dev/null @@ -1,153 +0,0 @@ -/*- - * ============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.be.resources.data; - -import com.datastax.driver.mapping.annotations.Column; -import com.datastax.driver.mapping.annotations.PartitionKey; -import com.datastax.driver.mapping.annotations.Table; - -import java.nio.ByteBuffer; -import java.util.Date; - -@Table(keyspace = "sdccomponent", name = "componentcache") -public class ComponentCacheData { - public final static String RRESOURCE_ID_FIELD = "resourceId"; - - public final static String SERVICE_NAME_FIELD = "serviceName"; - public final static String SERVICE_VERSION_FIELD = "serviceVersion"; - public final static String ARTIFACT_NAME_FIELD = "artifactName"; - - public final static String delim = ":"; - - @PartitionKey - @Column(name = "id") - private String id; - - @Column - private ByteBuffer data; - - @Column(name = "modification_time") - private Date modificationTime; - - @Column - private String type; - - @Column(name = "is_dirty") - private boolean isDirty; - - @Column(name = "is_zipped") - private boolean isZipped; - - public ComponentCacheData() { - - } - - public ComponentCacheData(String id, byte[] data, Date modificationTime, String type, boolean isDirty, - boolean isZipped) { - super(); - this.id = id; - if (data != null) { - this.data = ByteBuffer.wrap(data.clone()); - } - this.modificationTime = modificationTime; - this.type = type; - this.isDirty = isDirty; - this.isZipped = isZipped; - } - - public ComponentCacheData(String id) { - - this.id = id; - } - - public ComponentCacheData(String artifactId, byte[] data) { - super(); - this.id = artifactId; - if (data != null) { - this.data = ByteBuffer.wrap(data.clone()); - // this.data = data.clone(); - } - } - - public byte[] getDataAsArray() { - if (data != null) { - return data.array(); - } - return null; - } - - public void setDataAsArray(byte[] data) { - if (data != null) { - this.data = ByteBuffer.wrap(data.clone()); - } - } - - public ByteBuffer getData() { - return data; - } - - public void setData(ByteBuffer data) { - if (data != null) { - this.data = data.duplicate(); - } - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public Date getModificationTime() { - return modificationTime; - } - - public void setModificationTime(Date modificationTime) { - this.modificationTime = modificationTime; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public boolean getIsDirty() { - return isDirty; - } - - public void setIsDirty(boolean isDirty) { - this.isDirty = isDirty; - } - - public boolean getIsZipped() { - return isZipped; - } - - public void setIsZipped(boolean isZipped) { - this.isZipped = isZipped; - } - -} diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDaoTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDaoTest.java deleted file mode 100644 index 564ee5ae08..0000000000 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/ComponentCassandraDaoTest.java +++ /dev/null @@ -1,409 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.dao.cassandra; - -import com.datastax.driver.core.Session; -import com.datastax.driver.mapping.MappingManager; -import com.datastax.driver.mapping.Result; -import fj.data.Either; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; - -import java.util.*; - -import static org.junit.Assert.assertTrue; - -public class ComponentCassandraDaoTest { - - @InjectMocks - ComponentCassandraDao testSubject; - - @Mock - CassandraClient clientMock; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testInit() throws Exception { - - // default test - testSubject.init(); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - testSubject.init(); - } - - @Test - public void testInitException() throws Exception { - - // default test - testSubject.init(); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either - .right(CassandraOperationStatus.CLUSTER_NOT_CONNECTED); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - try { - testSubject.init(); - } catch (Exception e) { - assertTrue(e.getClass() == RuntimeException.class); - } - } - - @Test - public void testGetComponents() throws Exception { - List<String> ids; - Either<List<ComponentCacheData>, ActionStatus> result; - - // test 1 - ids = null; - result = testSubject.getComponents(ids); - - - // test 2 - ids = new LinkedList<>(); - result = testSubject.getComponents(ids); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Result<ComponentCacheData> value2 = Mockito.mock(Result.class); - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any())).thenReturn(value2); - List<ComponentCacheData> value3 = new LinkedList<>(); - value3.add(new ComponentCacheData("mock")); - Mockito.when(value2.all()).thenReturn(value3); - testSubject.init(); - - ids.add("mock"); - testSubject.getComponents(ids); - } - - @Test - public void testGetComponentsNull() throws Exception { - List<String> ids = new LinkedList<>(); - Either<List<ComponentCacheData>, ActionStatus> result; - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any())).thenReturn(null); - testSubject.init(); - - ids.add("mock"); - testSubject.getComponents(ids); - } - - @Test - public void testGetComponentsException() throws Exception { - List<String> ids = new LinkedList<>(); - Either<List<ComponentCacheData>, ActionStatus> result; - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any())).thenThrow(RuntimeException.class); - testSubject.init(); - - ids.add("mock"); - testSubject.getComponents(ids); - } - - @Test - public void testGetAllComponentIdTimeAndType() throws Exception { - Either<List<ComponentCacheData>, ActionStatus> result; - - // default test - result = testSubject.getAllComponentIdTimeAndType(); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Result<ComponentCacheData> value2 = Mockito.mock(Result.class); - Mockito.when(componentCacheAccessorMock.getAllComponentIdTimeAndType()).thenReturn(value2); - List<ComponentCacheData> value3 = new LinkedList<>(); - value3.add(new ComponentCacheData("mock")); - Mockito.when(value2.all()).thenReturn(value3); - testSubject.init(); - - testSubject.getAllComponentIdTimeAndType(); - } - - @Test - public void testGetAllComponentIdTimeAndTypeNull() throws Exception { - Either<List<ComponentCacheData>, ActionStatus> result; - - // default test - result = testSubject.getAllComponentIdTimeAndType(); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getAllComponentIdTimeAndType()).thenReturn(null); - testSubject.init(); - - result = testSubject.getAllComponentIdTimeAndType(); - } - - @Test - public void testGetComponent() throws Exception { - String id = ""; - Either<ComponentCacheData, ActionStatus> result; - - // test 1 - id = null; - result = testSubject.getComponent(id); - //Assert.assertEquals(null, result); - - // test 2 - id = ""; - result = testSubject.getComponent(id); - //Assert.assertEquals(null, result); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Result<ComponentCacheData> value2 = Mockito.mock(Result.class); - Mockito.when(componentCacheAccessorMock.getComponent(Mockito.any())).thenReturn(value2); - ComponentCacheData value3 = new ComponentCacheData(); - Mockito.when(value2.one()).thenReturn(value3); - testSubject.init(); - - result = testSubject.getComponent(id); - } - - @Test - public void testGetComponentNull1() throws Exception { - String id = ""; - Either<ComponentCacheData, ActionStatus> result; - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getComponent(Mockito.any())).thenReturn(null); - testSubject.init(); - - result = testSubject.getComponent(id); - } - - @Test - public void testGetComponentNull2() throws Exception { - String id = ""; - Either<ComponentCacheData, ActionStatus> result; - - // test 1 - id = null; - result = testSubject.getComponent(id); - //Assert.assertEquals(null, result); - - // test 2 - id = ""; - result = testSubject.getComponent(id); - //Assert.assertEquals(null, result); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Result<ComponentCacheData> value2 = Mockito.mock(Result.class); - Mockito.when(componentCacheAccessorMock.getComponent(Mockito.any())).thenReturn(value2); - Mockito.when(value2.one()).thenReturn(null); - testSubject.init(); - - result = testSubject.getComponent(id); - } - - @Test - public void testSaveComponent() throws Exception { - ComponentCacheData componentCacheData = null; - CassandraOperationStatus result; - - // default test - result = testSubject.saveComponent(componentCacheData); - } - - @Test - public void testIsTableEmpty() throws Exception { - String tableName = ""; - Either<Boolean, CassandraOperationStatus> result; - - // default test - result = testSubject.isTableEmpty(tableName); - } - - @Test - public void testGetComponents_1() throws Exception { - Map<String, Long> idToTimestampMap = null; - Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> result; - - // test 1 - idToTimestampMap = null; - result = testSubject.getComponents(idToTimestampMap); - //Assert.assertEquals(null, result); - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Result<ComponentCacheData> value2 = Mockito.mock(Result.class); - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any(List.class))).thenReturn(value2); - List<ComponentCacheData> value3 = new LinkedList<>(); - ComponentCacheData e = new ComponentCacheData("mock"); - Mockito.when(value2.all()).thenReturn(value3); - testSubject.init(); - - idToTimestampMap = new HashMap<>(); - idToTimestampMap.put("mock", 0L); - e.setModificationTime(new Date()); - value3.add(e); - result = testSubject.getComponents(idToTimestampMap); - } - - @Test - public void testGetComponents_1Null() throws Exception { - Map<String, Long> idToTimestampMap = null; - Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> result; - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any(List.class))).thenReturn(null); - testSubject.init(); - - idToTimestampMap = new HashMap<>(); - idToTimestampMap.put("mock", 0L); - result = testSubject.getComponents(idToTimestampMap); - } - - @Test - public void testGetComponents_1Exception() throws Exception { - Map<String, Long> idToTimestampMap = null; - Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> result; - - Session sessMock = Mockito.mock(Session.class); - MappingManager mappMock = Mockito.mock(MappingManager.class); - ComponentCacheAccessor componentCacheAccessorMock = Mockito.mock(ComponentCacheAccessor.class); - ImmutablePair<Session, MappingManager> ipMock = ImmutablePair.of(sessMock, mappMock); - - Mockito.when(clientMock.isConnected()).thenReturn(true); - Either<ImmutablePair<Session, MappingManager>, CassandraOperationStatus> value = Either.left(ipMock); - Mockito.when(clientMock.connect(Mockito.any())).thenReturn(value); - Mockito.when(mappMock.createAccessor(ComponentCacheAccessor.class)).thenReturn(componentCacheAccessorMock); - - Mockito.when(componentCacheAccessorMock.getComponents(Mockito.any(List.class))).thenThrow(RuntimeException.class); - testSubject.init(); - - idToTimestampMap = new HashMap<>(); - idToTimestampMap.put("mock", 0L); - result = testSubject.getComponents(idToTimestampMap); - } - - @Test - public void testDeleteComponent() throws Exception { - String id = ""; - CassandraOperationStatus result; - - // default test - result = testSubject.deleteComponent(id); - } -} diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescriptionTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescriptionTest.java deleted file mode 100644 index 4968bb6603..0000000000 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/tables/ComponentCacheTableDescriptionTest.java +++ /dev/null @@ -1,92 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.dao.cassandra.schema.tables; - -import java.util.List; -import java.util.Map; - -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.junit.Test; - -import com.datastax.driver.core.DataType; - - -public class ComponentCacheTableDescriptionTest { - - private ComponentCacheTableDescription createTestSubject() { - return new ComponentCacheTableDescription(); - } - - - @Test - public void testPrimaryKeys() throws Exception { - ComponentCacheTableDescription testSubject; - List<ImmutablePair<String, DataType>> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.primaryKeys(); - } - - - @Test - public void testGetColumnDescription() throws Exception { - ComponentCacheTableDescription testSubject; - Map<String, ImmutablePair<DataType, Boolean>> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getColumnDescription(); - } - - - @Test - public void testGetKeyspace() throws Exception { - ComponentCacheTableDescription testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getKeyspace(); - } - - - @Test - public void testGetTableName() throws Exception { - ComponentCacheTableDescription testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getTableName(); - } - - - @Test - public void testClusteringKeys() throws Exception { - ComponentCacheTableDescription testSubject; - List<ImmutablePair<String, DataType>> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.clusteringKeys(); - } -} diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/ComponentCacheDataTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/ComponentCacheDataTest.java deleted file mode 100644 index 54bfa502b5..0000000000 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/resources/data/ComponentCacheDataTest.java +++ /dev/null @@ -1,197 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.resources.data; - -import org.junit.Test; - -import java.nio.ByteBuffer; -import java.util.Date; - - -public class ComponentCacheDataTest { - - private ComponentCacheData createTestSubject() { - return new ComponentCacheData(); - } - - @Test - public void testCtor() throws Exception { - new ComponentCacheData("mock"); - new ComponentCacheData("mock", new byte[0]); - new ComponentCacheData("mock", new byte[0], new Date(), "mock", true, true); - } - - @Test - public void testGetDataAsArray() throws Exception { - ComponentCacheData testSubject; - byte[] result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDataAsArray(); - } - - - @Test - public void testSetDataAsArray() throws Exception { - ComponentCacheData testSubject; - byte[] data = new byte[] { ' ' }; - - // test 1 - testSubject = createTestSubject(); - data = null; - testSubject.setDataAsArray(data); - - // test 2 - testSubject = createTestSubject(); - data = new byte[] { ' ' }; - testSubject.setDataAsArray(data); - } - - - @Test - public void testGetData() throws Exception { - ComponentCacheData testSubject ; - - testSubject = createTestSubject(); - - testSubject.getData(); - } - - @Test - public void testSetData() throws Exception { - ComponentCacheData testSubject ; - - testSubject = createTestSubject(); - - ByteBuffer data = ByteBuffer.allocate(0); - testSubject.setData(data); - } - - @Test - public void testGetId() throws Exception { - ComponentCacheData testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getId(); - } - - - @Test - public void testSetId() throws Exception { - ComponentCacheData testSubject; - String id = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setId(id); - } - - - @Test - public void testGetModificationTime() throws Exception { - ComponentCacheData testSubject; - Date result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getModificationTime(); - } - - - @Test - public void testSetModificationTime() throws Exception { - ComponentCacheData testSubject; - Date modificationTime = null; - - // default test - testSubject = createTestSubject(); - testSubject.setModificationTime(modificationTime); - } - - - @Test - public void testGetType() throws Exception { - ComponentCacheData testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getType(); - } - - - @Test - public void testSetType() throws Exception { - ComponentCacheData testSubject; - String type = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setType(type); - } - - - @Test - public void testGetIsDirty() throws Exception { - ComponentCacheData testSubject; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getIsDirty(); - } - - - @Test - public void testSetIsDirty() throws Exception { - ComponentCacheData testSubject; - boolean isDirty = false; - - // default test - testSubject = createTestSubject(); - testSubject.setIsDirty(isDirty); - } - - - @Test - public void testGetIsZipped() throws Exception { - ComponentCacheData testSubject; - boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getIsZipped(); - } - - - @Test - public void testSetIsZipped() throws Exception { - ComponentCacheData testSubject; - boolean isZipped = false; - - // default test - testSubject = createTestSubject(); - testSubject.setIsZipped(isZipped); - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ComponentCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ComponentCache.java deleted file mode 100644 index 0fea33e8e5..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ComponentCache.java +++ /dev/null @@ -1,910 +0,0 @@ -/*- - * ============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.be.model.cache; - -import fj.data.Either; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.commons.lang3.tuple.ImmutableTriple; -import org.openecomp.sdc.be.config.BeEcompErrorManager; -import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; -import org.openecomp.sdc.be.config.Configuration; -import org.openecomp.sdc.be.config.Configuration.ApplicationL1CacheCatalogInfo; -import org.openecomp.sdc.be.config.Configuration.ApplicationL2CacheConfig; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; -import org.openecomp.sdc.be.dao.cassandra.ComponentCassandraDao; -import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.*; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; -import org.openecomp.sdc.common.log.wrappers.Logger; -import org.openecomp.sdc.common.util.SerializationUtils; -import org.openecomp.sdc.common.util.ZipUtil; -import org.springframework.beans.factory.annotation.Autowired; - -import javax.annotation.PostConstruct; -import java.io.IOException; -import java.util.*; -import java.util.concurrent.locks.Lock; -import java.util.concurrent.locks.ReentrantReadWriteLock; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.List; -import java.util.Map; -import java.util.Set; - -@org.springframework.stereotype.Component("component-cache") -public class ComponentCache { - - private static final String COMPONENT_CACHE_IS_DISABLED = "Component Cache is disabled"; - - private static final Logger log = Logger.getLogger(ComponentCache.class); - - @Autowired - ComponentCassandraDao componentCassandraDao; - - @Autowired - ToscaOperationFacade toscaOperationFacade; - - private Map<ComponentTypeEnum, Map<String, Component>> catalogInMemoryCache = new HashMap<>(); - private final ReentrantReadWriteLock rwCatalogLock = new ReentrantReadWriteLock(); - private final Lock rCatalogLock = rwCatalogLock.readLock(); - private final Lock wCatalogLock = rwCatalogLock.writeLock(); - - boolean enabled = true; - int catalogInMemorySizePerResource = 300; - int catalogInMemorySizePerService = 200; - int catalogInMemorySizePerProduct = 100; - boolean catalogInMemoryEnabled = true; - Map<ComponentTypeEnum, Integer> limitMemoryCatalogSizePerType = new HashMap<>(); - - @PostConstruct - public void init() { - - Configuration configuration = ConfigurationManager.getConfigurationManager().getConfiguration(); - if (configuration != null) { - ApplicationL2CacheConfig applicationL2Cache = configuration.getApplicationL2Cache(); - if (applicationL2Cache != null) { - this.enabled = applicationL2Cache.isEnabled(); - - ApplicationL1CacheCatalogInfo catalog = applicationL2Cache.getCatalogL1Cache(); - if (catalog != null) { - catalogInMemoryEnabled = catalog.getEnabled(); - catalogInMemorySizePerResource = catalog.getResourcesSizeInCache(); - catalogInMemorySizePerService = catalog.getServicesSizeInCache(); - catalogInMemorySizePerProduct = catalog.getProductsSizeInCache(); - } - } - } - - ComponentTypeEnum[] typesForCache = { ComponentTypeEnum.RESOURCE, ComponentTypeEnum.SERVICE, - ComponentTypeEnum.PRODUCT }; - for (ComponentTypeEnum typeEnum : typesForCache) { - Map<String, Component> map = new HashMap<>(); - catalogInMemoryCache.put(typeEnum, map); - } - - limitMemoryCatalogSizePerType.put(ComponentTypeEnum.RESOURCE, catalogInMemorySizePerResource); - limitMemoryCatalogSizePerType.put(ComponentTypeEnum.SERVICE, catalogInMemorySizePerService); - limitMemoryCatalogSizePerType.put(ComponentTypeEnum.PRODUCT, catalogInMemorySizePerProduct); - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public Either<Component, ActionStatus> getComponent(String componentUid, Long lastModificationTime, - Function<Component, Component> filterFieldsFunc) { - - Either<ImmutablePair<Component, ComponentCacheData>, ActionStatus> componentFromCache = getComponentFromCache( - componentUid, lastModificationTime, filterFieldsFunc); - - if (componentFromCache.isRight()) { - return Either.right(componentFromCache.right().value()); - } - - return Either.left(componentFromCache.left().value().left); - - } - - public Either<List<ComponentCacheData>, ActionStatus> getAllComponentIdTimeAndType() { - if (!isEnabled()) { - return Either.right(ActionStatus.NOT_ALLOWED); - } - - return componentCassandraDao - .getAllComponentIdTimeAndType(); - - } - - /** - * get components for catalog - * - * @param components - * @param componentTypeEnum - * @return - */ - @Deprecated - public Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> getComponentsForCatalog( - Set<String> components, ComponentTypeEnum componentTypeEnum) { - - if (!isEnabled()) { - log.debug("In getComponentsForCatalog for type {}. Cache is disabled.", - componentTypeEnum.name().toLowerCase()); - return Either.right(ActionStatus.NOT_ALLOWED); - } - log.debug("In getComponentsForCatalog for type {}", componentTypeEnum.name().toLowerCase()); - - Function<List<Component>, List<Component>> filterFieldsFunc = this::filterForCatalog; - - Set<String> leftComponentsForSearch = new HashSet<>(); - leftComponentsForSearch.addAll(components); - - // get components from inmemory cache - List<Component> componentsFromMemory = null; - if (catalogInMemoryEnabled) { - componentsFromMemory = getDataFromInMemoryCache(components, componentTypeEnum); - log.debug("The number of components of type {} fetched from memory is {}", - componentTypeEnum.name().toLowerCase(), - componentsFromMemory == null ? 0 : componentsFromMemory.size()); - if (componentsFromMemory != null) { - componentsFromMemory.forEach(p -> leftComponentsForSearch.remove(p.getUniqueId())); - } - } else { - log.debug("Catalog InMemory cache is disabled"); - } - - log.debug("Number of components from type {} needed to fetch is {}", componentTypeEnum.name().toLowerCase(), - leftComponentsForSearch.size()); - - // get components from cassandra cache and filter each component - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result = getComponents( - leftComponentsForSearch, filterFieldsFunc); - - if (result.isLeft()) { - // add inmemory components to the valid components(not dirty) - List<Component> foundComponents = result.left().value().getLeft(); - if (componentsFromMemory != null) { - foundComponents.addAll(componentsFromMemory); - } - if (catalogInMemoryEnabled) { - updateCatalogInMemoryCacheWithCertified(foundComponents, componentTypeEnum); - } - } - - return result; - } - - /** - * @param foundComponents - * @param componentTypeEnum - */ - private void updateCatalogInMemoryCacheWithCertified(List<Component> foundComponents, - ComponentTypeEnum componentTypeEnum) { - - try { - wCatalogLock.lock(); - - long start = System.currentTimeMillis(); - Map<String, Component> map = catalogInMemoryCache.get(componentTypeEnum); - int mapSizeBefore = map.size(); - map.clear(); - Map<String, Component> collect = foundComponents.stream() - .filter(p -> p.getLifecycleState() == LifecycleStateEnum.CERTIFIED) - .limit(limitMemoryCatalogSizePerType.get(componentTypeEnum)) - .collect(Collectors.toMap(Component::getUniqueId, p -> p)); - map.putAll(collect); - log.debug( - "Size of in memory cache for catalog {}(certified only): Before {}, After {}. Replacement Time is {} ms.", - componentTypeEnum.name().toLowerCase(), mapSizeBefore, map.size(), - System.currentTimeMillis() - start); - } finally { - wCatalogLock.unlock(); - } - - } - - private List<Component> getDataFromInMemoryCache(Set<String> components, ComponentTypeEnum componentTypeEnum) { - List<Component> foundComponents = new ArrayList<>(); - - try { - - rCatalogLock.lock(); - - Map<String, Component> map = catalogInMemoryCache.get(componentTypeEnum); - for (String compUid : components) { - Component component = map.get(compUid); - if (component != null) { - foundComponents.add(component); - } - } - - } finally { - rCatalogLock.unlock(); - } - - return foundComponents; - } - - /** - * - * get full components from cassandra. On each component apply filter - * function in order to remove unused members - * - * @param components - * @param filterFieldsFunc - * @return <found components, found dirty components, not found components - * list> or Error - */ - public Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> getComponents( - Set<String> components, Function<List<Component>, List<Component>> filterFieldsFunc) { - - if (!isEnabled()) { - log.debug(COMPONENT_CACHE_IS_DISABLED); - return Either.right(ActionStatus.NOT_ALLOWED); - } - - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> componentsFull = getComponentsFull( - components); - - if (componentsFull.isRight()) { - return Either.right(componentsFull.right().value()); - } - - ImmutableTriple<List<Component>, List<Component>, Set<String>> immutableTriple = componentsFull.left().value(); - List<Component> foundResources = immutableTriple.left; - List<Component> foundDirtyResources = immutableTriple.middle; - Set<String> notFoundResources = immutableTriple.right; - - List<Component> filterdFoundResources = filterFieldsFunc.apply(foundResources); - List<Component> filterdFoundDirtyResources = filterFieldsFunc.apply(foundDirtyResources); - - ImmutableTriple<List<Component>, List<Component>, Set<String>> result = new ImmutableTriple<>( - filterdFoundResources, filterdFoundDirtyResources, notFoundResources); - - return Either.left(result); - - } - - public Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> getComponentsForLeftPanel( - ComponentTypeEnum componentTypeEnum, String internalComponentType, Set<String> filteredResources) { - - log.debug("In getComponentsForLeftPanel componentTypeEnum = {}, internalComponentType = {}", - componentTypeEnum, internalComponentType); - - Function<List<Component>, List<Component>> filterFieldsFunc = this::filterForLeftPanel; - - return getComponents(filteredResources, filterFieldsFunc); - - } - - private List<Component> filterForLeftPanel(List<Component> components) { - - List<Component> result = new ArrayList<>(); - if (components != null) { - components.forEach(p -> result.add(filterFieldsForLeftPanel(p))); - } - - return result; - } - - private List<Component> filterForCatalog(List<Component> components) { - - List<Component> result = new ArrayList<>(); - if (components != null) { - components.forEach(p -> result.add(filterFieldsForCatalog(p))); - } - - return result; - } - - private Component filterFieldsForLeftPanel(Component component) { - - Component result = null; - ComponentTypeEnum componentTypeEnum = component.getComponentType(); - switch (componentTypeEnum) { - case RESOURCE: - result = new Resource(); - copyFieldsForLeftPanel(component, result); - break; - case SERVICE: - result = new Service(); - copyFieldsForLeftPanel(component, result); - break; - default: - break; - } - - return result; - } - - private Component filterFieldsForCatalog(Component component) { - - Component result = null; - ComponentTypeEnum componentTypeEnum = component.getComponentType(); - switch (componentTypeEnum) { - case RESOURCE: - result = new Resource(); - copyFieldsForCatalog(component, result); - break; - case SERVICE: - result = new Service(); - copyFieldsForCatalog(component, result); - break; - case PRODUCT: - result = new Product(); - copyFieldsForCatalog(component, result); - break; - default: - break; - } - - return result; - } - - /** - * Copy relevant fields to the filtered component for left panel - * - * @param component - * @param filteredComponent - */ - private void copyFieldsForLeftPanel(Component component, Component filteredComponent) { - - ComponentTypeEnum componentTypeEnum = component.getComponentType(); - filteredComponent.setCategories(component.getCategories()); - filteredComponent.setComponentType(component.getComponentType()); - if (ComponentTypeEnum.RESOURCE.equals(component.getComponentType()) - && ResourceTypeEnum.VL.equals(((ResourceMetadataDataDefinition) component - .getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType())) { - filteredComponent.setCapabilities(component.getCapabilities()); - filteredComponent.setRequirements(component.getRequirements()); - } - filteredComponent.setVersion(component.getVersion()); - filteredComponent.setDescription(component.getDescription()); - filteredComponent.setUniqueId(component.getUniqueId()); - filteredComponent.setIcon(component.getIcon()); - filteredComponent.setTags(component.getTags()); - filteredComponent.setLifecycleState(component.getLifecycleState()); - filteredComponent.setInvariantUUID(component.getInvariantUUID()); - filteredComponent.setUUID(component.getUUID()); - filteredComponent.setSystemName(component.getSystemName()); - filteredComponent.setName(component.getName()); - - if (componentTypeEnum == ComponentTypeEnum.RESOURCE) { - Resource resource = (Resource) component; - Resource filteredResource = (Resource) filteredComponent; - filteredResource.setToscaResourceName(resource.getToscaResourceName()); - filteredResource.setResourceType(resource.getResourceType()); - } - } - - private void copyFieldsForCatalog(Component component, Component filteredComponent) { - - ComponentTypeEnum componentTypeEnum = component.getComponentType(); - filteredComponent.setCategories(component.getCategories()); - filteredComponent.setComponentType(component.getComponentType()); - filteredComponent.setVersion(component.getVersion()); - filteredComponent.setDescription(component.getDescription()); - filteredComponent.setUniqueId(component.getUniqueId()); - filteredComponent.setIcon(component.getIcon()); - filteredComponent.setTags(component.getTags()); - filteredComponent.setLifecycleState(component.getLifecycleState()); - filteredComponent.setSystemName(component.getSystemName()); - filteredComponent.setName(component.getName()); - filteredComponent.setLastUpdateDate(component.getLastUpdateDate()); - - if (componentTypeEnum == ComponentTypeEnum.RESOURCE) { - Resource resource = (Resource) component; - Resource filteredResource = (Resource) filteredComponent; - filteredResource.setToscaResourceName(resource.getToscaResourceName()); - filteredResource.setResourceType(resource.getResourceType()); - } else if (componentTypeEnum == ComponentTypeEnum.SERVICE) { - Service service = (Service) component; - Service filteredService = (Service) filteredComponent; - filteredService.setDistributionStatus(service.getDistributionStatus()); - } - } - - /** - * get components from cache of a given list ou unique ids. - * - * for each component data from cassandra, unzip the data if needed and - * deserialize the unzipped data to java object(Component). - * - * @param filteredResources - * @return ImmutableTripple or ActionStatus. | |-- components |-- dirty - * components - components with dirty flag = true. |-- set of non - * cached components - * - */ - private Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> getComponentsFull( - Set<String> filteredResources) { - - if (!isEnabled()) { - log.debug(COMPONENT_CACHE_IS_DISABLED); - return Either.right(ActionStatus.NOT_ALLOWED); - } - - List<Component> foundResources = new LinkedList<>(); - List<Component> foundDirtyResources = new LinkedList<>(); - Set<String> notFoundResources = new HashSet<>(); - ImmutableTriple<List<Component>, List<Component>, Set<String>> result = new ImmutableTriple<>( - foundResources, foundDirtyResources, notFoundResources); - - long cassandraFetchStart = System.currentTimeMillis(); - List<String> uidsList = new ArrayList<>(); - uidsList.addAll(filteredResources); - Either<List<ComponentCacheData>, ActionStatus> componentsFromCache = componentCassandraDao - .getComponents(uidsList); - - long cassandraFetchEnd = System.currentTimeMillis(); - log.debug("Fetch time from cassandara of all components took {} ms", - (cassandraFetchEnd - cassandraFetchStart)); - if (componentsFromCache.isRight()) { - BeEcompErrorManager.getInstance().logInternalFlowError("FetchFromCache", - "Failed to fetch components from cache", ErrorSeverity.ERROR); - return Either.right(componentsFromCache.right().value()); - } - - List<ComponentCacheData> list = componentsFromCache.left().value(); - log.debug("Number of components fetched from cassandra is {}", (list == null ? 0 : list.size())); - if (list != null && !list.isEmpty()) { - - List<ComponentCacheData> filteredData = list.stream().filter(p -> filteredResources.contains(p.getId())) - .collect(Collectors.toList()); - log.debug("Number of components filterd is {}", filteredData == null ? 0 : filteredData.size()); - - if (filteredData != null) { - long desStart = System.currentTimeMillis(); - - for (ComponentCacheData componentCacheData : filteredData) { - - log.debug("Process uid {} from cache", componentCacheData.getId()); - - String compUid = componentCacheData.getId(); - - Either<? extends Component, Boolean> deserializeExt = convertComponentCacheToComponent( - componentCacheData); - - if (deserializeExt.isLeft()) { - Component component = deserializeExt.left().value(); - if (!componentCacheData.getIsDirty()) { - foundResources.add(component); - } else { - foundDirtyResources.add(component); - } - } else { - notFoundResources.add(compUid); - } - - } - long desEnd = System.currentTimeMillis(); - log.debug("Deserialization and unzip of {} components took {} ms", filteredData.size(), - (desEnd - desStart)); - } - } - List<String> foundResourcesUid = foundResources.stream().map(Component::getUniqueId).collect(Collectors.toList()); - List<String> foundDirtyResourcesUid = foundDirtyResources.stream().map(Component::getUniqueId) - .collect(Collectors.toList()); - log.debug("Number of processed components from cache is {}", - (foundResourcesUid.size() + foundDirtyResourcesUid.size())); - Set<String> notCachedResources = filteredResources.stream() - .filter(p -> !foundResourcesUid.contains(p) && !foundDirtyResourcesUid.contains(p)) - .collect(Collectors.toSet()); - notFoundResources.addAll(notCachedResources); - - if (log.isDebugEnabled()) { - log.debug("Number of components fetched is {}", foundResources.size()); - log.debug("Number of components fetched dirty is {}", foundDirtyResources.size()); - log.debug("Number of components non cached is {}", notCachedResources.size()); - } - - return Either.left(result); - } - - private Either<? extends Component, Boolean> convertComponentCacheToComponent( - ComponentCacheData componentCacheData) { - - String compUid = componentCacheData.getId(); - - byte[] dataAsArray = componentCacheData.getDataAsArray(); - - if (componentCacheData.getIsZipped()) { - long startUnzip = System.nanoTime(); - dataAsArray = ZipUtil.unzip(dataAsArray); - long endUnzip = System.nanoTime(); - log.trace("Unzip component {} took {} microsecond", compUid, (endUnzip - startUnzip) / 1000); - } - - long startDes = System.nanoTime(); - - Either<? extends Component, Boolean> deserializeExt = deserializeComponent(componentCacheData, dataAsArray); - - long endDes = System.nanoTime(); - log.trace("Deserialize component {} took {} microsecond", compUid, (endDes - startDes) / 1000); - return deserializeExt; - } - - private Either<? extends Component, Boolean> deserializeComponent(ComponentCacheData componentCacheData, - byte[] dataAsArray) { - String type = componentCacheData.getType(); - NodeTypeEnum typeEnum = NodeTypeEnum.getByNameIgnoreCase(type); - - Either<? extends Component, Boolean> deserializeExt = Either.right(false); - switch (typeEnum) { - case Resource: - deserializeExt = SerializationUtils.deserializeExt(dataAsArray, Resource.class, componentCacheData.getId()); - break; - case Service: - deserializeExt = SerializationUtils.deserializeExt(dataAsArray, Service.class, componentCacheData.getId()); - break; - case Product: - deserializeExt = SerializationUtils.deserializeExt(dataAsArray, Product.class, componentCacheData.getId()); - break; - default: - break; - } - return deserializeExt; - } - - public Either<Component, ActionStatus> getComponent(String componentUid) { - - return getComponent(componentUid, null, Function.identity()); - - } - - private boolean saveComponent(String componentUid, Long lastModificationTime, NodeTypeEnum nodeTypeEnum, - Component component) { - - log.trace("Going to save component {} of type {} in cache", componentUid, nodeTypeEnum.name().toLowerCase()); - - boolean result = false; - - Either<byte[], Boolean> serializeExt = SerializationUtils.serializeExt(component); - if (serializeExt.isLeft()) { - byte[] serializedData = serializeExt.left().value(); - byte[] zipBytes; - try { - zipBytes = ZipUtil.zipBytes(serializedData); - ComponentCacheData componentCacheData = new ComponentCacheData(); - componentCacheData.setDataAsArray(zipBytes); - componentCacheData.setIsZipped(true); - componentCacheData.setId(componentUid); - componentCacheData.setModificationTime(new Date(lastModificationTime)); - componentCacheData.setType(component.getComponentType().name().toLowerCase()); - - CassandraOperationStatus status = componentCassandraDao.saveComponent(componentCacheData); - - if (status == CassandraOperationStatus.OK) { - result = true; - } - - } catch (IOException e) { - log.debug("Failed to prepare component {} of type {} for cache", componentUid, - nodeTypeEnum.name().toLowerCase()); - if (log.isTraceEnabled()) { - log.trace("Failed to prepare component {} of type {} for cache",componentUid,nodeTypeEnum.name().toLowerCase()); - } - } - } else { - log.debug("Failed to serialize component {} of type {} for cache", componentUid, - nodeTypeEnum.name().toLowerCase()); - } - return result; - } - - public boolean setComponent(Component component, NodeTypeEnum nodeTypeEnum) { - - boolean result = false; - - if (!isEnabled()) { - log.debug(COMPONENT_CACHE_IS_DISABLED); - return false; - } - - String componentUid = component.getUniqueId(); - Long lastUpdateDate = component.getLastUpdateDate(); - - result = saveComponent(componentUid, lastUpdateDate, nodeTypeEnum, component); - - return result; - - } - - /** - * get components from cache of a given list ou unique ids. - * - * for each component data from cassandra, unzip the data if needed and - * deserialize the unzipped data to java object(Component). - * - * @param filteredResources - * @return ImmutableTripple or ActionStatus. | |-- components |-- set of non - * cached components - * - */ - private Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> getComponentsFull( - Map<String, Long> filteredResources) { - - if (!isEnabled()) { - log.debug(COMPONENT_CACHE_IS_DISABLED); - return Either.right(ActionStatus.NOT_ALLOWED); - } - - List<Component> foundResources = new LinkedList<>(); - Set<String> notFoundResources = new HashSet<>(); - ImmutablePair<List<Component>, Set<String>> result = new ImmutablePair<>( - foundResources, notFoundResources); - - long cassandraFetchStart = System.currentTimeMillis(); - - Either<ImmutablePair<List<ComponentCacheData>, Set<String>>, ActionStatus> componentsFromCache = componentCassandraDao - .getComponents(filteredResources); - - long cassandraFetchEnd = System.currentTimeMillis(); - log.debug("Fetch time from cassandara of all components took {} ms", - (cassandraFetchEnd - cassandraFetchStart)); - if (componentsFromCache.isRight()) { - BeEcompErrorManager.getInstance().logInternalFlowError("FetchFromCache", - "Failed to fetch components from cache", ErrorSeverity.ERROR); - return Either.right(componentsFromCache.right().value()); - } - - ImmutablePair<List<ComponentCacheData>, Set<String>> immutablePair = componentsFromCache.left().value(); - List<ComponentCacheData> list = immutablePair.getLeft(); - log.debug("Number of components fetched from cassandra is {}", (list == null ? 0 : list.size())); - if (list != null && !list.isEmpty()) { - - log.debug("Number of components filterd is {}", list == null ? 0 : list.size()); - - if (list != null) { - long desStart = System.currentTimeMillis(); - - for (ComponentCacheData componentCacheData : list) { - - log.debug("Process uid {} from cache", componentCacheData.getId()); - - String compUid = componentCacheData.getId(); - - Either<? extends Component, Boolean> deserializeExt = convertComponentCacheToComponent( - componentCacheData); - - if (deserializeExt.isLeft()) { - Component component = deserializeExt.left().value(); - foundResources.add(component); - } else { - notFoundResources.add(compUid); - } - - } - long desEnd = System.currentTimeMillis(); - log.debug("Deserialization and unzip of {} components took {} ms", list.size(), (desEnd - desStart)); - } - } - log.debug("Number of processed components from cache is {}", foundResources.size()); - - Set<String> notFoundInCache = immutablePair.getRight(); - notFoundResources.addAll(notFoundInCache); - - if (log.isDebugEnabled()) { - log.debug("Number of components fetched is {}", foundResources.size()); - log.debug("Number of components non cached is {}", notFoundResources.size()); - } - - return Either.left(result); - } - - /** - * get components for catalog - * - * @param components - * @param componentTypeEnum - * @return - */ - public Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> getComponentsForCatalog( - Map<String, Long> components, ComponentTypeEnum componentTypeEnum) { - - if (!isEnabled()) { - log.debug("In getComponentsForCatalog for type {}. Cache is disabled.", - componentTypeEnum.name().toLowerCase()); - return Either.right(ActionStatus.NOT_ALLOWED); - } - log.debug("In getComponentsForCatalog for type {}", componentTypeEnum.name().toLowerCase()); - - Function<List<Component>, List<Component>> filterFieldsFunc = this::filterForCatalog; - - Map<String, Long> leftComponentsForSearch = new HashMap<>(); - leftComponentsForSearch.putAll(components); - - // get components from inmemory cache - List<Component> componentsFromMemory = null; - if (catalogInMemoryEnabled) { - componentsFromMemory = getDataFromInMemoryCache(components.keySet(), componentTypeEnum); - log.debug("The number of components of type {} fetched from memory is {}", - componentTypeEnum.name().toLowerCase(), - componentsFromMemory == null ? 0 : componentsFromMemory.size()); - if (componentsFromMemory != null) { - List<String> ignoredComponents = new ArrayList<>(); - for (Component componentFromMem : componentsFromMemory) { - if (componentFromMem.getLastUpdateDate().longValue() != components - .get(componentFromMem.getUniqueId()).longValue()) { - // Ignore the component from memory - ignoredComponents.add(componentFromMem.getUniqueId()); - } - } - - log.debug("Number of components from type {} ignored from memory cache is {}", - componentTypeEnum.name().toLowerCase(), ignoredComponents.size()); - // remove from memory result the components which are not valid - componentsFromMemory = componentsFromMemory.stream() - .filter(p -> !ignoredComponents.contains(p.getUniqueId())).collect(Collectors.toList()); - // Remove from leftComponentsForSearch the valid components from - // memory - componentsFromMemory.forEach(p -> leftComponentsForSearch.remove(p.getUniqueId())); - - } - } else { - log.debug("Catalog InMemory cache is disabled"); - } - - log.debug("Number of components from type {} needed to fetch is {}", componentTypeEnum.name().toLowerCase(), - leftComponentsForSearch.size()); - - // get components from cassandra cache and filter each component - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result = getComponents( - leftComponentsForSearch, filterFieldsFunc); - - if (result.isLeft()) { - // add inmemory components to the valid components(not dirty) - List<Component> foundComponents = result.left().value().getLeft(); - if (componentsFromMemory != null) { - foundComponents.addAll(componentsFromMemory); - } - if (catalogInMemoryEnabled) { - updateCatalogInMemoryCacheWithCertified(foundComponents, componentTypeEnum); - } - } - - return result; - } - - /** - * @param components - * - Map of <componentUniqueId, last update date> - * @param filterFieldsFunc - * @return - */ - public Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> getComponents(Map<String, Long> components, - Function<List<Component>, List<Component>> filterFieldsFunc) { - - if (!isEnabled()) { - log.debug(COMPONENT_CACHE_IS_DISABLED); - return Either.right(ActionStatus.NOT_ALLOWED); - } - - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> componentsFull = getComponentsFull( - components); - - if (componentsFull.isRight()) { - return Either.right(componentsFull.right().value()); - } - - ImmutablePair<List<Component>, Set<String>> immutablePair = componentsFull.left().value(); - List<Component> foundResources = immutablePair.left; - Set<String> notFoundResources = immutablePair.right; - - List<Component> filterdFoundResources = filterFieldsFunc.apply(foundResources); - - ImmutablePair<List<Component>, Set<String>> result = new ImmutablePair<>( - filterdFoundResources, notFoundResources); - - return Either.left(result); - - } - - /** - * get the component and its modification time from cache - * - * @param componentUid - * @param filterFieldsFunc - * @return - */ - public Either<ImmutablePair<Component, Long>, ActionStatus> getComponentAndTime(String componentUid, - Function<Component, Component> filterFieldsFunc) { - - Either<ImmutablePair<Component, ComponentCacheData>, ActionStatus> componentFromCache = getComponentFromCache( - componentUid, null, filterFieldsFunc); - - if (componentFromCache.isRight()) { - return Either.right(componentFromCache.right().value()); - } - - ImmutablePair<Component, ComponentCacheData> immutablePair = componentFromCache.left().value(); - - ImmutablePair<Component, Long> result = new ImmutablePair<>(immutablePair.left, - immutablePair.right.getModificationTime().getTime()); - - return Either.left(result); - } - - private Either<ImmutablePair<Component, ComponentCacheData>, ActionStatus> getComponentFromCache( - String componentUid, Long lastModificationTime, Function<Component, Component> filterFieldsFunc) { - if (!isEnabled()) { - return Either.right(ActionStatus.NOT_ALLOWED); - } - - Either<ComponentCacheData, ActionStatus> componentRes = componentCassandraDao.getComponent(componentUid); - - if (componentRes.isRight()) { - return Either.right(componentRes.right().value()); - } - - ComponentCacheData componentCacheData = componentRes.left().value(); - - if (lastModificationTime != null) { - long cacheCompModificationTime = componentCacheData.getModificationTime().getTime(); - if (lastModificationTime != cacheCompModificationTime) { - log.debug( - "Component {} found in cache but its modification time {} does not match to the timestamp in cache {}.", - componentUid, lastModificationTime, cacheCompModificationTime); - return Either.right(ActionStatus.INVALID_CONTENT); - } - } - - Either<? extends Component, Boolean> convertRes = convertComponentCacheToComponent(componentCacheData); - if (convertRes.isRight()) { - return Either.right(ActionStatus.CONVERT_COMPONENT_ERROR); - } - - Component component = convertRes.left().value(); - - Component filteredComponent = component; - if (filterFieldsFunc != null) { - filteredComponent = filterFieldsFunc.apply(component); - } - - ImmutablePair<Component, ComponentCacheData> result = new ImmutablePair<>( - filteredComponent, componentCacheData); - - return Either.left(result); - } - - public ActionStatus deleteComponentFromCache(String id) { - if (!isEnabled()) { - return ActionStatus.NOT_ALLOWED; - } - CassandraOperationStatus status = this.componentCassandraDao.deleteComponent(id); - if (CassandraOperationStatus.OK.equals(status)) { - return ActionStatus.OK; - } else { - log.debug("delete component failed with error {}", status); - return ActionStatus.GENERAL_ERROR; - } - } - -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/DaoInfo.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/DaoInfo.java deleted file mode 100644 index 85a7f2ec59..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/DaoInfo.java +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * ============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.be.model.cache; - -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; - -public class DaoInfo { - private ToscaOperationFacade toscaOperationFacade; - private ComponentCache ComponentCache; - - public DaoInfo(ToscaOperationFacade toscaOperationFacade, org.openecomp.sdc.be.model.cache.ComponentCache componentCache) { - this.toscaOperationFacade = toscaOperationFacade; - ComponentCache = componentCache; - } - - public ToscaOperationFacade getToscaOperationFacade() { - return toscaOperationFacade; - } - - public org.openecomp.sdc.be.model.cache.ComponentCache getComponentCache() { - return ComponentCache; - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJob.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJob.java deleted file mode 100644 index daf14b0b79..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJob.java +++ /dev/null @@ -1,129 +0,0 @@ -/*- - * ============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.be.model.cache.jobs; - -import fj.data.Either; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.resources.data.ComponentMetadataData; -import org.openecomp.sdc.common.log.wrappers.Logger; - -import java.util.function.Function; - -/** - * Created by mlando on 9/7/2016. - */ -public class CheckAndUpdateJob extends Job { - private static final Logger log = Logger.getLogger(CheckAndUpdateJob.class.getName()); - - public CheckAndUpdateJob(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) { - super(daoInfo, componentId, nodeTypeEnum, timestamp); - } - - @Override - public Object doWork() { - log.trace("starting work on job."); - log.trace("update cache for componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, nodeTypeEnum, - timestamp); - - try { - - // get from cache - Either<ImmutablePair<Component, Long>, ActionStatus> cacheResult = daoInfo.getComponentCache() - .getComponentAndTime(componentId, Function.identity()); - // if error while getting from cache abort and update - if (cacheResult.isRight()) { - // genral error - if (!ActionStatus.RESOURCE_NOT_FOUND.equals(cacheResult.right().value()) - && !ActionStatus.INVALID_CONTENT.equals(cacheResult.right().value())) { - log.debug("failed to get component:{} from cache error:{}", componentId, - cacheResult.right().value()); - return false; - } - // component not in cache put there - else { - return updateCache(componentId, nodeTypeEnum, timestamp); - } - } - ImmutablePair<Component, Long> recored = cacheResult.left().value(); - // the cache has allready been updated exit - if (this.timestamp < recored.getRight()) { - log.debug("job timestemp:{} is smaller then the cache timestamp:{} no update is needed.", - this.timestamp, recored.getRight()); - return false; - } - return updateCache(componentId, nodeTypeEnum, timestamp); - - } catch (Exception e) { - log.debug("an exception was encountered during CheckAndUpdateJob", e); - } finally { - daoInfo.getToscaOperationFacade().commit(); - } - return false; - } - - /** - * @param componentId - * @param nodeTypeEnum - * @return - */ - private boolean updateCache(String componentId, NodeTypeEnum nodeTypeEnum, Long timestamp) { - // get component from cache - Either<ComponentMetadataData, StorageOperationStatus> metaDataRes = getComponentMetaData(componentId, - nodeTypeEnum); - if (metaDataRes.isRight()) { - return false; - } - ComponentMetadataData metaData = metaDataRes.left().value(); - // the job time is older then the one on graph nothing to do there is a - // job that will handle this. - Long graphTimestamp = metaData.getMetadataDataDefinition().getLastUpdateDate(); - if (timestamp < graphTimestamp) { - log.debug( - "the job timestamp:{} is smaller then the graph timestamp:{}. exiting because another job will update the cache.", - timestamp, graphTimestamp); - return false; - } else { - // update cache - // get component from grath - Either<Component, StorageOperationStatus> componentRes = daoInfo.getToscaOperationFacade().getToscaElement(componentId); - if (componentRes.isRight()) { - log.debug("failed to get full component:{} from graph status:{}", componentId, - componentRes.right().value()); - return false; - } - Component component = componentRes.left().value(); - // store in cache - if (!this.daoInfo.getComponentCache().setComponent(component, nodeTypeEnum)) { - log.debug("failed to store componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum); - return false; - } - } - log.debug("cache successfully updated for componentId:{} nodeTypeEnum:{} timestemp:{}.", componentId, - nodeTypeEnum, timestamp); - return true; - } - -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/DeleteJob.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/DeleteJob.java deleted file mode 100644 index 0d883a7877..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/DeleteJob.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ============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.be.model.cache.jobs; - -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.common.log.wrappers.Logger; - -/** - * Created by mlando on 9/20/2016. - */ -public class DeleteJob extends Job { - private static final Logger log = Logger.getLogger(DeleteJob.class.getName()); - - public DeleteJob(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) { - super(daoInfo, componentId, nodeTypeEnum, timestamp); - - } - - @Override - public Object doWork() { - try { - log.trace("starting work on job."); - log.trace("delete component in cache, componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, - nodeTypeEnum, timestamp); - ActionStatus status = this.daoInfo.getComponentCache().deleteComponentFromCache(componentId); - if (!ActionStatus.OK.equals(status)) { - log.debug("failed to delete componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum); - return false; - } - log.trace("cache successfully deleted componentId:{} nodeTypeEnum:{} timestamp:{}.", componentId, - nodeTypeEnum, timestamp); - return true; - } catch (Exception e) { - log.debug("an exception was encountered durring deletejob", e); - } - return false; - - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/Job.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/Job.java deleted file mode 100644 index 593df2b434..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/Job.java +++ /dev/null @@ -1,87 +0,0 @@ -/*- - * ============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.be.model.cache.jobs; - -import fj.data.Either; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.resources.data.ComponentMetadataData; -import org.openecomp.sdc.common.log.wrappers.Logger; - -public abstract class Job<E> { - private static final Logger log = Logger.getLogger(Job.class.getName()); - protected DaoInfo daoInfo; - protected String componentId; - protected long timestamp; - protected NodeTypeEnum nodeTypeEnum; - - protected Job(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) { - this.daoInfo = daoInfo; - this.componentId = componentId; - this.timestamp = timestamp; - this.nodeTypeEnum = nodeTypeEnum; - } - - protected Job(DaoInfo daoInfo, Component component, NodeTypeEnum nodeTypeEnum) { - this.daoInfo = daoInfo; - this.componentId = component.getUniqueId(); - this.timestamp = component.getLastUpdateDate(); - this.nodeTypeEnum = nodeTypeEnum; - } - - public abstract E doWork(); - - protected Either<ComponentMetadataData, StorageOperationStatus> getComponentMetaData(String componentId, - NodeTypeEnum nodeTypeEnum) { - Either<ComponentMetadataData, StorageOperationStatus> metaDataRes = daoInfo.getToscaOperationFacade().getComponentMetadata(componentId); - if (metaDataRes.isRight()) { - // in case we cant find the component on graph exit - if (StorageOperationStatus.NOT_FOUND.equals(metaDataRes.right().value())) { - log.debug("failed to locate component:{} on graph status:{}", componentId, metaDataRes.right().value()); - } else { - log.debug("failed to get component:{} from graph status:{}", componentId, metaDataRes.right().value()); - } - } - return metaDataRes; - } - - protected NodeTypeEnum getNodeTypeFromComponentType(ComponentTypeEnum type) { - NodeTypeEnum result = null; - switch (type) { - case PRODUCT: - result = NodeTypeEnum.Product; - break; - case RESOURCE: - result = NodeTypeEnum.Resource; - break; - case SERVICE: - result = NodeTypeEnum.Service; - break; - default: - - } - return result; - - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/OverrideJob.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/OverrideJob.java deleted file mode 100644 index cb33a02076..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/OverrideJob.java +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * ============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.be.model.cache.jobs; - -import fj.data.Either; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.common.log.wrappers.Logger; - -/** - * Created by mlando on 9/20/2016. - */ -public class OverrideJob extends Job { - private static final Logger log = Logger.getLogger(OverrideJob.class.getName()); - - public OverrideJob(DaoInfo daoInfo, String componentId, NodeTypeEnum nodeTypeEnum, long timestamp) { - super(daoInfo, componentId, nodeTypeEnum, timestamp); - - } - - @Override - public Object doWork() { - try { - log.trace("starting work on job."); - log.trace("override component in cache, componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, - nodeTypeEnum, timestamp); - // get component from grath - Either<Component, StorageOperationStatus> componentRes = daoInfo.getToscaOperationFacade().getToscaElement(componentId); - if (componentRes.isRight()) { - log.debug("failed to get full component:{} from graph status:{}", componentId, - componentRes.right().value()); - return false; - } - Component component = componentRes.left().value(); - // store in cache - if (!this.daoInfo.getComponentCache().setComponent(component, nodeTypeEnum)) { - log.debug("failed to store componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum); - return false; - } - log.debug("cache successfully overrided componentId:{} nodeTypeEnum:{} timestemp:{}.", componentId, - nodeTypeEnum, timestamp); - return true; - } catch (Exception e) { - log.debug("an exception was encountered during OverrideJob", e); - } finally { - this.daoInfo.getToscaOperationFacade().commit(); - } - return false; - - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/StoreJob.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/StoreJob.java deleted file mode 100644 index c8baafe114..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/jobs/StoreJob.java +++ /dev/null @@ -1,58 +0,0 @@ -/*- - * ============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.be.model.cache.jobs; - -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.common.log.wrappers.Logger; - -/** - * Created by mlando on 9/11/2016. - */ -public class StoreJob extends Job { - private static final Logger log = Logger.getLogger(StoreJob.class.getName()); - private Component component; - - public StoreJob(DaoInfo daoInfo, Component component, NodeTypeEnum nodeTypeEnum) { - super(daoInfo, component, nodeTypeEnum); - this.component = component; - } - - @Override - public Object doWork() { - try { - log.trace("starting work on job."); - log.trace("store component in cache, componentId:{} of nodeTypeEnum:{} with timestamp:{}.", componentId, nodeTypeEnum, timestamp); - if (!this.daoInfo.getComponentCache().setComponent(component, nodeTypeEnum)) { - log.debug("failed to store componentId:{} nodeTypeEnum:", componentId, nodeTypeEnum); - return false; - } - log.debug("cache successfully updated for componentId:{} nodeTypeEnum:{} timestemp:{}.", componentId, nodeTypeEnum, timestamp); - return true; - - } catch (Exception e) { - log.debug("an exception was encountered during StoreJob", e); - } - return false; - } - -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/CacheWorker.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/CacheWorker.java deleted file mode 100644 index 1db02c4c56..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/CacheWorker.java +++ /dev/null @@ -1,91 +0,0 @@ -/*- - * ============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.be.model.cache.workers; - -import org.openecomp.sdc.be.model.cache.jobs.Job; -import org.openecomp.sdc.be.workers.Worker; -import org.openecomp.sdc.common.log.wrappers.Logger; - -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.TimeUnit; - -/** - * Created by mlando on 9/6/2016. the class represents a worker the pull job - * from a queue and evacuates them. - * - */ -public class CacheWorker implements Runnable, IWorker { - private String workerName; - private static final Logger log = Logger.getLogger(Worker.class.getName()); - private LinkedBlockingQueue<Job> jobQueue; - private volatile boolean shutdown = false; - - /** - * constructor - * - * @param workerName - * the name of the given worker - * @param jobQueue - * the queue the worker will block on. - */ - public CacheWorker(String workerName, LinkedBlockingQueue<Job> jobQueue) { - this.workerName = workerName; - this.jobQueue = jobQueue; - } - - /** - * the method will try to get a job if one is avilable it will be retrieved - * and handled. if no jobs are available the worker will block for 500 - * milliseconds and then it wil check if it needs to shutdown. if not it - * will block again and so on until sutdown or a new job is available - */ - @Override - public void run() { - while (true) { - log.trace("CacheWorker:{} doing work", workerName); - try { - Job job = jobQueue.poll(500, TimeUnit.MILLISECONDS); - if (job != null) { - job.doWork(); - log.trace("worker:{} done with work", workerName); - } - } catch (Exception e) { - log.debug("worker {} failed during job execution.", workerName); - log.debug("exception", e); - } - if (shutdown) { - log.debug("worker:{} nothing to do stoping", workerName); - break; - } - } - - } - - /** - * the method sets the shutdown flag, when set the worker will stop it's - * execution as soon as possible with out completing its work - */ - @Override - public void shutDown() { - this.shutdown = true; - } - -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/IWorker.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/IWorker.java deleted file mode 100644 index fa508a4923..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/IWorker.java +++ /dev/null @@ -1,28 +0,0 @@ -/*- - * ============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.be.model.cache.workers; - -/** - * Created by mlando on 9/6/2016. - */ -public interface IWorker { - void shutDown(); -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/SyncWorker.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/SyncWorker.java deleted file mode 100644 index cd78063780..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/workers/SyncWorker.java +++ /dev/null @@ -1,267 +0,0 @@ -/*- - * ============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.be.model.cache.workers; - -import fj.data.Either; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.operations.impl.CacheMangerOperation; -import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; -import org.openecomp.sdc.be.resources.data.ComponentMetadataData; -import org.openecomp.sdc.common.log.wrappers.Logger; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -/** - * the class creates a worker that is used to update cache date, in case of - * failures and inconsistencies - */ -public class SyncWorker implements Runnable, IWorker { - - private static final Logger log = Logger.getLogger(SyncWorker.class.getName()); - private final CacheMangerOperation cacheMangerOperation; - private final String workerName; - private volatile boolean shutdown = false; - private Map<String, ComponentCacheData> cacheIdAndTimeMap; - private long updateDelayInMilliseconds = 60 * 60 * 1000L; - - /** - * creates the sync worker - * - * @param workerName - * the name of the worker - * @param cacheMangerOperation - * responsible for all persistence's operations to graph and the - * cache - */ - public SyncWorker(String workerName, CacheMangerOperation cacheMangerOperation) { - this.workerName = workerName; - this.cacheMangerOperation = cacheMangerOperation; - } - - /** - * the method collects all the resources/services/products from graph and - * checks if the component representing them in the cache is valid logic: if - * the record is present in the graph but not in cache -> create a job that - * will update the record oin cache if the timestamp of the record in cache - * is older than the timestamp on the graph -> create a job that will update - * the record oin cache otherwise no update is required - */ - @Override - public void run() { - try { - collectAllCacheRecords(); - syncCacheByComponentType(NodeTypeEnum.Resource); - syncCacheByComponentType(NodeTypeEnum.Service); - syncCacheByComponentType(NodeTypeEnum.Product); - clearCacheRecords(); - - } catch (Exception e) { - log.debug("sync worker:{} encounered an exception", workerName); - log.debug("exception", e); - } finally { - this.cacheMangerOperation.getJanusGraphGenericDao().commit(); - } - } - - /** - * the method checks for each component in the cache except the ones that - * were update during the sync, if they exist on the graph if not a job to - * remove them is created - */ - private void clearCacheRecords() { - cacheIdAndTimeMap.forEach((k, v) -> { - try { - Either<ComponentMetadataData, JanusGraphOperationStatus> componentFromGraphRes = getComponentMetaData(k, - NodeTypeEnum.getByName(v.getType())); - if (componentFromGraphRes.isRight()) { - JanusGraphOperationStatus error = componentFromGraphRes.right().value(); - if (JanusGraphOperationStatus.NOT_FOUND.equals(error)) { - long delay = System.currentTimeMillis() - v.getModificationTime().getTime(); - if (delay > updateDelayInMilliseconds) { - this.cacheMangerOperation.deleteComponentInCache(k, v.getModificationTime().getTime(), - NodeTypeEnum.getByName(v.getType())); - } else { - log.trace( - "no delete done because an hour did not pass since the delete was done timeSinceUpdate {} < updateDelayInMilliseconds {} ", - delay, updateDelayInMilliseconds); - } - } else { - log.debug("failed to get metadata for id:{} from graph error:{}", k, error); - } - } else { - log.trace("id {} is in graph nothing to do", k); - } - } catch (Exception e) { - log.debug("during clean cache records an exception was thrown", e); - } - }); - } - - /** - * the method collects all the records from cache except the component - * itself - */ - public void collectAllCacheRecords() { - Either<List<ComponentCacheData>, ActionStatus> getAllRes = this.cacheMangerOperation.getComponentCache() - .getAllComponentIdTimeAndType(); - if (getAllRes.isRight()) { - log.debug("error while trying to get all records from cache error:{}", getAllRes.right().value()); - cacheIdAndTimeMap = new HashMap<>(); - } else { - cacheIdAndTimeMap = getAllRes.left().value().stream().collect(Collectors.toMap(ComponentCacheData::getId, e -> e)); - } - } - - /** - * the method checks that the records ot the given type are sync between the - * cache and the graph - * - * @param nodeTypeEnum - * the type of components we want to sync - */ - private void syncCacheByComponentType(NodeTypeEnum nodeTypeEnum) { - if (!this.shutdown) { - log.trace("syncCache records of type:{} .", nodeTypeEnum); - Either<List<ComponentMetadataData>, JanusGraphOperationStatus> getAllResult = getAllComponentsMetaData( - nodeTypeEnum); - List<ComponentMetadataData> componentList = new ArrayList<>(); - if (getAllResult.isRight() && !JanusGraphOperationStatus.NOT_FOUND.equals(getAllResult.right().value())) { - log.debug("error while trying to get all components of type:{} JanusGraphOperationStatus:{}.", nodeTypeEnum, - getAllResult.right().value()); - return; - } - if (getAllResult.isLeft()) { - componentList = getAllResult.left().value(); - log.trace("get all components of type:{} returned:{} components.", nodeTypeEnum, componentList.size()); - } - componentList.forEach(this::checkAndUpdateCacheComponent); - log.trace("syncCache records of type:{} was successful.", nodeTypeEnum); - } - } - - /** - * the method compares the given component to the record in the cache if the - * record is not in the cache a job to update the cache for this record will - * be created. if the record is present in the graph but not in cache -> - * create a job that will update the record oin cache if the timestamp of - * the record in cache is older than the timestamp on the graph -> create a - * job that will update the record oin cache if the retried component from - * cache fails to be deserialized -> create job to override it otherwise no - * update is required - * - * @param metadataData - * the date of the node we want to compare to the value in the - * cache - */ - private void checkAndUpdateCacheComponent(ComponentMetadataData metadataData) { - long timeSinceUpdate = System.currentTimeMillis() - - metadataData.getMetadataDataDefinition().getLastUpdateDate(); - if (timeSinceUpdate >= updateDelayInMilliseconds) { - String uid = metadataData.getMetadataDataDefinition().getUniqueId(); - log.trace("checking cache if record for uid:{} needs to be updated.", uid); - Either<Component, ActionStatus> cacheResult = this.cacheMangerOperation.getComponentCache() - .getComponent(uid); - if (cacheResult.isRight()) { - ActionStatus actionStatus = cacheResult.right().value(); - if (ActionStatus.RESOURCE_NOT_FOUND.equals(actionStatus)) { - log.trace("record for uid:{} not found in cache. creating an update job.", uid); - this.cacheMangerOperation.updateComponentInCache(uid, - metadataData.getMetadataDataDefinition().getLastUpdateDate(), - NodeTypeEnum.getByName(metadataData.getLabel())); - } else if (ActionStatus.CONVERT_COMPONENT_ERROR.equals(actionStatus)) { - log.trace("uid:{} found in cache but we failed deserializing it. creating an override job .", uid); - this.cacheMangerOperation.overideComponentInCache(uid, - metadataData.getMetadataDataDefinition().getLastUpdateDate(), - NodeTypeEnum.getByName(metadataData.getLabel())); - } else { - log.debug("during lookup for uid:{} an error accords status:{} .", uid, actionStatus); - } - } else { - log.trace("uid:{} found in cache.", uid); - this.cacheIdAndTimeMap.remove(uid); - Component cacheComponent = cacheResult.left().value(); - Long cacheTimestamp = cacheComponent.getLastUpdateDate(); - Long graphTimestamp = metadataData.getMetadataDataDefinition().getLastUpdateDate(); - if (cacheTimestamp < graphTimestamp) { - log.trace("uid:{} found in cache. cache Timestamp {} < graph timestamp {} , creating an update job .", - uid, cacheTimestamp, graphTimestamp); - this.cacheMangerOperation.updateComponentInCache(uid, graphTimestamp, - NodeTypeEnum.getByName(metadataData.getLabel())); - } else { - log.trace("uid:{} found in cache. cache Timestamp {} => graph timestamp {}, no update is needed .", - uid, cacheTimestamp, graphTimestamp); - } - } - } else { - log.trace( - "no update done because an hour did not pass since the update was done timeSinceUpdate {} < updateDelayInMilliseconds {} ", - timeSinceUpdate, updateDelayInMilliseconds); - } - } - - /** - * the method sets the shutdown flag, when set the worker will stop it's - * execution as soon as possible with out completing its work - */ - @Override - public void shutDown() { - log.debug("syncWorker {} shuting down.", workerName); - this.shutdown = true; - } - - /** - * the method retrieves all nodes matching the given node type from the graph - * - * @param nodeTypeEnum - * node type we want to lookup on the graph - * @return a list of retrieved nodes matching the given type or not found in - * case no nodes were found or error in case of failure - */ - private Either<List<ComponentMetadataData>, JanusGraphOperationStatus> getAllComponentsMetaData( - NodeTypeEnum nodeTypeEnum) { - return this.cacheMangerOperation.getJanusGraphGenericDao().getByCriteria(nodeTypeEnum, null, - ComponentMetadataData.class); - } - - /** - * the method retrieves the metadata from graph for the given id - * - * @param uid - * the unique id of the component we want to retrieve - * @param nodeTypeEnum - * the type of the recored we want to retrieve - * @return the meta dat of the component or the error encountered during the - * get - */ - private Either<ComponentMetadataData, JanusGraphOperationStatus> getComponentMetaData(String uid, - NodeTypeEnum nodeTypeEnum) { - return this.cacheMangerOperation.getJanusGraphGenericDao().getNode(UniqueIdBuilder.getKeyByNodeType(nodeTypeEnum), - uid, ComponentMetadataData.class); - } -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/ICacheMangerOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/ICacheMangerOperation.java deleted file mode 100644 index fcf5fab019..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/api/ICacheMangerOperation.java +++ /dev/null @@ -1,41 +0,0 @@ -/*- - * ============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.be.model.operations.api; - -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; - -/** - * Created by mlando on 9/5/2016. - */ -public interface ICacheMangerOperation { - - /** - * - * - * @param componentId - * @param timestamp - * @param nodeTypeEnum - */ - void updateComponentInCache(String componentId, long timestamp, NodeTypeEnum nodeTypeEnum); - - void storeComponentInCache(org.openecomp.sdc.be.model.Component component, NodeTypeEnum nodeTypeEnum); - -} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CacheMangerOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CacheMangerOperation.java deleted file mode 100644 index abd3c91eea..0000000000 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/CacheMangerOperation.java +++ /dev/null @@ -1,193 +0,0 @@ -/*- - * ============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.be.model.operations.impl; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import org.openecomp.sdc.be.config.Configuration; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.cache.ComponentCache; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.be.model.cache.jobs.*; -import org.openecomp.sdc.be.model.cache.workers.CacheWorker; -import org.openecomp.sdc.be.model.cache.workers.IWorker; -import org.openecomp.sdc.be.model.cache.workers.SyncWorker; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation; -import org.openecomp.sdc.common.log.wrappers.Logger; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; -import java.util.LinkedList; -import java.util.concurrent.*; - -/** - * Created by mlando on 9/5/2016. the class is responsible for handling all cache update operations asynchronously including sync between the graph and cache and on demand update requests - */ -@Component("cacheManger-operation") -public class CacheMangerOperation implements ICacheMangerOperation { - @Autowired - private ToscaOperationFacade toscaOperationFacade; - @Autowired - private JanusGraphGenericDao janusGraphGenericDao; - @Autowired - private ComponentCache componentCache; - - private static final Logger log = Logger.getLogger(CacheMangerOperation.class.getName()); - private LinkedBlockingQueue<Job> jobQueue = null; - private int waitOnShutDownInMinutes; - private ScheduledExecutorService syncExecutor; - private ExecutorService workerExecutor; - private LinkedList<IWorker> workerList = new LinkedList<>(); - private DaoInfo daoInfo; - - /** - * constructor - */ - public CacheMangerOperation() { - } - - /** - * the method checks in the cache is enabled, if it is, it initializes all the workers according to the configuration values. - */ - @PostConstruct - public void init() { - - daoInfo = new DaoInfo(toscaOperationFacade, componentCache); - - Configuration.ApplicationL2CacheConfig applicationL2CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL2Cache(); - if (applicationL2CacheConfig != null && applicationL2CacheConfig.isEnabled()) { - Integer numberOfWorkers = applicationL2CacheConfig.getQueue().getNumberOfCacheWorkers(); - this.waitOnShutDownInMinutes = applicationL2CacheConfig.getQueue().getWaitOnShutDownInMinutes(); - jobQueue = new LinkedBlockingQueue<>(); - log.info("L2 Cache is enabled initializing queue"); - log.debug("initializing SyncWorker, creating {} workers", numberOfWorkers); - ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("Sync-Cache-Worker-%d").build(); - this.syncExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory); - log.debug("initializing workers, creating {} cacheWorkers", numberOfWorkers); - threadFactory = new ThreadFactoryBuilder().setNameFormat("Cache-Worker-%d").build(); - String workerName = "Sync-Worker"; - Integer syncWorkerExacutionIntrval = applicationL2CacheConfig.getQueue().getSyncIntervalInSecondes(); - log.debug("starting Sync worker:{} with executions interval:{} ", workerName, syncWorkerExacutionIntrval); - SyncWorker syncWorker = new SyncWorker(workerName, this); - this.syncExecutor.scheduleAtFixedRate(syncWorker, 5 * 60L, syncWorkerExacutionIntrval, TimeUnit.SECONDS); - this.workerExecutor = Executors.newFixedThreadPool(numberOfWorkers, threadFactory); - CacheWorker cacheWorker; - for (int i = 0; i < numberOfWorkers; i++) { - workerName = "Cache-Worker-" + i; - log.debug("starting Cache worker:{}", workerName); - cacheWorker = new CacheWorker(workerName, jobQueue); - this.workerExecutor.submit(cacheWorker); - this.workerList.add(cacheWorker); - } - } else { - log.info("L2 Cache is disabled"); - } - log.info("L2 Cache has been initialized and the workers are running"); - } - - /** - * the method creates a job to check it the given component is in the cach and if so is it valid if the value in the cache is not valid it will be updated. - * - * @param componentId - * the uid of the component we want to update - * @param timestamp - * the time of the component update - * @param nodeTypeEnum - * the type of the component resource/service/product - */ - @Override - public void updateComponentInCache(String componentId, long timestamp, NodeTypeEnum nodeTypeEnum) { - Configuration.ApplicationL2CacheConfig applicationL2CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL2Cache(); - if (applicationL2CacheConfig != null && applicationL2CacheConfig.isEnabled()) { - this.jobQueue.add(new CheckAndUpdateJob(daoInfo, componentId, nodeTypeEnum, timestamp)); - } - } - - public void overideComponentInCache(String componentId, long timestamp, NodeTypeEnum nodeTypeEnum) { - Configuration.ApplicationL2CacheConfig applicationL2CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL2Cache(); - if (applicationL2CacheConfig != null && applicationL2CacheConfig.isEnabled()) { - this.jobQueue.add(new OverrideJob(daoInfo, componentId, nodeTypeEnum, timestamp)); - } - } - - public void deleteComponentInCache(String componentId, long timestamp, NodeTypeEnum nodeTypeEnum) { - Configuration.ApplicationL2CacheConfig applicationL2CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL2Cache(); - if (applicationL2CacheConfig != null && applicationL2CacheConfig.isEnabled()) { - this.jobQueue.add(new DeleteJob(daoInfo, componentId, nodeTypeEnum, timestamp)); - } - } - - /** - * the method stores the given component in the cache - * - * @param component - * componet to store in cache - * @param nodeTypeEnum - * the type of the component we want to store - */ - @Override - public void storeComponentInCache(org.openecomp.sdc.be.model.Component component, NodeTypeEnum nodeTypeEnum) { - Configuration.ApplicationL2CacheConfig applicationL2CacheConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getApplicationL2Cache(); - if (applicationL2CacheConfig != null && applicationL2CacheConfig.isEnabled()) { - this.jobQueue.add(new StoreJob(daoInfo, component, nodeTypeEnum)); - } - } - - /** - * the method shutdown's all the worker's. the method has a pre set of how long it will wait for the workers to shutdown. the pre defined value is taken from the configuration. - */ - @PreDestroy - public void shutDown() { - workerExecutor.shutdown(); - syncExecutor.shutdown(); - this.workerList.forEach(IWorker::shutDown); - try { - if (!workerExecutor.awaitTermination(this.waitOnShutDownInMinutes, TimeUnit.MINUTES)) { - log.error("timer elapsed while waiting for Cache workers to finish, forcing a shutdown. "); - } - log.debug("all Cache workers finished"); - } catch (InterruptedException e) { - log.error("failed while waiting for Cache worker", e); - Thread.currentThread().interrupt(); - } - try { - if (!workerExecutor.awaitTermination(1, TimeUnit.MINUTES)) { - log.error("timer elapsed while waiting for the Sync worker's to finish, forcing a shutdown. "); - } - log.debug("sync worker finished"); - } catch (InterruptedException e) { - log.error("failed while waiting for sync worker", e); - Thread.currentThread().interrupt(); - } - } - - public JanusGraphGenericDao getJanusGraphGenericDao() { - return janusGraphGenericDao; - } - - public ComponentCache getComponentCache() { - return componentCache; - } -} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ComponentCacheTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ComponentCacheTest.java deleted file mode 100644 index f891f05f82..0000000000 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ComponentCacheTest.java +++ /dev/null @@ -1,553 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.model.cache; - -import fj.data.Either; -import mockit.Deencapsulation; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.commons.lang3.tuple.ImmutableTriple; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; -import org.openecomp.sdc.be.dao.cassandra.ComponentCassandraDao; -import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.Product; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.Service; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.resources.data.ComponentCacheData; -import org.openecomp.sdc.be.unittests.utils.ModelConfDependentTest; - -import java.util.*; -import java.util.function.Function; - -public class ComponentCacheTest extends ModelConfDependentTest { - - @InjectMocks - ComponentCache testSubject; - - @Mock - ComponentCassandraDao componentCassandraDao; - - @Mock - ToscaOperationFacade toscaOperationFacade; - - @Before - public void setUpMocks() throws Exception { - MockitoAnnotations.initMocks(this); - } - - @Test - public void testInit() throws Exception { - // default test - testSubject.init(); - } - - @Test - public void testIsEnabled() throws Exception { - - boolean result; - - // default test - - result = testSubject.isEnabled(); - } - - @Test - public void testSetEnabled() throws Exception { - - boolean enabled = false; - - // default test - - testSubject.setEnabled(enabled); - } - - @Test - public void testGetComponentNotFound() throws Exception { - - String componentUid = "mock"; - Long lastModificationTime = null; - Function<Component, Component> filterFieldsFunc = null; - Either<Component, ActionStatus> result; - - Mockito.when(componentCassandraDao.getComponent("mock")) - .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND)); - // default test - result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc); - } - - @Test - public void testGetComponentInvalidDate() throws Exception { - - String componentUid = "mock"; - Long lastModificationTime = 0L; - Function<Component, Component> filterFieldsFunc = null; - Either<Component, ActionStatus> result; - - ComponentCacheData a = new ComponentCacheData(); - a.setModificationTime(new Date()); - Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a)); - // default test - result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc); - } - - @Test - public void testGetComponentDeserializeError() throws Exception { - - String componentUid = "mock"; - Long lastModificationTime = 0L; - Function<Component, Component> filterFieldsFunc = null; - Either<Component, ActionStatus> result; - - ComponentCacheData a = new ComponentCacheData(); - a.setModificationTime(new Date(0L)); - a.setType(NodeTypeEnum.Resource.getName()); - Mockito.when(componentCassandraDao.getComponent("mock")).thenReturn(Either.left(a)); - // default test - result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc); - } - - @Test - public void testGetAllComponentIdTimeAndType() throws Exception { - - Either<List<ComponentCacheData>, ActionStatus> result; - - // default test - - result = testSubject.getAllComponentIdTimeAndType(); - testSubject.setEnabled(false); - result = testSubject.getAllComponentIdTimeAndType(); - } - - @Test - public void testUpdateCatalogInMemoryCacheWithCertified() throws Exception { - - List<Component> foundComponents = new LinkedList<>(); - - // default test - testSubject.init(); - Deencapsulation.invoke(testSubject, "updateCatalogInMemoryCacheWithCertified", foundComponents, - ComponentTypeEnum.RESOURCE); - } - - @Test - public void testGetDataFromInMemoryCache() throws Exception { - - Set<String> components = new HashSet<>(); - components.add("mock"); - ComponentTypeEnum componentTypeEnum = null; - List<Component> result; - - // default test - testSubject.init(); - result = Deencapsulation.invoke(testSubject, "getDataFromInMemoryCache", components, - ComponentTypeEnum.RESOURCE); - } - - @Test - public void testGetComponents() throws Exception { - - Set<String> components = new HashSet<>(); - Function<List<Component>, List<Component>> filterFieldsFunc = new Function<List<Component>, List<Component>>() { - - @Override - public List<Component> apply(List<Component> t) { - return t; - } - }; - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - List<ComponentCacheData> list = new LinkedList<>(); - Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list)); - - // default test - testSubject.init(); - result = testSubject.getComponents(components, filterFieldsFunc); - } - - @Test - public void testGetComponentsNotAllowed() throws Exception { - - Set<String> components = new HashSet<>(); - Function<List<Component>, List<Component>> filterFieldsFunc = null; - - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - // default test - testSubject.setEnabled(false); - result = testSubject.getComponents(components, filterFieldsFunc); - } - - @Test - public void testGetComponentsCassndraError() throws Exception { - - Set<String> components = new HashSet<>(); - Function<List<Component>, List<Component>> filterFieldsFunc = null; - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))) - .thenReturn(Either.right(ActionStatus.GENERAL_ERROR)); - - // default test - testSubject.init(); - result = testSubject.getComponents(components, filterFieldsFunc); - } - - @Test - public void testGetComponentsForLeftPanel() throws Exception { - - ComponentTypeEnum componentTypeEnum = null; - String internalComponentType = "mock"; - Set<String> filteredResources = new HashSet<>(); - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - List<ComponentCacheData> list = new LinkedList<>(); - Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(list)); - - // default test - result = testSubject.getComponentsForLeftPanel(ComponentTypeEnum.RESOURCE, internalComponentType, - filteredResources); - } - - @Test - public void testFilterForLeftPanel() throws Exception { - - List<Component> components = new LinkedList<>(); - List<Component> result; - - // test 1 - - result = Deencapsulation.invoke(testSubject, "filterForLeftPanel", components); - Assert.assertNotEquals(null, result); - } - - @Test - public void testFilterForCatalog() throws Exception { - - List<Component> components = new LinkedList<>(); - List<Component> result; - - // test 1 - result = Deencapsulation.invoke(testSubject, "filterForCatalog", components); - Assert.assertNotEquals(null, result); - } - - @Test - public void testFilterFieldsForLeftPanel() throws Exception { - Component result; - - // default test - Resource resource = new Resource(); - resource.setComponentType(ComponentTypeEnum.RESOURCE); - result = Deencapsulation.invoke(testSubject, "filterFieldsForLeftPanel", resource); - Service service = new Service(); - service.setComponentType(ComponentTypeEnum.SERVICE); - result = Deencapsulation.invoke(testSubject, "filterFieldsForLeftPanel", service); - } - - @Test - public void testFilterFieldsForCatalog() throws Exception { - Component result; - - // default test - - Resource resource = new Resource(); - resource.setComponentType(ComponentTypeEnum.RESOURCE); - result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", resource); - Service service = new Service(); - service.setComponentType(ComponentTypeEnum.SERVICE); - result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", service); - Product product = new Product(); - product.setComponentType(ComponentTypeEnum.PRODUCT); - result = Deencapsulation.invoke(testSubject, "filterFieldsForCatalog", product); - } - - @Test - public void testCopyFieldsForLeftPanel() throws Exception { - - Component component = new Resource(); - Component filteredComponent = new Resource(); - ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()) - .setResourceType(ResourceTypeEnum.VL); - // default test - - Deencapsulation.invoke(testSubject, "copyFieldsForLeftPanel", component, filteredComponent); - } - - @Test - public void testGetComponentsFullDisabled() throws Exception { - - Set<String> filteredResources = null; - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - // default test - testSubject.setEnabled(false); - result = Deencapsulation.invoke(testSubject, "getComponentsFull", Set.class); - } - - - @Test - public void testGetComponentsFullDesirializeError() throws Exception { - - Set<String> filteredResources = new HashSet<>(); - filteredResources.add("mock"); - Either<ImmutableTriple<List<Component>, List<Component>, Set<String>>, ActionStatus> result; - - List<ComponentCacheData> a = new LinkedList<>(); - ComponentCacheData e = new ComponentCacheData(); - e.setId("mock"); - e.setType(NodeTypeEnum.Resource.getName()); - a.add(e); - Mockito.when(componentCassandraDao.getComponents(Mockito.any(List.class))).thenReturn(Either.left(a)); - - // default test - - result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources); - } - - - @Test - public void testGetComponent_1() throws Exception { - - String componentUid = "mock"; - Either<Component, ActionStatus> result; - - Mockito.when(componentCassandraDao.getComponent("mock")) - .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND)); - - // default test - result = testSubject.getComponent(componentUid); - } - - @Test - public void testGetComponent_2() throws Exception { - - String componentUid = "mock"; - Long lastModificationTime = null; - Either<Component, ActionStatus> result; - - Mockito.when(componentCassandraDao.getComponent("mock")) - .thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND)); - - // default test - Function<Component, Component> filterFieldsFunc = new Function<Component, Component>() { - @Override - public Component apply(Component component) { - return new Resource(); - } - }; - result = testSubject.getComponent(componentUid, lastModificationTime, filterFieldsFunc); - } - - @Test - public void testSaveComponent() throws Exception { - - String componentUid = ""; - Component component = new Resource(); - boolean result; - - // default test - Mockito.when(componentCassandraDao.saveComponent(Mockito.any(ComponentCacheData.class))) - .thenReturn(CassandraOperationStatus.OK); - - result = Deencapsulation.invoke(testSubject, "saveComponent", componentUid, 0L, NodeTypeEnum.Resource, - component); - } - - @Test - public void testSetComponent_1Disabled() throws Exception { - - Component component = new Resource(); - component.setLastUpdateDate(0L); - boolean result; - - // default test - testSubject.setEnabled(false); - result = testSubject.setComponent(component, NodeTypeEnum.Resource); - } - - @Test - public void testSetComponent_1() throws Exception { - - Component component = new Resource(); - component.setLastUpdateDate(0L); - boolean result; - - // default test - - result = testSubject.setComponent(component, NodeTypeEnum.Resource); - } - - - @Test - public void testGetComponentsFull_1CannotDeserialize() throws Exception { - Map<String, Long> filteredResources = new HashMap<>(); - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - LinkedList<ComponentCacheData> left = new LinkedList<>(); - ComponentCacheData e = new ComponentCacheData(); - e.setType(NodeTypeEnum.Resource.getName()); - left.add(e); - ImmutablePair<List<ComponentCacheData>, Set<String>> immutablePair = ImmutablePair.of(left, new HashSet<>()); - Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.left(immutablePair)); - - result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources); - } - - @Test - public void testGetComponentsFull_1Disabled() throws Exception { - Map<String, Long> filteredResources = new HashMap<>(); - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - testSubject.setEnabled(false); - result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources); - } - - @Test - public void testGetComponentsFull_1NotFound() throws Exception { - Map<String, Long> filteredResources = new HashMap<>(); - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.ARTIFACT_NOT_FOUND)); - - result = Deencapsulation.invoke(testSubject, "getComponentsFull", filteredResources); - } - - @Test - public void testGetComponentsForCatalog_1Disabled() throws Exception { - - Map<String, Long> components = null; - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - testSubject.setEnabled(false); - result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE); - } - - @Test - public void testGetComponentsForCatalog_1() throws Exception { - Map<String, Long> components = new HashMap<>(); - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - ImmutablePair<List<ComponentCacheData>, Set<String>> value = ImmutablePair.of(new LinkedList<>(), new HashSet<>()); - Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.left(value)); - testSubject.init(); - result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE); - } - - @Test - public void testGetComponentsForCatalog_1Error() throws Exception { - Map<String, Long> components = new HashMap<>(); - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - Mockito.when(componentCassandraDao.getComponents(Mockito.any(Map.class))).thenReturn(Either.right(ActionStatus.COMPONENT_NOT_FOUND)); - - result = testSubject.getComponentsForCatalog(components, ComponentTypeEnum.RESOURCE); - } - - @Test - public void testGetComponents_1Disabled() throws Exception { - - Map<String, Long> components = null; - Function<List<Component>, List<Component>> filterFieldsFunc = null; - Either<ImmutablePair<List<Component>, Set<String>>, ActionStatus> result; - - // default test - testSubject.setEnabled(false); - result = testSubject.getComponents(components, filterFieldsFunc); - } - - @Test - public void testGetComponentAndTimeNotFound() throws Exception { - - String componentUid = ""; - Function<Component, Component> filterFieldsFunc = null; - Either<ImmutablePair<Component, Long>, ActionStatus> result; - - // default test - Mockito.when(componentCassandraDao.getComponent(Mockito.anyString())).thenReturn(Either.right(ActionStatus.API_RESOURCE_NOT_FOUND)); - - result = testSubject.getComponentAndTime(componentUid, filterFieldsFunc); - } - - @Test - public void testGetComponentFromCacheDisabled() throws Exception { - String componentUid = ""; - Long lastModificationTime = null; - Function<Component, Component> filterFieldsFunc = null; - Either<ImmutablePair<Component, ComponentCacheData>, ActionStatus> result; - - // test 1 - lastModificationTime = null; - testSubject.setEnabled(false); - result = Deencapsulation.invoke(testSubject, "getComponentFromCache", - new Object[]{componentUid, Long.class, Function.class}); - } - - @Test - public void testDeleteComponentFromCacheFails() throws Exception { - - String id = ""; - ActionStatus result; - - // default test - - result = testSubject.deleteComponentFromCache(id); - } - - @Test - public void testDeleteComponentFromCacheDisabled() throws Exception { - - String id = ""; - ActionStatus result; - - // default test - testSubject.setEnabled(false); - result = testSubject.deleteComponentFromCache(id); - } - - @Test - public void testDeleteComponentFromCache() throws Exception { - - String id = ""; - ActionStatus result; - - // default test - Mockito.when(componentCassandraDao.deleteComponent(Mockito.anyString())).thenReturn(CassandraOperationStatus.OK); - result = testSubject.deleteComponentFromCache(id); - } -} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/DaoInfoTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/DaoInfoTest.java deleted file mode 100644 index cf41d1273f..0000000000 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/DaoInfoTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.model.cache; - -import org.junit.Test; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; - - -public class DaoInfoTest { - - private DaoInfo createTestSubject() { - return new DaoInfo(new ToscaOperationFacade(), new ComponentCache()); - } - - @Test - public void testGetToscaOperationFacade() throws Exception { - DaoInfo testSubject; - ToscaOperationFacade result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getToscaOperationFacade(); - } - - @Test - public void testGetComponentCache() throws Exception { - DaoInfo testSubject; - ComponentCache result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getComponentCache(); - } -} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJobTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJobTest.java deleted file mode 100644 index 646b4ef018..0000000000 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/jobs/CheckAndUpdateJobTest.java +++ /dev/null @@ -1,234 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 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.be.model.cache.jobs; - -import fj.data.Either; -import mockit.Deencapsulation; -import org.apache.commons.lang3.tuple.ImmutablePair; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; -import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.Resource; -import org.openecomp.sdc.be.model.cache.ComponentCache; -import org.openecomp.sdc.be.model.cache.DaoInfo; -import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; -import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.resources.data.ComponentMetadataData; -import org.openecomp.sdc.be.resources.data.ResourceMetadataData; - -import java.util.function.Function; - -public class CheckAndUpdateJobTest { - - CheckAndUpdateJob testSubject; - - @Mock - DaoInfo daoInfo; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - - testSubject = new CheckAndUpdateJob(daoInfo, "mock", NodeTypeEnum.Resource, 0L); - } - - @Test - public void testDoWorkException() throws Exception { - Object result; - - // default test - ToscaOperationFacade answer = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer); - - result = testSubject.doWork(); - } - - @Test - public void testDoWorkFalse() throws Exception { - Object result; - - // default test - ComponentCache answer = Mockito.mock(ComponentCache.class); - Mockito.when(answer.getComponentAndTime(Mockito.anyString(), Mockito.any(Function.class))) - .thenReturn(Either.right(ActionStatus.ACCEPTED)); - Mockito.when(daoInfo.getComponentCache()).thenReturn(answer); - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = testSubject.doWork(); - } - - @Test - public void testDoWorkResourceNotFound() throws Exception { - Object result; - - // default test - ComponentCache answer = Mockito.mock(ComponentCache.class); - Either<ImmutablePair<Component, Long>, ActionStatus> value; - Mockito.when(answer.getComponentAndTime(Mockito.anyString(), Mockito.any(Function.class))) - .thenReturn(Either.right(ActionStatus.RESOURCE_NOT_FOUND)); - Mockito.when(daoInfo.getComponentCache()).thenReturn(answer); - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = testSubject.doWork(); - } - - @Test - public void testDoWork() throws Exception { - Object result; - - // default test - ComponentCache answer = Mockito.mock(ComponentCache.class); - ImmutablePair<Component, Long> value = ImmutablePair.of(new Resource(), 0L); - Mockito.when(answer.getComponentAndTime(Mockito.anyString(), Mockito.any(Function.class))) - .thenReturn(Either.left(value)); - Mockito.when(daoInfo.getComponentCache()).thenReturn(answer); - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = testSubject.doWork(); - } - - @Test - public void testDoWork1() throws Exception { - Object result; - - // default test - ComponentCache answer = Mockito.mock(ComponentCache.class); - ImmutablePair<Component, Long> value = ImmutablePair.of(new Resource(), 1L); - Mockito.when(answer.getComponentAndTime(Mockito.anyString(), Mockito.any(Function.class))) - .thenReturn(Either.left(value)); - Mockito.when(daoInfo.getComponentCache()).thenReturn(answer); - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = testSubject.doWork(); - } - - @Test - public void testUpdateCache() throws Exception { - String componentId = "mock"; - NodeTypeEnum nodeTypeEnum = null; - Long timestamp = null; - boolean result; - - // default test - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - ComponentMetadataData a = new ResourceMetadataData(); - a.getMetadataDataDefinition().setLastUpdateDate(0L); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.left(a)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - Mockito.when(answer1.getToscaElement(Mockito.anyString())).thenReturn(Either.left(new Resource())); - ComponentCache compCache = Mockito.mock(ComponentCache.class); - Mockito.when(compCache.setComponent(Mockito.any(), Mockito.any())).thenReturn(true); - Mockito.when(daoInfo.getComponentCache()).thenReturn(compCache); - - result = Deencapsulation.invoke(testSubject, "updateCache", componentId, NodeTypeEnum.Resource, 0L); - } - - @Test - public void testUpdateCacheFailedToUpdateCache() throws Exception { - String componentId = "mock"; - NodeTypeEnum nodeTypeEnum = null; - Long timestamp = null; - boolean result; - - // default test - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - ComponentMetadataData a = new ResourceMetadataData(); - a.getMetadataDataDefinition().setLastUpdateDate(0L); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.left(a)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - Mockito.when(answer1.getToscaElement(Mockito.anyString())).thenReturn(Either.left(new Resource())); - ComponentCache compCache = Mockito.mock(ComponentCache.class); - Mockito.when(daoInfo.getComponentCache()).thenReturn(compCache); - - result = Deencapsulation.invoke(testSubject, "updateCache", componentId, NodeTypeEnum.Resource, 0L); - } - - @Test - public void testUpdateCacheToscaElemntNotFound() throws Exception { - String componentId = "mock"; - NodeTypeEnum nodeTypeEnum = null; - Long timestamp = null; - boolean result; - - // default test - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - ComponentMetadataData a = new ResourceMetadataData(); - a.getMetadataDataDefinition().setLastUpdateDate(0L); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.left(a)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - Mockito.when(answer1.getToscaElement(Mockito.anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND)); - - result = Deencapsulation.invoke(testSubject, "updateCache", componentId, NodeTypeEnum.Resource, 0L); - } - - @Test - public void testUpdateCacheNotUpdatedTimestamp() throws Exception { - String componentId = "mock"; - NodeTypeEnum nodeTypeEnum = null; - Long timestamp = null; - boolean result; - - // default test - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - ComponentMetadataData a = new ResourceMetadataData(); - a.getMetadataDataDefinition().setLastUpdateDate(1L); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.left(a)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = Deencapsulation.invoke(testSubject, "updateCache", componentId, NodeTypeEnum.Resource, 0L); - } - - @Test - public void testUpdateCacheNotFound() throws Exception { - String componentId = "mock"; - NodeTypeEnum nodeTypeEnum = null; - Long timestamp = null; - boolean result; - - // default test - ToscaOperationFacade answer1 = Mockito.mock(ToscaOperationFacade.class); - Mockito.when(answer1.getComponentMetadata(Mockito.anyString())) - .thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND)); - Mockito.when(daoInfo.getToscaOperationFacade()).thenReturn(answer1); - - result = Deencapsulation.invoke(testSubject, "updateCache", componentId, NodeTypeEnum.Resource, 0L); - } -} |