2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
24 import java.io.FileFilter;
25 import java.io.IOException;
26 import java.nio.file.Files;
29 import javax.annotation.PostConstruct;
31 import org.apache.commons.io.filefilter.WildcardFileFilter;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.common.util.ZipUtil;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.gson.Gson;
39 import com.google.gson.JsonArray;
40 import com.google.gson.JsonElement;
41 import com.google.gson.JsonParser;
43 import fj.data.Either;
45 @org.springframework.stereotype.Component("csar-operation")
46 public class CsarOperation {
48 private static Logger log = LoggerFactory.getLogger(CsarOperation.class.getName());
50 @javax.annotation.Resource
51 private OnboardingClient onboardingClient;
53 public static void main(String[] args) {
55 CsarOperation csarOperation = new CsarOperation();
58 String csarUuid = "70025CF6081B489CA7B1CBA583D5278D";
59 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUuid, null);
60 System.out.println(csar.left().value());
69 public Either<Map<String, byte[]>, StorageOperationStatus> getMockCsar(String csarUuid) {
70 File dir = new File("/var/tmp/mockCsar");
71 FileFilter fileFilter = new WildcardFileFilter("*.csar");
72 File[] files = dir.listFiles(fileFilter);
73 for (int i = 0; i < files.length; i++) {
75 if (csar.getName().startsWith(csarUuid)) {
76 log.debug("Found CSAR file {} matching the passed csarUuid {}", csar.getAbsolutePath(), csarUuid);
79 data = Files.readAllBytes(csar.toPath());
80 } catch (IOException e) {
81 log.debug("Error reading mock file for CSAR, error: {}", e);
82 return Either.right(StorageOperationStatus.NOT_FOUND);
84 Map<String, byte[]> readZip = ZipUtil.readZip(data);
85 return Either.left(readZip);
88 log.debug("Couldn't find mock file for CSAR starting with {}", csarUuid);
89 return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
93 * get csar from remote repository
98 public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
100 Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
102 if (result.isRight()) {
103 log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
105 Map<String, byte[]> values = result.left().value();
106 if (values != null) {
107 log.debug("The returned files are {}", values.keySet());
114 @SuppressWarnings("unchecked")
115 public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
117 Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
119 if (result.isRight()) {
120 log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
122 String latestVersion = null;
123 JsonElement root = new JsonParser().parse(result.left().value());
124 JsonArray csarsInfo = root.getAsJsonObject().get("results").getAsJsonArray();
125 for (JsonElement csarInfo : csarsInfo) {
126 Map<String, String> csarInfoMap = new Gson().fromJson(csarInfo, Map.class);
127 if(csarInfoMap.get("packageId").equals(csarUuid)){
128 String curVersion = csarInfoMap.get("version");
129 if(latestVersion == null || isGreater(latestVersion, curVersion)){
130 latestVersion = curVersion;
134 if (latestVersion != null) {
135 result = Either.left(latestVersion);
137 log.debug("The returned packages are {}. Failed to find latest version for package with Id {}. ", result.left().value(), csarUuid);
138 result = Either.right(StorageOperationStatus.NOT_FOUND);
145 private boolean isGreater(String latestVersion, String currentVersion) {
146 return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
149 public OnboardingClient getOnboardingClient() {
150 return onboardingClient;