aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/wrapper/PackageWrapper.java
blob: 5bee0382bbfd1e8ea09143bb18a0e669cd69f0b0 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
 * Copyright 2016 [ZTE] and others.
 *
 * 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.
 */

package org.openo.commontosca.catalog.wrapper;

import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.openo.commontosca.catalog.common.CommonConstant;
import org.openo.commontosca.catalog.common.HttpServerPathConfig;
import org.openo.commontosca.catalog.common.RestUtil;
import org.openo.commontosca.catalog.common.ToolUtil;
import org.openo.commontosca.catalog.db.entity.PackageData;
import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
import org.openo.commontosca.catalog.db.resource.PackageManager;
import org.openo.commontosca.catalog.db.resource.TemplateManager;
import org.openo.commontosca.catalog.entity.request.PackageBasicInfo;
import org.openo.commontosca.catalog.entity.response.CsarFileUriResponse;
import org.openo.commontosca.catalog.entity.response.PackageMeta;
import org.openo.commontosca.catalog.entity.response.UploadPackageResponse;
import org.openo.commontosca.catalog.filemanage.FileManagerFactory;
import org.openo.commontosca.catalog.model.parser.ModelParserFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

public class PackageWrapper {
  private static PackageWrapper packageWrapper;
  private static final Logger LOG = LoggerFactory.getLogger(PackageWrapper.class);

  /**
   * get PackageWrapper instance.
   * @return package wrapper instance
   */
  public static PackageWrapper getInstance() {
    if (packageWrapper == null) {
      packageWrapper = new PackageWrapper();
    }
    return packageWrapper;
  }

  /**
   * query package by id.
   * @param csarId package id
   * @return Response
   */
  public Response queryPackageById(String csarId) {
    ArrayList<PackageData> dbResult = new ArrayList<PackageData>();
    ArrayList<PackageMeta> result = new ArrayList<PackageMeta>();
    dbResult = PackageWrapperUtil.getPackageInfoById(csarId);
    result = PackageWrapperUtil.packageDataList2PackageMetaList(dbResult);
    return Response.ok(result).build();
  }

