Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / asdc / local / LocalAsdcClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.vid.asdc.local;
23
24 import com.fasterxml.jackson.core.JsonParseException;
25 import com.fasterxml.jackson.databind.JsonMappingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import io.joshworks.restclient.http.HttpResponse;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.UnsupportedEncodingException;
32 import java.net.URLDecoder;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.UUID;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.onap.vid.aai.HttpResponseWithRequestInfo;
39 import org.onap.vid.asdc.AsdcCatalogException;
40 import org.onap.vid.asdc.AsdcClient;
41 import org.onap.vid.asdc.beans.Service;
42 import org.onap.vid.exceptions.GenericUncheckedException;
43
44 /**
45  * The Class LocalAsdcClient.
46  */
47 public class LocalAsdcClient implements AsdcClient {
48
49
50     public static final String SERVICES = "services";
51     /**
52      * The catalog.
53      */
54     private final JSONObject catalog;
55
56     /**
57      * The mapper.
58      */
59     private final ObjectMapper mapper;
60
61     /**
62      * Instantiates a new in local sdc client.
63      *
64      * @param builder the builder
65      */
66     public LocalAsdcClient(org.onap.vid.asdc.local.LocalAsdcClient.Builder builder) {
67         catalog = builder.catalog;
68         mapper = builder.mapper;
69     }
70
71     /**
72      * Gets the catalog.
73      *
74      * @return the catalog
75      */
76     private JSONObject getCatalog() {
77         return catalog;
78     }
79
80     /**
81      * Gets the mapper.
82      *
83      * @return the mapper
84      */
85     private ObjectMapper getMapper() {
86         return mapper;
87     }
88
89     /**
90      * Convert.
91      *
92      * @param <T>   the generic type
93      * @param json  the json
94      * @param clazz the clazz
95      * @return the t
96      * @throws AsdcCatalogException the sdc catalog exception
97      */
98     private <T> T convert(JSONObject json, Class<T> clazz) throws AsdcCatalogException {
99         try {
100             return getMapper().readValue(json.toString(), clazz);
101         } catch (JsonParseException e) {
102             throw new AsdcCatalogException("Failed to parse SDC response (bad data)", e);
103         } catch (JsonMappingException e) {
104             throw new AsdcCatalogException("Failed to map SDC response to internal VID data structure(s)", e);
105         } catch (IOException e) {
106             throw new AsdcCatalogException("Failed to get a response from SDC", e);
107         }
108     }
109
110     /* (non-Javadoc)
111      * @see org.onap.vid.asdc.AsdcClient#getService(java.util.UUID)
112      */
113     public Service getService(UUID uuid) throws AsdcCatalogException {
114
115         JSONObject serviceJsonObject = null;
116         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
117
118         for (int i = 0; i < categoryJsonArray.length(); i++) {
119             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
120             if (jsonServiceObject.get("uuid").equals(uuid.toString())) {
121                 serviceJsonObject = jsonServiceObject;
122                 break;
123             }
124         }
125
126         if (serviceJsonObject != null)
127             return convert(serviceJsonObject, Service.class);
128         else return null;
129     }
130
131     /* (non-Javadoc)
132      * @see org.onap.vid.asdc.AsdcClient#getServiceToscaModel(java.util.UUID)
133      */
134     public Path getServiceToscaModel(UUID serviceUuid) {
135
136         String toscaModelURL = null;
137
138         final JSONArray categoryJsonArray = getCatalog().getJSONArray(SERVICES);
139
140         for (int i = 0; i < categoryJsonArray.length(); i++) {
141
142             JSONObject jsonServiceObject = categoryJsonArray.getJSONObject(i);
143             if (jsonServiceObject.get("uuid").equals(serviceUuid.toString())) {
144                 toscaModelURL = jsonServiceObject.getString("toscaModelURL");
145             }
146         }
147         if (toscaModelURL == null) {
148             return null;
149         }
150         ClassLoader classLoader = getClass().getClassLoader();
151
152         try {
153             File file = new File(classLoader.getResource(toscaModelURL).getFile());
154             //using URLDecoder.decode to convert special characters from %XX to real character
155             //see https://stackoverflow.com/questions/32251251/java-classloader-getresource-with-special-characters-in-path
156             return Paths.get(URLDecoder.decode(file.getPath(), "UTF-8"));
157         } catch (RuntimeException | UnsupportedEncodingException e) {
158             throw new GenericUncheckedException("Can't find " + toscaModelURL, e);
159         }
160     }
161
162     @Override
163     public HttpResponse<String> checkSDCConnectivity() {
164         return HttpResponse.fallback("");
165     }
166
167     @Override
168     public HttpResponseWithRequestInfo<InputStream> getServiceInputStream(UUID serviceUuid, boolean warpException) {
169         return null;
170     }
171
172     @Override
173     public String getBaseUrl(){
174         return "";
175     }
176
177     /**
178      * The Class Builder.
179      */
180     public static class Builder {
181
182         /**
183          * The catalog.
184          */
185         private JSONObject catalog = new JSONObject()
186                 .put("resources", new JSONObject())
187                 .put(SERVICES, new JSONObject());
188
189         /**
190          * The mapper.
191          */
192         private ObjectMapper mapper = new ObjectMapper();
193
194         /**
195          * Instantiates a new builder.
196          */
197         public Builder() {
198         }
199
200         /**
201          * Catalog.
202          *
203          * @param catalog the catalog
204          * @return the builder
205          */
206         public org.onap.vid.asdc.local.LocalAsdcClient.Builder catalog(JSONObject catalog) {
207             this.catalog = catalog;
208             return this;
209         }
210
211         /**
212          * Mapper.
213          *
214          * @param mapper the mapper
215          * @return the builder
216          */
217         public org.onap.vid.asdc.local.LocalAsdcClient.Builder mapper(ObjectMapper mapper) {
218             this.mapper = mapper;
219             return this;
220         }
221
222         /**
223          * Builds the.
224          *
225          * @return the in local sdc client
226          */
227         public org.onap.vid.asdc.local.LocalAsdcClient build() {
228             return new org.onap.vid.asdc.local.LocalAsdcClient(this);
229         }
230     }
231
232 }