diff options
Diffstat (limited to 'catalog-model/src')
15 files changed, 0 insertions, 2814 deletions
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); - } -} |