  /**
   * upload package.
   * @param uploadedInputStream inputStream
   * @param fileDetail package detail
   * @param head http header
   * @return Response
   * @throws Exception e
   */
  public Response uploadPackage(InputStream uploadedInputStream,
      FormDataContentDisposition fileDetail, HttpHeaders head) throws Exception {
    int fileSize = 0;
    if (uploadedInputStream == null) {
      LOG.info("the uploadStream is null");
      return Response.serverError().build();
    }
    if (fileDetail == null) {
      LOG.info("the fileDetail is null");
      return Response.serverError().build();
    }
    LOG.info("the fileDetail = " + ToolUtil.objectToString(fileDetail));
    String contentRange = null;
    String fileName = "";
    fileName = ToolUtil.processFileName(fileDetail.getFileName());
    String tempDirName = null;
    tempDirName = ToolUtil.getTempDir(CommonConstant.CATALOG_CSAR_DIR_NAME, fileName);
    if (head != null) {
      contentRange = head.getHeaderString(CommonConstant.HTTP_HEADER_CONTENT_RANGE);
    }
    LOG.info("store package chunk file, fileName:" + fileName + ",contentRange:" + contentRange);
    if (ToolUtil.isEmptyString(contentRange)) {
      fileSize = uploadedInputStream.available();
      contentRange = "0-" + fileSize + "/" + fileSize;
    }
    String fileLocation =
        ToolUtil.storeChunkFileInLocal(tempDirName, fileName, uploadedInputStream);
    LOG.info("the fileLocation when upload package is :" + fileLocation);
    uploadedInputStream.close();

    PackageBasicInfo basicInfo = new PackageBasicInfo();
    basicInfo = PackageWrapperUtil.getPacageBasicInfo(fileLocation);
    String path = basicInfo.getType().toString() + File.separator + basicInfo.getProvider()
        + File.separator + fileName.replace(".csar", "") + File.separator + basicInfo.getVersion();
    LOG.info("dest path is : " + path);
    PackageMeta packageMeta = new PackageMeta();
    packageMeta = PackageWrapperUtil.getPackageMeta(fileName, fileLocation, basicInfo);
    String dowloadUri = File.separator + path + File.separator;
    String destPath = File.separator + path;
    packageMeta.setDownloadUri(dowloadUri);
    LOG.info("packageMeta = " + ToolUtil.objectToString(packageMeta));
    Boolean isEnd = PackageWrapperUtil.isUploadEnd(contentRange, fileName);
    PackageData packateDbData = new PackageData();
    if (isEnd) {
      String serviceTemplateId = null;
      boolean uploadResult = FileManagerFactory.createFileManager().upload(tempDirName, destPath);
      if (uploadResult == true) {
        PackageData packageData = PackageWrapperUtil.getPackageData(packageMeta);
        packateDbData = PackageManager.getInstance().addPackage(packageData);
        LOG.info("Store package data to database succed ! packateDbData = "
            + ToolUtil.objectToString(packateDbData));
        try {
          String tempCsarPath = tempDirName + File.separator + fileName;
          serviceTemplateId = ModelParserFactory.getInstance().parse(packateDbData.getCsarId(),
              tempCsarPath, PackageWrapperUtil.getPackageFormat(packateDbData.getFormat()));
          LOG.info("Package parse success ! serviceTemplateId = " + serviceTemplateId);
        } catch (Exception e1) {
          LOG.error("Parse package error ! ");
          PackageManager.getInstance().deletePackage(packateDbData.getCsarId());
          throw new Exception(e1);
        }

        if (null != packateDbData && null == serviceTemplateId) {
          LOG.info("Service template Id is null !");
          PackageManager.getInstance().deletePackage(packateDbData.getCsarId());
        }
      }
      LOG.info("upload package file end, fileName:" + fileName);
    }
    UploadPackageResponse result = new UploadPackageResponse();
    result.setCsarId(packateDbData.getCsarId());
    if (tempDirName != null) {
      ToolUtil.deleteDir(new File(tempDirName));
    }
    return Response.ok(result).build();
  }

  /**
   * delete package by package id.
   * @param csarId package id
   * @return Response
   */
  public Response delPackage(String csarId) {
    LOG.info("delete package  info.csarId:" + csarId);
    if (ToolUtil.isEmptyString(csarId)) {
      LOG.error("delete package  fail, csarid is null");
      return Response.serverError().build();
    }
    try {
      DelCsarThread thread = new DelCsarThread(csarId, false);
      new Thread(thread).start();
      return Response.noContent().build();
    } catch (Exception e1) {
      LOG.error("delete fail.", e1);
      return RestUtil.getRestException(e1.getMessage());
    }
  }

  // public Response delPackageByServiceTemplateId(String serviceTemplateId) {
  // LOG.info("delete package  info.serviceTemplateId:" + serviceTemplateId);
  // if (ToolUtil.isEmptyString(serviceTemplateId)) {
  // LOG.error("delete package  fail, serviceTemplateId is null");
  // return Response.serverError().build();
  // }
  // ArrayList<PackageData> result = new ArrayList<PackageData>();
  // try {
  // result = PackageManager.getInstance().queryPackageByServiceTemplateId(serviceTemplateId);
  //
  // } catch (CatalogResourceException e) {
  // LOG.error("query package by csarId from db error ! " + e.getMessage());
  // return RestUtil.getRestException(e.getMessage());
  // }
  // if (result.size() <= 0) {
  // LOG.warn("not exist package by serviceTemplateId");
  // return Response.status(Status.NOT_FOUND).build();
  // }
  // if ("true".equals(result.get(0).getDeletionPending())) {
  // LOG.info("start delete package.csarId:" + result.get(0).getCsarId());
  // delCsarThread thread = new delCsarThread(result.get(0).getCsarId(), true);
  // new Thread(thread).start();
  // }
  // return Response.noContent().build();
  // }

