aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-versioning-lib/openecomp-sdc-versioning-api/src/main/java/org/openecomp/sdc/versioning/VersioningUtil.java
blob: d51f8c0a2bc7f8dc26ba7a528dfbcac10b5c0a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.versioning;

import org.openecomp.core.dao.BaseDao;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdc.versioning.dao.types.VersionableEntity;
import org.openecomp.sdc.versioning.errors.RequestedVersionInvalidErrorBuilder;
import org.openecomp.sdc.versioning.errors.VersionableSubEntityNotFoundErrorBuilder;
import org.openecomp.sdc.versioning.types.VersionInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * The type Versioning util.
 */
public class VersioningUtil {

  /**
   * Validate entity existence.
   *
   * @param <T>                   the type parameter
   * @param retrievedEntity       the retrieved entity
   * @param inputEntity           the input entity
   * @param firstClassCitizenType the first class citizen type
   */
  public static <T extends VersionableEntity> void validateEntityExistence(Object retrievedEntity,
                                                                 T inputEntity,
                                                                 String firstClassCitizenType) {
    if (retrievedEntity == null) {
      throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
          inputEntity.getEntityType(),
          inputEntity.getId(),
          firstClassCitizenType,
          inputEntity.getFirstClassCitizenId(),
          inputEntity.getVersion()).build());
    }
  }

  /**
   * Validate entities existence.
   *
   * @param <T>                   the type parameter
   * @param <D>                   the type parameter
   * @param entityIds             the entity ids
   * @param entity                the entity
   * @param entityDao             the entity dao
   * @param firstClassCitizenType the first class citizen type
   */
  public static <T extends VersionableEntity, D extends BaseDao<T>> void validateEntitiesExistence(
      Set<String> entityIds, T entity, D entityDao, String firstClassCitizenType) {
    if (entityIds == null) {
      return;
    }

    List<String> nonExistingIds = new ArrayList<>();
    for (String entityId : entityIds) {
      entity.setId(entityId);
      if (entityDao.get(entity) == null) {
        nonExistingIds.add(entityId);
      }
    }

    if (nonExistingIds.size() > 0) {
      if (nonExistingIds.size() == 1) {
        throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
            entity.getEntityType(),
            nonExistingIds.get(0),
            firstClassCitizenType,
            entity.getFirstClassCitizenId(),
            entity.getVersion()).build());
      }
      throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
          entity.getEntityType(),
          nonExistingIds,
          firstClassCitizenType,
          entity.getFirstClassCitizenId(),
          entity.getVersion()).build());
    }
  }

  /**
   * Validate contained entities existence.
   *
   * @param <T>                         the type parameter
   * @param containedEntityType         the contained entity type
   * @param inputContainedEntityIds     the input contained entity ids
   * @param containingEntity            the containing entity
   * @param retrievedContainedEntityIds the retrieved contained entity ids
   */
  public static <T extends VersionableEntity> void validateContainedEntitiesExistence(
      String containedEntityType, Set<String> inputContainedEntityIds, T containingEntity,
      Set<String> retrievedContainedEntityIds) {
    if (inputContainedEntityIds == null) {
      return;
    }

    List<String> nonExistingIds = inputContainedEntityIds.stream()
        .filter(entityId -> !retrievedContainedEntityIds.contains(entityId))
        .collect(Collectors.toList());

    if (nonExistingIds.size() > 0) {
      if (nonExistingIds.size() == 1) {
        throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
            containedEntityType,
            nonExistingIds.get(0),
            containingEntity.getEntityType(),
            containingEntity.getId(),
            containingEntity.getVersion()).build());
      }
      throw new CoreException(new VersionableSubEntityNotFoundErrorBuilder(
          containedEntityType,
          nonExistingIds,
          containingEntity.getEntityType(),
          containingEntity.getId(),
          containingEntity.getVersion()).build());
    }
  }

  /**
   * Resolve version version.
   *
   * @param requestedVersion the requested version
   * @param versionInfo      the version info
   * @param finalOnly        the final only
   * @return the version
   */
  public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo,
                                       boolean finalOnly) {
    if (requestedVersion == null) {
      if (finalOnly) {
        if (versionInfo.getLatestFinalVersion() == null) {
          throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
        }
        requestedVersion = versionInfo.getLatestFinalVersion();
      } else {
        requestedVersion = versionInfo.getActiveVersion();
      }
    } else {
      if ((finalOnly && !requestedVersion.isFinal())
          || !versionInfo.getViewableVersions().contains(requestedVersion)) {
        throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
      }
    }
    return requestedVersion;
  }

  /**
   * Resolve version version.
   *
   * @param requestedVersion the requested version
   * @param versionInfo      the version info
   * @return the version
   */
  public static Version resolveVersion(Version requestedVersion, VersionInfo versionInfo) {
    if (requestedVersion == null) {
      requestedVersion = versionInfo.getActiveVersion();
    } else {
      if (!versionInfo.getViewableVersions().contains(requestedVersion)) {
        throw new CoreException(new RequestedVersionInvalidErrorBuilder().build());
      }
    }
    return requestedVersion;
  }
}