Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / CsarOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdc.be.model.operations.impl;
21
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;
27 import java.util.Map;
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;
32
33 @org.springframework.stereotype.Component("csar-operation")
34 public class CsarOperation {
35
36     private static final Logger log = Logger.getLogger(CsarOperation.class.getName());
37     @javax.annotation.Resource
38     private OnboardingClient onboardingClient;
39
40     @PostConstruct
41     public void init() {
42     }
43
44     /**
45      * get csar from remote repository
46      *
47      * @param csarUuid
48      * @return
49      */
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());
54         } else {
55             Map<String, byte[]> values = result.left().value();
56             if (values != null) {
57                 log.debug("The returned files are {}", values.keySet());
58             }
59         }
60         return result;
61     }
62
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());
68         } else {
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;
78                     }
79                 }
80             }
81             if (latestVersion != null) {
82                 result = Either.left(latestVersion);
83             } else {
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);
86             }
87         }
88         return result;
89     }
90
91     private boolean isGreater(String latestVersion, String currentVersion) {
92         return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
93     }
94
95     public OnboardingClient getOnboardingClient() {
96         return onboardingClient;
97     }
98 }