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