91cfb00911dc92b0b06aa683cbc80ecceee2fde2
[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 com.google.common.base.Strings;
24 import java.net.URI;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import javax.annotation.PostConstruct;
30 import javax.persistence.EntityNotFoundException;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.UriBuilder;
33 import org.apache.http.HttpStatus;
34 import org.onap.logging.filter.base.Constants;
35 import org.onap.logging.filter.spring.SpringClientPayloadFilter;
36 import org.onap.so.db.catalog.beans.BBNameSelectionReference;
37 import org.onap.so.db.catalog.beans.BuildingBlockDetail;
38 import org.onap.so.db.catalog.beans.CloudSite;
39 import org.onap.so.db.catalog.beans.CloudifyManager;
40 import org.onap.so.db.catalog.beans.CollectionNetworkResourceCustomization;
41 import org.onap.so.db.catalog.beans.CollectionResourceInstanceGroupCustomization;
42 import org.onap.so.db.catalog.beans.ControllerSelectionReference;
43 import org.onap.so.db.catalog.beans.CvnfcConfigurationCustomization;
44 import org.onap.so.db.catalog.beans.CvnfcCustomization;
45 import org.onap.so.db.catalog.beans.ExternalServiceToInternalService;
46 import org.onap.so.db.catalog.beans.HomingInstance;
47 import org.onap.so.db.catalog.beans.InstanceGroup;
48 import org.onap.so.db.catalog.beans.NetworkCollectionResourceCustomization;
49 import org.onap.so.db.catalog.beans.NetworkRecipe;
50 import org.onap.so.db.catalog.beans.NetworkResource;
51 import org.onap.so.db.catalog.beans.NetworkResourceCustomization;
52 import org.onap.so.db.catalog.beans.OrchestrationAction;
53 import org.onap.so.db.catalog.beans.OrchestrationStatus;
54 import org.onap.so.db.catalog.beans.OrchestrationStatusStateTransitionDirective;
55 import org.onap.so.db.catalog.beans.PnfResource;
56 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
57 import org.onap.so.db.catalog.beans.ProcessingFlags;
58 import org.onap.so.db.catalog.beans.ResourceType;
59 import org.onap.so.db.catalog.beans.Service;
60 import org.onap.so.db.catalog.beans.ServiceRecipe;
61 import org.onap.so.db.catalog.beans.VfModule;
62 import org.onap.so.db.catalog.beans.VfModuleCustomization;
63 import org.onap.so.db.catalog.beans.VnfComponentsRecipe;
64 import org.onap.so.db.catalog.beans.VnfRecipe;
65 import org.onap.so.db.catalog.beans.VnfResource;
66 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
67 import org.onap.so.db.catalog.beans.VnfcInstanceGroupCustomization;
68 import org.onap.so.db.catalog.beans.Workflow;
69 import org.onap.so.db.catalog.beans.macro.NorthBoundRequest;
70 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
71 import org.onap.so.db.catalog.beans.macro.RainyDayHandlerStatus;
72 import org.onap.so.logging.jaxrs.filter.SOSpringClientFilter;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75 import org.springframework.beans.factory.annotation.Autowired;
76 import org.springframework.beans.factory.annotation.Value;
77 import org.springframework.core.ParameterizedTypeReference;
78 import org.springframework.http.HttpEntity;
79 import org.springframework.http.HttpHeaders;
80 import org.springframework.http.HttpMethod;
81 import org.springframework.http.client.BufferingClientHttpRequestFactory;
82 import org.springframework.http.client.ClientHttpRequestFactory;
83 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
84 import org.springframework.stereotype.Component;
85 import org.springframework.web.client.HttpClientErrorException;
86 import org.springframework.web.client.RestTemplate;
87 import org.springframework.web.util.UriComponentsBuilder;
88 import uk.co.blackpepper.bowman.Client;
89 import uk.co.blackpepper.bowman.ClientFactory;
90 import uk.co.blackpepper.bowman.Configuration;
91
92 @Component("CatalogDbClient")
93 public class CatalogDbClient {
94
95     private static final Logger logger = LoggerFactory.getLogger(CatalogDbClient.class);
96
97     private static final String CLOUD_SITE = "/cloudSite";
98     private static final String CLOUDIFY_MANAGER = "/cloudifyManager";
99     private static final String RAINY_DAY_HANDLER_MACRO = "/rainy_day_handler_macro";
100     private static final String NORTHBOUND_REQUEST_REF_LOOKUP = "/northbound_request_ref_lookup";
101     private static final String NETWORK_RESOURCE_CUSTOMIZATION = "/networkResourceCustomization";
102     private static final String NETWORK_RESOURCE = "/networkResource";
103     private static final String COLLECTION_RESOURCE_INSTANCE_GROUP_CUSTOMIZATION =
104             "/collectionResourceInstanceGroupCustomization";
105     private static final String VNFC_INSTANCE_GROUP_CUSTOMIZATION = "/vnfcInstanceGroupCustomization";
106     private static final String ORCHESTRATION_FLOW = "/orchestrationFlow";
107     private static final String ORCHESTRATION_STATUS_STATE_TRANSITION_DIRECTIVE =
108             "/orchestrationStatusStateTransitionDirective";
109     private static final String INSTANCE_GROUP = "/instanceGroup";
110     private static final String COLLECTION_NETWORK_RESOURCE_CUSTOMIZATION = "/collectionNetworkResourceCustomization";
111     private static final String BUILDING_BLOCK_DETAIL = "/buildingBlockDetail";
112     private static final String NETWORK_COLLECTION_RESOURCE_CUSTOMIZATION = "/networkCollectionResourceCustomization";
113     private static final String VNF_RESOURCE_CUSTOMIZATION = "/vnfResourceCustomization";
114     private static final String SERVICE = "/service";
115     private static final String EXTERNAL_SERVICE_TO_INTERNAL_MODEL_MAPPING = "/externalServiceToInternalService";
116     private static final String VNF_RESOURCE = "/vnfResource";
117     private static final String VNF_RECIPE = "/vnfRecipe";
118     private static final String VFMODULE = "/vfModule";
119     private static final String VFMODULE_CUSTOMIZATION = "/vfModuleCustomization";
120     private static final String VNF_COMPONENTS_RECIPE = "/vnfComponentsRecipe";
121     private static final String SERVICE_RECIPE = "/serviceRecipe";
122     private static final String NETWORK_RECIPE = "/networkRecipe";
123     private static final String PNF_RESOURCE = "/pnfResource";
124     private static final String PNF_RESOURCE_CUSTOMIZATION = "/pnfResourceCustomization";
125     private static final String WORKFLOW = "/workflow";
126     private static final String BB_NAME_SELECTION_REFERENCE = "/bbNameSelectionReference";
127     private static final String PROCESSING_FLAGS = "/processingFlags";
128
129
130     private static final String SEARCH = "/search";
131     private static final String URI_SEPARATOR = "/";
132
133     protected static final String SERVICE_MODEL_UUID = "serviceModelUUID";
134     protected static final String SERVICE_NAME = "serviceName";
135     protected static final String MODEL_UUID = "modelUUID";
136     protected static final String MODEL_CUSTOMIZATION_UUID = "modelCustomizationUUID";
137     protected static final String ACTION = "action";
138     protected static final String MODEL_NAME = "modelName";
139     protected static final String MODEL_VERSION = "modelVersion";
140     protected static final String MODEL_INVARIANT_UUID = "modelInvariantUUID";
141     protected static final String VNF_RESOURCE_MODEL_UUID = "vnfResourceModelUUID";
142     protected static final String PNF_RESOURCE_MODEL_UUID = "pnfResourceModelUUID";
143     protected static final String NF_ROLE = "nfRole";
144     protected static final String VF_MODULE_MODEL_UUID = "vfModuleModelUUID";
145     protected static final String VNF_COMPONENT_TYPE = "vnfComponentType";
146     protected static final String BUILDING_BLOCK_NAME = "buildingBlockName";
147     protected static final String RESOURCE_TYPE = "resourceType";
148     protected static final String ORCHESTRATION_STATUS = "orchestrationStatus";
149     protected static final String TARGET_ACTION = "targetAction";
150     protected static final String REQUEST_SCOPE = "requestScope";
151     protected static final String IS_ALACARTE = "isALaCarte";
152     protected static final String CLOUD_OWNER = "cloudOwner";
153     protected static final String FLOW_NAME = "flowName";
154     protected static final String ERROR_MESSAGE = "errorMessage";
155     protected static final String SERVICE_ROLE = "serviceRole";
156     protected static final String SERVICE_TYPE = "serviceType";
157     protected static final String VNF_TYPE = "vnfType";
158     protected static final String ERROR_CODE = "errorCode";
159     protected static final String WORK_STEP = "workStep";
160     protected static final String CLLI = "clli";
161     protected static final String CLOUD_VERSION = "cloudVersion";
162     protected static final String HOMING_INSTANCE = "/homingInstance";
163     protected static final String ARTIFACT_UUID = "artifactUUID";
164     protected static final String SOURCE = "source";
165     protected static final String RESOURCE_TARGET = "resourceTarget";
166     protected static final String FLAG = "flag";
167     protected static final String OPERATION_NAME = "operationName";
168
169     private static final String TARGET_ENTITY = "SO:CatalogDB";
170     private static final String ASTERISK = "*";
171
172     private String findExternalToInternalServiceByServiceName = "/findByServiceName";
173     private String findServiceByModelName = "/findOneByModelName";
174     private String findServiceRecipeByActionAndServiceModelUUID = "/findByActionAndServiceModelUUID";
175     private String findServiceByModelUUID = "/findOneByModelUUID";
176     private String findFirstByModelNameURI = "/findFirstByModelNameOrderByModelVersionDesc";
177     private String findFirstByServiceModelUUIDAndActionURI = "/findFirstByServiceModelUUIDAndAction";
178     private String findFirstByModelVersionAndModelInvariantUUIDURI = "/findFirstByModelVersionAndModelInvariantUUID";
179     private String findByModelInvariantUUIDURI = "/findByModelInvariantUUIDOrderByModelVersionDesc";
180     private String findFirstByModelNameAndAction = "/findFirstByModelNameAndAction";
181     private String findFirstResourceByModelInvariantUUIDAndModelVersion =
182             "/findFirstResourceByModelInvariantUUIDAndModelVersion";
183     private String findByModelInstanceNameAndVnfResources = "/findByModelInstanceNameAndVnfResources";
184     private String findFirstVnfRecipeByNfRoleAndAction = "/findFirstVnfRecipeByNfRoleAndAction";
185     private String findByModelCustomizationUUIDAndVfModuleModelUUID =
186             "/findFirstByModelCustomizationUUIDAndVfModuleModelUUIDOrderByCreatedDesc";
187     private String findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction =
188             "/findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction";
189     private String findFirstVnfComponentsRecipeByVnfComponentTypeAndAction =
190             "/findFirstVnfComponentsRecipeByVnfComponentTypeAndAction";
191     private String findVfModuleByModelInvariantUUIDOrderByModelVersionDesc =
192             "/findByModelInvariantUUIDOrderByModelVersionDesc";
193     private String findFirstVfModuleByModelInvariantUUIDAndModelVersion =
194             "/findFirstVfModuleByModelInvariantUUIDAndModelVersion";
195     private String findOneByBuildingBlockName = "/findOneByBuildingBlockName";
196     private String findOneByResourceTypeAndOrchestrationStatusAndTargetAction =
197             "/findOneByResourceTypeAndOrchestrationStatusAndTargetAction";
198     private String findByAction = "/findByAction";
199     private String findVnfcInstanceGroupCustomizationByModelCustomizationUUID = "/findByModelCustomizationUUID";
200     private String findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID =
201             "/findByModelCustomizationUUID";
202     private String findOneByActionAndRequestScopeAndIsAlacarte = "/findOneByActionAndRequestScopeAndIsAlacarte";
203     private String findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner =
204             "/findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner";
205     private String findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType =
206             "/findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType";
207
208     private static final String findRainyDayHandler = "/findRainyDayHandler";
209     private String findByClliAndCloudVersion = "/findByClliAndCloudVersion";
210     private String findServiceByServiceInstanceId = "/findServiceByServiceInstanceId";
211     private String findPnfResourceCustomizationByModelUuid = "/findPnfResourceCustomizationByModelUuid";
212     private String findWorkflowByArtifactUUID = "/findByArtifactUUID";
213     private String findWorkflowByVnfModelUUID = "/findWorkflowByVnfModelUUID";
214     private String findWorkflowByPnfModelUUID = "/findWorkflowByPnfModelUUID";
215     private String findWorkflowBySource = "/findBySource";
216     private String findVnfResourceCustomizationByModelUuid = "/findVnfResourceCustomizationByModelUuid";
217     private String findBBNameSelectionReferenceByControllerActorAndScopeAndAction =
218             "/findBBNameSelectionReferenceByControllerActorAndScopeAndAction";
219     private String findWorkflowByResourceTarget = "/findByResourceTarget";
220     private String findProcessingFlagsByFlag = "/findByFlag";
221     private String findWorkflowByOperationName = "/findByOperationName";
222
223     private String serviceURI;
224     private String vfModuleURI;
225     private String vnfResourceURI;
226     private String networkCollectionResourceCustomizationURI;
227     private String networkResourceCustomizationURI;
228     private String networkResourceURI;
229     private String collectionNetworkResourceCustomizationURI;
230     private String instanceGroupURI;
231     private String cloudifyManagerURI;
232     private String cloudSiteURI;
233     private String homingInstanceURI;
234     private String cvnfcResourceCustomizationURI;
235     private String pnfResourceURI;
236     private String pnfResourceCustomizationURI;
237     private String workflowURI;
238
239     private final Client<Service> serviceClient;
240
241     private final Client<NetworkRecipe> networkRecipeClient;
242
243     private final Client<NetworkResourceCustomization> networkResourceCustomizationClient;
244
245     private final Client<VnfResource> vnfResourceClient;
246
247     private final Client<VnfResourceCustomization> vnfResourceCustomizationClient;
248
249     private final Client<VnfRecipe> vnfRecipeClient;
250
251     private final Client<VfModuleCustomization> vfModuleCustomizationClient;
252
253     private final Client<VfModule> vfModuleClient;
254
255     private final Client<VnfComponentsRecipe> vnfComponentsRecipeClient;
256
257     private final Client<OrchestrationFlow> orchestrationClient;
258
259     private final Client<NorthBoundRequest> northBoundRequestClient;
260
261     private final Client<RainyDayHandlerStatus> rainyDayHandlerStatusClient;
262
263     private final Client<BuildingBlockDetail> buildingBlockDetailClient;
264
265     private final Client<OrchestrationStatusStateTransitionDirective> orchestrationStatusStateTransitionDirectiveClient;
266
267     private final Client<VnfcInstanceGroupCustomization> vnfcInstanceGroupCustomizationClient;
268
269     private final Client<CollectionResourceInstanceGroupCustomization> collectionResourceInstanceGroupCustomizationClient;
270
271     private final Client<InstanceGroup> instanceGroupClient;
272
273     private final Client<NetworkCollectionResourceCustomization> networkCollectionResourceCustomizationClient;
274
275     private final Client<CollectionNetworkResourceCustomization> collectionNetworkResourceCustomizationClient;
276
277     private final Client<ServiceRecipe> serviceRecipeClient;
278
279     private final Client<ExternalServiceToInternalService> externalServiceToInternalServiceClient;
280
281     private final Client<CloudSite> cloudSiteClient;
282
283     private final Client<HomingInstance> homingInstanceClient;
284
285     private final Client<CloudifyManager> cloudifyManagerClient;
286
287     private final Client<ControllerSelectionReference> controllerSelectionReferenceClient;
288
289     private final Client<PnfResource> pnfResourceClient;
290
291     private final Client<PnfResourceCustomization> pnfResourceCustomizationClient;
292
293     private final Client<Workflow> workflowClient;
294
295     private final Client<BBNameSelectionReference> bbNameSelectionReferenceClient;
296
297     private final Client<ProcessingFlags> processingFlagsClient;
298
299     @Value("${mso.catalog.db.spring.endpoint:#{null}}")
300     private String endpoint;
301
302     @Value("${mso.db.auth:#{null}}")
303     private String msoAdaptersAuth;
304
305     @Autowired
306     RestTemplate restTemplate;
307
308     @PostConstruct
309     public void init() {
310         findExternalToInternalServiceByServiceName = endpoint + EXTERNAL_SERVICE_TO_INTERNAL_MODEL_MAPPING + SEARCH
311                 + findExternalToInternalServiceByServiceName;
312         findServiceByModelName = endpoint + SERVICE + SEARCH + findServiceByModelName;
313         findServiceRecipeByActionAndServiceModelUUID =
314                 endpoint + SERVICE_RECIPE + SEARCH + findServiceRecipeByActionAndServiceModelUUID;
315         findServiceByModelUUID = endpoint + SERVICE + SEARCH + findServiceByModelUUID;
316         findFirstByModelNameURI = endpoint + SERVICE + SEARCH + findFirstByModelNameURI;
317         findFirstByModelVersionAndModelInvariantUUIDURI =
318                 endpoint + SERVICE + SEARCH + findFirstByModelVersionAndModelInvariantUUIDURI;
319         findByModelInvariantUUIDURI = endpoint + SERVICE + SEARCH + findByModelInvariantUUIDURI;
320         findFirstByServiceModelUUIDAndActionURI =
321                 endpoint + SERVICE_RECIPE + SEARCH + findFirstByServiceModelUUIDAndActionURI;
322         findFirstByModelNameAndAction = endpoint + NETWORK_RECIPE + SEARCH + findFirstByModelNameAndAction;
323         findFirstResourceByModelInvariantUUIDAndModelVersion =
324                 endpoint + VNF_RESOURCE + SEARCH + findFirstResourceByModelInvariantUUIDAndModelVersion;
325         findByModelInstanceNameAndVnfResources =
326                 endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH + findByModelInstanceNameAndVnfResources;
327         findFirstVnfRecipeByNfRoleAndAction = endpoint + VNF_RECIPE + SEARCH + findFirstVnfRecipeByNfRoleAndAction;
328         findByModelCustomizationUUIDAndVfModuleModelUUID =
329                 endpoint + VFMODULE_CUSTOMIZATION + SEARCH + findByModelCustomizationUUIDAndVfModuleModelUUID;
330         findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction = endpoint + VNF_COMPONENTS_RECIPE
331                 + SEARCH + findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction;
332         findFirstVnfComponentsRecipeByVnfComponentTypeAndAction =
333                 endpoint + VNF_COMPONENTS_RECIPE + SEARCH + findFirstVnfComponentsRecipeByVnfComponentTypeAndAction;
334         findVfModuleByModelInvariantUUIDOrderByModelVersionDesc =
335                 endpoint + VFMODULE + SEARCH + findVfModuleByModelInvariantUUIDOrderByModelVersionDesc;
336         findFirstVfModuleByModelInvariantUUIDAndModelVersion =
337                 endpoint + VFMODULE + SEARCH + findFirstVfModuleByModelInvariantUUIDAndModelVersion;
338         findOneByBuildingBlockName = endpoint + BUILDING_BLOCK_DETAIL + SEARCH + findOneByBuildingBlockName;
339         findOneByResourceTypeAndOrchestrationStatusAndTargetAction =
340                 endpoint + ORCHESTRATION_STATUS_STATE_TRANSITION_DIRECTIVE + SEARCH
341                         + findOneByResourceTypeAndOrchestrationStatusAndTargetAction;
342         findByAction = endpoint + ORCHESTRATION_FLOW + SEARCH + findByAction;
343         findVnfcInstanceGroupCustomizationByModelCustomizationUUID = endpoint + VNFC_INSTANCE_GROUP_CUSTOMIZATION
344                 + SEARCH + findVnfcInstanceGroupCustomizationByModelCustomizationUUID;
345         findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID =
346                 endpoint + COLLECTION_RESOURCE_INSTANCE_GROUP_CUSTOMIZATION + SEARCH
347                         + findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID;
348         findOneByActionAndRequestScopeAndIsAlacarte =
349                 endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH + findOneByActionAndRequestScopeAndIsAlacarte;
350         findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner = endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH
351                 + findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwner;
352         findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType =
353                 endpoint + NORTHBOUND_REQUEST_REF_LOOKUP + SEARCH
354                         + findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType;
355         findByClliAndCloudVersion = endpoint + CLOUD_SITE + SEARCH + findByClliAndCloudVersion;
356
357         findPnfResourceCustomizationByModelUuid =
358                 endpoint + PNF_RESOURCE_CUSTOMIZATION + SEARCH + findPnfResourceCustomizationByModelUuid;
359
360         findWorkflowByArtifactUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByArtifactUUID;
361         findWorkflowByVnfModelUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByVnfModelUUID;
362         findWorkflowByPnfModelUUID = endpoint + WORKFLOW + SEARCH + findWorkflowByPnfModelUUID;
363         findWorkflowBySource = endpoint + WORKFLOW + SEARCH + findWorkflowBySource;
364         findWorkflowByResourceTarget = endpoint + WORKFLOW + SEARCH + findWorkflowByResourceTarget;
365
366         findVnfResourceCustomizationByModelUuid =
367                 endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH + findVnfResourceCustomizationByModelUuid;
368
369         findBBNameSelectionReferenceByControllerActorAndScopeAndAction = endpoint + BB_NAME_SELECTION_REFERENCE + SEARCH
370                 + findBBNameSelectionReferenceByControllerActorAndScopeAndAction;
371
372         findProcessingFlagsByFlag = endpoint + PROCESSING_FLAGS + SEARCH + findProcessingFlagsByFlag;
373         findWorkflowByOperationName = endpoint + WORKFLOW + SEARCH + findWorkflowByOperationName;
374
375         serviceURI = endpoint + SERVICE + URI_SEPARATOR;
376         vfModuleURI = endpoint + VFMODULE + URI_SEPARATOR;
377         vnfResourceURI = endpoint + VNF_RESOURCE + URI_SEPARATOR;
378         networkCollectionResourceCustomizationURI =
379                 endpoint + NETWORK_COLLECTION_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
380         networkResourceCustomizationURI = endpoint + NETWORK_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
381         networkResourceURI = endpoint + NETWORK_RESOURCE + SEARCH;
382         collectionNetworkResourceCustomizationURI =
383                 endpoint + COLLECTION_NETWORK_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
384         instanceGroupURI = endpoint + INSTANCE_GROUP + URI_SEPARATOR;
385         cloudifyManagerURI = endpoint + CLOUDIFY_MANAGER + URI_SEPARATOR;
386         cloudSiteURI = endpoint + CLOUD_SITE + URI_SEPARATOR;
387         homingInstanceURI = endpoint + HOMING_INSTANCE + URI_SEPARATOR;
388         pnfResourceURI = endpoint + PNF_RESOURCE + URI_SEPARATOR;
389         pnfResourceCustomizationURI = endpoint + PNF_RESOURCE_CUSTOMIZATION + URI_SEPARATOR;
390         workflowURI = endpoint + WORKFLOW + URI_SEPARATOR;
391
392     }
393
394     public CatalogDbClient() {
395         ClientHttpRequestFactory factory =
396                 new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory());
397
398         ClientFactory clientFactory =
399                 Configuration.builder().setClientHttpRequestFactory(factory).setRestTemplateConfigurer(restTemplate -> {
400                     restTemplate.getInterceptors().add((new SOSpringClientFilter()));
401                     restTemplate.getInterceptors().add((new SpringClientPayloadFilter()));
402
403                     restTemplate.getInterceptors().add((request, body, execution) -> {
404
405                         request.getHeaders().add(HttpHeaders.AUTHORIZATION, msoAdaptersAuth);
406                         request.getHeaders().add(Constants.HttpHeaders.TARGET_ENTITY_HEADER, TARGET_ENTITY);
407                         return execution.execute(request, body);
408                     });
409                 }).build().buildClientFactory();
410         serviceClient = clientFactory.create(Service.class);
411         networkRecipeClient = clientFactory.create(NetworkRecipe.class);
412         networkResourceCustomizationClient = clientFactory.create(NetworkResourceCustomization.class);
413         vnfResourceClient = clientFactory.create(VnfResource.class);
414         vnfResourceCustomizationClient = clientFactory.create(VnfResourceCustomization.class);
415         vnfRecipeClient = clientFactory.create(VnfRecipe.class);
416         orchestrationClient = clientFactory.create(OrchestrationFlow.class);
417         vfModuleCustomizationClient = clientFactory.create(VfModuleCustomization.class);
418         vfModuleClient = clientFactory.create(VfModule.class);
419         vnfComponentsRecipeClient = clientFactory.create(VnfComponentsRecipe.class);
420         northBoundRequestClient = clientFactory.create(NorthBoundRequest.class);
421         rainyDayHandlerStatusClient = clientFactory.create(RainyDayHandlerStatus.class);
422         buildingBlockDetailClient = clientFactory.create(BuildingBlockDetail.class);
423         orchestrationStatusStateTransitionDirectiveClient =
424                 clientFactory.create(OrchestrationStatusStateTransitionDirective.class);
425         vnfcInstanceGroupCustomizationClient = clientFactory.create(VnfcInstanceGroupCustomization.class);
426         collectionResourceInstanceGroupCustomizationClient =
427                 clientFactory.create(CollectionResourceInstanceGroupCustomization.class);
428         instanceGroupClient = clientFactory.create(InstanceGroup.class);
429         networkCollectionResourceCustomizationClient =
430                 clientFactory.create(NetworkCollectionResourceCustomization.class);
431         collectionNetworkResourceCustomizationClient =
432                 clientFactory.create(CollectionNetworkResourceCustomization.class);
433         cloudSiteClient = clientFactory.create(CloudSite.class);
434         homingInstanceClient = clientFactory.create(HomingInstance.class);
435         cloudifyManagerClient = clientFactory.create(CloudifyManager.class);
436         serviceRecipeClient = clientFactory.create(ServiceRecipe.class);
437         controllerSelectionReferenceClient = clientFactory.create(ControllerSelectionReference.class);
438         externalServiceToInternalServiceClient = clientFactory.create(ExternalServiceToInternalService.class);
439         pnfResourceClient = clientFactory.create(PnfResource.class);
440         pnfResourceCustomizationClient = clientFactory.create(PnfResourceCustomization.class);
441         workflowClient = clientFactory.create(Workflow.class);
442         bbNameSelectionReferenceClient = clientFactory.create(BBNameSelectionReference.class);
443         processingFlagsClient = clientFactory.create(ProcessingFlags.class);
444
445     }
446
447     public CatalogDbClient(String baseUri, String auth) {
448         ClientHttpRequestFactory factory =
449                 new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory());
450
451         ClientFactory clientFactory = Configuration.builder().setBaseUri(baseUri).setClientHttpRequestFactory(factory)
452                 .setRestTemplateConfigurer(restTemplate -> {
453                     restTemplate.getInterceptors().add((new SOSpringClientFilter()));
454                     restTemplate.getInterceptors().add((new SpringClientPayloadFilter()));
455
456                     restTemplate.getInterceptors().add((request, body, execution) -> {
457
458                         request.getHeaders().add(HttpHeaders.AUTHORIZATION, auth);
459                         request.getHeaders().add(Constants.HttpHeaders.TARGET_ENTITY_HEADER, TARGET_ENTITY);
460                         return execution.execute(request, body);
461                     });
462                 }).build().buildClientFactory();
463         serviceClient = clientFactory.create(Service.class);
464         networkRecipeClient = clientFactory.create(NetworkRecipe.class);
465         networkResourceCustomizationClient = clientFactory.create(NetworkResourceCustomization.class);
466         vnfResourceClient = clientFactory.create(VnfResource.class);
467         vnfResourceCustomizationClient = clientFactory.create(VnfResourceCustomization.class);
468         vnfRecipeClient = clientFactory.create(VnfRecipe.class);
469         orchestrationClient = clientFactory.create(OrchestrationFlow.class);
470         vfModuleCustomizationClient = clientFactory.create(VfModuleCustomization.class);
471         vfModuleClient = clientFactory.create(VfModule.class);
472         vnfComponentsRecipeClient = clientFactory.create(VnfComponentsRecipe.class);
473         northBoundRequestClient = clientFactory.create(NorthBoundRequest.class);
474         rainyDayHandlerStatusClient = clientFactory.create(RainyDayHandlerStatus.class);
475         buildingBlockDetailClient = clientFactory.create(BuildingBlockDetail.class);
476         orchestrationStatusStateTransitionDirectiveClient =
477                 clientFactory.create(OrchestrationStatusStateTransitionDirective.class);
478         vnfcInstanceGroupCustomizationClient = clientFactory.create(VnfcInstanceGroupCustomization.class);
479         collectionResourceInstanceGroupCustomizationClient =
480                 clientFactory.create(CollectionResourceInstanceGroupCustomization.class);
481         instanceGroupClient = clientFactory.create(InstanceGroup.class);
482         networkCollectionResourceCustomizationClient =
483                 clientFactory.create(NetworkCollectionResourceCustomization.class);
484         collectionNetworkResourceCustomizationClient =
485                 clientFactory.create(CollectionNetworkResourceCustomization.class);
486         cloudSiteClient = clientFactory.create(CloudSite.class);
487         homingInstanceClient = clientFactory.create(HomingInstance.class);
488         cloudifyManagerClient = clientFactory.create(CloudifyManager.class);
489         serviceRecipeClient = clientFactory.create(ServiceRecipe.class);
490         controllerSelectionReferenceClient = clientFactory.create(ControllerSelectionReference.class);
491         externalServiceToInternalServiceClient = clientFactory.create(ExternalServiceToInternalService.class);
492         pnfResourceClient = clientFactory.create(PnfResource.class);
493         pnfResourceCustomizationClient = clientFactory.create(PnfResourceCustomization.class);
494         workflowClient = clientFactory.create(Workflow.class);
495         bbNameSelectionReferenceClient = clientFactory.create(BBNameSelectionReference.class);
496         processingFlagsClient = clientFactory.create(ProcessingFlags.class);
497     }
498
499     public NetworkCollectionResourceCustomization getNetworkCollectionResourceCustomizationByID(
500             String modelCustomizationUUID) {
501         NetworkCollectionResourceCustomization networkCollectionResourceCustomization =
502                 this.getSingleResource(networkCollectionResourceCustomizationClient,
503                         getUri(networkCollectionResourceCustomizationURI + modelCustomizationUUID));
504         if (networkCollectionResourceCustomization != null) {
505             networkCollectionResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
506         }
507         return networkCollectionResourceCustomization;
508     }
509
510     public Service getServiceByID(String modelUUID) {
511         Service service = getSingleResource(serviceClient, getUri(serviceURI + modelUUID));
512         if (service != null) {
513             service.setModelUUID(modelUUID);
514         }
515         return service;
516     }
517
518     public VfModule getVfModuleByModelUUID(String modelUUID) {
519         VfModule vfModule = getSingleResource(vfModuleClient, getUri(vfModuleURI + modelUUID));
520         if (vfModule != null) {
521             vfModule.setModelUUID(modelUUID);
522         }
523         return vfModule;
524     }
525
526     public VnfResource getVnfResourceByModelUUID(String modelUUID) {
527
528         VnfResource vnfResource = this.getSingleResource(vnfResourceClient, getUri(vnfResourceURI + modelUUID));
529         if (vnfResource != null) {
530             vnfResource.setModelUUID(modelUUID);
531         }
532         return vnfResource;
533     }
534
535     // A VNFResource customization UUID is the same object across services, so we can return anyone from the list
536     // In the future the client should query starting at a service model uuid
537     public VnfResourceCustomization getVnfResourceCustomizationByModelCustomizationUUID(String modelCustomizationUUID) {
538         List<VnfResourceCustomization> vnfResourceCustomization = this.getMultipleResources(
539                 vnfResourceCustomizationClient, getUri(endpoint + VNF_RESOURCE_CUSTOMIZATION + SEARCH
540                         + "/findByModelCustomizationUUID" + "?MODEL_CUSTOMIZATION_UUID=" + modelCustomizationUUID));
541         if (vnfResourceCustomization != null && !vnfResourceCustomization.isEmpty()) {
542             return vnfResourceCustomization.get(0);
543         } else {
544             return null;
545         }
546     }
547
548     public List<VnfResourceCustomization> getVnfResourceCustomizationByModelUuid(String modelUuid) {
549         return this.getMultipleResources(vnfResourceCustomizationClient,
550                 getUri(UriBuilder.fromUri(findVnfResourceCustomizationByModelUuid)
551                         .queryParam("SERVICE_MODEL_UUID", modelUuid).build().toString()));
552     }
553
554     public PnfResource getPnfResourceByModelUUID(String modelUUID) {
555         PnfResource PnfResource = this.getSingleResource(pnfResourceClient, getUri(pnfResourceURI + modelUUID));
556         if (PnfResource != null) {
557             PnfResource.setModelUUID(modelUUID);
558         }
559         return PnfResource;
560     }
561
562     public PnfResourceCustomization getPnfResourceCustomizationByModelCustomizationUUID(String modelCustomizationUUID) {
563         PnfResourceCustomization pnfResourceCustomization = getSingleResource(pnfResourceCustomizationClient,
564                 getUri(pnfResourceCustomizationURI + modelCustomizationUUID));
565         if (pnfResourceCustomization != null) {
566             pnfResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
567         }
568         return pnfResourceCustomization;
569     }
570
571     public List<PnfResourceCustomization> getPnfResourceCustomizationByModelUuid(String modelUuid) {
572         return this.getMultipleResources(pnfResourceCustomizationClient,
573                 getUri(UriBuilder.fromUri(findPnfResourceCustomizationByModelUuid)
574                         .queryParam("SERVICE_MODEL_UUID", modelUuid).build().toString()));
575     }
576
577     public CollectionNetworkResourceCustomization getCollectionNetworkResourceCustomizationByID(
578             String modelCustomizationUUID) {
579         CollectionNetworkResourceCustomization collectionNetworkResourceCustomization =
580                 this.getSingleResource(collectionNetworkResourceCustomizationClient,
581                         getUri(UriBuilder.fromUri(collectionNetworkResourceCustomizationURI + modelCustomizationUUID)
582                                 .build().toString()));
583         if (collectionNetworkResourceCustomization != null) {
584             collectionNetworkResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
585         }
586         return collectionNetworkResourceCustomization;
587     }
588
589     public InstanceGroup getInstanceGroupByModelUUID(String modelUUID) {
590         InstanceGroup instanceGroup = this.getSingleResource(instanceGroupClient, getUri(instanceGroupURI + modelUUID));
591         if (instanceGroup != null) {
592             instanceGroup.setModelUUID(modelUUID);
593         }
594         return instanceGroup;
595     }
596
597     public VfModuleCustomization getVfModuleCustomizationByModelCuztomizationUUID(String modelCustomizationUUID) {
598         return this.getSingleResource(vfModuleCustomizationClient,
599                 getUri(endpoint + VFMODULE_CUSTOMIZATION + SEARCH
600                         + "/findFirstByModelCustomizationUUIDOrderByCreatedDesc" + "?MODEL_CUSTOMIZATION_UUID="
601                         + modelCustomizationUUID));
602     }
603
604     public NetworkResourceCustomization getNetworkResourceCustomizationByModelCustomizationUUID(
605             String modelCustomizationUUID) {
606         NetworkResourceCustomization networkResourceCustomization = this.getSingleResource(
607                 networkResourceCustomizationClient, getUri(networkResourceCustomizationURI + modelCustomizationUUID));
608         if (networkResourceCustomization != null) {
609             networkResourceCustomization.setModelCustomizationUUID(modelCustomizationUUID);
610         }
611         return networkResourceCustomization;
612     }
613
614     public NetworkResource getNetworkResourceByModelName(String networkType) {
615         if (Strings.isNullOrEmpty(networkType)) {
616             throw new EntityNotFoundException("networkType passed as Null or Empty String");
617         }
618         try {
619             HttpEntity<?> entity = getHttpEntity();
620             return restTemplate.exchange(
621                     UriBuilder.fromUri(networkResourceURI + "/findFirstByModelNameOrderByModelVersionDesc")
622                             .queryParam("modelName", networkType).build(),
623                     HttpMethod.GET, entity, NetworkResource.class).getBody();
624         } catch (HttpClientErrorException e) {
625             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
626                 throw new EntityNotFoundException("Unable to find NetworkResource By networkType " + networkType);
627             }
628             throw e;
629         }
630     }
631
632
633
634     public BuildingBlockDetail getBuildingBlockDetail(String buildingBlockName) {
635         BuildingBlockDetail buildingBlockDetail =
636                 getSingleResource(buildingBlockDetailClient, getUri(UriBuilder.fromUri(findOneByBuildingBlockName)
637                         .queryParam(BUILDING_BLOCK_NAME, buildingBlockName).build().toString()));
638         if (buildingBlockDetail != null) {
639             buildingBlockDetail.setBuildingBlockName(buildingBlockName);
640         }
641         return buildingBlockDetail;
642     }
643
644
645     public OrchestrationStatusStateTransitionDirective getOrchestrationStatusStateTransitionDirective(
646             ResourceType resourceType, OrchestrationStatus orchestrationStatus, OrchestrationAction targetAction) {
647         return getSingleResource(orchestrationStatusStateTransitionDirectiveClient,
648                 UriBuilder.fromUri(findOneByResourceTypeAndOrchestrationStatusAndTargetAction)
649                         .queryParam(RESOURCE_TYPE, resourceType.name())
650                         .queryParam(ORCHESTRATION_STATUS, orchestrationStatus.name())
651                         .queryParam(TARGET_ACTION, targetAction.name()).build());
652     }
653
654     public List<OrchestrationFlow> getOrchestrationFlowByAction(String action) {
655         return this.getMultipleResources(orchestrationClient,
656                 UriBuilder.fromUri(findByAction).queryParam(ACTION, action).build());
657     }
658
659     public List<VnfcInstanceGroupCustomization> getVnfcInstanceGroupsByVnfResourceCust(String modelCustomizationUUID) {
660         return this.getMultipleResources(vnfcInstanceGroupCustomizationClient,
661                 UriBuilder.fromUri(findVnfcInstanceGroupCustomizationByModelCustomizationUUID)
662                         .queryParam(MODEL_CUSTOMIZATION_UUID, modelCustomizationUUID).build());
663     }
664
665     public List<CollectionResourceInstanceGroupCustomization> getCollectionResourceInstanceGroupCustomizationByModelCustUUID(
666             String modelCustomizationUUID) {
667         return this.getMultipleResources(collectionResourceInstanceGroupCustomizationClient,
668                 UriBuilder.fromUri(findCollectionResourceInstanceGroupCustomizationByModelCustomizationUUID)
669                         .queryParam(MODEL_CUSTOMIZATION_UUID, modelCustomizationUUID).build());
670     }
671
672     public VfModuleCustomization getVfModuleCustomizationByModelCustomizationUUIDAndVfModuleModelUUID(
673             String modelCustomizationUUID, String vfModuleModelUUID) {
674         return this.getSingleResource(vfModuleCustomizationClient,
675                 getUri(UriBuilder.fromUri(findByModelCustomizationUUIDAndVfModuleModelUUID)
676                         .queryParam("MODEL_CUSTOMIZATION_UUID", modelCustomizationUUID)
677                         .queryParam("MODEL_UUID", vfModuleModelUUID).build().toString()));
678     }
679
680     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScope(String requestAction,
681             String resourceName, boolean aLaCarte) {
682         return this.getSingleResource(northBoundRequestClient,
683                 UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarte).queryParam(ACTION, requestAction)
684                         .queryParam(REQUEST_SCOPE, resourceName).queryParam(IS_ALACARTE, aLaCarte).build());
685     }
686
687     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwner(String requestAction,
688             String resourceName, boolean aLaCarte, String cloudOwner) {
689         return this.getSingleResource(northBoundRequestClient,
690                 getUri(UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType)
691                         .queryParam(ACTION, requestAction).queryParam(REQUEST_SCOPE, resourceName)
692                         .queryParam(IS_ALACARTE, aLaCarte).queryParam(CLOUD_OWNER, cloudOwner)
693                         .queryParam(SERVICE_TYPE, ASTERISK).build().toString()));
694     }
695
696     public NorthBoundRequest getNorthBoundRequestByActionAndIsALaCarteAndRequestScopeAndCloudOwnerAndServiceType(
697             String requestAction, String resourceName, boolean aLaCarte, String cloudOwner, String serviceType) {
698         return this.getSingleResource(northBoundRequestClient,
699                 getUri(UriBuilder.fromUri(findOneByActionAndRequestScopeAndIsAlacarteAndCloudOwnerAndServiceType)
700                         .queryParam(ACTION, requestAction).queryParam(REQUEST_SCOPE, resourceName)
701                         .queryParam(IS_ALACARTE, aLaCarte).queryParam(CLOUD_OWNER, cloudOwner)
702                         .queryParam(SERVICE_TYPE, serviceType).build().toString()));
703     }
704
705     public RainyDayHandlerStatus getRainyDayHandlerStatus(String flowName, String serviceType, String vnfType,
706             String errorCode, String workStep, String errorMessage, String serviceRole) {
707         logger.debug(
708                 "Get Rainy Day Status - Flow Name {}, Service Type: {} , vnfType {} , errorCode {}, workStep {}, errorMessage {}",
709                 flowName, serviceType, vnfType, errorCode, workStep, errorMessage);
710         return this.getSingleResource(rainyDayHandlerStatusClient,
711                 UriComponentsBuilder.fromUriString(endpoint + RAINY_DAY_HANDLER_MACRO + SEARCH + findRainyDayHandler)
712                         .queryParam(FLOW_NAME, flowName).queryParam(SERVICE_TYPE, serviceType)
713                         .queryParam(VNF_TYPE, vnfType).queryParam(ERROR_CODE, errorCode).queryParam(WORK_STEP, workStep)
714                         .queryParam(ERROR_MESSAGE, errorMessage).queryParam(SERVICE_ROLE, serviceRole).build().encode()
715                         .toUri());
716     }
717
718     public ServiceRecipe getFirstByServiceModelUUIDAndAction(String modelUUID, String action) {
719         return this.getSingleResource(serviceRecipeClient,
720                 getUri(UriBuilder.fromUri(findFirstByServiceModelUUIDAndActionURI)
721                         .queryParam(SERVICE_MODEL_UUID, modelUUID).queryParam(ACTION, action).build().toString()));
722     }
723
724
725
726     public NetworkRecipe getFirstNetworkRecipeByModelNameAndAction(String modelName, String action) {
727         return this.getSingleResource(networkRecipeClient, UriBuilder.fromUri(findFirstByModelNameAndAction)
728                 .queryParam(MODEL_NAME, modelName).queryParam(ACTION, action).build());
729     }
730
731     public ControllerSelectionReference getControllerSelectionReferenceByVnfTypeAndActionCategory(String vnfType,
732             String actionCategory) {
733         return this.getSingleResource(controllerSelectionReferenceClient, UriBuilder.fromUri(endpoint
734                 + "/controllerSelectionReference/search/findControllerSelectionReferenceByVnfTypeAndActionCategory")
735                 .queryParam("VNF_TYPE", vnfType).queryParam("ACTION_CATEGORY", actionCategory).build());
736     }
737
738     public Service getFirstByModelNameOrderByModelVersionDesc(String modelName) {
739         return this.getSingleResource(serviceClient,
740                 UriBuilder.fromUri(findFirstByModelNameURI).queryParam(MODEL_NAME, modelName).build());
741     }
742
743     public BBNameSelectionReference getBBNameSelectionReference(String controllerActor, String scope, String action) {
744
745         return this.getSingleResource(bbNameSelectionReferenceClient,
746                 getUri(UriBuilder.fromUri(findBBNameSelectionReferenceByControllerActorAndScopeAndAction)
747                         .queryParam("CONTROLLER_ACTOR", controllerActor).queryParam("SCOPE", scope)
748                         .queryParam("ACTION", action).build().toString()));
749     }
750
751     public ExternalServiceToInternalService findExternalToInternalServiceByServiceName(String serviceName) {
752         return this.getSingleResource(externalServiceToInternalServiceClient,
753                 getUri(UriBuilder.fromUri(findExternalToInternalServiceByServiceName)
754                         .queryParam(SERVICE_NAME, serviceName).build().toString()));
755     }
756
757     public ServiceRecipe findServiceRecipeByActionAndServiceModelUUID(String action, String modelUUID) {
758         return this.getSingleResource(serviceRecipeClient,
759                 getUri(UriBuilder.fromUri(findServiceRecipeByActionAndServiceModelUUID).queryParam(ACTION, action)
760                         .queryParam(SERVICE_MODEL_UUID, modelUUID).build().toString()));
761     }
762
763     public Service getServiceByModelName(String modelName) {
764         return this.getSingleResource(serviceClient, getUri(
765                 UriBuilder.fromUri(findServiceByModelName).queryParam(MODEL_NAME, modelName).build().toString()));
766     }
767
768     public Service getServiceByModelUUID(String modelModelUUID) {
769         return this.getSingleResource(serviceClient, getUri(
770                 UriBuilder.fromUri(findServiceByModelUUID).queryParam(MODEL_UUID, modelModelUUID).build().toString()));
771     }
772
773     public VnfResource getFirstVnfResourceByModelInvariantUUIDAndModelVersion(String modelInvariantUUID,
774             String modelVersion) {
775         return this.getSingleResource(vnfResourceClient,
776                 getUri(UriBuilder.fromUri(findFirstResourceByModelInvariantUUIDAndModelVersion)
777                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).queryParam(MODEL_VERSION, modelVersion)
778                         .build().toString()));
779     }
780
781
782     public VnfResourceCustomization getFirstVnfResourceCustomizationByModelInstanceNameAndVnfResources(
783             String modelInstanceName, VnfResource vnfResource) {
784         return this.getSingleResource(vnfResourceCustomizationClient,
785                 getUri(UriBuilder.fromUri(findByModelInstanceNameAndVnfResources)
786                         .queryParam("MODEL_INSTANCE_NAME", modelInstanceName)
787                         .queryParam("VNF_RESOURCE_MODEL_UUID", vnfResource.getModelUUID()).build().toString()));
788     }
789
790     public VnfRecipe getFirstVnfRecipeByNfRoleAndAction(String nfRole, String action) {
791         return this.getSingleResource(vnfRecipeClient, getUri(UriBuilder.fromUri(findFirstVnfRecipeByNfRoleAndAction)
792                 .queryParam(NF_ROLE, nfRole).queryParam(ACTION, action).build().toString()));
793     }
794
795     public VnfComponentsRecipe getFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction(
796             String vfModuleModelUUID, String modelType, String action) {
797         return this.getSingleResource(vnfComponentsRecipeClient,
798                 getUri(UriBuilder.fromUri(findFirstVnfComponentsRecipeByVfModuleModelUUIDAndVnfComponentTypeAndAction)
799                         .queryParam(VF_MODULE_MODEL_UUID, vfModuleModelUUID).queryParam(VNF_COMPONENT_TYPE, modelType)
800                         .queryParam(ACTION, action).build().toString()));
801     }
802
803     public VnfComponentsRecipe getFirstVnfComponentsRecipeByVnfComponentTypeAndAction(String modelType, String action) {
804         return this.getSingleResource(vnfComponentsRecipeClient,
805                 getUri(UriBuilder.fromUri(findFirstVnfComponentsRecipeByVnfComponentTypeAndAction)
806                         .queryParam(VNF_COMPONENT_TYPE, modelType).queryParam(ACTION, action).build().toString()));
807     }
808
809
810     protected URI getUri(String template) {
811         return URI.create(template);
812     }
813
814     public CloudifyManager getCloudifyManager(String id) {
815         return this.getSingleResource(cloudifyManagerClient, getUri(cloudifyManagerURI + id));
816     }
817
818     public CloudSite getCloudSite(String id) {
819         return this.getSingleResource(cloudSiteClient, getUri(cloudSiteURI + id));
820     }
821
822     public CloudSite getCloudSite(String id, String uri) {
823         return this.getSingleResource(cloudSiteClient, getUri(uri + id));
824     }
825
826     // Bring back old version of methind since the caller - OofInfraUtils.java - is not running in a spring context
827     public void postOofHomingCloudSite(CloudSite cloudSite) {
828         this.postSingleResource(cloudSiteClient, cloudSite);
829     }
830
831     public CloudSite postCloudSite(CloudSite cloudSite) {
832         if (cloudSite == null) {
833             throw new EntityNotFoundException("CloudSite passed as null");
834         }
835         try {
836             HttpHeaders headers = getHttpHeaders();
837             HttpEntity<CloudSite> entity = new HttpEntity<>(cloudSite, headers);
838             CloudSite updatedCloudSite = restTemplate
839                     .exchange(UriComponentsBuilder.fromUriString(endpoint + "/cloudSite").build().encode().toString(),
840                             HttpMethod.POST, entity, CloudSite.class)
841                     .getBody();
842             return updatedCloudSite;
843         } catch (HttpClientErrorException e) {
844             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
845                 throw new EntityNotFoundException("Unable to find CloudSite with Cloud Site Id: " + cloudSite.getId());
846             }
847             throw e;
848         }
849     }
850
851     public CloudSite updateCloudSite(CloudSite cloudSite) {
852         if (cloudSite == null) {
853             throw new EntityNotFoundException("CloudSite passed as null");
854         }
855         try {
856             HttpHeaders headers = getHttpHeaders();
857             HttpEntity<CloudSite> entity = new HttpEntity<>(cloudSite, headers);
858             CloudSite updatedCloudSite = restTemplate
859                     .exchange(UriComponentsBuilder.fromUriString(endpoint + "/cloudSite/" + cloudSite.getId()).build()
860                             .encode().toString(), HttpMethod.PUT, entity, CloudSite.class)
861                     .getBody();
862             return updatedCloudSite;
863         } catch (HttpClientErrorException e) {
864             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
865                 throw new EntityNotFoundException("Unable to find CloudSite with Cloud Site Id: " + cloudSite.getId());
866             }
867             throw e;
868         }
869     }
870
871     public void deleteCloudSite(String cloudSiteId) {
872         if (cloudSiteId == null) {
873             throw new EntityNotFoundException("CloudSiteId passed as null");
874         }
875         try {
876             HttpHeaders headers = getHttpHeaders();
877             HttpEntity<String> entity = new HttpEntity<>(null, headers);
878             restTemplate.exchange(UriComponentsBuilder.fromUriString(endpoint + "/cloudSite/" + cloudSiteId).build()
879                     .encode().toString(), HttpMethod.DELETE, entity, CloudSite.class).getBody();
880         } catch (HttpClientErrorException e) {
881             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
882                 throw new EntityNotFoundException("Unable to find CloudSite with Cloud Site Id: " + cloudSiteId);
883             }
884             throw e;
885         }
886     }
887
888     public List<CloudSite> getCloudSites() {
889         return this.getMultipleResources(cloudSiteClient,
890                 UriBuilder.fromUri(endpoint + CLOUD_SITE).queryParam("size", "1000").build());
891     }
892
893
894     public CloudSite getCloudSiteByClliAndAicVersion(String clli, String cloudVersion) {
895         return this.getSingleResource(cloudSiteClient, getUri(UriBuilder.fromUri(findByClliAndCloudVersion)
896                 .queryParam(CLLI, clli).queryParam(CLOUD_VERSION, cloudVersion).build().toString()));
897     }
898
899     public HomingInstance getHomingInstance(String serviceInstanceId) {
900         return this.getSingleResource(homingInstanceClient, getUri(homingInstanceURI + serviceInstanceId));
901     }
902
903     public HomingInstance getHomingInstance(String serviceInstanceId, String uri) {
904         return this.getSingleResource(homingInstanceClient, getUri(uri + serviceInstanceId));
905     }
906
907     public void postHomingInstance(HomingInstance homingInstance) {
908         this.postSingleResource(homingInstanceClient, homingInstance);
909     }
910
911     public Service getServiceByModelVersionAndModelInvariantUUID(String modelVersion, String modelInvariantUUID) {
912         return this.getSingleResource(serviceClient,
913                 getUri(UriBuilder.fromUri(findFirstByModelVersionAndModelInvariantUUIDURI)
914                         .queryParam(MODEL_VERSION, modelVersion).queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID)
915                         .build().toString()));
916     }
917
918     public VfModule getVfModuleByModelInvariantUUIDAndModelVersion(String modelInvariantUUID, String modelVersion) {
919         return this.getSingleResource(vfModuleClient,
920                 getUri(UriBuilder.fromUri(findFirstVfModuleByModelInvariantUUIDAndModelVersion)
921                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).queryParam(MODEL_VERSION, modelVersion)
922                         .build().toString()));
923     }
924
925     public List<Service> getServiceByModelInvariantUUIDOrderByModelVersionDesc(String modelInvariantUUID) {
926         return this.getMultipleResources(serviceClient, getUri(UriBuilder.fromUri(findByModelInvariantUUIDURI)
927                 .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).build().toString()));
928     }
929
930     public List<VfModule> getVfModuleByModelInvariantUUIDOrderByModelVersionDesc(String modelInvariantUUID) {
931         return this.getMultipleResources(vfModuleClient,
932                 getUri(UriBuilder.fromUri(findVfModuleByModelInvariantUUIDOrderByModelVersionDesc)
933                         .queryParam(MODEL_INVARIANT_UUID, modelInvariantUUID).build().toString()));
934     }
935
936     private <T> T getSingleResource(Client<T> client, URI uri) {
937         return client.get(uri);
938     }
939
940     protected <T> List<T> getMultipleResources(Client<T> client, URI uri) {
941         Iterable<T> iterator = client.getAll(uri);
942         List<T> list = new ArrayList<>();
943         Iterator<T> it = iterator.iterator();
944         it.forEachRemaining(list::add);
945         return list;
946     }
947
948     private <T> URI postSingleResource(Client<T> client, T type) {
949         return client.post(type);
950     }
951
952     public List<CvnfcCustomization> getCvnfcCustomization(String serviceModelUUID, String vnfCustomizationUUID,
953             String vfModuleCustomizationUUID) {
954         Service service = this.getServiceByID(serviceModelUUID);
955         VnfResourceCustomization vnfResourceCust =
956                 findVnfResourceCustomizationInList(vnfCustomizationUUID, service.getVnfCustomizations());
957         VfModuleCustomization vfModuleCust =
958                 findVfModuleCustomizationInList(vfModuleCustomizationUUID, vnfResourceCust.getVfModuleCustomizations());
959         return vfModuleCust.getCvnfcCustomization();
960     }
961
962     public VnfResourceCustomization findVnfResourceCustomizationInList(String vnfCustomizationUUID,
963             List<VnfResourceCustomization> vnfResourceCusts) {
964         if (vnfCustomizationUUID == null) {
965             throw new EntityNotFoundException(
966                     "a NULL UUID was provided in query to search for VnfResourceCustomization");
967         }
968         List<VnfResourceCustomization> filtered =
969                 vnfResourceCusts.stream().filter(v -> v.getModelCustomizationUUID() != null)
970                         .filter(vnfCustRes -> vnfCustomizationUUID.equals(vnfCustRes.getModelCustomizationUUID()))
971                         .collect(Collectors.toList());
972         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
973             return filtered.get(0);
974         } else
975             throw new EntityNotFoundException(
976                     "Unable to find VnfResourceCustomization ModelCustomizationUUID:" + vnfCustomizationUUID);
977     }
978
979     protected VfModuleCustomization findVfModuleCustomizationInList(String vfModuleCustomizationUUID,
980             List<VfModuleCustomization> vfModuleList) {
981         if (vfModuleCustomizationUUID == null) {
982             throw new EntityNotFoundException("a NULL UUID was provided in query to search for VfModuleCustomization");
983         }
984         List<VfModuleCustomization> filtered = vfModuleList.stream().filter(v -> v.getModelCustomizationUUID() != null)
985                 .filter(vfModuleCust -> vfModuleCustomizationUUID.equals(vfModuleCust.getModelCustomizationUUID()))
986                 .collect(Collectors.toList());
987         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
988             return filtered.get(0);
989         } else
990             throw new EntityNotFoundException(
991                     "Unable to find VfModuleCustomization ModelCustomizationUUID:" + vfModuleCustomizationUUID);
992     }
993
994     protected CvnfcCustomization findCvnfcCustomizationInAList(String cvnfcCustomizationUuid,
995             List<CvnfcCustomization> cvnfcCustomList) {
996         if (cvnfcCustomizationUuid == null) {
997             throw new EntityNotFoundException("a NULL UUID was provided in query to search for CvnfcCustomization");
998         }
999         List<CvnfcCustomization> filtered = cvnfcCustomList.stream().filter(c -> c.getModelCustomizationUUID() != null)
1000                 .filter(cvnfc -> cvnfcCustomizationUuid.equals(cvnfc.getModelCustomizationUUID()))
1001                 .collect(Collectors.toList());
1002         if (filtered != null && !filtered.isEmpty() && filtered.size() == 1) {
1003             logger.debug("Found CvnfcCustomization: {}", filtered.get(0));
1004             return filtered.get(0);
1005         } else
1006             throw new EntityNotFoundException(
1007                     "Unable to find CvnfcCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
1008     }
1009
1010     public CvnfcConfigurationCustomization getCvnfcCustomization(String serviceModelUUID, String vnfCustomizationUuid,
1011             String vfModuleCustomizationUuid, String cvnfcCustomizationUuid) {
1012         List<CvnfcCustomization> cvnfcCustomization =
1013                 getCvnfcCustomization(serviceModelUUID, vnfCustomizationUuid, vfModuleCustomizationUuid);
1014         CvnfcCustomization cvnfc = findCvnfcCustomizationInAList(cvnfcCustomizationUuid, cvnfcCustomization);
1015         List<CvnfcConfigurationCustomization> fabricConfigs = cvnfc.getCvnfcConfigurationCustomization();
1016         fabricConfigs.stream().filter(cvnfcCustom -> cvnfcCustom.getConfigurationResource().getToscaNodeType()
1017                 .contains("FabricConfiguration")).collect(Collectors.toList());
1018         if (fabricConfigs != null && !fabricConfigs.isEmpty() && fabricConfigs.size() == 1) {
1019             logger.debug("Found Fabric Configuration: {}", fabricConfigs.get(0));
1020             return fabricConfigs.get(0);
1021         } else
1022             throw new EntityNotFoundException(
1023                     "Unable to find CvnfcConfigurationCustomization ModelCustomizationUUID:" + cvnfcCustomizationUuid);
1024     }
1025
1026     public org.onap.so.rest.catalog.beans.Service getServiceModelInformation(String serviceModelUUID, String depth) {
1027         if (Strings.isNullOrEmpty(serviceModelUUID)) {
1028             throw new EntityNotFoundException("Service Model UUID passed as Null or Empty String");
1029         }
1030         try {
1031             HttpEntity<?> entity = getHttpEntity();
1032             return restTemplate.exchange(
1033                     UriComponentsBuilder.fromUriString(endpoint + "/ecomp/mso/catalog/v1/services/" + serviceModelUUID)
1034                             .queryParam("depth", depth).build().encode().toString(),
1035                     HttpMethod.GET, entity, org.onap.so.rest.catalog.beans.Service.class).getBody();
1036         } catch (HttpClientErrorException e) {
1037             logger.warn("Entity Not found in DLP", e);
1038             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
1039                 throw new EntityNotFoundException("Unable to find Service with ServiceModelUUID:" + serviceModelUUID);
1040             }
1041             throw e;
1042         }
1043     }
1044
1045     public List<org.onap.so.rest.catalog.beans.Service> getServices() {
1046         try {
1047             HttpEntity<?> entity = getHttpEntity();
1048             return restTemplate
1049                     .exchange(
1050                             UriComponentsBuilder.fromUriString(endpoint + "/ecomp/mso/catalog/v1/services").build()
1051                                     .encode().toString(),
1052                             HttpMethod.GET, entity,
1053                             new ParameterizedTypeReference<List<org.onap.so.rest.catalog.beans.Service>>() {})
1054                     .getBody();
1055         } catch (HttpClientErrorException e) {
1056             logger.error("Error Calling catalog database", e);
1057             throw e;
1058         }
1059     }
1060
1061     public org.onap.so.rest.catalog.beans.Vnf getVnfModelInformation(String serviceModelUUID,
1062             String vnfCustomizationUUID, String depth) {
1063         if (Strings.isNullOrEmpty(serviceModelUUID)) {
1064             throw new EntityNotFoundException("Service Model UUID passed as Null or Empty String");
1065         }
1066         if (Strings.isNullOrEmpty(vnfCustomizationUUID)) {
1067             throw new EntityNotFoundException("Vnf Customization UUID passed as Null or Empty String");
1068         }
1069         try {
1070             HttpEntity<?> entity = getHttpEntity();
1071             return restTemplate
1072                     .exchange(
1073                             UriComponentsBuilder
1074                                     .fromUriString(endpoint + "/ecomp/mso/catalog/v1/services/" + serviceModelUUID
1075                                             + "/vnfs/" + vnfCustomizationUUID)
1076                                     .queryParam("depth", depth).build().encode().toString(),
1077                             HttpMethod.GET, entity, org.onap.so.rest.catalog.beans.Vnf.class)
1078                     .getBody();
1079         } catch (HttpClientErrorException e) {
1080             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
1081                 throw new EntityNotFoundException(
1082                         "Unable to find Vnf with Vnf Customization UUID:" + vnfCustomizationUUID);
1083             }
1084             throw e;
1085         }
1086     }
1087
1088     public void updateVnf(String serviceModelUUID, org.onap.so.rest.catalog.beans.Vnf vnf) {
1089         if (vnf == null) {
1090             throw new EntityNotFoundException("Vnf passed as null");
1091         }
1092         try {
1093             HttpHeaders headers = getHttpHeaders();
1094             HttpEntity<org.onap.so.rest.catalog.beans.Vnf> entity = new HttpEntity<>(vnf, headers);
1095
1096             restTemplate.exchange(
1097                     UriComponentsBuilder.fromUriString(endpoint + "/ecomp/mso/catalog/v1/services/" + serviceModelUUID
1098                             + "/vnfs/" + vnf.getModelCustomizationId()).build().encode().toString(),
1099                     HttpMethod.PUT, entity, org.onap.so.rest.catalog.beans.Vnf.class).getBody();
1100         } catch (HttpClientErrorException e) {
1101             if (HttpStatus.SC_NOT_FOUND == e.getStatusCode().value()) {
1102                 throw new EntityNotFoundException(
1103                         "Unable to find Vnf with Vnf Customization UUID:" + vnf.getModelCustomizationId());
1104             }
1105             throw e;
1106         }
1107     }
1108
1109
1110     public Workflow findWorkflowByArtifactUUID(String artifactUUID) {
1111         return this.getSingleResource(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByArtifactUUID)
1112                 .queryParam(ARTIFACT_UUID, artifactUUID).build().toString()));
1113     }
1114
1115     public List<Workflow> findWorkflowByVnfModelUUID(String vnfResourceModelUUID) {
1116         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByVnfModelUUID)
1117                 .queryParam(VNF_RESOURCE_MODEL_UUID, vnfResourceModelUUID).build().toString()));
1118     }
1119
1120     public List<Workflow> findWorkflowByPnfModelUUID(String pnfResourceModelUUID) {
1121         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByPnfModelUUID)
1122                 .queryParam(PNF_RESOURCE_MODEL_UUID, pnfResourceModelUUID).build().toString()));
1123     }
1124
1125     public List<Workflow> findWorkflowBySource(String source) {
1126         return this.getMultipleResources(workflowClient,
1127                 getUri(UriBuilder.fromUri(findWorkflowBySource).queryParam(SOURCE, source).build().toString()));
1128     }
1129
1130     public List<Workflow> findWorkflowByResourceTarget(String resourceTarget) {
1131         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByResourceTarget)
1132                 .queryParam(RESOURCE_TARGET, resourceTarget).build().toString()));
1133     }
1134
1135     public List<Workflow> findWorkflowByOperationName(String operationName) {
1136         return this.getMultipleResources(workflowClient, getUri(UriBuilder.fromUri(findWorkflowByOperationName)
1137                 .queryParam(OPERATION_NAME, operationName).build().toString()));
1138     }
1139
1140     public ProcessingFlags findProcessingFlagsByFlag(String flag) {
1141         return this.getSingleResource(processingFlagsClient,
1142                 getUri(UriBuilder.fromUri(findProcessingFlagsByFlag).queryParam(FLAG, flag).build().toString()));
1143     }
1144
1145     public String getEndpoint() {
1146         return endpoint;
1147     }
1148
1149     public void setEndpoint(String endpoint) {
1150         this.endpoint = endpoint;
1151     }
1152
1153     private HttpHeaders getHttpHeaders() {
1154         HttpHeaders headers = new HttpHeaders();
1155         headers.set(HttpHeaders.AUTHORIZATION, msoAdaptersAuth);
1156         headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
1157         headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
1158         headers.set(Constants.HttpHeaders.TARGET_ENTITY_HEADER, TARGET_ENTITY);
1159         return headers;
1160     }
1161
1162     private HttpEntity<?> getHttpEntity() {
1163         HttpHeaders headers = getHttpHeaders();
1164         return new HttpEntity<>(headers);
1165     }
1166 }