aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/ProcessManagerImpl.java
blob: 4fa67ff2d3b517861f1ef7c46e099479f06675fc (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*-
 * ============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.vendorsoftwareproduct.impl;

import org.openecomp.core.util.UniqueValueUtil;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.sdc.activityLog.ActivityLogManager;
import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
import org.openecomp.sdc.logging.types.LoggerConstants;
import org.openecomp.sdc.logging.types.LoggerErrorCode;
import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
import org.openecomp.sdc.versioning.VersioningUtil;
import org.openecomp.sdc.versioning.dao.types.Version;
import org.openecomp.sdcrests.activitylog.types.ActivityType;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;

public class ProcessManagerImpl implements ProcessManager {
  private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
  private ActivityLogManager activityLogManager;

  private VendorSoftwareProductDao vendorSoftwareProductDao;

  public ProcessManagerImpl(VendorSoftwareProductDao vendorSoftwareProductDao, ActivityLogManager activityLogManager) {
    this.vendorSoftwareProductDao = vendorSoftwareProductDao;
    this.activityLogManager = activityLogManager;
  }

  @Override
  public Collection<ProcessEntity> listProcesses(String vspId, Version version,
                                                 String componentId,
                                                 String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);
    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);

    return vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
  }

  @Override
  public void deleteProcesses(String vspId, Version version, String componentId, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    Collection<ProcessEntity> processes =
        vendorSoftwareProductDao.listProcesses(vspId, version, componentId);

    if (!processes.isEmpty()) {
      for (ProcessEntity process : processes) {
        deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
            process.getName());
      }

      vendorSoftwareProductDao.deleteProcesses(vspId, version, componentId);
    }
    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
  }

  @Override
  public ProcessEntity createProcess(ProcessEntity process, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", process.getId(),
        process.getComponentId());
    validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
        process.getName());
    //process.setId(CommonMethods.nextUuId());

    vendorSoftwareProductDao.createProcess(process);
    createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
        process.getName());

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", process.getId(),
        process.getComponentId());

    return process;
  }


  @Override
  public ProcessEntity getProcess(String vspId, Version version, String componentId,
                                  String processId, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    ProcessEntity retrieved =
        vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
    validateProcessExistence(vspId, version, componentId, processId, retrieved);

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);

    return retrieved;
  }

  @Override
  public void updateProcess(ProcessEntity process, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", process.getId(),
        process.getComponentId());

    ProcessEntity retrieved = vendorSoftwareProductDao
        .getProcess(process.getVspId(), process.getVersion(), process.getComponentId(),
            process.getId());
    validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
        process.getId(), retrieved);

    updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
        retrieved.getName(), process.getName());
    vendorSoftwareProductDao.updateProcess(process);

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", process.getId(),
        process.getComponentId());
  }

  @Override
  public void deleteProcess(String vspId, Version version, String componentId, String processId,
                            String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    ProcessEntity retrieved =
        vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
    validateProcessExistence(vspId, version, componentId, processId, retrieved);

    vendorSoftwareProductDao.deleteProcess(vspId, version, componentId, processId);
    deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
        retrieved.getName());

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
  }


  @Override
  public File getProcessArtifact(String vspId, Version version, String componentId,
                                 String processId, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    ProcessEntity retrieved =
        vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
    validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);

    File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
    try {
      FileOutputStream fos = new FileOutputStream(file);
      fos.write(retrieved.getArtifact().array());
      fos.close();
    } catch (IOException exception) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.GET_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get process artifact");
      throw new CoreException(new UploadInvalidErrorBuilder().build());
    }

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);

    return file;
  }

  @Override
  public void deleteProcessArtifact(String vspId, Version version, String componentId,
                                    String processId, String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    ProcessEntity retrieved =
        vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
    validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);

    vendorSoftwareProductDao.deleteProcessArtifact(vspId, version, componentId, processId);

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
  }

  @Override
  public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
                                    Version version, String componentId, String processId,
                                    String user) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id", vspId, componentId);

    ProcessEntity retrieved =
        vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
    validateProcessExistence(vspId, version, componentId, processId, retrieved);

    if (artifactFile == null) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
      throw new CoreException(new UploadInvalidErrorBuilder().build());
    }

    byte[] artifact;
    try {
      artifact = FileUtils.toByteArray(artifactFile);
    } catch (RuntimeException exception) {
      MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
          LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
          LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
      throw new CoreException(new UploadInvalidErrorBuilder().build());
    }

    vendorSoftwareProductDao.uploadProcessArtifact(vspId, version, componentId, processId, artifact,
            artifactFileName);
    ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspId, String.valueOf(version.getMajor()+1),
            ActivityType.UPLOAD_ARTIFACT.toString(), user, true, "", "");
    activityLogManager.addActionLog(activityLogEntity, user);

    mdcDataDebugMessage.debugExitMessage("VSP id, component id", vspId, componentId);
  }


  private void validateProcessExistence(String vspId, Version version, String componentId,
                                        String processId, ProcessEntity retrieved) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id, process id", vspId, componentId,
        processId);

    if (retrieved != null) {
      return;
    }
    VersioningUtil.validateEntityExistence(retrieved,
        new ProcessEntity(vspId, version, componentId, processId),
        VspDetails.ENTITY_TYPE);//todo retrieved is always null ??

    mdcDataDebugMessage.debugExitMessage("VSP id, component id, process id", vspId, componentId,
        processId);
  }

  private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
                                                String processId, ProcessEntity retrieved) {
    mdcDataDebugMessage.debugEntryMessage("VSP id, component id, process id", vspId, componentId,
        processId);

    if (retrieved != null) {
      VersioningUtil.validateEntityExistence(retrieved.getArtifact(),
          new ProcessEntity(vspId, version, componentId, processId),
          VspDetails.ENTITY_TYPE);
    } else {
      VersioningUtil.validateEntityExistence(retrieved,
          new ProcessEntity(vspId, version, componentId, processId),
          VspDetails.ENTITY_TYPE); //todo retrieved is always null ??
    }

    mdcDataDebugMessage.debugExitMessage("VSP id, component id, process id", vspId, componentId,
        processId);
  }


  protected void validateUniqueName(String vspId, Version version, String componentId,
                                    String processName) {
    UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
        vspId, version.toString(), componentId, processName);
  }

  protected void createUniqueName(String vspId, Version version, String componentId,
                                  String processName) {
    UniqueValueUtil
        .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
            version.toString(), componentId, processName);
  }

  protected void updateUniqueName(String vspId, Version version, String componentId,
                                  String oldProcessName, String newProcessName) {
    UniqueValueUtil
        .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
            newProcessName, vspId, version.toString(), componentId);
  }

  protected void deleteUniqueValue(String vspId, Version version, String componentId,
                                   String processName) {
    UniqueValueUtil
        .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
            version.toString(), componentId, processName);
  }
}