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.VersionStatus;
27 import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
28 import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
29 import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
30 import org.openecomp.sdc.versioning.types.VersionInfo;
32 import java.util.ArrayList;
33 import java.util.List;
35 import java.util.stream.Collectors;
38 * The type Versioning util.
40 public class VersioningUtil {
42 private VersioningUtil() {
43 // prevent instantiation
47 * Validate entity existence.
49 * @param <T> the type parameter
50 * @param retrievedEntity the retrieved entity
51 * @param inputEntity the input entity
52 * @param firstClassCitizenType the first class citizen type
54 public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
56 String firstClassCitizenType) {
57 if (retrievedEntity == null) {
58 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
59 inputEntity.getEntityType(),
61 firstClassCitizenType,
62 inputEntity.getFirstClassCitizenId(),
63 inputEntity.getVersion()).build());
68 * Validate entities existence.
70 * @param <T> the type parameter
71 * @param <D> the type parameter
72 * @param entityIds the entity ids
73 * @param entity the entity
74 * @param entityDao the entity dao
75 * @param firstClassCitizenType the first class citizen type
77 public static <T extends VersionableEntity, D extends BaseDao<T>> void validateEntitiesExistence(
78 Set<String> entityIds, T entity, D entityDao, String firstClassCitizenType) {
79 if (entityIds == null) {
83 List<String> nonExistingIds = new ArrayList<>();
84 for (String entityId : entityIds) {
85 entity.setId(entityId);
86 if (entityDao.get(entity) == null) {
87 nonExistingIds.add(entityId);
91 if (!nonExistingIds.isEmpty()) {
92 if (nonExistingIds.size() == 1) {
93 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
94 entity.getEntityType(),
95 nonExistingIds.get(0),
96 firstClassCitizenType,
97 entity.getFirstClassCitizenId(),
98 entity.getVersion()).build());
100 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
101 entity.getEntityType(),
103 firstClassCitizenType,
104 entity.getFirstClassCitizenId(),
105 entity.getVersion()).build());
110 * Validate contained entities existence.
112 * @param <T> the type parameter
113 * @param containedEntityType the contained entity type
114 * @param inputContainedEntityIds the input contained entity ids
115 * @param containingEntity the containing entity
116 * @param retrievedContainedEntityIds the retrieved contained entity ids
118 public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
119 String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
120 Set<String> retrievedContainedEntityIds) {
121 if (inputContainedEntityIds == null) {
125 List<String> nonExistingIds = inputContainedEntityIds.stream()
126 .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
127 .collect(Collectors.toList());
129 if (!nonExistingIds.isEmpty()) {
130 if (nonExistingIds.size() == 1) {
131 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
133 nonExistingIds.get(0),
134 containingEntity.getEntityType(),
135 containingEntity.getId(),
136 containingEntity.getVersion()).build());
138 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
141 containingEntity.getEntityType(),
142 containingEntity.getId(),
143 containingEntity.getVersion()).build());
148 * Resolve version version.
150 * @param requestedVersion the requested version
151 * @param versionInfo the version info
152 * @param finalOnly the final only
153 * @return the version
155 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
157 if (requestedVersion == null) {
159 if (versionInfo.getLatestFinalVersion() == null) {
160 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
162 requestedVersion = versionInfo.getLatestFinalVersion();
164 requestedVersion = versionInfo.getActiveVersion();
167 if ((finalOnly && !requestedVersion.isFinal())
168 || !versionInfo.getViewableVersions().contains(requestedVersion)) {
169 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
172 return requestedVersion;
178 * @param requestedVersion the requested version
179 * @param versionInfo the version info
180 * @return the version
182 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
184 if (requestedVersion == null) {
185 requestedVersion = versionInfo.getActiveVersion();
187 if (versionInfo.getActiveVersion().equals(requestedVersion)
188 && user.equals(versionInfo.getLockingUser())) {
189 requestedVersion.setStatus(VersionStatus.Locked);
191 if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
192 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
194 return requestedVersion;