Merge "add junit coverage for SvnfmService"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / orchestration / AAIServiceInstanceResources.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.client.orchestration;
24
25 import java.util.List;
26 import java.util.Optional;
27 import org.onap.aai.domain.yang.OwningEntities;
28 import org.onap.so.bpmn.common.InjectionHelper;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.client.aai.AAIObjectPlurals;
34 import org.onap.so.client.aai.AAIObjectType;
35 import org.onap.so.client.aai.AAIResourcesClient;
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
38 import org.onap.so.client.aai.mapper.AAIObjectMapper;
39 import org.onap.so.db.catalog.beans.OrchestrationStatus;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class AAIServiceInstanceResources {
47     private static final Logger logger = LoggerFactory.getLogger(AAIServiceInstanceResources.class);
48
49     @Autowired
50     private InjectionHelper injectionHelper;
51
52     @Autowired
53     private AAIObjectMapper aaiObjectMapper;
54
55     public boolean existsServiceInstance(ServiceInstance serviceInstance) {
56         AAIResourceUri serviceInstanceURI =
57                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
58         return injectionHelper.getAaiClient().exists(serviceInstanceURI);
59     }
60
61     public void createServiceInstance(ServiceInstance serviceInstance, Customer customer) {
62         AAIResourceUri serviceInstanceURI =
63                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, customer.getGlobalCustomerId(),
64                         customer.getServiceSubscription().getServiceType(), serviceInstance.getServiceInstanceId());
65         serviceInstance.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
66         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance =
67                 aaiObjectMapper.mapServiceInstance(serviceInstance);
68         injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(aaiServiceInstance));
69     }
70
71     /**
72      * Create ServiceSubscription in A&AI
73      * 
74      * @param customer
75      */
76     public void createServiceSubscription(Customer customer) {
77         AAIResourceUri serviceSubscriptionURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION,
78                 customer.getGlobalCustomerId(), customer.getServiceSubscription().getServiceType());
79         org.onap.aai.domain.yang.ServiceSubscription serviceSubscription =
80                 aaiObjectMapper.mapServiceSubscription(customer.getServiceSubscription());
81         injectionHelper.getAaiClient().createIfNotExists(serviceSubscriptionURI, Optional.of(serviceSubscription));
82     }
83
84     public void deleteServiceInstance(ServiceInstance serviceInstance) {
85         AAIResourceUri serviceInstanceURI =
86                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
87         injectionHelper.getAaiClient().delete(serviceInstanceURI);
88     }
89
90     public void createProject(Project project) {
91         AAIResourceUri projectURI = AAIUriFactory.createResourceUri(AAIObjectType.PROJECT, project.getProjectName());
92         org.onap.aai.domain.yang.Project aaiProject = aaiObjectMapper.mapProject(project);
93         injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(aaiProject));
94     }
95
96     public void createProjectandConnectServiceInstance(Project project, ServiceInstance serviceInstance) {
97         AAIResourceUri projectURI = AAIUriFactory.createResourceUri(AAIObjectType.PROJECT, project.getProjectName());
98         AAIResourceUri serviceInstanceURI =
99                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
100         org.onap.aai.domain.yang.Project aaiProject = aaiObjectMapper.mapProject(project);
101         injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(aaiProject)).connect(projectURI,
102                 serviceInstanceURI);
103     }
104
105     public void createOwningEntity(OwningEntity owningEntity) {
106         AAIResourceUri owningEntityURI =
107                 AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntity.getOwningEntityId());
108         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
109         injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(aaiOwningEntity));
110     }
111
112     public boolean existsOwningEntity(OwningEntity owningEntity) {
113         AAIResourceUri owningEntityUri =
114                 AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntity.getOwningEntityId());
115         return injectionHelper.getAaiClient().exists(owningEntityUri);
116     }
117
118     public boolean existsOwningEntityName(String owningEntityName) {
119         AAIResourceUri owningEntityUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.OWNING_ENTITY)
120                 .queryParam("owning-entity-name", owningEntityName);
121         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
122         return aaiRC.exists(owningEntityUri);
123     }
124
125     public org.onap.aai.domain.yang.OwningEntity getOwningEntityByName(String owningEntityName)
126             throws AAIEntityNotFoundException {
127         AAIResourceUri owningEntityUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.OWNING_ENTITY)
128                 .queryParam("owning-entity-name", owningEntityName);
129         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
130         Optional<OwningEntities> owningEntities = aaiRC.get(OwningEntities.class, owningEntityUri);
131         if (owningEntities.isPresent()) {
132             List<org.onap.aai.domain.yang.OwningEntity> owningEntityList = owningEntities.get().getOwningEntity();
133             if (owningEntityList.size() > 1) {
134                 throw new AAIEntityNotFoundException(
135                         "Non unique result returned for owning entity name: " + owningEntityName);
136             } else {
137                 return owningEntityList.get(0);
138             }
139         } else {
140             throw new AAIEntityNotFoundException("No result returned for owning entity name: " + owningEntityName);
141         }
142     }
143
144     public void connectOwningEntityandServiceInstance(OwningEntity owningEntity, ServiceInstance serviceInstance) {
145         AAIResourceUri owningEntityURI =
146                 AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntity.getOwningEntityId());
147         AAIResourceUri serviceInstanceURI =
148                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
149         injectionHelper.getAaiClient().connect(owningEntityURI, serviceInstanceURI);
150     }
151
152     public void createOwningEntityandConnectServiceInstance(OwningEntity owningEntity,
153             ServiceInstance serviceInstance) {
154         AAIResourceUri owningEntityURI =
155                 AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntity.getOwningEntityId());
156         AAIResourceUri serviceInstanceURI =
157                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
158         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
159         injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(aaiOwningEntity))
160                 .connect(owningEntityURI, serviceInstanceURI);
161     }
162
163     public void updateOrchestrationStatusServiceInstance(ServiceInstance serviceInstance,
164             OrchestrationStatus orchestrationStatus) {
165         ServiceInstance copiedServiceInstance = serviceInstance.shallowCopyId();
166
167         copiedServiceInstance.setOrchestrationStatus(orchestrationStatus);
168         copiedServiceInstance.setServiceInstanceName(serviceInstance.getServiceInstanceName());
169         serviceInstance.setOrchestrationStatus(orchestrationStatus);
170         updateServiceInstance(copiedServiceInstance);
171     }
172
173     public void updateServiceInstance(ServiceInstance serviceInstance) {
174         AAIResourceUri serviceInstanceURI =
175                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
176         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance =
177                 aaiObjectMapper.mapServiceInstance(serviceInstance);
178         injectionHelper.getAaiClient().update(serviceInstanceURI, aaiServiceInstance);
179     }
180
181     public boolean checkInstanceServiceNameInUse(ServiceInstance serviceInstance) {
182         AAIResourceUri uriSI = AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
183                 .queryParam("service-instance-name", serviceInstance.getServiceInstanceName());
184         return injectionHelper.getAaiClient().exists(uriSI);
185     }
186
187 }