  class DelCsarThread implements Runnable {
    private String csarid;
    private boolean isInstanceTemplate = false;

    public DelCsarThread(String csarid, boolean isInstanceTemplate) {
      this.csarid = csarid;
      this.isInstanceTemplate = isInstanceTemplate;
    }

    @Override
    public void run() {
      try {
        if (!ToolUtil.isEmptyString(csarid)) {
          delCsarData(csarid);
        }
      } catch (Exception e1) {
        LOG.error("del instance csar fail.", e1);
        updatePackageStatus(csarid, null, null, null, CommonConstant.PACKAGE_STATUS_DELETE_FAIL,
            null);
        // publishDelFinishCometdMessage(csarid, "false");
      }
    }

    private void delCsarData(String csarId) {
      updatePackageStatus(csarid, null, null, null, CommonConstant.PACKAGE_STATUS_DELETING, null);
      String packagePath = PackageWrapperUtil.getPackagePath(csarId);
      if (packagePath == null) {
        LOG.error("package path is null! ");
        return;
      }
      FileManagerFactory.createFileManager().delete(packagePath);
      try {
        PackageManager.getInstance().deletePackage(csarId);
      } catch (CatalogResourceException e1) {
        LOG.error("delete package  by csarId from db error ! " + e1.getMessage());
      }
      // delete template data from db
      PackageData packageData = new PackageData();
      packageData.setCsarId(csarId);
      try {
        TemplateManager.getInstance().deleteServiceTemplateByCsarPackageInfo(packageData);
      } catch (CatalogResourceException e2) {
        LOG.error("delete template data from db error! csarId = " + csarId);
      }
      // publishDelFinishCometdMessage(csarid, "true");
    }

    // private void publishDelFinishCometdMessage(String csarid, String csarDelStatus) {
    // if (isInstanceTemplate) {
    // LOG.info("delete instance Template finish. csarid:{}", csarid);
    // return;
    // }
    // try {
    // Map<String, Object> cometdMessage = new HashMap<String, Object>();
    // cometdMessage.put("csarid", csarid);
    // cometdMessage.put("status", csarDelStatus);
    // CometdService.getInstance().publish(CommonConstant.COMETD_CHANNEL_PACKAGE_DELETE,
    // cometdMessage);
    // } catch (CometdException e) {
    // LOG.error("publish delfinish cometdmsg fail.", e);
    // }
    // }
  }

  /**
   * update package status.
   * @param csarId package id
   * @param operationalState package operational state
   * @param usageState package usage state
   * @param onBoardState package onboard state
   * @param processState package process state
   * @param deletionPending package deletionPending status
   * @return Response
   */
  public Response updatePackageStatus(String csarId, String operationalState, String usageState,
      String onBoardState, String processState, String deletionPending) {
    LOG.info("update package status info.csarId:" + csarId + " operationalState:"
        + operationalState);
    if (ToolUtil.isEmptyString(csarId)) {
      LOG.error("update csar status fail, csarid is null");
      return Response.serverError().build();
    }
    try {
      // UpdatePackageResponse result = new UpdatePackageResponse();
      PackageData packageInfo = new PackageData();
      if (operationalState != null) {
        packageInfo.setOperationalState(operationalState);
      }
      if (usageState != null) {
        packageInfo.setUsageState(usageState);
      }
      if (onBoardState != null) {
        packageInfo.setOnBoardState(onBoardState);
      }
      if (processState != null) {
        packageInfo.setProcessState(processState);
      }
      if (deletionPending != null) {
        packageInfo.setDeletionPending(deletionPending);
      }
      SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      String currentTime = sdf1.format(new Date());
      packageInfo.setModifyTime(currentTime);
      PackageManager.getInstance().updatePackage(packageInfo, csarId);
      // ArrayList<PackageData> pacackgeList = PackageWrapperUtil.getPackageInfoById(csarId);
      // String finalUsageState = pacackgeList.get(0).getUsageState();
      // result.setUsageState(finalUsageState);
      return Response.ok().build();
    } catch (CatalogResourceException e1) {
      LOG.error("update package status by csarId from db error ! " + e1.getMessage());
      return RestUtil.getRestException(e1.getMessage());
    }
  }

