summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/composition/impl/CompositionCatalogBusinessLogic.java
blob: ff596b52463a745a7f90b4581d662a337a4a45c5 (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
package org.onap.sdc.dcae.composition.impl;

import org.json.JSONArray;
import org.json.JSONException;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.common.onaplog.OnapLoggerError;
import org.onap.sdc.dcae.catalog.Catalog;
import org.onap.sdc.dcae.catalog.engine.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;

@Component
public class CompositionCatalogBusinessLogic {

	@Autowired
	private CatalogController catalogController;

	protected OnapLoggerError errLogger = OnapLoggerError.getInstance();

	public DeferredResult<CatalogResponse> getItems(ItemsRequest theRequest) {

		final ItemsRequest request = (theRequest == null) ? ItemsRequest.EMPTY_REQUEST : theRequest;

		Catalog catalog = catalogController.getCatalog(request.getCatalog());
		DeferredResult<CatalogResponse> result = new DeferredResult<>(request.getTimeout());

		catalog.rootsByLabel(request.getStartingLabel()).setHandler(catalogController.new CatalogHandler<Catalog.Folders>(request, result) {
			public CatalogResponse handleData(Catalog.Folders theFolders) {
				JSONArray ja = new JSONArray();
				if (theFolders != null) {
					for (Catalog.Folder folder : theFolders) {
						ja.put(catalogController.patchData(catalog, folder.data()));
					}
				}
				CatalogResponse response = new CatalogResponse(this.request);
				try {
					response.data().put("elements", ja);
				} catch (JSONException e) {
					errLogger.log(LogLevel.ERROR, this.getClass().getName(), "JSONException putting json elements to response {}", e);
				}
				return response;
			}
		});
		return result;
	}

	public DeferredResult<CatalogResponse> getItemById(ItemsRequest theRequest, String theItemId) {

		final ItemsRequest request = (theRequest == null) ? ItemsRequest.EMPTY_REQUEST : theRequest;

		Catalog catalog = catalogController.getCatalog(request.getCatalog());
		DeferredResult<CatalogResponse> result = new DeferredResult<>(request.getTimeout());

		catalog.folder(theItemId).withParts().withPartAnnotations().withItems().withItemAnnotations().withItemModels().execute().setHandler(new FolderHandler(catalog, request, result));
		return result;
	}

	public DeferredResult getModelById(ElementRequest theRequest, String theItemId) {
		final ElementRequest request = (theRequest == null) ? ElementRequest.EMPTY_REQUEST : theRequest;

		Catalog catalog = catalogController.getCatalog(request.getCatalog());
		DeferredResult<CatalogResponse> result = new DeferredResult<>(request.getTimeout());

//		try {
			catalog.item(theItemId).withModels().execute().setHandler(new ItemHandler(catalog, request, result));
//		} catch (IllegalArgumentException e) {
//			errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Error fetching catalog model with id {}. Message: {}", theItemId, e);
//			result.setErrorResult(new CatalogError(request, "Catalog API failed", e));
//		}
		return result;
	}

	public DeferredResult<CatalogResponse> getTypeInfo(ElementRequest theRequest, String theItemId, String theTypeName) {
		final ElementRequest request = (theRequest == null) ? ElementRequest.EMPTY_REQUEST : theRequest;

		Catalog catalog = catalogController.getCatalog(request.getCatalog());
		DeferredResult<CatalogResponse> result = new DeferredResult<>(request.getTimeout());

		catalog.type(theItemId, theTypeName).withHierarchy().withCapabilities().withRequirements().execute().setHandler(catalogController.new CatalogHandler<Catalog.Type>(request, result) {
			public CatalogResponse handleData(Catalog.Type theType) {
				CatalogResponse response = new CatalogResponse(this.request);
				if (theType != null) {
					try {
						response.data().put("type", catalogController.patchData(catalog, theType.data()));
					} catch (JSONException e) {
						errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Exception processing catalog {}", e);
					}
				}
				return response;
			}
		});
		return result;
	}

	/// Nested Catalog Data Handlers ///

	private class FolderHandler extends CatalogController.CatalogHandler<Catalog.Folder> {

		private Catalog catalog;

		private FolderHandler(Catalog catalog, ItemsRequest request, DeferredResult result) {
			catalogController.super(request, result);
			this.catalog = catalog;
		}

		private void patchCatalogData(Catalog.Elements folders, Catalog catalog) {
			if (folders != null) {
				folders.forEach(folder -> {
					catalogController.patchData(catalog, ((Catalog.Element) folder).data());
					// lots of ephemere proxies created here ..
					Catalog.Elements annotations = ((Catalog.Element) folder).elements("annotations", Catalog.Annotations.class);
					if (annotations != null) {
						annotations.forEach(a -> catalogController.patchData(catalog, ((Catalog.Annotation) a).data()));
					}
				});
			}
		}

		public CatalogResponse handleData(Catalog.Folder theFolder) {
			CatalogResponse response = new CatalogResponse(this.request);
			if (theFolder == null) {
				return response;
			}
			try {
				Catalog.Elements folders = theFolder.elements("parts", Catalog.Folders.class);
				patchCatalogData(folders, catalog);
				Catalog.Elements items = theFolder.elements("items", Catalog.Items.class);
				patchCatalogData(items, catalog);
			} catch (Exception x) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Exception processing catalog {}", x);
				return new CatalogError(this.request, "", x);
			}
			try {
				response.data().put("element", theFolder.data());
			} catch (JSONException e) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "JSONException putting element to response {}", e);
			}
			return response;
		}
	}

	private class ItemHandler extends CatalogController.CatalogHandler<Catalog.Item> {

		private Catalog catalog;

		private ItemHandler(Catalog catalog, ElementRequest request, DeferredResult result) {
			catalogController.super(request, result);
			this.catalog = catalog;
		}

		public CatalogResponse handleData(Catalog.Item theItem) {
			if (theItem == null) {
				return new CatalogError(this.request, "No such item");
			}
			Catalog.Templates models;
			try {
				models = (Catalog.Templates) theItem.elements("models", Catalog.Templates.class);
			} catch (Exception x) {
				return new CatalogError(this.request, "Failed to decode templates from result", x);
			}
			if (models == null || models.isEmpty()) {
				return new CatalogError(this.request, "Item has no models");
			}
			if (models.size() > 1) {
				return new CatalogError(this.request, "Item has more than one model !?");
			}
			try {
				catalog.template(models.get(0).id()).withInputs().withOutputs().withNodes().withNodeProperties().withNodePropertiesAssignments().withNodeRequirements().withNodeCapabilities().withNodeCapabilityProperties()
						.withNodeCapabilityPropertyAssignments().withPolicies().withPolicyProperties().withPolicyPropertiesAssignments().execute().setHandler(new TemplateHandler(this.catalog, this.request, this.result));
			} catch (Exception e) {
				errLogger.log(LogLevel.ERROR, this.getClass().getName(), "Exception caught during Catalog Item Handler: {}", e);
			}
			return null;
		}
	}

	private class TemplateHandler extends CatalogController.CatalogHandler<Catalog.Template> {

		private Catalog catalog;

		private TemplateHandler(Catalog catalog, CatalogRequest request, DeferredResult result) {
			catalogController.super(request, result);
			this.catalog = catalog;
		}

		public CatalogResponse handleData(Catalog.Template theTemplate) {
			CatalogResponse response = new CatalogResponse(this.request);
			if (theTemplate != null) {
				try {
					response.data().put("model", catalogController.patchData(catalog, theTemplate.data()));
				} catch (JSONException e) {
					errLogger.log(LogLevel.ERROR, this.getClass().getName(), "JSONException putting model to response {}", e);
				}
			}
			return response;
		}
	}
}