eca3b4b1d317c17ccdcca8f9e9968931856bdf06
[ccsdk/features.git] /
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     private String alias;
37     private String esId;
38     private int retries;
39
40     public UpdateRequest(String alias, String dataType, String esId) {
41         this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES);
42     }
43
44     public UpdateRequest(String alias, String dataType, String esId, boolean refresh) {
45         this(alias, dataType, esId, BaseRequest.DEFAULT_RETRIES, refresh);
46     }
47
48     public UpdateRequest(String alias, String dataType, String esId, int retries) {
49         this(alias, dataType, esId, retries, false);
50     }
51
52     public UpdateRequest(String alias, String dataType, String esId, int retries, boolean refresh) {
53         this(String.format("/%s/%s/%s/_update", alias, dataType, BaseRequest.urlEncodeValue(esId)), refresh);
54         this.alias = alias;
55         this.esId = esId;
56         this.retries = retries;
57     }
58
59     public UpdateRequest(String uri, boolean refresh) {
60         super("POST", uri, refresh, BaseRequest.DEFAULT_RETRIES);
61         this.params = null;
62         this.retries = 1;
63
64     }
65
66
67
68     private UpdateRequest withParam(String key, JSONObject p) {
69         if (this.params == null) {
70             this.params = new JSONObject();
71         }
72         this.params.put(key, p);
73         return this;
74     }
75
76     private UpdateRequest withParam(String key, JSONArray p) {
77         if (this.params == null) {
78             this.params = new JSONObject();
79         }
80         this.params.put(key, p);
81         return this;
82     }
83
84     public void source(JSONObject map) {
85         this.source(map, null);
86     }
87
88     public void source(JSONObject map, List<String> onlyForInsert) {
89         JSONObject outer = new JSONObject();
90         JSONObject script = new JSONObject();
91         script.put("lang", "painless");
92         script.put("source", this.createInline(map, onlyForInsert));
93         if (this.params != null) {
94             script.put("params", this.params);
95         }
96         outer.put("script", script);
97         outer.put("upsert", map);
98         LOG.debug("update payload: " + outer.toString());
99         super.setQuery(outer.toString());
100     }
101
102     private String createInline(JSONObject map, List<String> onlyForInsert) {
103         if (onlyForInsert == null) {
104             onlyForInsert = new ArrayList<String>();
105         }
106         String s = "", k = "";
107         Object value;
108         String pkey;
109         int i = 0;
110         for (Object key : map.keySet()) {
111             k = String.valueOf(key);
112             if (onlyForInsert.contains(k)) {
113                 continue;
114             }
115             value = map.get(k);
116             if (value instanceof JSONObject || value instanceof JSONArray) {
117                 pkey = String.format("p%d", i++);
118                 if (value instanceof JSONObject) {
119                     this.withParam(pkey, (JSONObject) value);
120                 } else {
121                     this.withParam(pkey, (JSONArray) value);
122                 }
123
124                 s += String.format("ctx._source['%s']=%s;", key, "params." + pkey);
125             } else {
126                 s += String.format("ctx._source['%s']=%s;", key, escpaped(value));
127             }
128         }
129         return s;
130     }
131
132     private String escpaped(Object value) {
133         String s = "";
134         if (value instanceof Boolean || value instanceof Integer || value instanceof Long || value instanceof Float
135                 || value instanceof Double) {
136             s = String.valueOf(value);
137         } else {
138             s = "\"" + String.valueOf(value) + "\"";
139         }
140         return s;
141
142     }
143
144     protected String getAlias() {
145         return this.alias;
146     }
147
148     protected String getEsId() {
149         return this.esId;
150     }
151
152     protected int getRetries() {
153         return this.retries;
154     }
155 }