new unit tests for sdc-dao
[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 java.io.File;
24 import java.io.FileFilter;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.util.Map;
28
29 import javax.annotation.PostConstruct;
30
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;
37
38 import com.google.gson.Gson;
39 import com.google.gson.JsonArray;
40 import com.google.gson.JsonElement;
41 import com.google.gson.JsonParser;
42
43 import fj.data.Either;
44
45 @org.springframework.stereotype.Component("csar-operation")
46 public class CsarOperation {
47
48         private static Logger log = LoggerFactory.getLogger(CsarOperation.class.getName());
49
50         @javax.annotation.Resource
51         private OnboardingClient onboardingClient;
52
53         public static void main(String[] args) {
54
55                 CsarOperation csarOperation = new CsarOperation();
56                 csarOperation.init();
57
58                 String csarUuid = "70025CF6081B489CA7B1CBA583D5278D";
59                 Either<Map<String, byte[]>, StorageOperationStatus> csar = csarOperation.getCsar(csarUuid, null);
60                 System.out.println(csar.left().value());
61
62         }
63
64         @PostConstruct
65         public void init() {
66
67         }
68
69         public Either<Map<String, byte[]>, StorageOperationStatus> getMockCsar(String csarUuid) {
70                 File dir = new File("/var/tmp/mockCsar");
71                 FileFilter fileFilter = new WildcardFileFilter("*.csar");
72                 File[] files = dir.listFiles(fileFilter);
73                 for (int i = 0; i < files.length; i++) {
74                         File csar = files[i];
75                         if (csar.getName().startsWith(csarUuid)) {
76                                 log.debug("Found CSAR file {} matching the passed csarUuid {}", csar.getAbsolutePath(), csarUuid);
77                                 byte[] data;
78                                 try {
79                                         data = Files.readAllBytes(csar.toPath());
80                                 } catch (IOException e) {
81                                         log.debug("Error reading mock file for CSAR, error: {}", e);
82                                         return Either.right(StorageOperationStatus.NOT_FOUND);
83                                 }
84                                 Map<String, byte[]> readZip = ZipUtil.readZip(data);
85                                 return Either.left(readZip);
86                         }
87                 }
88                 log.debug("Couldn't find mock file for CSAR starting with {}", csarUuid);
89                 return Either.right(StorageOperationStatus.CSAR_NOT_FOUND);
90         }
91
92         /**
93          * get csar from remote repository
94          * 
95          * @param csarUuid
96          * @return
97          */
98         public Either<Map<String, byte[]>, StorageOperationStatus> getCsar(String csarUuid, User user) {
99
100                 Either<Map<String, byte[]>, StorageOperationStatus> result = onboardingClient.getCsar(csarUuid, user.getUserId());
101
102                 if (result.isRight()) {
103                         log.debug("Cannot find csar {}. Staus returned is {}", csarUuid, result.right().value());
104                 } else {
105                         Map<String, byte[]> values = result.left().value();
106                         if (values != null) {
107                                 log.debug("The returned files are {}", values.keySet());
108                         }
109                 }
110
111                 return result;
112         }
113         
114         @SuppressWarnings("unchecked")
115         public Either<String, StorageOperationStatus> getCsarLatestVersion(String csarUuid, User user) {
116
117                 Either<String, StorageOperationStatus> result = onboardingClient.getPackages(user.getUserId());
118
119                 if (result.isRight()) {
120                         log.debug("Cannot find version for package with Id {}. Status returned is {}", csarUuid, result.right().value());
121                 } else {
122                         String latestVersion = null;
123                         JsonElement root = new JsonParser().parse(result.left().value());
124                         JsonArray csarsInfo = root.getAsJsonObject().get("results").getAsJsonArray();
125                         for (JsonElement csarInfo : csarsInfo) {
126                                 Map<String, String> csarInfoMap = new Gson().fromJson(csarInfo, Map.class);
127                                 if(csarInfoMap.get("packageId").equals(csarUuid)){
128                                     String curVersion = csarInfoMap.get("version");
129                                     if(latestVersion == null || isGreater(latestVersion, curVersion)){
130                                         latestVersion = curVersion;
131                                     }
132                                 }
133                         }
134                         if (latestVersion != null) {
135                                 result = Either.left(latestVersion);
136                         } else {
137                                 log.debug("The returned packages are {}. Failed to find latest version for package with Id {}. ", result.left().value(), csarUuid);
138                                 result = Either.right(StorageOperationStatus.NOT_FOUND);
139                         }
140                 }
141
142                 return result;
143         }
144
145         private boolean isGreater(String latestVersion, String currentVersion) {
146                 return Double.parseDouble(latestVersion) < Double.parseDouble(currentVersion);
147         }
148
149         public OnboardingClient getOnboardingClient() {
150                 return onboardingClient;
151         }
152
153 }