  /**
   * download package by package id.
   * @param csarId package id
   * @return Response
   */
  public Response downloadCsarPackagesById(String csarId) {
    ArrayList<PackageData> packageList = PackageWrapperUtil.getPackageInfoById(csarId);
    String packageName = null;
    if (null != packageList && packageList.size() > 0) {
      packageName = packageList.get(0).getName();
    }
    String path = ToolUtil.getCatalogueCsarPath() + File.separator + packageName;
    File csarFile = new File(path);
    if (!csarFile.exists()) {
      return Response.status(Status.NOT_FOUND).build();
    }

    try {
      InputStream fis = new BufferedInputStream(new FileInputStream(path));
      return Response.ok(fis)
          .header("Content-Disposition", "attachment; filename=\"" + csarFile.getName() + "\"")
          .build();
    } catch (Exception e1) {
      LOG.error("download vnf package fail.", e1);
      return RestUtil.getRestException(e1.getMessage());
    }
  }

  /**
   * query package list by condition.
   * @param name package name
   * @param provider package provider
   * @param version package version
   * @param deletionPending package deletionPending
   * @param type package type
   * @return Response
   */
  public Response queryPackageListByCond(String name, String provider, String version,
      String deletionPending, String type) {
    ArrayList<PackageData> dbresult = new ArrayList<PackageData>();
    ArrayList<PackageMeta> result = new ArrayList<PackageMeta>();
    LOG.info("query package info.name:" + name + " provider:" + provider + " version" + version
        + " deletionPending" + deletionPending + " type:" + type);
    try {
      dbresult =
          PackageManager.getInstance().queryPackage(name, provider, version, deletionPending, type);
      result = PackageWrapperUtil.packageDataList2PackageMetaList(dbresult);
      return Response.ok(result).build();
    } catch (CatalogResourceException e1) {
      LOG.error("query package by csarId from db error ! " + e1.getMessage());
      return RestUtil.getRestException(e1.getMessage());
    }
  }

  /**
   * get package file uri.
   * @param csarId package id
   * @param relativePath file relative path
   * @return Response
   */
  public Response getCsarFileUri(String csarId, String relativePath) {
    try {
      CsarFileUriResponse result = getCsarFileDownloadUri(csarId, relativePath);
      return Response.ok(result).build();
    } catch (CatalogResourceException e1) {
      LOG.error("Query CSAR package by ID failed ! csarId = " + csarId);
    }

    return Response.serverError().build();
  }

  /**
   * get package file download uri.
   * @param csarId package id
   * @param relativePath package file relative path
   * @return CsarFileUriResponse
   * @throws CatalogResourceException e
   */
  public CsarFileUriResponse getCsarFileDownloadUri(String csarId, String relativePath)
      throws CatalogResourceException {
    CsarFileUriResponse result = new CsarFileUriResponse();
    String downloadUrl = null;
    String downloadUri = null;
    String localPath = null;
    ArrayList<PackageData> packageList = PackageManager.getInstance().queryPackageByCsarId(csarId);
    if (packageList != null && packageList.size() != 0) {
      String packageName = packageList.get(0).getName();
      String relativeUri = packageList.get(0).getDownloadUri() + packageName;
      downloadUri = relativeUri + relativePath;
      downloadUrl = PackageWrapperUtil.getUrl(downloadUri);
      String httpUri = HttpServerPathConfig.getHttpServerPath() + downloadUri;
      localPath = PackageWrapperUtil.getLocalPath(httpUri);
    }
    result.setDownloadUri(downloadUrl);
    result.setLocalPath(localPath);
    return result;
  }

  
}