improved handling of indexed data
[ccsdk/sli/adaptors.git] / aai-service / provider / src / main / java / org / onap / ccsdk / sli / adaptors / aai / UpdateRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *             reserved.
7  * Modifications Copyright (C) 2018 IBM.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 /**
23  * The UpdateRequest class provides processing related to update transaction.
24  * @author  richtabedzki
25  */
26
27 package org.onap.ccsdk.sli.adaptors.aai;
28
29 import java.io.UnsupportedEncodingException;
30 import java.net.MalformedURLException;
31 import java.net.URISyntaxException;
32 import java.net.URL;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.Properties;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39
40 import org.onap.ccsdk.sli.adaptors.aai.data.AAIDatum;
41
42 import com.fasterxml.jackson.core.JsonProcessingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45 public class UpdateRequest extends AAIRequest {
46   
47     private AAIRequest request;
48     private Map<String, String> params;
49
50     public UpdateRequest(AAIRequest request, Map<String, String> parms) {
51         this.request = request;
52         this.params = parms;
53     }
54
55     @Override
56     public URL getRequestUrl(String method, String resourceVersion)
57             throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
58         return request.getRequestUrl(method, resourceVersion);
59     }
60
61     @Override
62     public URL getRequestQueryUrl(String method) throws UnsupportedEncodingException, MalformedURLException, URISyntaxException {
63         return request.getRequestQueryUrl(method);
64     }
65
66     @Override
67     public String toJSONString() {
68         updateArrayEntries(params);
69         ObjectMapper mapper = AAIService.getObjectMapper();
70         String json = null;
71       
72         try {
73             json = mapper.writeValueAsString(params);
74         } catch (JsonProcessingException e) {
75             LOG.error("Could not convert parameters of " + request.getRequestObject().getClass().getName(), e);
76         }
77       
78         return json;
79     }
80
81     /**
82     *
83     * Update array entries.
84     * The method converts indexed data entries to an array of values
85     *
86     * @param data Map containing String:String values representing input data
87     */
88     private void updateArrayEntries( Map<String, String> data) {
89         Set<String> set = data.keySet()
90                 .stream()
91                 .filter(s -> s.endsWith("_length"))
92                 .collect(Collectors.toSet());
93           
94         for(String lenghtKey : set) {
95             String key = lenghtKey.replace("_length", "");
96 //            String index = data.get(lenghtKey);
97             List<String> array = new ArrayList<>();
98           
99             Set<String> subset = data.keySet()
100                     .stream()
101                     .filter(s -> s.startsWith(String.format("%s[",key)))
102                     .collect(Collectors.toSet());
103             for(String subKey : subset) {
104                 String subValue = data.get(subKey);
105                 array.add(subValue);
106                 LOG.trace("{} : {} ", subKey, subValue);
107             }
108             data.put(key, array.toString());
109             data.remove(lenghtKey);
110             for(String subKey : subset) {
111                 data.remove(subKey);
112             }
113         }
114     }
115
116     @Override
117     public String[] getArgsList() {
118         return request.getArgsList();
119     }
120
121     @Override
122     public Class<? extends AAIDatum> getModelClass() {
123         return request.getModelClass();
124     }
125   
126     @Override
127     public void addRequestProperty(String key, String value) {
128         request.requestProperties.put(key, value);
129     }
130
131     public static String processPathData(String requestUrl, Properties requestProperties) {
132       
133 //        if(request != null) {
134 //            Class<?> clazz = request.getClass();
135 //            Method function = null;
136 //            try {
137 //                function = clazz.getMethod("processPathData", request_url.getClass(), requestProperties.getClass());
138 //                request_url = (String) function.invoke(null, request_url,  requestProperties);
139 //            } catch (Exception e) {
140 //                e.printStackTrace();
141 //            }
142 //        }
143       
144 //        request.processPathData(request_url, requestProperties);
145         return requestUrl;
146     }
147   
148     public void processRequestPathValues(Map<String, String> nameValues) {
149         request.processRequestPathValues(nameValues);
150     }
151
152 }