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;
23 import com.google.gson.Gson;
24 import com.google.gson.JsonArray;
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonParser;
27 import fj.data.Either;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import javax.annotation.PostConstruct;
35 @org.springframework.stereotype.Component("csar-operation")
36 public class CsarOperation {
38 private static final Logger log = Logger.getLogger(CsarOperation.class.getName());
40 @javax.annotation.Resource
41 private OnboardingClient onboardingClient;
43 public static void main(String[] args) {
45 CsarOperation csarOperation = new CsarOperation();
48 String csarUuid = "70025CF6081B489CA7B1CBA583D5278D";
49 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUuid, null);
50 System.out.println(csar.left().value());
60 * get csar from remote repository
65 public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
67 Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
69 if (result.isRight()) {
70 log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
72 Map<String, byte[]> values = result.left().value();
74 log.debug("The returned files are {}", values.keySet());
81 @SuppressWarnings("unchecked")
82 public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
84 Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
86 if (result.isRight()) {
87 log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
89 String latestVersion = null;
90 JsonElement root = new JsonParser().parse(result.left().value());
91 JsonArray csarsInfo = root.getAsJsonObject().get("results").getAsJsonArray();
92 for (JsonElement csarInfo : csarsInfo) {
93 Map<String, String> csarInfoMap = new Gson().fromJson(csarInfo, Map.class);
94 if(csarInfoMap.get("packageId").equals(csarUuid)){
95 String curVersion = csarInfoMap.get("version");
96 if(latestVersion == null || isGreater(latestVersion, curVersion)){
97 latestVersion = curVersion;
101 if (latestVersion != null) {
102 result = Either.left(latestVersion);
104 log.debug("The returned packages are {}. Failed to find latest version for package with Id {}. ", result.left().value(), csarUuid);
105 result = Either.right(StorageOperationStatus.NOT_FOUND);
112 private boolean isGreater(String latestVersion, String currentVersion) {
113 return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
116 public OnboardingClient getOnboardingClient() {
117 return onboardingClient;