summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/validation/modeldriven/validator/InstanceReader.java
blob: 7832e519570130fece86b9170223972a70ab2922 (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
/*
 * ============LICENSE_START===================================================
 * Copyright (c) 2018 Amdocs
 * ============================================================================
 * 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.onap.aai.validation.modeldriven.validator;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jayway.jsonpath.DocumentContext;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import javax.inject.Inject;
import org.onap.aai.validation.exception.ValidationServiceError;
import org.onap.aai.validation.exception.ValidationServiceException;
import org.onap.aai.validation.modeldriven.configuration.mapping.ModelInstanceMapper;
import org.onap.aai.validation.modeldriven.configuration.mapping.ModelInstanceMapper.MappingType;
import org.onap.aai.validation.reader.JsonReader;
import org.onap.aai.validation.reader.OxmReader;

/**
 * Reads values from an instance object.
 */
public class InstanceReader {

	private static final String MODEL_NAME = "model-name";
	private static final String[] INVALID_ENTRIES = { "inventory-response-items", "extra-properties", MODEL_NAME };
	private static final String RESOURCE_VERSION = "resource-version";
	private static final String JSON_PATH_MODEL_ID = "$.*.persona-model-id";

	private JsonReader jsonReader;
	private OxmReader oxmReader;
	private JsonParser jsonParser = new JsonParser();

	/**
	 * @param jsonReader
	 * @param oxmReader
	 */
	@Inject
	public InstanceReader(JsonReader jsonReader, OxmReader oxmReader) {
		this.jsonReader = jsonReader;
		this.oxmReader = oxmReader;
	}

	public OxmReader getOxmReader() {
		return oxmReader;
	}

	/**
	 * Gets object instance values.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @param mapping
	 *            defines the paths that allow the extraction of values from the object instance. This includes:
	 *            <ul>
	 *            <li>origin: path that serves as the starting point for the instance search</li>
	 *            <li>root: path to underlying instance objects that can be examined by recursively calling the
	 *            getValues method</li>
	 *            </ul>
	 *
	 * @return a {@link Multimap} of instances keyed by their model id.
	 * @throws ValidationServiceException
	 */
	public Multimap<String, String> getValues(String json, ModelInstanceMapper mapping) throws ValidationServiceException {
		Multimap<String, String> values = HashMultimap.create();

		DocumentContext document = jsonReader.parse(json);

		if (MappingType.RELATIONSHIP.equals(mapping.getMappingType())) {
			String rootPath = mapping.getInstance().getRoot();
			if (rootPath == null || rootPath.isEmpty()) {
				throw new ValidationServiceException(ValidationServiceError.INSTANCE_MAPPING_ROOT_ERROR);
			}

			JsonElement jsonElement = jsonReader.getJsonElement(document, rootPath);

			if (jsonElement instanceof JsonArray) {
				JsonArray jsonArray = jsonElement.getAsJsonArray();

				processRelatedObjects(values, jsonArray);
			}
		} else {
			// We are dealing with attributes.
			String valuePath = mapping.getInstance().getValue();
			if (valuePath != null && !valuePath.isEmpty()) {
				List<String> attributes = jsonReader.get(json, valuePath);
				for (String attribute : attributes) {
					values.put(attribute, null);
				}
			}
		}

		return values;
	}

	/**
	 * Gets the instance type, e.g. connector, pserver, etc.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @return the type of the entity
	 */
	public String getInstanceType(String json) {
		return getNamedQueryEntity(json).getEntityType();
	}

	/**
	 * Gets the id of the instance. Uses the {@link OxmReader} to identify the property holding the primary key.<br>
	 *
	 * WARNING: Some types of object appear to have more than one primary key. This method uses the first primary key.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @return the identifier of the object instance
	 * @throws ValidationServiceException
	 */
	public String getInstanceId(String json) throws ValidationServiceException {
		String instanceId = null;

		InstanceEntity entity = getNamedQueryEntity(json);

		List<String> primaryKeys = oxmReader.getPrimaryKeys(entity.getEntityType());

		if (primaryKeys != null && !primaryKeys.isEmpty()) {
			JsonObject instance = entity.getObject().getAsJsonObject();
			JsonElement primaryKey = instance.get(primaryKeys.get(0));
			instanceId = primaryKey == null ? null : primaryKey.getAsString();
		}

		return instanceId;
	}

	/**
	 * Strips the instance out of its payload wrapping.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @param mappings
	 *            the definition of the paths that allow the extraction of the instance from the JSON payload
	 * @return
	 * @throws ValidationServiceException
	 */
	public String getInstance(String json, List<ModelInstanceMapper> mappings) throws ValidationServiceException {
		String origin = mappings.iterator().next().getInstance().getOrigin();
		List<String> jsonList = jsonReader.get(json, origin);

		if (!jsonList.isEmpty()) {
			return jsonList.get(0);
		} else {
			throw new ValidationServiceException(ValidationServiceError.INSTANCE_READER_NO_INSTANCE, origin, json);
		}
	}

	/**
	 * Extracts the entity from a Named Query JSON payload.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @return an {@link InstanceEntity} object
	 */
	public InstanceEntity getNamedQueryEntity(String json) {
		return getNamedQueryEntity(jsonParser.parse(json).getAsJsonObject());
	}

	/**
	 * Gets the model identifier of a given entity.
	 *
	 * @param entity
	 *            a JSON entity
	 * @return a model identifier attribute value if the attribute exists else a null is returned.
	 * @throws ValidationServiceException
	 */
	public String getModelId(String entity) throws ValidationServiceException {
		String modelId = null;
		List<String> readResult = jsonReader.get(entity, JSON_PATH_MODEL_ID);
		if (!readResult.isEmpty()) {
			modelId = readResult.get(0);
		}
		return modelId;
	}

	/**
	 * Gets the resource version of the instance.
	 *
	 * @param json
	 *            a Named Query JSON payload
	 * @return the resource version of the object instance
	 */
	public String getResourceVersion(String json) {
		String resourceVersion = null;

		InstanceEntity entity = getNamedQueryEntity(json);

		if (entity != null && entity.getObject() != null && entity.getObject().getAsJsonObject().has(RESOURCE_VERSION)) {
			resourceVersion = entity.getObject().getAsJsonObject().get(RESOURCE_VERSION).getAsString();
		}
		return resourceVersion;
	}

	/**
	 * Gets the model name of the instance.
	 *
	 * @param jsonString
	 *            a Named Query JSON payload
	 * @return the model name of the object instance
	 * @throws ValidationServiceException
	 */
	public String getModelName(String jsonString) {
		JsonObject jsonObject = jsonParser.parse(jsonString).getAsJsonObject();
		return getModelName(jsonObject);
	}

	/**
	 * @param jsonObject
	 * @return
	 */
	private String getModelName(JsonObject jsonObject) {
		for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
			if (MODEL_NAME.equals(entry.getKey())) {
				return entry.getValue().getAsString();
			}
		}
		return null;
	}

	private void processRelatedObjects(Multimap<String, String> values, JsonArray jsonArray) {
		for (JsonElement relatedObject : jsonArray) {
			JsonObject jsonObject = relatedObject.getAsJsonObject();

			InstanceEntity entity = getNamedQueryEntity(jsonObject);
			if (entity != null) {
				values.put(entity.getModelName() == null ? entity.getEntityType() : entity.getModelName(), jsonObject.toString());
			}
		}
	}

	private InstanceEntity getNamedQueryEntity(JsonObject jsonObject) {
		Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();

		String modelName = getModelName(jsonObject);

		for (Entry<String, JsonElement> entry : entrySet) {
			if (!Arrays.asList(INVALID_ENTRIES).contains(entry.getKey())) {
				return new InstanceEntity(entry.getKey(), modelName, entry.getValue().getAsJsonObject(), jsonObject);
			}
		}

		return null;
	}

	/**
	 * An Entity bean for the InstanceReader
	 *
	 */
	public class InstanceEntity {

		private String entityType;
		private String modelName;
		private JsonObject object;
		private JsonObject objectAndGraph;

		/**
		 * @param entityType
		 * @param modelName
		 * @param object
		 * @param objectAndGraph
		 */
		public InstanceEntity(String entityType, String modelName, JsonObject object, JsonObject objectAndGraph) {
			this.entityType = entityType;
			this.modelName = modelName;
			this.object = object;
			this.objectAndGraph = objectAndGraph;
		}

		public String getEntityType() {
			return entityType;
		}

		public String getModelName() {
			return modelName;
		}

		public JsonObject getObject() {
			return object;
		}

		public JsonObject getObjectAndGraph() {
			return objectAndGraph;
		}

		@Override
		public String toString() {
			return "Entity [entityType=" + entityType + ", modelName=" + modelName + ", object=" + object.toString() + ", fullObject="
					+ objectAndGraph.toString() + "]";
		}
	}
}