summaryrefslogtreecommitdiffstats
path: root/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/bpmn/servicedecomposition/tasks/BBInputSetupUtils.java
blob: b3b4559f1dbdd26637de8674281eac7c32215f7b (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Modifications Copyright (c) 2019 Samsung
 * ================================================================================
 * 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.so.bpmn.servicedecomposition.tasks;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.Configuration;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.GenericVnfs;
import org.onap.aai.domain.yang.InstanceGroup;
import org.onap.aai.domain.yang.L3Network;
import org.onap.aai.domain.yang.L3Networks;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.ServiceInstances;
import org.onap.aai.domain.yang.ServiceSubscription;
import org.onap.aai.domain.yang.VfModule;
import org.onap.aai.domain.yang.VolumeGroup;
import org.onap.aai.domain.yang.VolumeGroups;
import org.onap.so.bpmn.common.InjectionHelper;
import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
import org.onap.so.client.aai.AAIObjectPlurals;
import org.onap.so.client.aai.AAIObjectType;
import org.onap.so.client.aai.entities.AAIResultWrapper;
import org.onap.so.client.aai.entities.uri.AAIResourceUri;
import org.onap.so.client.aai.entities.uri.AAIUriFactory;

import org.onap.so.client.graphinventory.entities.uri.Depth;
import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
import org.onap.so.db.catalog.beans.Service;
import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
import org.onap.so.db.catalog.client.CatalogDbClient;
import org.onap.so.db.request.beans.InfraActiveRequests;
import org.onap.so.db.request.client.RequestsDbClient;
import org.onap.so.serviceinstancebeans.CloudConfiguration;
import org.onap.so.serviceinstancebeans.RequestDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Component("BBInputSetupUtils")
public class BBInputSetupUtils {

	private static final Logger logger = LoggerFactory.getLogger(BBInputSetupUtils.class);
	private ObjectMapper objectMapper = new ObjectMapper();
	private static final String REQUEST_ERROR = "Could not find request.";

	@Autowired
	protected CatalogDbClient catalogDbClient;

	@Autowired
	protected RequestsDbClient requestsDbClient;

	@Autowired
	protected InjectionHelper injectionHelper;

	public void updateInfraActiveRequestVnfId(InfraActiveRequests request, String vnfId) {
		if(request != null) {
			request.setVnfId(vnfId);
			this.requestsDbClient.updateInfraActiveRequests(request);
		} else {
			logger.debug(REQUEST_ERROR);
		}
	}

	public void updateInfraActiveRequestVfModuleId(InfraActiveRequests request, String vfModuleId) {
		if(request != null) {
			request.setVfModuleId(vfModuleId);
			this.requestsDbClient.updateInfraActiveRequests(request);
		} else {
			logger.debug(REQUEST_ERROR);
		}
	}

	public void updateInfraActiveRequestVolumeGroupId(InfraActiveRequests request, String volumeGroupId) {
		if(request != null) {
			request.setVolumeGroupId(volumeGroupId);
			this.requestsDbClient.updateInfraActiveRequests(request);
		} else {
			logger.debug(REQUEST_ERROR);
		}
	}

	public void updateInfraActiveRequestNetworkId(InfraActiveRequests request, String networkId) {
		if(request != null) {
			request.setNetworkId(networkId);
			this.requestsDbClient.updateInfraActiveRequests(request);
		} else {
			logger.debug(REQUEST_ERROR);
		}
	}
	
	public Service getCatalogServiceByModelUUID(String modelUUID) {
		return catalogDbClient.getServiceByID(modelUUID);
	}
	
	public Service getCatalogServiceByModelVersionAndModelInvariantUUID(String modelVersion, String modelInvariantUUID) {
		return catalogDbClient.getServiceByModelVersionAndModelInvariantUUID(modelVersion, modelInvariantUUID);
	}

	public CollectionNetworkResourceCustomization getCatalogCollectionNetworkResourceCustByID(String key) {
		return catalogDbClient.getCollectionNetworkResourceCustomizationByID(key);
	}

	public NetworkCollectionResourceCustomization getCatalogNetworkCollectionResourceCustByID(
			String collectionCustomizationId) {
		return catalogDbClient.getNetworkCollectionResourceCustomizationByID(collectionCustomizationId);
	}

	public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroups(String modelCustomizationUUID) {
		return catalogDbClient.getVnfcInstanceGroupsByVnfResourceCust(modelCustomizationUUID);
	}

	public Map<String, String> getURIKeysFromServiceInstance(String serviceInstanceId) {
		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
		return uri.getURIKeys();
	}

	protected RequestDetails getRequestDetails(String requestId) throws IOException {
		if (requestId != null && !requestId.isEmpty()) {
			InfraActiveRequests activeRequest = this.getInfraActiveRequest(requestId);
			String requestBody = activeRequest.getRequestBody().replaceAll("\\\\", "");
			objectMapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
			objectMapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
			return objectMapper.readValue(requestBody, RequestDetails.class);
		} else {
			return null;
		}
	}

	protected InfraActiveRequests getInfraActiveRequest(String requestId) {
		if (requestId != null && !requestId.isEmpty()) {
			return requestsDbClient.getInfraActiveRequestbyRequestId(requestId);
		} else {
			return null;
		}
	}

	protected CloudRegion getCloudRegion(CloudConfiguration cloudConfiguration) {
		if (cloudConfiguration != null) {
			String cloudRegionId = cloudConfiguration.getLcpCloudRegionId();
			String cloudOwner = cloudConfiguration.getCloudOwner();
			if (cloudRegionId != null && cloudOwner != null && !cloudRegionId.isEmpty() && !cloudOwner.isEmpty()) {
				return injectionHelper.getAaiClient().get(CloudRegion.class,
						AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner,
								cloudRegionId)).orElse(null);
			
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	protected InstanceGroup getAAIInstanceGroup(String instanceGroupId) {
		return injectionHelper.getAaiClient().get(InstanceGroup.class,
			AAIUriFactory.createResourceUri(AAIObjectType.INSTANCE_GROUP, instanceGroupId))
			.orElse(null);
	}

	public org.onap.aai.domain.yang.Customer getAAICustomer(String globalSubscriberId) {
		return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Customer.class,
			AAIUriFactory.createResourceUri(AAIObjectType.CUSTOMER, globalSubscriberId))
			.orElse(null);
	}

	public ServiceSubscription getAAIServiceSubscription(String globalSubscriberId, String subscriptionServiceType) {
	
		if(globalSubscriberId == null || globalSubscriberId.equals("") || subscriptionServiceType == null || subscriptionServiceType.equals("")) {
			return null;
		} else {
			return injectionHelper.getAaiClient().get(ServiceSubscription.class, AAIUriFactory.createResourceUri(
					AAIObjectType.SERVICE_SUBSCRIPTION, globalSubscriberId, subscriptionServiceType))
					.orElse(null);
		}
		
	}

	public ServiceInstance getAAIServiceInstanceById(String serviceInstanceId) {
		return injectionHelper.getAaiClient().get(ServiceInstance.class,
				AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId).depth(Depth.TWO))
				.orElse(null);
	}

	protected ServiceInstance getAAIServiceInstanceByIdAndCustomer(String globalCustomerId, String serviceType,
			String serviceInstanceId) {
		return injectionHelper.getAaiClient().get(ServiceInstance.class, AAIUriFactory.createResourceUri(
				AAIObjectType.SERVICE_INSTANCE, globalCustomerId, serviceType, serviceInstanceId).depth(Depth.TWO))
				.orElse(null);
	}

	protected org.onap.aai.domain.yang.ServiceInstances getAAIServiceInstancesByName(String serviceInstanceName,
			Customer customer) {
		
		return injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.ServiceInstances.class,
				AAIUriFactory
						.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
								customer.getServiceSubscription().getServiceType())
						.queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
				.orElseGet(() -> {
					logger.debug("No Service Instance matched by name");
					return null;
				});
	}

	public org.onap.aai.domain.yang.ServiceInstance getAAIServiceInstanceByName(String serviceInstanceName,
			Customer customer) throws Exception {
		org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance = null;
		org.onap.aai.domain.yang.ServiceInstances aaiServiceInstances = null;
		aaiServiceInstances = getAAIServiceInstancesByName(serviceInstanceName, customer);

		if (aaiServiceInstances == null) {
			return null;
		} else if (aaiServiceInstances.getServiceInstance().size() > 1) {
			throw new Exception("Multiple Service Instances Returned");
		} else {
			aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
		}
		return aaiServiceInstance;
	}

	protected ServiceInstances getAAIServiceInstancesByName(String globalCustomerId, String serviceType,
			String serviceInstanceName) {
		
		return injectionHelper.getAaiClient().get(ServiceInstances.class,
				AAIUriFactory.createResourceUri(AAIObjectPlurals.SERVICE_INSTANCE, globalCustomerId, serviceType)
						.queryParam("service-instance-name", serviceInstanceName).depth(Depth.TWO))
				.orElseGet(() -> {
					logger.debug("No Service Instance matched by name");
					return null;
				});
	}

	public Optional<ServiceInstance> getAAIServiceInstanceByName(String globalCustomerId, String serviceType,
			String serviceInstanceName) throws Exception {
		ServiceInstance aaiServiceInstance = null;
		ServiceInstances aaiServiceInstances = null;
		aaiServiceInstances = getAAIServiceInstancesByName(globalCustomerId, serviceType, serviceInstanceName);

		if (aaiServiceInstances == null) {
			return Optional.empty();
		} else if (aaiServiceInstances.getServiceInstance().size() > 1) {
			throw new Exception("Multiple Service Instances Returned");
		} else {
			aaiServiceInstance = aaiServiceInstances.getServiceInstance().get(0);
		}
		return Optional.of(aaiServiceInstance);
	}

	public org.onap.so.db.catalog.beans.InstanceGroup getCatalogInstanceGroup(String modelUUID) {
		return this.catalogDbClient.getInstanceGroupByModelUUID(modelUUID);
	}

	public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomization(
			String modelCustomizationUUID) {
		return this.catalogDbClient
				.getCollectionResourceInstanceGroupCustomizationByModelCustUUID(modelCustomizationUUID);
	}

	public AAIResultWrapper getAAIResourceDepthOne(AAIResourceUri aaiResourceUri) {
		AAIResourceUri clonedUri = aaiResourceUri.clone();
		return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.ONE));
	}
	
	public AAIResultWrapper getAAIResourceDepthTwo(AAIResourceUri aaiResourceUri) {
		AAIResourceUri clonedUri = aaiResourceUri.clone();
		return this.injectionHelper.getAaiClient().get(clonedUri.depth(Depth.TWO));
	}

	public Configuration getAAIConfiguration(String configurationId) {
		return this.injectionHelper.getAaiClient().get(Configuration.class,
				AAIUriFactory.createResourceUri(AAIObjectType.CONFIGURATION, configurationId).depth(Depth.ONE))
				.orElseGet(() -> {
					logger.debug("No Configuration matched by id");
					return null;
				});
	}
	
	public GenericVnf getAAIGenericVnf(String vnfId) {
		
		return this.injectionHelper.getAaiClient().get(GenericVnf.class,
				AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId).depth(Depth.ONE))
				.orElseGet(() -> {
					logger.debug("No Generic Vnf matched by id");
					return null;
				});
	}
	
	public VolumeGroup getAAIVolumeGroup(String cloudOwnerId, String cloudRegionId, String volumeGroupId) {
		return this.injectionHelper.getAaiClient().get(VolumeGroup.class,
				AAIUriFactory.createResourceUri(AAIObjectType.VOLUME_GROUP, cloudOwnerId, cloudRegionId, volumeGroupId).depth(Depth.ONE))
				.orElseGet(() -> {
					logger.debug("No Generic Vnf matched by id");
					return null;
				});
	}

	public VfModule getAAIVfModule(String vnfId, String vfModuleId) {
		return this.injectionHelper.getAaiClient().get(VfModule.class,
				AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId).depth(Depth.ONE))
				.orElseGet(() -> {
					logger.debug("No Generic Vnf matched by id");
					return null;
				});
	}
	
	public L3Network getAAIL3Network(String networkId) {

		return this.injectionHelper.getAaiClient().get(L3Network.class,
				AAIUriFactory.createResourceUri(AAIObjectType.L3_NETWORK, networkId).depth(Depth.ONE))
				.orElseGet(() -> {
					logger.debug("No Generic Vnf matched by id");
					return null;
				});
		
	}
	
	public Optional<L3Network> getRelatedNetworkByNameFromServiceInstance(String serviceInstanceId, String networkName) throws Exception{
		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
		uri.relatedTo(AAIObjectPlurals.L3_NETWORK).queryParam("network-name", networkName);
		Optional<L3Networks> networks = injectionHelper.getAaiClient().get(L3Networks.class, uri);
		L3Network network = null;
		if (!networks.isPresent()) {
			logger.debug("No Networks matched by name");
			return Optional.empty();
		} else {
			if (networks.get().getL3Network().size() > 1) {
				throw new Exception("Multiple Networks Returned");
			} else {
				network = networks.get().getL3Network().get(0);
			}
			return Optional.of(network);
		}
	}

	public Optional<GenericVnf> getRelatedVnfByNameFromServiceInstance(String serviceInstanceId, String vnfName) throws Exception {
		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId);
		uri.relatedTo(AAIObjectPlurals.GENERIC_VNF).queryParam("vnf-name", vnfName);
		Optional<GenericVnfs> vnfs = injectionHelper.getAaiClient().get(GenericVnfs.class, uri);
		GenericVnf vnf = null;
		if (!vnfs.isPresent()) {
			logger.debug("No Vnfs matched by name");
			return Optional.empty();
		} else {
			 if (vnfs.get().getGenericVnf().size() > 1) {
				throw new Exception("Multiple Vnfs Returned");
			} else {
				vnf = vnfs.get().getGenericVnf().get(0);
			}
			return Optional.of(vnf);
		}
	}

	public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVnf(String vnfId, String volumeGroupName) throws Exception{
		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId);
		uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
		Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
		VolumeGroup volumeGroup = null;
		if (!volumeGroups.isPresent()) {
			logger.debug("No VolumeGroups matched by name");
			return Optional.empty();
		} else {
			if (volumeGroups.get().getVolumeGroup().size() > 1) {
				throw new Exception("Multiple VolumeGroups Returned");
			} else {
				volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
			}
			return Optional.of(volumeGroup);
		}
	}

	public Optional<VolumeGroup> getRelatedVolumeGroupByNameFromVfModule(String vnfId, String vfModuleId, String volumeGroupName) throws Exception {
		AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.VF_MODULE, vnfId, vfModuleId);
		uri.relatedTo(AAIObjectPlurals.VOLUME_GROUP).queryParam("volume-group-name", volumeGroupName);
		Optional<VolumeGroups> volumeGroups = injectionHelper.getAaiClient().get(VolumeGroups.class, uri);
		VolumeGroup volumeGroup = null;
		if (!volumeGroups.isPresent()) {
			logger.debug("No VolumeGroups matched by name");
			return Optional.empty();
		} else {
			if (volumeGroups.get().getVolumeGroup().size() > 1) {
				throw new Exception("Multiple VolumeGroups Returned");
			} else {
				volumeGroup = volumeGroups.get().getVolumeGroup().get(0);
			}
			return Optional.of(volumeGroup);
		}
	}
}