Increase unit test coverage
[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
21 package org.openecomp.sdc.be.model.operations.impl;
22
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;
31
32 import javax.annotation.PostConstruct;
33 import java.util.Map;
34
35 @org.springframework.stereotype.Component("csar-operation")
36 public class CsarOperation {
37
38     private static final Logger log = Logger.getLogger(CsarOperation.class.getName());
39
40     @javax.annotation.Resource
41     private OnboardingClient onboardingClient;
42
43     public static void main(String[] args) {
44
45         CsarOperation csarOperation = new CsarOperation();
46         csarOperation.init();
47
48         String csarUuid = "70025CF6081B489CA7B1CBA583D5278D";
49         Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUuid, null);
50         System.out.println(csar.left().value());
51
52     }
53
54     @PostConstruct
55     public void init() {
56
57     }
58
59     /**
60      * get csar from remote repository
61      *
62      * @param csarUuid
63      * @return
64      */
65     public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
66
67         Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
68
69         if (result.isRight()) {
70             log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
71         } else {
72             Map<String, byte[]> values = result.left().value();
73             if (values != null) {
74                 log.debug("The returned files are {}", values.keySet());
75             }
76         }
77
78         return result;
79     }
80
81     @SuppressWarnings("unchecked")
82     public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
83
84         Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
85
86         if (result.isRight()) {
87             log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
88         } else {
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;
98                     }
99                 }
100             }
101             if (latestVersion != null) {
102                 result = Either.left(latestVersion);
103             } else {
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);
106             }
107         }
108
109         return result;
110     }
111
112     private boolean isGreater(String latestVersion, String currentVersion) {
113         return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
114     }
115
116     public OnboardingClient getOnboardingClient() {
117         return onboardingClient;
118     }
119
120 }