2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.versioning;
23 import org.openecomp.core.dao.BaseDao;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.versioning.dao.types.Version;
26 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
27 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
28 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
29 import org.openecomp.sdc.versioning.types.VersionInfo;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.stream.Collectors;
37 * The type Versioning util.
39 public class VersioningUtil {
42 * Validate entity existence.
44 * @param <T> the type parameter
45 * @param retrievedEntity the retrieved entity
46 * @param inputEntity the input entity
47 * @param firstClassCitizenType the first class citizen type
49 public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
51 String firstClassCitizenType) {
52 if (retrievedEntity == null) {
53 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
54 inputEntity.getEntityType(),
56 firstClassCitizenType,
57 inputEntity.getFirstClassCitizenId(),
58 inputEntity.getVersion()).build());
63 * Validate entities existence.
65 * @param <T> the type parameter
66 * @param <D> the type parameter
67 * @param entityIds the entity ids
68 * @param entity the entity
69 * @param entityDao the entity dao
70 * @param firstClassCitizenType the first class citizen type
72 public static <T extends VersionableEntity, D extends BaseDao<T>> void validateEntitiesExistence(
73 Set<String> entityIds, T entity, D entityDao, String firstClassCitizenType) {
74 if (entityIds == null) {
78 List<String> nonExistingIds = new ArrayList<>();
79 for (String entityId : entityIds) {
80 entity.setId(entityId);
81 if (entityDao.get(entity) == null) {
82 nonExistingIds.add(entityId);
86 if (nonExistingIds.size() > 0) {
87 if (nonExistingIds.size() == 1) {
88 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
89 entity.getEntityType(),
90 nonExistingIds.get(0),
91 firstClassCitizenType,
92 entity.getFirstClassCitizenId(),
93 entity.getVersion()).build());
95 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
96 entity.getEntityType(),
98 firstClassCitizenType,
99 entity.getFirstClassCitizenId(),
100 entity.getVersion()).build());
105 * Validate contained entities existence.
107 * @param <T> the type parameter
108 * @param containedEntityType the contained entity type
109 * @param inputContainedEntityIds the input contained entity ids
110 * @param containingEntity the containing entity
111 * @param retrievedContainedEntityIds the retrieved contained entity ids
113 public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
114 String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
115 Set<String> retrievedContainedEntityIds) {
116 if (inputContainedEntityIds == null) {
120 List<String> nonExistingIds = inputContainedEntityIds.stream()
121 .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
122 .collect(Collectors.toList());
124 if (nonExistingIds.size() > 0) {
125 if (nonExistingIds.size() == 1) {
126 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
128 nonExistingIds.get(0),
129 containingEntity.getEntityType(),
130 containingEntity.getId(),
131 containingEntity.getVersion()).build());
133 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
136 containingEntity.getEntityType(),
137 containingEntity.getId(),
138 containingEntity.getVersion()).build());
143 * Resolve version version.
145 * @param requestedVersion the requested version
146 * @param versionInfo the version info
147 * @param finalOnly the final only
148 * @return the version
150 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
152 if (requestedVersion == null) {
154 if (versionInfo.getLatestFinalVersion() == null) {
155 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
157 requestedVersion = versionInfo.getLatestFinalVersion();
159 requestedVersion = versionInfo.getActiveVersion();
162 if ((finalOnly && !requestedVersion.isFinal())
163 || !versionInfo.getViewableVersions().contains(requestedVersion)) {
164 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
167 return requestedVersion;
171 * Resolve version version.
173 * @param requestedVersion the requested version
174 * @param versionInfo the version info
175 * @return the version
177 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo) {
178 if (requestedVersion == null) {
179 requestedVersion = versionInfo.getActiveVersion();
181 if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
182 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
185 return requestedVersion;