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