aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-controller-core/dcae-controller-platform-server/src/main/java/org/openecomp/dcae/controller/platform/servers/controller/inventory/DcaeDcaeInventoryProvider.java
blob: 109db525185e8ac693a7185792a72cdb3e18f287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 */

package org.openecomp.dcae.controller.platform.servers.controller.inventory;

import org.openecomp.ncomp.sirius.manager.ISiriusServer;
import org.openecomp.ncomp.sirius.manager.JavaHttpClient;
import org.openecomp.ncomp.sirius.manager.ManagementServer;
import org.openecomp.ncomp.sirius.manager.console.Utils;
import org.yaml.snakeyaml.Yaml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.json.JSONArray;
import org.json.JSONObject;
import org.mvel2.optimizers.impl.refl.nodes.ArrayLength;
import org.openecomp.ncomp.core.DeploymentStatus;
import org.openecomp.ncomp.sirius.manager.BasicAdaptorProvider;
import org.openecomp.dcae.controller.core.service.DcaeService;
import org.openecomp.dcae.controller.core.service.DcaeServiceInstance;
import org.openecomp.dcae.controller.inventory.ApiResponseMessage;
import org.openecomp.dcae.controller.inventory.DCAEService;
import org.openecomp.dcae.controller.inventory.DCAEServiceGroupByResults;
import org.openecomp.dcae.controller.inventory.DCAEServiceRequest;
import org.openecomp.dcae.controller.inventory.DCAEServiceType;
import org.openecomp.dcae.controller.inventory.DCAEServiceTypeRequest;
import org.openecomp.dcae.controller.inventory.DcaeInventory;
import org.openecomp.dcae.controller.inventory.DcaeServiceTypeResults;
import org.openecomp.dcae.controller.inventory.InventoryConfigurationRule;
import org.openecomp.dcae.controller.inventory.InventoryPackage;
import org.openecomp.dcae.controller.inventory.Link;
import org.openecomp.dcae.controller.platform.controller.DcaePlatformController;
import org.openecomp.dcae.controller.service.cdap.CdapService;
import org.openecomp.dcae.controller.service.cdap.CdapServiceInstance;
import org.openecomp.dcae.controller.service.docker.DockerService;
import org.openecomp.dcae.controller.service.vm.VirtualMachineService;
import org.openecomp.dcae.controller.inventory.DcaeServiceResults;

import static org.openecomp.dcae.controller.platform.servers.controller.DcaeDcaePlatformControllerProvider.*;

public class DcaeDcaeInventoryProvider extends BasicAdaptorProvider {
	private static final Logger logger = Logger.getLogger(DcaeDcaeInventoryProvider.class);
	DcaeInventory o;
	DcaeInventoryFactory f;

	public DcaeDcaeInventoryProvider(ISiriusServer controller, DcaeInventory o) {
		super(controller, o);
		f = new DcaeInventoryFactory(controller);
		this.o = o;
	}

	public DcaeServiceTypeResults getDcaeServiceTypes(String typeName, Boolean onlyLatest, Boolean onlyActive,
			String vnfType, String serviceId, String serviceLocation, String asdcServiceId, String asdcResourceId,
			Integer offset) {
		DcaeServiceTypeResults res = f.createDcaeServiceTypeResults();
		JavaHttpClient client = new JavaHttpClient("controller.properties", "inventory");
		HashMap<String, String> headers = new HashMap<String, String>();
		HashMap<String, String> query = new HashMap<String, String>();
		// TODO handle parameters, but for now only use is complete dump
		addQuery(query,"offset", offset);
		JSONObject json = client.httpJsonTransaction("/dcae-service-types" + queryUrl(query), "GET", headers, null);
//		System.out.println(json.toString(2));
		JSONArray items = json.getJSONArray("items");
		for (int i = 0; i < items.length(); i++) {
			JSONObject item = items.getJSONObject(i);
			item.put("name", item.getString("typeId"));
			fixCreated(item);
		}
		nullClean(json);
		EClass c = InventoryPackage.eINSTANCE.getDcaeServiceTypeResults();
		res = (DcaeServiceTypeResults) controller.getServer().json2ecore(c, json);
		System.out.println(ManagementServer.ecore2json(res, 1000, null, true).toString(2));
		return res;
	}

