push addional code
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-api / src / main / java / org / openecomp / sdc / versioning / VersioningUtil.java
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.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;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35
36 /**
37  * The type Versioning util.
38  */
39 public class VersioningUtil {
40
41   /**
42    * Validate entity existence.
43    *
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
48    */
49   public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
50                                                                  T inputEntity,
51                                                                  String firstClassCitizenType) {
52     if (retrievedEntity == null) {
53       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
54           inputEntity.getEntityType(),
55           inputEntity.getId(),
56           firstClassCitizenType,
57           inputEntity.getFirstClassCitizenId(),
58           inputEntity.getVersion()).build());
59     }
60   }
61
62   /**
63    * Validate entities existence.
64    *
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
71    */
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) {
75       return;
76     }
77
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);
83       }
84     }
85
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());
94       }
95       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
96           entity.getEntityType(),
97           nonExistingIds,
98           firstClassCitizenType,
99           entity.getFirstClassCitizenId(),
100           entity.getVersion()).build());
101     }
102   }
103
104   /**
105    * Validate contained entities existence.
106    *
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
112    */
113   public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
114       String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
115       Set<String> retrievedContainedEntityIds) {
116     if (inputContainedEntityIds == null) {
117       return;
118     }
119
120     List<String> nonExistingIds = inputContainedEntityIds.stream()
121         .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
122         .collect(Collectors.toList());
123
124     if (nonExistingIds.size() > 0) {
125       if (nonExistingIds.size() == 1) {
126         throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
127             containedEntityType,
128             nonExistingIds.get(0),
129             containingEntity.getEntityType(),
130             containingEntity.getId(),
131             containingEntity.getVersion()).build());
132       }
133       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
134           containedEntityType,
135           nonExistingIds,
136           containingEntity.getEntityType(),
137           containingEntity.getId(),
138           containingEntity.getVersion()).build());
139     }
140   }
141
142   /**
143    * Resolve version version.
144    *
145    * @param requestedVersion the requested version
146    * @param versionInfo      the version info
147    * @param finalOnly        the final only
148    * @return the version
149    */
150   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
151                                        boolean finalOnly) {
152     if (requestedVersion == null) {
153       if (finalOnly) {
154         if (versionInfo.getLatestFinalVersion() == null) {
155           throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
156         }
157         requestedVersion = versionInfo.getLatestFinalVersion();
158       } else {
159         requestedVersion = versionInfo.getActiveVersion();
160       }
161     } else {
162       if ((finalOnly && !requestedVersion.isFinal())
163           || !versionInfo.getViewableVersions().contains(requestedVersion)) {
164         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
165       }
166     }
167     return requestedVersion;
168   }
169
170   /**
171    * Resolve version version.
172    *
173    * @param requestedVersion the requested version
174    * @param versionInfo      the version info
175    * @return the version
176    */
177   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo) {
178     if (requestedVersion == null) {
179       requestedVersion = versionInfo.getActiveVersion();
180     } else {
181       if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
182         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
183       }
184     }
185     return requestedVersion;
186   }
187 }