[SDC-29] Amdocs OnBoard 1707 initial commit.
[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.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   /**
43    * Validate entity existence.
44    *
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
49    */
50   public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
51                                                                            T inputEntity,
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(),
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.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());
103       }
104       throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
105           entity.getEntityType(),
106           nonExistingIds,
107           firstClassCitizenType,
108           entity.getFirstClassCitizenId(),
109           entity.getVersion()).build());
110     }
111   }
112
113   /**
114    * Validate contained entities existence.
115    *
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
121    */
122   public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
123       String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
124       Set<String> retrievedContainedEntityIds) {
125     if (inputContainedEntityIds == null) {
126       return;
127     }
128
129     List<String> nonExistingIds = inputContainedEntityIds.stream()
130         .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
131         .collect(Collectors.toList());
132
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(
140             containedEntityType,
141             nonExistingIds.get(0),
142             containingEntity.getEntityType(),
143             containingEntity.getId(),
144             containingEntity.getVersion()).build());
145       }
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(
151           containedEntityType,
152           nonExistingIds,
153           containingEntity.getEntityType(),
154           containingEntity.getId(),
155           containingEntity.getVersion()).build());
156     }
157   }
158
159   /**
160    * Resolve version version.
161    *
162    * @param requestedVersion the requested version
163    * @param versionInfo      the version info
164    * @param finalOnly        the final only
165    * @return the version
166    */
167   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
168                                        boolean finalOnly) {
169     if (requestedVersion == null) {
170       if (finalOnly) {
171         if (versionInfo.getLatestFinalVersion() == null) {
172           throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
173         }
174         requestedVersion = versionInfo.getLatestFinalVersion();
175       } else {
176         requestedVersion = versionInfo.getActiveVersion();
177       }
178     } else {
179       if ((finalOnly && !requestedVersion.isFinal())
180           || !versionInfo.getViewableVersions().contains(requestedVersion)) {
181         throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
182       }
183     }
184     return requestedVersion;
185   }
186
187   /**
188    * Resolve version.
189    *
190    * @param requestedVersion the requested version
191    * @param versionInfo      the version info
192    * @return the version
193    */
194   public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
195                                        String user) {
196     if (requestedVersion == null) {
197       requestedVersion = versionInfo.getActiveVersion();
198     }
199     if (versionInfo.getActiveVersion().equals(requestedVersion)
200         && user.equals(versionInfo.getLockingUser())) {
201       requestedVersion.setStatus(VersionStatus.Locked);
202     }
203     if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
204       throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
205     }
206     return requestedVersion;
207   }
208 }