	public ApiResponseMessage deleteDcaeServiceTypesType(String typeId) {
		ApiResponseMessage res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public DCAEServiceType getDcaeServiceTypesType(String typeId) {
		DCAEServiceType res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public DCAEServiceType createDcaeServiceTypesTypeName(DCAEServiceTypeRequest body) {
		DCAEServiceType res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public DcaeServiceResults getDcaeServices(String typeId, String vnfId, String vnfType, String vnfLocation,
			String componentType, Boolean shareable, String created, Integer offset) {
		DcaeServiceResults res = f.createDcaeServiceResults();
		JavaHttpClient client = new JavaHttpClient("controller.properties", "inventory");
		HashMap<String, String> headers = new HashMap<String, String>();
		HashMap<String, String> query = new HashMap<String, String>();
		// TODO handle parameters, but for now only use is complete dump
		addQuery(query,"offset", offset);
		JSONObject json = client.httpJsonTransaction("/dcae-services" + queryUrl(query), "GET", headers, null);
//		System.out.println(json.toString(2));
		JSONArray items = json.getJSONArray("items");
		for (int i = 0; i < items.length(); i++) {
			JSONObject item = items.getJSONObject(i);
			item.put("name", item.getString("serviceId"));
			fixCreated(item);
			JSONArray components = item.getJSONArray("components");
			for (int j = 0; j < components.length(); j++) {
				JSONObject component = components.getJSONObject(j);
				fixCreated(component);
				component.put("name", component.getString("componentId"));
			}
		}
		nullClean(json);
		EClass c = InventoryPackage.eINSTANCE.getDcaeServiceResults();
		res = (DcaeServiceResults) controller.getServer().json2ecore(c, json);
		System.out.println(ManagementServer.ecore2json(res, 1000, null, true).toString(2));
		return res;
	}

	private String queryUrl(HashMap<String, String> query) {
		if (query.size() == 0) return "";
		StringBuffer b = new StringBuffer();
		boolean first = true;
		for (String k : query.keySet()) {
			b.append(first ? "?" : "&");
			first = false;
			b.append(k).append("=").append(query.get(k));
		}
		return b.toString();
	}

	private void addQuery(HashMap<String, String> query, String k, Object v) {
		if (v != null) query.put(k, v.toString());
	}

	private void nullClean(Object json) {
		if (json instanceof JSONObject) {
			JSONObject o = (JSONObject) json;
			List<String> delete = new ArrayList<String>();
			for (Iterator<String> iterator = o.keys(); iterator.hasNext();) {
				String key = (String) iterator.next();
				Object o1 = o.get(key);
				if (o1 == null) {
					delete.add(key);
					continue;
				}
				nullClean(o1);
			}
			for (String key : delete) {
				o.remove(key);
			}
		}
		if (json instanceof JSONArray) {
			JSONArray a = (JSONArray) json;
			for (int i = 0; i < a.length(); i++) {
				Object o1 = a.get(i);
				nullClean(o1);
			}
		}
	}

	private void fixCreated(JSONObject j) {
		if (j.has("created")) {
			j.put("created1",j.get("created"));
			j.remove("created");
		}
	}

	public DCAEServiceGroupByResults getDcaeServicesGroupbyPropertyName(String propertyName) {
		DCAEServiceGroupByResults res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public void deleteDcaeServicesService(String serviceId) {

		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public DCAEService getDcaeServicesService(String serviceId) {
		DCAEService res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public DCAEService createDcaeServicesService(String serviceId, DCAEServiceRequest body) {
		DCAEService res = null;
		// TODO IMPLEMENT
		throw new UnsupportedOperationException();
	}

	public void poll() {
		o.getServices().clear();
		int offset = 0;
		while (offset != -1) {
			DcaeServiceResults s = o.getDcaeServices(null, null, null, null, null, null, null, offset);
			o.getServices().addAll(s.getItems());
			offset = offset(s.getLinks());
		}
		o.getServiceTypes().clear();
		offset = 0;
		while (offset != -1) {
			DcaeServiceTypeResults s = o.getDcaeServiceTypes(null, null, null, null, null, null, null, null, null);
			o.getServiceTypes().addAll(s.getItems());
			offset = offset(s.getLinks());
		}
//		System.out.println(ManagementServer.ecore2json(o, 1000, null, true).toString(2));
	}

	private int offset(EList<Link> links) {
		for (Link l : links) {
			if ("nextLink".equals(l.getName())) {
				return Integer.parseInt(l.getHref().split("=")[1]);
			}
		}
		return -1;
	}
	
	
	public void updateConfiguration() {
		System.out.println("BBBBBBB: updateConfiguration");
		DcaePlatformController root = (DcaePlatformController) controller.getServer().getObject();
		for (DcaeService s : root.getServices()) {
			for (DcaeServiceInstance i : instances(s)) {
				if (! ( i instanceof CdapServiceInstance)) continue;
				CdapServiceInstance i1 = (CdapServiceInstance) i;
				for (InventoryConfigurationRule r : o.getRules()) {
					try {
						if (r.getServiceNameMatch() != null && ! r.getServiceNameMatch().equals(s.getName())) continue;
						if (r.getInstanceNameMatch() != null && ! r.getInstanceNameMatch().equals(i.getName())) continue;
						JSONObject json = findBluePrint(r);
						System.out.println("BBBBBBB: updateConfiguration " + s.getName() + " " + i.getName() + " " + r + " " + (json ==null ? "NULL" : json.toString(2)));
						if (json == null) {
							System.out.println("BBBBBBB: NULL");
							continue;
						}
						EObject oo = controller.getServer().json2ecore(i1.getConfiguration().eClass(), json);
						JSONObject before = cleanJson(i1.getConfiguration());
						ManagementServer.merge(i1.getConfiguration(), oo, json, true, null);
						JSONObject after = cleanJson(i1.getConfiguration());
						System.out.println("BBBBBBB before: " + before.toString(2));
						System.out.println("BBBBBBB after: " + after.toString(2));
						System.out.println("BBBBBBB equal: " + !before.toString(2).equals(after.toString(2)));
						if (!before.toString(2).equals(after.toString(2))) {
							System.out.println("BBBBBBB before: " + before.toString(2));
							System.out.println("BBBBBBB after: " + after.toString(2));
							if (i.getStatus() == DeploymentStatus.DEPLOYED)
								s.pushManagerConfiguration(i.getName());
						}
					}
					catch (Exception e) {
                                                logger.error("exception: ", e);
						System.out.println("BBBBBBB: error "+ s.getName() + " " + i.getName() + " " + e);
					}
				}
			}
		}
	}

	private JSONObject findBluePrint(InventoryConfigurationRule r) {
		DCAEServiceType t = null;
		int version = -1;
		for (DCAEServiceType t1 : o.getServiceTypes()) {
			System.out.println("BBBBBBB: type match: " + t1.getTypeName() + " " + r.getTypeNameMatch());
			if (t1.getTypeName() == null) continue;
			if ( ! t1.getTypeName().equals(r.getTypeNameMatch())) continue;
			System.out.println("BBBBBBB: type version: " + t1.getTypeVersion() + " " + version);
			if (t1.getTypeVersion() != null && version  > t1.getTypeVersion()) continue;
			System.out.println("BBBBBBB: type match found: " + t1.getTypeName());
			t = t1;
		}
		if (t == null) {
			System.out.println("BBBBBBB: no type for: " + r.getServiceNameMatch());
			return null;
		}
		JSONObject bp;
		try {
			System.out.println("BBBBBBB: bp 1=" );
			Yaml y = new Yaml();
			System.out.println("BBBBBBB: bp 2=");
			bp = (JSONObject) Utils.object2json(y.load(t.getBlueprintTemplate()));
			System.out.println("BBBBBBB: bp 3=");
			System.out.println("BBBBBBB: bp=" + bp.toString(2));
			JSONObject topology_template = getJSONObject(bp,"topology_template");
			JSONObject node_templates = getJSONObject(topology_template,"node_templates");
			JSONObject node = null;
			for (Iterator<String> i = node_templates.keys(); i.hasNext();) {
				String type = (String) i.next();
				System.out.println("BBBBBBB: node: " + type);
				if (type.matches(r.getBpNodeNameMatch())) node = getJSONObject(node_templates,type); 
			}
			if (node == null) {
				System.out.println("BBBBBBB: no node for: " + r.getBpNodeNameMatch());
				return null;
			}
			JSONObject p = getJSONObject(node,"properties");
			JSONObject app_preferences = getJSONObject(p ,"app_preferences");
			System.out.println("BBBBBBB: app_preferences=" + app_preferences.toString(2));
			JSONObject json = new JSONObject();
			for (Iterator<String> i = app_preferences.keys(); i.hasNext();) {
				String path = (String) i.next();
				String a[] = path.split("\\.");
				try {
					updateJson(json,a,0, app_preferences.get(path));
				}
				catch (Exception e) {
                                    logger.error("exception: ", e);
                                }
			}
			System.out.println("BBBBBBB: fixed value: " + json.toString(2));
			return json;
		}
		catch (Exception e) {
                        logger.error("exception: ", e);
			System.out.println("BBBBBBB: bp error=" + e);
			return null;
		}
	}

	private void updateJson(JSONObject json, String[] a, int i, Object v) {
		if (i == a.length - 1) {
			json.put(a[i], v);
		}
		else {
			if (! json.has(a[i])) 
				json.put(a[i], new JSONObject());
			updateJson(json.getJSONObject(a[i]), a, i+1, v);
		}
	}

	private JSONObject getJSONObject(JSONObject json, String key) {
		if (json.has(key)) return json.getJSONObject(key);
		System.out.println("BBBBBBB: json does not have key =" + json.toString(2) + " " + key);
		throw new RuntimeException("Unable to find: " + key);
	}

	
}