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=========================================================
20 package org.openecomp.sdc.be.model.operations.impl;
22 import com.google.gson.Gson;
23 import com.google.gson.JsonArray;
24 import com.google.gson.JsonElement;
25 import com.google.gson.JsonParser;
26 import fj.data.Either;
28 import javax.annotation.PostConstruct;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
33 @org.springframework.stereotype.Component("csar-operation")
34 public class CsarOperation {
36 private static final Logger log = Logger.getLogger(CsarOperation.class.getName());
37 @javax.annotation.Resource
38 private OnboardingClient onboardingClient;
45 * get csar from remote repository
50 public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
51 Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
52 if (result.isRight()) {
53 log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
55 Map<String, byte[]> values = result.left().value();
57 log.debug("The returned files are {}", values.keySet());
63 @SuppressWarnings("unchecked")
64 public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
65 Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
66 if (result.isRight()) {
67 log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
69 String latestVersion = null;
70 JsonElement root = new JsonParser().parse(result.left().value());
71 JsonArray csarsInfo = root.getAsJsonObject().get("results").getAsJsonArray();
72 for (JsonElement csarInfo : csarsInfo) {
73 Map<String, String> csarInfoMap = new Gson().fromJson(csarInfo, Map.class);
74 if (csarInfoMap.get("packageId").equals(csarUuid)) {
75 String curVersion = csarInfoMap.get("version");
76 if (latestVersion == null || isGreater(latestVersion, curVersion)) {
77 latestVersion = curVersion;
81 if (latestVersion != null) {
82 result = Either.left(latestVersion);
84 log.debug("The returned packages are {}. Failed to find latest version for package with Id {}. ", result.left().value(), csarUuid);
85 result = Either.right(StorageOperationStatus.NOT_FOUND);
91 private boolean isGreater(String latestVersion, String currentVersion) {
92 return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
95 public OnboardingClient getOnboardingClient() {
96 return onboardingClient;