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;
28 import java.util.Map.Entry;
30 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.JsonObject;
42 import com.google.gson.JsonParser;
44 import fj.data.Either;
46 @org.springframework.stereotype.Component("csar-operation")
47 public class CsarOperation {
49 private static Logger log = LoggerFactory.getLogger(CsarOperation.class.getName());
51 @javax.annotation.Resource
52 private OnboardingClient onboardingClient;
54 public static void main(String[] args) {
56 CsarOperation csarOperation = new CsarOperation();
59 String csarUuid = "70025CF6081B489CA7B1CBA583D5278D";
60 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUuid, null);
61 System.out.println(csar.left().value());
70 public Either<Map<String, byte[]>, StorageOperationStatus> getMockCsar(String csarUuid) {
71 File dir = new File("/var/tmp/mockCsar");
72 FileFilter fileFilter = new WildcardFileFilter("*.csar");
73 File[] files = dir.listFiles(fileFilter);
74 for (int i = 0; i < files.length; i++) {
76 if (csar.getName().startsWith(csarUuid)) {
77 log.debug("Found CSAR file {} matching the passed csarUuid {}", csar.getAbsolutePath(), csarUuid);
80 data = Files.readAllBytes(csar.toPath());
81 } catch (IOException e) {
82 log.debug("Error reading mock file for CSAR, error: {}", e);
83 return Either.right(StorageOperationStatus.NOT_FOUND);
85 Map<String, byte[]> readZip = ZipUtil.readZip(data);
86 return Either.left(readZip);
89 log.debug("Couldn't find mock file for CSAR starting with {}", csarUuid);
90 return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
94 * get csar from remote repository
99 public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
101 Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
103 if (result.isRight()) {
104 log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
106 Map<String, byte[]> values = result.left().value();
107 if (values != null) {
108 log.debug("The returned files are {}", values.keySet());
115 @SuppressWarnings("unchecked")
116 public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
118 Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
120 if (result.isRight()) {
121 log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
123 String latestVersion = null;
124 JsonElement root = new JsonParser().parse(result.left().value());
125 JsonArray csarsInfo = root.getAsJsonObject().get("results").getAsJsonArray();
126 for (JsonElement csarInfo : csarsInfo) {
127 Map<String, String> csarInfoMap = new Gson().fromJson(csarInfo, Map.class);
128 if(csarInfoMap.get("packageId").equals(csarUuid)){
129 String curVersion = csarInfoMap.get("version");
130 if(latestVersion == null || isGreater(latestVersion, curVersion)){
131 latestVersion = curVersion;
135 if (latestVersion != null) {
136 result = Either.left(latestVersion);
138 log.debug("The returned packages are {}. Failed to find latest version for package with Id {}. ", result.left().value(), csarUuid);
139 result = Either.right(StorageOperationStatus.NOT_FOUND);
146 private boolean isGreater(String latestVersion, String currentVersion) {
147 return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
150 public OnboardingClient getOnboardingClient() {
151 return onboardingClient;