7411edec088c27a7a7ed5c5e8f6dcc58bec13dd8
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.versioning;
22
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;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Set;
35 import java.util.stream.Collectors;
36
37 /**
38  * The type Versioning util.
39  */
40 public class VersioningUtil {
41
42   private VersioningUtil() {
43     // prevent instantiation
44   }
45
46   /**
47    * Validate entity existence.
48    *
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
53    */
54   public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
55                                                                            T inputEntity,
56                                                                            String firstClassCitizenType) {
57     if (retrievedEntity == null) {
58       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
59           inputEntity.getEntityType(),
60           inputEntity.getId(),
61           firstClassCitizenType,
62           inputEntity.getFirstClassCitizenId(),
63           inputEntity.getVersion()).build());
64     }
65   }
66
67   /**
68    * Validate entities existence.
69    *
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
76    */
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) {
80       return;
81     }
82
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);
88       }
89     }
90
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());
99       }
100       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
101           entity.getEntityType(),
102           nonExistingIds,
103           firstClassCitizenType,
104           entity.getFirstClassCitizenId(),
105           entity.getVersion()).build());
106     }
107   }
108
109   /**
110    * Validate contained entities existence.
111    *
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
117    */
118   public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
119       String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
120       Set<String> retrievedContainedEntityIds) {
121     if (inputContainedEntityIds == null) {
122       return;
123     }
124
125     List<String> nonExistingIds = inputContainedEntityIds.stream()
126         .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
127         .collect(Collectors.toList());
128
129     if (!nonExistingIds.isEmpty()) {
130       if (nonExistingIds.size() == 1) {
131         throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
132             containedEntityType,
133             nonExistingIds.get(0),
134             containingEntity.getEntityType(),
135             containingEntity.getId(),
136             containingEntity.getVersion()).build());
137       }
138       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
139           containedEntityType,
140           nonExistingIds,
141           containingEntity.getEntityType(),
142           containingEntity.getId(),
143           containingEntity.getVersion()).build());
144     }
145   }
146
147   /**
148    * Resolve version version.
149    *
150    * @param requestedVersion the requested version
151    * @param versionInfo      the version info
152    * @param finalOnly        the final only
153    * @return the version
154    */
155   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
156                                        boolean finalOnly) {
157     if (requestedVersion == null) {
158       if (finalOnly) {
159         if (versionInfo.getLatestFinalVersion() == null) {
160           throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
161         }
162         requestedVersion = versionInfo.getLatestFinalVersion();
163       } else {
164         requestedVersion = versionInfo.getActiveVersion();
165       }
166     } else {
167       if ((finalOnly && !requestedVersion.isFinal())
168           || !versionInfo.getViewableVersions().contains(requestedVersion)) {
169         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
170       }
171     }
172     return requestedVersion;
173   }
174
175   /**
176    * Resolve version.
177    *
178    * @param requestedVersion the requested version
179    * @param versionInfo      the version info
180    * @return the version
181    */
182   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
183                                        String user) {
184     if (requestedVersion == null) {
185       requestedVersion = versionInfo.getActiveVersion();
186     }
187     if (versionInfo.getActiveVersion().equals(requestedVersion)
188         && user.equals(versionInfo.getLockingUser())) {
189       requestedVersion.setStatus(VersionStatus.Locked);
190     }
191     if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
192       throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
193     }
194     return requestedVersion;
195   }
196 }