aboutsummaryrefslogtreecommitdiffstats
path: root/dcae-controller-core/dcae-controller-platform-server/src/main/java/org/openecomp/dcae/controller/platform/servers/controller/inventory/DcaeDcaeInventoryProvider.java
blob: ba6f379aa49c4f68c315b9198c1f3a1e1b4abc59 (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
/*-
 * ============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 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.json.JSONArray;
import org.json.JSONObject;
import org.mvel2.optimizers.impl.refl.nodes.ArrayLength;
import org.openecomp.ncomp.sirius.manager.BasicAdaptorProvider;

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.InventoryPackage;
import org.openecomp.dcae.controller.inventory.Link;
import org.openecomp.dcae.controller.inventory.DcaeServiceResults;

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.err.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.err.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.equals(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;
	}
}