Update command and dataprovider components
[ccsdk/features.git] / sdnr / wt / common / src / main / java / org / onap / ccsdk / features / sdnr / wt / common / database / requests / UpdateRequest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * 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.ccsdk.features.sdnr.wt.common.database.requests;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import org.json.JSONArray;
28 import org.json.JSONObject;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class UpdateRequest extends BaseRequest {
33
34         private static final Logger LOG = LoggerFactory.getLogger(UpdateRequest.class);
35         private JSONObject params;
36
37         public UpdateRequest(String alias, String dataType, String esId) {
38                 super("POST", String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)));
39                 this.params = null;
40         }
41
42         private UpdateRequest withParam(String key, JSONObject p) {
43                 if (this.params == null) {
44                         this.params = new JSONObject();
45                 }
46                 this.params.put(key, p);
47                 return this;
48         }
49
50         private UpdateRequest withParam(String key, JSONArray p) {
51                 if (this.params == null) {
52                         this.params = new JSONObject();
53                 }
54                 this.params.put(key, p);
55                 return this;
56         }
57         public void source(JSONObject map) {
58                 this.source(map,null);
59         }
60         public void source(JSONObject map, List<String> onlyForInsert) {
61                 JSONObject outer = new JSONObject();
62                 JSONObject script = new JSONObject();
63                 script.put("lang", "painless");
64                 script.put("source", this.createInline(map,onlyForInsert));
65                 if(this.params!=null) {
66                         script.put("params",this.params);
67                 }
68                 outer.put("script", script);
69                 outer.put("upsert", map);
70                 LOG.debug("update payload: " + outer.toString());
71                 super.setQuery(outer.toString());
72         }
73
74         private String createInline(JSONObject map, List<String> onlyForInsert) {
75                 if(onlyForInsert==null) {
76                         onlyForInsert = new ArrayList<String>();
77                 }
78                 String s = "",k="";
79                 Object value;
80                 String pkey;
81                 int i = 0;
82                 for (Object key : map.keySet()) {
83                         k=String.valueOf(key);
84                         if(onlyForInsert.contains(k)) {
85                                 continue;
86                         }
87                         value = map.get(k);
88                         if (value instanceof JSONObject || value instanceof JSONArray) {
89                                 pkey = String.format("p%d", i++);
90                                 if (value instanceof JSONObject) {
91                                         this.withParam(pkey, (JSONObject) value);
92                                 } else {
93                                         this.withParam(pkey, (JSONArray) value);
94                                 }
95
96                                 s += String.format("ctx._source['%s']=%s;", key, "params."+pkey);
97                         } else {
98                                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
99                         }
100                 }
101                 return s;
102         }
103
104         private String escpaped(Object value) {
105                 String s = "";
106                 if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
107                                 || value instanceof Double) {
108                         s = String.valueOf(value);
109                 } else {
110                         s = "\"" + String.valueOf(value) + "\"";
111                 }
112                 return s;
113
114         }
115
116 }