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 {
43 * Validate entity existence.
45 * @param <T> the type parameter
46 * @param retrievedEntity the retrieved entity
47 * @param inputEntity the input entity
48 * @param firstClassCitizenType the first class citizen type
50 public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
52 String firstClassCitizenType) {
53 if (retrievedEntity == null) {
54 //MdcDataErrorMessage.createErrorMessageAndUpdateMDC(LoggerServiceName
55 // .getServiceName(LoggerServiceName.Submit_Entity),
56 // LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.SUBMIT_ENTITY,
57 // ErrorLevel.ERROR.name(), null, LoggerErrorDescription.SUBMIT_ENTITY);
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.size() > 0) {
92 //MdcDataErrorMessage.createErrorMessageAndUpdateMDC
93 // (LoggerServiceName.getServiceName(LoggerServiceName.Submit_Entity),
94 // LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.SUBMIT_ENTITY,
95 // ErrorLevel.ERROR.name(), null, LoggerErrorDescription.SUBMIT_ENTITY);
96 if (nonExistingIds.size() == 1) {
97 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
98 entity.getEntityType(),
99 nonExistingIds.get(0),
100 firstClassCitizenType,
101 entity.getFirstClassCitizenId(),
102 entity.getVersion()).build());
104 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
105 entity.getEntityType(),
107 firstClassCitizenType,
108 entity.getFirstClassCitizenId(),
109 entity.getVersion()).build());
114 * Validate contained entities existence.
116 * @param <T> the type parameter
117 * @param containedEntityType the contained entity type
118 * @param inputContainedEntityIds the input contained entity ids
119 * @param containingEntity the containing entity
120 * @param retrievedContainedEntityIds the retrieved contained entity ids
122 public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
123 String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
124 Set<String> retrievedContainedEntityIds) {
125 if (inputContainedEntityIds == null) {
129 List<String> nonExistingIds = inputContainedEntityIds.stream()
130 .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
131 .collect(Collectors.toList());
133 if (nonExistingIds.size() > 0) {
134 if (nonExistingIds.size() == 1) {
135 //MdcDataErrorMessage.createErrorMessageAndUpdateMDC(LoggerServiceName
136 // .getServiceName(LoggerServiceName.Submit_Entity), LoggerConstants.TARGET_ENTITY_DB,
137 // LoggerTragetServiceName.ENTIT, ErrorLevel.ERROR.name(),
138 // null, LoggerErrorDescription.SUBMIT_ENTITY);
139 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
141 nonExistingIds.get(0),
142 containingEntity.getEntityType(),
143 containingEntity.getId(),
144 containingEntity.getVersion()).build());
146 //MdcDataErrorMessage.createErrorMessageAndUpdateMDC(LoggerServiceName
147 // .getServiceName(LoggerServiceName.Submit_Entity),
148 // LoggerConstants.TARGET_ENTITY_DB, LoggerTragetServiceName.SUBMIT_ENTITY,
149 // ErrorLevel.ERROR.name(), null, LoggerErrorDescription.SUBMIT_ENTITY);
150 throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
153 containingEntity.getEntityType(),
154 containingEntity.getId(),
155 containingEntity.getVersion()).build());
160 * Resolve version version.
162 * @param requestedVersion the requested version
163 * @param versionInfo the version info
164 * @param finalOnly the final only
165 * @return the version
167 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
169 if (requestedVersion == null) {
171 if (versionInfo.getLatestFinalVersion() == null) {
172 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
174 requestedVersion = versionInfo.getLatestFinalVersion();
176 requestedVersion = versionInfo.getActiveVersion();
179 if ((finalOnly && !requestedVersion.isFinal())
180 || !versionInfo.getViewableVersions().contains(requestedVersion)) {
181 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
184 return requestedVersion;
190 * @param requestedVersion the requested version
191 * @param versionInfo the version info
192 * @return the version
194 public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
196 if (requestedVersion == null) {
197 requestedVersion = versionInfo.getActiveVersion();
199 if (versionInfo.getActiveVersion().equals(requestedVersion)
200 && user.equals(versionInfo.getLockingUser())) {
201 requestedVersion.setStatus(VersionStatus.Locked);
203 if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
204 throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
206 return requestedVersion;