Merge "added in mapping of aggregate routes in"
[so.git] / mso-catalog-db / src / main / java / org / onap / so / db / catalog / client / CatalogDbClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.db.catalog.client;
22
23 import java.net.URI;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.stream.Collectors;
28 import javax.annotation.PostConstruct;
29 import javax.persistence.EntityNotFoundException;
30 import javax.ws.rs.core.UriBuilder;
31 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
32 import org.onap.so.db.catalog.beans.CloudSite;
33 import org.onap.so.db.catalog.beans.CloudifyManager;
34 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
35 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
36 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
37 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
38 import org.onap.so.db.catalog.beans.CvnfcCustomization;
39 import org.onap.so.db.catalog.beans.ExternalServiceToInternalService;
40 import org.onap.so.db.catalog.beans.HomingInstance;
41 import org.onap.so.db.catalog.beans.InstanceGroup;
42 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
43 import org.onap.so.db.catalog.beans.NetworkRecipe;
44 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
45 import org.onap.so.db.catalog.beans.OrchestrationAction;
46 import org.onap.so.db.catalog.beans.OrchestrationStatus;
47 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
48 import org.onap.so.db.catalog.beans.PnfResource;
49 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
50 import org.onap.so.db.catalog.beans.ResourceType;
51 import org.onap.so.db.catalog.beans.Service;
52 import org.onap.so.db.catalog.beans.ServiceRecipe;
53 import org.onap.so.db.catalog.beans.VfModule;
54 import org.onap.so.db.catalog.beans.VfModuleCustomization;
55 import org.onap.so.db.catalog.beans.VnfComponentsRecipe;
56 import org.onap.so.db.catalog.beans.VnfRecipe;
57 import org.onap.so.db.catalog.beans.VnfResource;
58 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
59 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
60 import org.onap.so.db.catalog.beans.Workflow;
61 import org.onap.so.db.catalog.beans.macro.NorthBoundRequest;
62 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
63 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
64 import org.onap.so.logger.LogConstants;
65 import org.onap.so.logging.jaxrs.filter.SpringClientFilter;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.beans.factory.annotation.Value;
70 import org.springframework.http.HttpHeaders;
71 import org.springframework.http.client.BufferingClientHttpRequestFactory;
72 import org.springframework.http.client.ClientHttpRequestFactory;
73 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
74 import org.springframework.stereotype.Component;
75 import org.springframework.web.client.RestTemplate;
76 import org.springframework.web.util.UriComponentsBuilder;
77 import uk.co.blackpepper.bowman.Client;
78 import uk.co.blackpepper.bowman.ClientFactory;
79 import uk.co.blackpepper.bowman.Configuration;
80
81 @Component("CatalogDbClient")
82 public class CatalogDbClient {
83
84     private static final Logger logger = LoggerFactory.getLogger(CatalogDbClient.class);
85
86     private static final String CLOUD_SITE = "/cloudSite";
87     private static final String CLOUDIFY_MANAGER = "/cloudifyManager";
88     private static final String RAINY_DAY_HANDLER_MACRO = "/rainy_day_handler_macro";
89     private static final String NORTHBOUND_REQUEST_REF_LOOKUP = "/northbound_request_ref_lookup";
90     private static final String NETWORK_RESOURCE_CUSTOMIZATION = "/networkResourceCustomization";
91     private static final String COLLECTION_RESOURCE_INSTANCE_GROUP_CUSTOMIZATION =
92             "/collectionResourceInstanceGroupCustomization";
93     private static final String VNFC_INSTANCE_GROUP_CUSTOMIZATION = "/vnfcInstanceGroupCustomization";
94     private static final String ORCHESTRATION_FLOW = "/orchestrationFlow";
95     private static final String ORCHESTRATION_STATUS_STATE_TRANSITION_DIRECTIVE =
96             "/orchestrationStatusStateTransitionDirective";
97     private static final String INSTANCE_GROUP = "/instanceGroup";
98     private static final String COLLECTION_NETWORK_RESOURCE_CUSTOMIZATION = "/collectionNetworkResourceCustomization";
99     private static final String BUILDING_BLOCK_DETAIL = "/buildingBlockDetail";
100     private static final String NETWORK_COLLECTION_RESOURCE_CUSTOMIZATION = "/networkCollectionResourceCustomization";
101     private static final String VNF_RESOURCE_CUSTOMIZATION = "/vnfResourceCustomization";
102     private static final String SERVICE = "/service";
103     private static final String EXTERNAL_SERVICE_TO_INTERNAL_MODEL_MAPPING = "/externalServiceToInternalService";
104     private static final String VNF_RESOURCE = "/vnfResource";
105     private static final String VNF_RECIPE = "/vnfRecipe";
106     private static final String VFMODULE = "/vfModule";
107     private static final String VFMODULE_CUSTOMIZATION = "/vfModuleCustomization";
108     private static final String VNF_COMPONENTS_RECIPE = "/vnfComponentsRecipe";
109     private static final String SERVICE_RECIPE = "/serviceRecipe";
110     private static final String NETWORK_RECIPE = "/networkRecipe";
111     private static final String PNF_RESOURCE = "/pnfResource";
112     private static final String PNF_RESOURCE_CUSTOMIZATION = "/pnfResourceCustomization";
113     private static final String WORKFLOW = "/workflow";
114
115
116     private static final String SEARCH = "/search";
117     private static final String URI_SEPARATOR = "/";
118
119     private static final String SERVICE_MODEL_UUID = "serviceModelUUID";
120     private static final String SERVICE_NAME = "serviceName";
121     private static final String MODEL_UUID = "modelUUID";
122     private static final String MODEL_CUSTOMIZATION_UUID = "modelCustomizationUUID";
123     private static final String ACTION = "action";
124     private static final String MODEL_NAME = "modelName";
125     private static final String MODEL_VERSION = "modelVersion";
126     private static final String MODEL_INVARIANT_UUID = "modelInvariantUUID";
127     private static final String VNF_RESOURCE_MODEL_UUID = "vnfResourceModelUUID";
128     private static final String NF_ROLE = "nfRole";
129     private static final String VF_MODULE_MODEL_UUID = "vfModuleModelUUID";
130     private static final String VNF_COMPONENT_TYPE = "vnfComponentType";
131     private static final String BUILDING_BLOCK_NAME = "buildingBlockName";
132     private static final String RESOURCE_TYPE = "resourceType";
133     private static final String ORCHESTRATION_STATUS = "orchestrationStatus";
134     private static final String TARGET_ACTION = "targetAction";
135     private static final String REQUEST_SCOPE = "requestScope";
136     private static final String IS_ALACARTE = "isALaCarte";
137     private static final String CLOUD_OWNER = "cloudOwner";
138     private static final String FLOW_NAME = "flowName";
139     private static final String ERROR_MESSAGE = "errorMessage";
140     private static final String SERVICE_ROLE = "serviceRole";
141     private static final String SERVICE_TYPE = "serviceType";
142     private static final String VNF_TYPE = "vnfType";
143     private static final String ERROR_CODE = "errorCode";
144     private static final String WORK_STEP = "workStep";
145     private static final String CLLI = "clli";
146     private static final String CLOUD_VERSION = "cloudVersion";
147     private static final String HOMING_INSTANCE = "/homingInstance";
148     private static final String ARTIFACT_UUID = "artifactUUID";
149     private static final String SOURCE = "source";
150
151     private static final String TARGET_ENTITY = "SO:CatalogDB";
152     private static final String ASTERISK = "*";
153
154     private String findExternalToInternalServiceByServiceName = "/findByServiceName";
155     private String findServiceByModelName = "/findOneByModelName";
156     private String findServiceRecipeByActionAndServiceModelUUID = "/findByActionAndServiceModelUUID";
157     private String findServiceByModelUUID = "/findOneByModelUUID";
158     private String findFirstByModelNameURI = "/findFirstByModelNameOrderByModelVersionDesc";
159     private String findFirstByServiceModelUUIDAndActionURI = "/findFirstByServiceModelUUIDAndAction";
160     private String findFirstByModelVersionAndModelInvariantUUIDURI = "/findFirstByModelVersionAndModelInvariantUUID";
161     private String findByModelInvariantUUIDURI = "/findByModelInvariantUUIDOrderByModelVersionDesc";
162     private String findFirstByModelNameAndAction = "/findFirstByModelNameAndAction";
163     private String findFirstResourceByModelInvariantUUIDAndModelVersion =
164             "/findFirstResourceByModelInvariantUUIDAndModelVersion";
165     private String findByModelInstanceNameAndVnfResources = "/findByModelInstanceNameAndVnfResources";
166     private String findFirstVnfRecipeByNfRoleAndAction = "/findFirstVnfRecipeByNfRoleAndAction";
167     private String findByModelCustomizationUUIDAndVfModuleModelUUID =
168             "/findFirstByModelCustomizationUUIDAndVfModuleModelUUIDOrderByCreatedDesc";
169     private String findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction =
170             "/findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction";
171     private String findFirstVnfComponentsRecipeByVnfComponentTypeAndAction =
172             "/findFirstVnfComponentsRecipeByVnfComponentTypeAndAction";
173     private String findVfModuleByModelInvariantUUIDOrderByModelVersionDesc =
174             "/findByModelInvariantUUIDOrderByModelVersionDesc";
175     private String findFirstVfModuleByModelInvariantUUIDAndModelVersion =
176             "/findFirstVfModuleByModelInvariantUUIDAndModelVersion";
177     private String findOneByBuildingBlockName = "/findOneByBuildingBlockName";
178     private String findOneByResourceTypeAndOrchestrationStatusAndTargetAction =
179             "/findOneByResourceTypeAndOrchestrationStatusAndTargetAction";
180     private String findByAction = "/findByAction";
181     private String findVnfcInstanceGroupCustomizationByModelCustomizationUUID = "/findByModelCustomizationUUID";
182     private String findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID =
183             "/findByModelCustomizationUUID";
184     private String findOneByActionAndRequestScopeAndIsAlacarte = "/findOneByActionAndRequestScopeAndIsAlacarte";
185     private String findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner =
186             "/findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner";
187     private String findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType =
188             "/findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType";
189
190     private static final String findRainyDayHandler = "/findRainyDayHandler";
191     private String findByClliAndCloudVersion = "/findByClliAndCloudVersion";
192     private String findServiceByServiceInstanceId = "/findServiceByServiceInstanceId";
193     private String findPnfResourceCustomizationByModelUuid = "/findPnfResourceCustomizationByModelUuid";
194     private String findWorkflowByArtifactUUID = "/findByArtifactUUID";
195     private String findWorkflowByModelUUID = "/findWorkflowByModelUUID";
196     private String findWorkflowBySource = "/findBySource";
197     private String findVnfResourceCustomizationByModelUuid = "/findVnfResourceCustomizationByModelUuid";
198
199     private String serviceURI;
200     private String vfModuleURI;
201     private String vnfResourceURI;
202     private String networkCollectionResourceCustomizationURI;
203     private String networkResourceCustomizationURI;
204     private String collectionNetworkResourceCustomizationURI;
205     private String instanceGroupURI;
206     private String cloudifyManagerURI;
207     private String cloudSiteURI;
208     private String homingInstanceURI;
209     private String cvnfcResourceCustomizationURI;
210     private String pnfResourceURI;
211     private String pnfResourceCustomizationURI;
212     private String workflowURI;
213
214     private final Client<Service> serviceClient;
215
216     private final Client<NetworkRecipe> networkRecipeClient;
217
218     private final Client<NetworkResourceCustomization> networkResourceCustomizationClient;
219
220     private final Client<VnfResource> vnfResourceClient;
221
222     private final Client<VnfResourceCustomization> vnfResourceCustomizationClient;
223
224     private final Client<VnfRecipe> vnfRecipeClient;
225
226     private final Client<VfModuleCustomization> vfModuleCustomizationClient;
227
228     private final Client<VfModule> vfModuleClient;
229
230     private final Client<VnfComponentsRecipe> vnfComponentsRecipeClient;
231
232     private final Client<OrchestrationFlow> orchestrationClient;
233
234     private final Client<NorthBoundRequest> northBoundRequestClient;
235
236     private final Client<RainyDayHandlerStatus> rainyDayHandlerStatusClient;
237
238     private final Client<BuildingBlockDetail> buildingBlockDetailClient;
239
240     private final Client<OrchestrationStatusStateTransitionDirective> orchestrationStatusStateTransitionDirectiveClient;
241
242     private final Client<VnfcInstanceGroupCustomization> vnfcInstanceGroupCustomizationClient;
243
244     private final Client<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationClient;
245
246     private final Client<InstanceGroup> instanceGroupClient;
247
248     private final Client<NetworkCollectionResourceCustomization> networkCollectionResourceCustomizationClient;
249
250     private final Client<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationClient;
251
252     private final Client<ServiceRecipe> serviceRecipeClient;
253
254     private final Client<ExternalServiceToInternalService> externalServiceToInternalServiceClient;
255
256     private final Client<CloudSite> cloudSiteClient;
257
258     private final Client<HomingInstance> homingInstanceClient;
259
260     private final Client<CloudifyManager> cloudifyManagerClient;
261
262     private final Client<ControllerSelectionReference> controllerSelectionReferenceClient;
263
264     private final Client<PnfResource> pnfResourceClient;
265
266     private final Client<PnfResourceCustomization> pnfResourceCustomizationClient;
267
268     private final Client<Workflow> workflowClient;
269
270     @Value("${mso.catalog.db.spring.endpoint:#{null}}")
271     private String endpoint;
272
273     @Value("${mso.db.auth:#{null}}")
274     private String msoAdaptersAuth;
275
276     @Autowired
277     RestTemplate restTemplate;
278
279     @PostConstruct
280     public void init() {
281         findExternalToInternalServiceByServiceName = endpoint + EXTERNAL_SERVICE_TO_INTERNAL_MODEL_MAPPING + SEARCH
282                 + findExternalToInternalServiceByServiceName;
283         findServiceByModelName = endpoint + SERVICE + SEARCH + findServiceByModelName;
284         findServiceRecipeByActionAndServiceModelUUID =
285                 endpoint + SERVICE_RECIPE + SEARCH + findServiceRecipeByActionAndServiceModelUUID;
286         findServiceByModelUUID = endpoint + SERVICE + SEARCH + findServiceByModelUUID;
287         findFirstByModelNameURI = endpoint + SERVICE + SEARCH + findFirstByModelNameURI;
288         findFirstByModelVersionAndModelInvariantUUIDURI =
289                 endpoint + SERVICE + SEARCH + findFirstByModelVersionAndModelInvariantUUIDURI;
290         findByModelInvariantUUIDURI = endpoint + SERVICE + SEARCH + findByModelInvariantUUIDURI;
291         findFirstByServiceModelUUIDAndActionURI =
292                 endpoint + SERVICE_RECIPE + SEARCH + findFirstByServiceModelUUIDAndActionURI;
293         findFirstByModelNameAndAction = endpoint + NETWORK_RECIPE + SEARCH + findFirstByModelNameAndAction;
294         findFirstResourceByModelInvariantUUIDAndModelVersion =
295                 endpoint + VNF_RESOURCE + SEARCH + findFirstResourceByModelInvariantUUIDAndModelVersion;
296         findByModelInstanceNameAndVnfResources =
297                 endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH + findByModelInstanceNameAndVnfResources;
298         findFirstVnfRecipeByNfRoleAndAction = endpoint + VNF_RECIPE + SEARCH + findFirstVnfRecipeByNfRoleAndAction;
299         findByModelCustomizationUUIDAndVfModuleModelUUID =
300                 endpoint + VFMODULE_CUSTOMIZATION + SEARCH + findByModelCustomizationUUIDAndVfModuleModelUUID;
301         findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction = endpoint + VNF_COMPONENTS_RECIPE
302                 + SEARCH + findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction;
303         findFirstVnfComponentsRecipeByVnfComponentTypeAndAction =
304                 endpoint + VNF_COMPONENTS_RECIPE + SEARCH + findFirstVnfComponentsRecipeByVnfComponentTypeAndAction;
305         findVfModuleByModelInvariantUUIDOrderByModelVersionDesc =
306                 endpoint + VFMODULE + SEARCH + findVfModuleByModelInvariantUUIDOrderByModelVersionDesc;
307         findFirstVfModuleByModelInvariantUUIDAndModelVersion =
308                 endpoint + VFMODULE + SEARCH + findFirstVfModuleByModelInvariantUUIDAndModelVersion;
309         findOneByBuildingBlockName = endpoint + BUILDING_BLOCK_DETAIL + SEARCH + findOneByBuildingBlockName;
310         findOneByResourceTypeAndOrchestrationStatusAndTargetAction =
311                 endpoint + ORCHESTRATION_STATUS_STATE_TRANSITION_DIRECTIVE + SEARCH
312                         + findOneByResourceTypeAndOrchestrationStatusAndTargetAction;
313         findByAction = endpoint + ORCHESTRATION_FLOW + SEARCH + findByAction;
314         findVnfcInstanceGroupCustomizationByModelCustomizationUUID = endpoint + VNFC_INSTANCE_GROUP_CUSTOMIZATION
315                 + SEARCH + findVnfcInstanceGroupCustomizationByModelCustomizationUUID;
316         findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID =
317                 endpoint + COLLECTION_RESOURCE_INSTANCE_GROUP_CUSTOMIZATION + SEARCH
318                         + findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID;
319         findOneByActionAndRequestScopeAndIsAlacarte =
320                 endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH + findOneByActionAndRequestScopeAndIsAlacarte;
321         findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner = endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH
322                 + findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner;
323         findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType =
324                 endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH
325                         + findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType;
326         findByClliAndCloudVersion = endpoint + CLOUD_SITE + SEARCH + findByClliAndCloudVersion;
327
328         findPnfResourceCustomizationByModelUuid =
329                 endpoint + PNF_RESOURCE_CUSTOMIZATION + SEARCH + findPnfResourceCustomizationByModelUuid;
330
331         findWorkflowByArtifactUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByArtifactUUID;
332         findWorkflowByModelUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByModelUUID;
333         findWorkflowBySource = endpoint + WORKFLOW + SEARCH + findWorkflowBySource;
334
335         findVnfResourceCustomizationByModelUuid =
336                 endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH + findVnfResourceCustomizationByModelUuid;
337
338         serviceURI = endpoint + SERVICE + URI_SEPARATOR;
339         vfModuleURI = endpoint + VFMODULE + URI_SEPARATOR;
340         vnfResourceURI = endpoint + VNF_RESOURCE + URI_SEPARATOR;
341         networkCollectionResourceCustomizationURI =
342                 endpoint + NETWORK_COLLECTION_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
343         networkResourceCustomizationURI = endpoint + NETWORK_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
344         collectionNetworkResourceCustomizationURI =
345                 endpoint + COLLECTION_NETWORK_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
346         instanceGroupURI = endpoint + INSTANCE_GROUP + URI_SEPARATOR;
347         cloudifyManagerURI = endpoint + CLOUDIFY_MANAGER + URI_SEPARATOR;
348         cloudSiteURI = endpoint + CLOUD_SITE + URI_SEPARATOR;
349         homingInstanceURI = endpoint + HOMING_INSTANCE + URI_SEPARATOR;
350         pnfResourceURI = endpoint + PNF_RESOURCE + URI_SEPARATOR;
351         pnfResourceCustomizationURI = endpoint + PNF_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
352         workflowURI = endpoint + WORKFLOW + URI_SEPARATOR;
353
354     }
355
356     public CatalogDbClient() {
357         ClientHttpRequestFactory factory =
358                 new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory());
359
360         ClientFactory clientFactory =
361                 Configuration.builder().setClientHttpRequestFactory(factory).setRestTemplateConfigurer(restTemplate -> {
362                     restTemplate.getInterceptors().add((new SpringClientFilter()));
363
364                     restTemplate.getInterceptors().add((request, body, execution) -> {
365
366                         request.getHeaders().add(HttpHeaders.AUTHORIZATION, msoAdaptersAuth);
367                         request.getHeaders().add(LogConstants.TARGET_ENTITY_HEADER, TARGET_ENTITY);
368                         return execution.execute(request, body);
369                     });
370                 }).build().buildClientFactory();
371         serviceClient = clientFactory.create(Service.class);
372         networkRecipeClient = clientFactory.create(NetworkRecipe.class);
373         networkResourceCustomizationClient = clientFactory.create(NetworkResourceCustomization.class);
374         vnfResourceClient = clientFactory.create(VnfResource.class);
375         vnfResourceCustomizationClient = clientFactory.create(VnfResourceCustomization.class);
376         vnfRecipeClient = clientFactory.create(VnfRecipe.class);
377         orchestrationClient = clientFactory.create(OrchestrationFlow.class);
378         vfModuleCustomizationClient = clientFactory.create(VfModuleCustomization.class);
379         vfModuleClient = clientFactory.create(VfModule.class);
380         vnfComponentsRecipeClient = clientFactory.create(VnfComponentsRecipe.class);
381         northBoundRequestClient = clientFactory.create(NorthBoundRequest.class);
382         rainyDayHandlerStatusClient = clientFactory.create(RainyDayHandlerStatus.class);
383         buildingBlockDetailClient = clientFactory.create(BuildingBlockDetail.class);
384         orchestrationStatusStateTransitionDirectiveClient =
385                 clientFactory.create(OrchestrationStatusStateTransitionDirective.class);
386         vnfcInstanceGroupCustomizationClient = clientFactory.create(VnfcInstanceGroupCustomization.class);
387         collectionResourceInstanceGroupCustomizationClient =
388                 clientFactory.create(CollectionResourceInstanceGroupCustomization.class);
389         instanceGroupClient = clientFactory.create(InstanceGroup.class);
390         networkCollectionResourceCustomizationClient =
391                 clientFactory.create(NetworkCollectionResourceCustomization.class);
392         collectionNetworkResourceCustomizationClient =
393                 clientFactory.create(CollectionNetworkResourceCustomization.class);
394         cloudSiteClient = clientFactory.create(CloudSite.class);
395         homingInstanceClient = clientFactory.create(HomingInstance.class);
396         cloudifyManagerClient = clientFactory.create(CloudifyManager.class);
397         serviceRecipeClient = clientFactory.create(ServiceRecipe.class);
398         controllerSelectionReferenceClient = clientFactory.create(ControllerSelectionReference.class);
399         externalServiceToInternalServiceClient = clientFactory.create(ExternalServiceToInternalService.class);
400         pnfResourceClient = clientFactory.create(PnfResource.class);
401         pnfResourceCustomizationClient = clientFactory.create(PnfResourceCustomization.class);
402         workflowClient = clientFactory.create(Workflow.class);
403     }
404
405     public CatalogDbClient(String baseUri, String auth) {
406         ClientHttpRequestFactory factory =
407                 new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory());
408
409         ClientFactory clientFactory = Configuration.builder().setBaseUri(baseUri).setClientHttpRequestFactory(factory)
410                 .setRestTemplateConfigurer(restTemplate -> {
411                     restTemplate.getInterceptors().add((new SpringClientFilter()));
412
413                     restTemplate.getInterceptors().add((request, body, execution) -> {
414
415                         request.getHeaders().add(HttpHeaders.AUTHORIZATION, auth);
416                         request.getHeaders().add(LogConstants.TARGET_ENTITY_HEADER, TARGET_ENTITY);
417                         return execution.execute(request, body);
418                     });
419                 }).build().buildClientFactory();
420         serviceClient = clientFactory.create(Service.class);
421         networkRecipeClient = clientFactory.create(NetworkRecipe.class);
422         networkResourceCustomizationClient = clientFactory.create(NetworkResourceCustomization.class);
423         vnfResourceClient = clientFactory.create(VnfResource.class);
424         vnfResourceCustomizationClient = clientFactory.create(VnfResourceCustomization.class);
425         vnfRecipeClient = clientFactory.create(VnfRecipe.class);
426         orchestrationClient = clientFactory.create(OrchestrationFlow.class);
427         vfModuleCustomizationClient = clientFactory.create(VfModuleCustomization.class);
428         vfModuleClient = clientFactory.create(VfModule.class);
429         vnfComponentsRecipeClient = clientFactory.create(VnfComponentsRecipe.class);
430         northBoundRequestClient = clientFactory.create(NorthBoundRequest.class);
431         rainyDayHandlerStatusClient = clientFactory.create(RainyDayHandlerStatus.class);
432         buildingBlockDetailClient = clientFactory.create(BuildingBlockDetail.class);
433         orchestrationStatusStateTransitionDirectiveClient =
434                 clientFactory.create(OrchestrationStatusStateTransitionDirective.class);
435         vnfcInstanceGroupCustomizationClient = clientFactory.create(VnfcInstanceGroupCustomization.class);
436         collectionResourceInstanceGroupCustomizationClient =
437                 clientFactory.create(CollectionResourceInstanceGroupCustomization.class);
438         instanceGroupClient = clientFactory.create(InstanceGroup.class);
439         networkCollectionResourceCustomizationClient =
440                 clientFactory.create(NetworkCollectionResourceCustomization.class);
441         collectionNetworkResourceCustomizationClient =
442                 clientFactory.create(CollectionNetworkResourceCustomization.class);
443         cloudSiteClient = clientFactory.create(CloudSite.class);
444         homingInstanceClient = clientFactory.create(HomingInstance.class);
445         cloudifyManagerClient = clientFactory.create(CloudifyManager.class);
446         serviceRecipeClient = clientFactory.create(ServiceRecipe.class);
447         controllerSelectionReferenceClient = clientFactory.create(ControllerSelectionReference.class);
448         externalServiceToInternalServiceClient = clientFactory.create(ExternalServiceToInternalService.class);
449         pnfResourceClient = clientFactory.create(PnfResource.class);
450         pnfResourceCustomizationClient = clientFactory.create(PnfResourceCustomization.class);
451         workflowClient = clientFactory.create(Workflow.class);
452     }
453
454     public NetworkCollectionResourceCustomization getNetworkCollectionResourceCustomizationByID(
455             String modelCustomizationUUID) {
456         NetworkCollectionResourceCustomization networkCollectionResourceCustomization =
457                 this.getSingleResource(networkCollectionResourceCustomizationClient,
458                         getUri(networkCollectionResourceCustomizationURI + modelCustomizationUUID));
459         if (networkCollectionResourceCustomization != null) {
460             networkCollectionResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
461         }
462         return networkCollectionResourceCustomization;
463     }
464
465     public Service getServiceByID(String modelUUID) {
466         Service service = getSingleResource(serviceClient, getUri(serviceURI + modelUUID));
467         if (service != null) {
468             service.setModelUUID(modelUUID);
469         }
470         return service;
471     }
472
473     public VfModule getVfModuleByModelUUID(String modelUUID) {
474         VfModule vfModule = getSingleResource(vfModuleClient, getUri(vfModuleURI + modelUUID));
475         if (vfModule != null) {
476             vfModule.setModelUUID(modelUUID);
477         }
478         return vfModule;
479     }
480
481     public VnfResource getVnfResourceByModelUUID(String modelUUID) {
482
483         VnfResource vnfResource = this.getSingleResource(vnfResourceClient, getUri(vnfResourceURI + modelUUID));
484         if (vnfResource != null) {
485             vnfResource.setModelUUID(modelUUID);
486         }
487         return vnfResource;
488     }
489
490     // A VNFResource customization UUID is the same object across services, so we can return anyone from the list
491     // In the future the client should query starting at a service model uuid
492     public VnfResourceCustomization getVnfResourceCustomizationByModelCustomizationUUID(String modelCustomizationUUID) {
493         List<VnfResourceCustomization> vnfResourceCustomization = this.getMultipleResources(
494                 vnfResourceCustomizationClient, getUri(endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH
495                         + "/findByModelCustomizationUUID" + "?MODEL_CUSTOMIZATION_UUID=" + modelCustomizationUUID));
496         if (vnfResourceCustomization != null && !vnfResourceCustomization.isEmpty()) {
497             return vnfResourceCustomization.get(0);
498         } else {
499             return null;
500         }
501     }
502
503     public List<VnfResourceCustomization> getVnfResourceCustomizationByModelUuid(String modelUuid) {
504         return this.getMultipleResources(vnfResourceCustomizationClient,
505                 getUri(UriBuilder.fromUri(findVnfResourceCustomizationByModelUuid)
506                         .queryParam("SERVICE_MODEL_UUID", modelUuid).build().toString()));
507     }
508
509     public PnfResource getPnfResourceByModelUUID(String modelUUID) {
510         PnfResource PnfResource = this.getSingleResource(pnfResourceClient, getUri(pnfResourceURI + modelUUID));
511         if (PnfResource != null) {
512             PnfResource.setModelUUID(modelUUID);
513         }
514         return PnfResource;
515     }
516
517     public PnfResourceCustomization getPnfResourceCustomizationByModelCustomizationUUID(String modelCustomizationUUID) {
518         PnfResourceCustomization pnfResourceCustomization = getSingleResource(pnfResourceCustomizationClient,
519                 getUri(pnfResourceCustomizationURI + modelCustomizationUUID));
520         if (pnfResourceCustomization != null) {
521             pnfResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
522         }
523         return pnfResourceCustomization;
524     }
525
526     public List<PnfResourceCustomization> getPnfResourceCustomizationByModelUuid(String modelUuid) {
527         return this.getMultipleResources(pnfResourceCustomizationClient,
528                 getUri(UriBuilder.fromUri(findPnfResourceCustomizationByModelUuid)
529                         .queryParam("SERVICE_MODEL_UUID", modelUuid).build().toString()));
530     }
531
532     public CollectionNetworkResourceCustomization getCollectionNetworkResourceCustomizationByID(
533             String modelCustomizationUUID) {
534         CollectionNetworkResourceCustomization collectionNetworkResourceCustomization =
535                 this.getSingleResource(collectionNetworkResourceCustomizationClient,
536                         getUri(UriBuilder.fromUri(collectionNetworkResourceCustomizationURI + modelCustomizationUUID)
537                                 .build().toString()));
538         if (collectionNetworkResourceCustomization != null) {
539             collectionNetworkResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
540         }
541         return collectionNetworkResourceCustomization;
542     }
543
544     public InstanceGroup getInstanceGroupByModelUUID(String modelUUID) {
545         InstanceGroup instanceGroup = this.getSingleResource(instanceGroupClient, getUri(instanceGroupURI + modelUUID));
546         if (instanceGroup != null) {
547             instanceGroup.setModelUUID(modelUUID);
548         }
549         return instanceGroup;
550     }
551
552     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
553         return this.getSingleResource(vfModuleCustomizationClient,
554                 getUri(endpoint + VFMODULE_CUSTOMIZATION + SEARCH
555                         + "/findFirstByModelCustomizationUUIDOrderByCreatedDesc" + "?MODEL_CUSTOMIZATION_UUID="
556                         + modelCustomizationUUID));
557     }
558
559     public NetworkResourceCustomization getNetworkResourceCustomizationByModelCustomizationUUID(
560             String modelCustomizationUUID) {
561         NetworkResourceCustomization networkResourceCustomization = this.getSingleResource(
562                 networkResourceCustomizationClient, getUri(networkResourceCustomizationURI + modelCustomizationUUID));
563         if (networkResourceCustomization != null) {
564             networkResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
565         }
566         return networkResourceCustomization;
567     }
568
569
570     public BuildingBlockDetail getBuildingBlockDetail(String buildingBlockName) {
571         BuildingBlockDetail buildingBlockDetail =
572                 getSingleResource(buildingBlockDetailClient, getUri(UriBuilder.fromUri(findOneByBuildingBlockName)
573                         .queryParam(BUILDING_BLOCK_NAME, buildingBlockName).build().toString()));
574         if (buildingBlockDetail != null) {
575             buildingBlockDetail.setBuildingBlockName(buildingBlockName);
576         }
577         return buildingBlockDetail;
578     }
579
580
581     public OrchestrationStatusStateTransitionDirective getOrchestrationStatusStateTransitionDirective(
582             ResourceType resourceType, OrchestrationStatus orchestrationStatus, OrchestrationAction targetAction) {
583         return getSingleResource(orchestrationStatusStateTransitionDirectiveClient,
584                 UriBuilder.fromUri(findOneByResourceTypeAndOrchestrationStatusAndTargetAction)
585                         .queryParam(RESOURCE_TYPE, resourceType.name())
586                         .queryParam(ORCHESTRATION_STATUS, orchestrationStatus.name())
587                         .queryParam(TARGET_ACTION, targetAction.name()).build());
588     }
589
590     public List<OrchestrationFlow> getOrchestrationFlowByAction(String action) {
591         return this.getMultipleResources(orchestrationClient,
592                 UriBuilder.fromUri(findByAction).queryParam(ACTION, action).build());
593     }
594
595     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroupsByVnfResourceCust(String modelCustomizationUUID) {
596         return this.getMultipleResources(vnfcInstanceGroupCustomizationClient,
597                 UriBuilder.fromUri(findVnfcInstanceGroupCustomizationByModelCustomizationUUID)
598                         .queryParam(MODEL_CUSTOMIZATION_UUID, modelCustomizationUUID).build());
599     }
600
601     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomizationByModelCustUUID(
602             String modelCustomizationUUID) {
603         return this.getMultipleResources(collectionResourceInstanceGroupCustomizationClient,
604                 UriBuilder.fromUri(findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID)
605                         .queryParam(MODEL_CUSTOMIZATION_UUID, modelCustomizationUUID).build());
606     }
607
608     public VfModuleCustomization getVfModuleCustomizationByModelCustomizationUUIDAndVfModuleModelUUID(
609             String modelCustomizationUUID, String vfModuleModelUUID) {
610         return this.getSingleResource(vfModuleCustomizationClient,
611                 getUri(UriBuilder.fromUri(findByModelCustomizationUUIDAndVfModuleModelUUID)
612                         .queryParam("MODEL_CUSTOMIZATION_UUID", modelCustomizationUUID)
613                         .queryParam("MODEL_UUID", vfModuleModelUUID).build().toString()));
614     }
615
616     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScope(String requestAction,
617             String resourceName, boolean aLaCarte) {
618         return this.getSingleResource(northBoundRequestClient,
619                 UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarte).queryParam(ACTION, requestAction)
620                         .queryParam(REQUEST_SCOPE, resourceName).queryParam(IS_ALACARTE, aLaCarte).build());
621     }
622
623     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwner(String requestAction,
624             String resourceName, boolean aLaCarte, String cloudOwner) {
625         return this.getSingleResource(northBoundRequestClient,
626                 getUri(UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType)
627                         .queryParam(ACTION, requestAction).queryParam(REQUEST_SCOPE, resourceName)
628                         .queryParam(IS_ALACARTE, aLaCarte).queryParam(CLOUD_OWNER, cloudOwner)
629                         .queryParam(SERVICE_TYPE, ASTERISK).build().toString()));
630     }
631
632     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwnerAndServiceType(
633             String requestAction, String resourceName, boolean aLaCarte, String cloudOwner, String serviceType) {
634         return this.getSingleResource(northBoundRequestClient,
635                 getUri(UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType)
636                         .queryParam(ACTION, requestAction).queryParam(REQUEST_SCOPE, resourceName)
637                         .queryParam(IS_ALACARTE, aLaCarte).queryParam(CLOUD_OWNER, cloudOwner)
638                         .queryParam(SERVICE_TYPE, serviceType).build().toString()));
639     }
640
641     public RainyDayHandlerStatus getRainyDayHandlerStatus(String flowName, String serviceType, String vnfType,
642             String errorCode, String workStep, String errorMessage, String serviceRole) {
643         logger.debug(
644                 "Get Rainy Day Status - Flow Name {}, Service Type: {} , vnfType {} , errorCode {}, workStep {}, errorMessage {}",
645                 flowName, serviceType, vnfType, errorCode, workStep, errorMessage);
646         return this.getSingleResource(rainyDayHandlerStatusClient,
647                 UriComponentsBuilder.fromUriString(endpoint + RAINY_DAY_HANDLER_MACRO + SEARCH + findRainyDayHandler)
648                         .queryParam(FLOW_NAME, flowName).queryParam(SERVICE_TYPE, serviceType)
649                         .queryParam(VNF_TYPE, vnfType).queryParam(ERROR_CODE, errorCode).queryParam(WORK_STEP, workStep)
650                         .queryParam(ERROR_MESSAGE, errorMessage).queryParam(SERVICE_ROLE, serviceRole).build().encode()
651                         .toUri());
652     }
653
654     public ServiceRecipe getFirstByServiceModelUUIDAndAction(String modelUUID, String action) {
655         return this.getSingleResource(serviceRecipeClient,
656                 getUri(UriBuilder.fromUri(findFirstByServiceModelUUIDAndActionURI)
657                         .queryParam(SERVICE_MODEL_UUID, modelUUID).queryParam(ACTION, action).build().toString()));
658     }
659
660
661
662     public NetworkRecipe getFirstNetworkRecipeByModelNameAndAction(String modelName, String action) {
663         return this.getSingleResource(networkRecipeClient, UriBuilder.fromUri(findFirstByModelNameAndAction)
664                 .queryParam(MODEL_NAME, modelName).queryParam(ACTION, action).build());
665     }
666
667     public ControllerSelectionReference getControllerSelectionReferenceByVnfTypeAndActionCategory(String vnfType,
668             String actionCategory) {
669         return this.getSingleResource(controllerSelectionReferenceClient, UriBuilder.fromUri(endpoint
670                 + "/controllerSelectionReference/search/findControllerSelectionReferenceByVnfTypeAndActionCategory")
671                 .queryParam("VNF_TYPE", vnfType).queryParam("ACTION_CATEGORY", actionCategory).build());
672     }
673
674     public Service getFirstByModelNameOrderByModelVersionDesc(String modelName) {
675         return this.getSingleResource(serviceClient,
676                 UriBuilder.fromUri(findFirstByModelNameURI).queryParam(MODEL_NAME, modelName).build());
677     }
678
679     public ExternalServiceToInternalService findExternalToInternalServiceByServiceName(String serviceName) {
680         return this.getSingleResource(externalServiceToInternalServiceClient,
681                 getUri(UriBuilder.fromUri(findExternalToInternalServiceByServiceName)
682                         .queryParam(SERVICE_NAME, serviceName).build().toString()));
683     }
684
685     public ServiceRecipe findServiceRecipeByActionAndServiceModelUUID(String action, String modelUUID) {
686         return this.getSingleResource(serviceRecipeClient,
687                 getUri(UriBuilder.fromUri(findServiceRecipeByActionAndServiceModelUUID).queryParam(ACTION, action)
688                         .queryParam(SERVICE_MODEL_UUID, modelUUID).build().toString()));
689     }
690
691     public Service getServiceByModelName(String modelName) {
692         return this.getSingleResource(serviceClient, getUri(
693                 UriBuilder.fromUri(findServiceByModelName).queryParam(MODEL_NAME, modelName).build().toString()));
694     }
695
696     public Service getServiceByModelUUID(String modelModelUUID) {
697         return this.getSingleResource(serviceClient, getUri(
698                 UriBuilder.fromUri(findServiceByModelUUID).queryParam(MODEL_UUID, modelModelUUID).build().toString()));
699     }
700
701     public VnfResource getFirstVnfResourceByModelInvariantUUIDAndModelVersion(String modelInvariantUUID,
702             String modelVersion) {
703         return this.getSingleResource(vnfResourceClient,
704                 getUri(UriBuilder.fromUri(findFirstResourceByModelInvariantUUIDAndModelVersion)
705                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).queryParam(MODEL_VERSION, modelVersion)
706                         .build().toString()));
707     }
708
709
710     public VnfResourceCustomization getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
711             String modelInstanceName, VnfResource vnfResource) {
712         return this.getSingleResource(vnfResourceCustomizationClient,
713                 getUri(UriBuilder.fromUri(findByModelInstanceNameAndVnfResources)
714                         .queryParam("MODEL_INSTANCE_NAME", modelInstanceName)
715                         .queryParam("VNF_RESOURCE_MODEL_UUID", vnfResource.getModelUUID()).build().toString()));
716     }
717
718     public VnfRecipe getFirstVnfRecipeByNfRoleAndAction(String nfRole, String action) {
719         return this.getSingleResource(vnfRecipeClient, getUri(UriBuilder.fromUri(findFirstVnfRecipeByNfRoleAndAction)
720                 .queryParam(NF_ROLE, nfRole).queryParam(ACTION, action).build().toString()));
721     }
722
723     public VnfComponentsRecipe getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
724             String vfModuleModelUUID, String modelType, String action) {
725         return this.getSingleResource(vnfComponentsRecipeClient,
726                 getUri(UriBuilder.fromUri(findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction)
727                         .queryParam(VF_MODULE_MODEL_UUID, vfModuleModelUUID).queryParam(VNF_COMPONENT_TYPE, modelType)
728                         .queryParam(ACTION, action).build().toString()));
729     }
730
731     public VnfComponentsRecipe getFirstVnfComponentsRecipeByVnfComponentTypeAndAction(String modelType, String action) {
732         return this.getSingleResource(vnfComponentsRecipeClient,
733                 getUri(UriBuilder.fromUri(findFirstVnfComponentsRecipeByVnfComponentTypeAndAction)
734                         .queryParam(VNF_COMPONENT_TYPE, modelType).queryParam(ACTION, action).build().toString()));
735     }
736
737
738     protected URI getUri(String template) {
739         return URI.create(template);
740     }
741
742     public CloudifyManager getCloudifyManager(String id) {
743         return this.getSingleResource(cloudifyManagerClient, getUri(cloudifyManagerURI + id));
744     }
745
746     public CloudSite getCloudSite(String id) {
747         return this.getSingleResource(cloudSiteClient, getUri(cloudSiteURI + id));
748     }
749
750     public CloudSite getCloudSite(String id, String uri) {
751         return this.getSingleResource(cloudSiteClient, getUri(uri + id));
752     }
753
754     public void postCloudSite(CloudSite cloudSite) {
755         this.postSingleResource(cloudSiteClient, cloudSite);
756     }
757
758     public CloudSite getCloudSiteByClliAndAicVersion(String clli, String cloudVersion) {
759         return this.getSingleResource(cloudSiteClient, getUri(UriBuilder.fromUri(findByClliAndCloudVersion)
760                 .queryParam(CLLI, clli).queryParam(CLOUD_VERSION, cloudVersion).build().toString()));
761     }
762
763     public HomingInstance getHomingInstance(String serviceInstanceId) {
764         return this.getSingleResource(homingInstanceClient, getUri(homingInstanceURI + serviceInstanceId));
765     }
766
767     public HomingInstance getHomingInstance(String serviceInstanceId, String uri) {
768         return this.getSingleResource(homingInstanceClient, getUri(uri + serviceInstanceId));
769     }
770
771     public void postHomingInstance(HomingInstance homingInstance) {
772         this.postSingleResource(homingInstanceClient, homingInstance);
773     }
774
775     public Service getServiceByModelVersionAndModelInvariantUUID(String modelVersion, String modelInvariantUUID) {
776         return this.getSingleResource(serviceClient,
777                 getUri(UriBuilder.fromUri(findFirstByModelVersionAndModelInvariantUUIDURI)
778                         .queryParam(MODEL_VERSION, modelVersion).queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID)
779                         .build().toString()));
780     }
781
782     public VfModule getVfModuleByModelInvariantUUIDAndModelVersion(String modelInvariantUUID, String modelVersion) {
783         return this.getSingleResource(vfModuleClient,
784                 getUri(UriBuilder.fromUri(findFirstVfModuleByModelInvariantUUIDAndModelVersion)
785                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).queryParam(MODEL_VERSION, modelVersion)
786                         .build().toString()));
787     }
788
789     public List<Service> getServiceByModelInvariantUUIDOrderByModelVersionDesc(String modelInvariantUUID) {
790         return this.getMultipleResources(serviceClient, getUri(UriBuilder.fromUri(findByModelInvariantUUIDURI)
791                 .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).build().toString()));
792     }
793
794     public List<VfModule> getVfModuleByModelInvariantUUIDOrderByModelVersionDesc(String modelInvariantUUID) {
795         return this.getMultipleResources(vfModuleClient,
796                 getUri(UriBuilder.fromUri(findVfModuleByModelInvariantUUIDOrderByModelVersionDesc)
797                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).build().toString()));
798     }
799
800     private <T> T getSingleResource(Client<T> client, URI uri) {
801         return client.get(uri);
802     }
803
804     private <T> List<T> getMultipleResources(Client<T> client, URI uri) {
805         Iterable<T> iterator = client.getAll(uri);
806         List<T> list = new ArrayList<>();
807         Iterator<T> it = iterator.iterator();
808         it.forEachRemaining(list::add);
809         return list;
810     }
811
812     private <T> URI postSingleResource(Client<T> client, T type) {
813         return client.post(type);
814     }
815
816     public List<CvnfcCustomization> getCvnfcCustomization(String serviceModelUUID, String vnfCustomizationUUID,
817             String vfModuleCustomizationUUID) {
818         Service service = this.getServiceByID(serviceModelUUID);
819         VnfResourceCustomization vnfResourceCust =
820                 findVnfResourceCustomizationInList(vnfCustomizationUUID, service.getVnfCustomizations());
821         VfModuleCustomization vfModuleCust =
822                 findVfModuleCustomizationInList(vfModuleCustomizationUUID, vnfResourceCust.getVfModuleCustomizations());
823         return vfModuleCust.getCvnfcCustomization().stream().collect(Collectors.toList());
824     }
825
826     public VnfResourceCustomization findVnfResourceCustomizationInList(String vnfCustomizationUUID,
827             List<VnfResourceCustomization> vnfResourceCusts) {
828         if (vnfCustomizationUUID == null) {
829             throw new EntityNotFoundException(
830                     "a NULL UUID was provided in query to search for VnfResourceCustomization");
831         }
832         List<VnfResourceCustomization> filtered =
833                 vnfResourceCusts.stream().filter(v -> v.getModelCustomizationUUID() != null)
834                         .filter(vnfCustRes -> vnfCustomizationUUID.equals(vnfCustRes.getModelCustomizationUUID()))
835                         .collect(Collectors.toList());
836         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
837             return filtered.get(0);
838         } else
839             throw new EntityNotFoundException(
840                     "Unable to find VnfResourceCustomization ModelCustomizationUUID:" + vnfCustomizationUUID);
841     }
842
843     protected VfModuleCustomization findVfModuleCustomizationInList(String vfModuleCustomizationUUID,
844             List<VfModuleCustomization> vfModuleList) {
845         if (vfModuleCustomizationUUID == null) {
846             throw new EntityNotFoundException("a NULL UUID was provided in query to search for VfModuleCustomization");
847         }
848         List<VfModuleCustomization> filtered = vfModuleList.stream().filter(v -> v.getModelCustomizationUUID() != null)
849                 .filter(vfModuleCust -> vfModuleCustomizationUUID.equals(vfModuleCust.getModelCustomizationUUID()))
850                 .collect(Collectors.toList());
851         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
852             return filtered.get(0);
853         } else
854             throw new EntityNotFoundException(
855                     "Unable to find VfModuleCustomization ModelCustomizationUUID:" + vfModuleCustomizationUUID);
856     }
857
858     protected CvnfcCustomization findCvnfcCustomizationInAList(String cvnfcCustomizationUuid,
859             List<CvnfcCustomization> cvnfcCustomList) {
860         if (cvnfcCustomizationUuid == null) {
861             throw new EntityNotFoundException("a NULL UUID was provided in query to search for CvnfcCustomization");
862         }
863         List<CvnfcCustomization> filtered = cvnfcCustomList.stream().filter(c -> c.getModelCustomizationUUID() != null)
864                 .filter(cvnfc -> cvnfcCustomizationUuid.equals(cvnfc.getModelCustomizationUUID()))
865                 .collect(Collectors.toList());
866         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
867             logger.debug("Found CvnfcCustomization: {}", filtered.get(0));
868             return filtered.get(0);
869         } else
870             throw new EntityNotFoundException(
871                     "Unable to find CvnfcCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
872     }
873
874     public CvnfcConfigurationCustomization getCvnfcCustomization(String serviceModelUUID, String vnfCustomizationUuid,
875             String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
876         List<CvnfcCustomization> cvnfcCustomization =
877                 getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid);
878         CvnfcCustomization cvnfc = findCvnfcCustomizationInAList(cvnfcCustomizationUuid, cvnfcCustomization);
879         List<CvnfcConfigurationCustomization> fabricConfigs = cvnfc
880                 .getCvnfcConfigurationCustomization().stream().filter(cvnfcCustom -> cvnfcCustom
881                         .getConfigurationResource().getToscaNodeType().contains("FabricConfiguration"))
882                 .collect(Collectors.toList());
883         if (fabricConfigs != null && !fabricConfigs.isEmpty() && fabricConfigs.size() == 1) {
884             logger.debug("Found Fabric Configuration: {}", fabricConfigs.get(0));
885             return fabricConfigs.get(0);
886         } else
887             throw new EntityNotFoundException(
888                     "Unable to find CvnfcConfigurationCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
889     }
890
891     public Workflow findWorkflowByArtifactUUID(String artifactUUID) {
892         return this.getSingleResource(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByArtifactUUID)
893                 .queryParam(ARTIFACT_UUID, artifactUUID).build().toString()));
894     }
895
896     public List<Workflow> findWorkflowByModelUUID(String vnfResourceModelUUID) {
897         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByModelUUID)
898                 .queryParam(VNF_RESOURCE_MODEL_UUID, vnfResourceModelUUID).build().toString()));
899     }
900
901     public List<Workflow> findWorkflowBySource(String source) {
902         return this.getMultipleResources(workflowClient,
903                 getUri(UriBuilder.fromUri(findWorkflowBySource).queryParam(SOURCE, source).build().toString()));
904     }
905
906     public String getEndpoint() {
907         return endpoint;
908     }
909
910     public void setEndpoint(String endpoint) {
911         this.endpoint = endpoint;
912     }
913 }