Add new endpoint and macro for service upgrade
[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  * Modifications Copyright (c) 2020 Nokia
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.so.client.orchestration;
26
27 import java.util.List;
28 import java.util.Optional;
29 import org.onap.aai.domain.yang.OwningEntities;
30 import org.onap.aaiclient.client.aai.AAIResourcesClient;
31 import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
34 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
35 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
36 import org.onap.so.bpmn.common.InjectionHelper;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.so.client.aai.mapper.AAIObjectMapper;
42 import org.onap.so.db.catalog.beans.OrchestrationStatus;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Component;
45
46 @Component
47 public class AAIServiceInstanceResources {
48
49     @Autowired
50     private InjectionHelper injectionHelper;
51
52     @Autowired
53     private AAIObjectMapper aaiObjectMapper;
54
55     public boolean existsServiceInstance(ServiceInstance serviceInstance) {
56         AAIResourceUri serviceInstanceURI = AAIUriFactory
57                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
58         return injectionHelper.getAaiClient().exists(serviceInstanceURI);
59     }
60
61     public void createServiceInstance(ServiceInstance serviceInstance, Customer customer) {
62         AAIResourceUri serviceInstanceURI =
63                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
64                         .serviceSubscription(customer.getServiceSubscription().getServiceType())
65                         .serviceInstance(serviceInstance.getServiceInstanceId()));
66         serviceInstance.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
67         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance =
68                 aaiObjectMapper.mapServiceInstance(serviceInstance);
69         injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(aaiServiceInstance));
70     }
71
72     /**
73      * Create ServiceSubscription in A&AI
74      * 
75      * @param customer
76      */
77     public void createServiceSubscription(Customer customer) {
78         AAIResourceUri serviceSubscriptionURI =
79                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
80                         .serviceSubscription(customer.getServiceSubscription().getServiceType()));
81         org.onap.aai.domain.yang.ServiceSubscription serviceSubscription =
82                 aaiObjectMapper.mapServiceSubscription(customer.getServiceSubscription());
83         injectionHelper.getAaiClient().createIfNotExists(serviceSubscriptionURI, Optional.of(serviceSubscription));
84     }
85
86     public void deleteServiceInstance(ServiceInstance serviceInstance) {
87         AAIResourceUri serviceInstanceURI = AAIUriFactory
88                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
89         injectionHelper.getAaiClient().delete(serviceInstanceURI);
90     }
91
92     public void createProject(Project project) {
93         AAIResourceUri projectURI =
94                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().project(project.getProjectName()));
95         org.onap.aai.domain.yang.Project aaiProject = aaiObjectMapper.mapProject(project);
96         injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(aaiProject));
97     }
98
99     public void createProjectandConnectServiceInstance(Project project, ServiceInstance serviceInstance) {
100         AAIResourceUri projectURI =
101                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().project(project.getProjectName()));
102         AAIResourceUri serviceInstanceURI = AAIUriFactory
103                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
104         org.onap.aai.domain.yang.Project aaiProject = aaiObjectMapper.mapProject(project);
105         injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(aaiProject)).connect(projectURI,
106                 serviceInstanceURI);
107     }
108
109     public void createOwningEntity(OwningEntity owningEntity) {
110         AAIResourceUri owningEntityURI = AAIUriFactory
111                 .createResourceUri(AAIFluentTypeBuilder.business().owningEntity(owningEntity.getOwningEntityId()));
112         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
113         injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(aaiOwningEntity));
114     }
115
116     public boolean existsOwningEntity(OwningEntity owningEntity) {
117         AAIResourceUri owningEntityUri = AAIUriFactory
118                 .createResourceUri(AAIFluentTypeBuilder.business().owningEntity(owningEntity.getOwningEntityId()));
119         return injectionHelper.getAaiClient().exists(owningEntityUri);
120     }
121
122     public boolean existsOwningEntityName(String owningEntityName) {
123         AAIPluralResourceUri owningEntityUri =
124                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().owningEntities())
125                         .queryParam("owning-entity-name", owningEntityName);
126         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
127         return aaiRC.exists(owningEntityUri);
128     }
129
130     public org.onap.aai.domain.yang.OwningEntity getOwningEntityByName(String owningEntityName)
131             throws AAIEntityNotFoundException {
132         AAIPluralResourceUri owningEntityUri =
133                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().owningEntities())
134                         .queryParam("owning-entity-name", owningEntityName);
135         AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
136         Optional<OwningEntities> owningEntities = aaiRC.get(OwningEntities.class, owningEntityUri);
137         if (owningEntities.isPresent()) {
138             List<org.onap.aai.domain.yang.OwningEntity> owningEntityList = owningEntities.get().getOwningEntity();
139             if (owningEntityList.size() > 1) {
140                 throw new AAIEntityNotFoundException(
141                         "Non unique result returned for owning entity name: " + owningEntityName);
142             } else {
143                 return owningEntityList.get(0);
144             }
145         } else {
146             throw new AAIEntityNotFoundException("No result returned for owning entity name: " + owningEntityName);
147         }
148     }
149
150     public void connectOwningEntityandServiceInstance(OwningEntity owningEntity, ServiceInstance serviceInstance) {
151         AAIResourceUri owningEntityURI = AAIUriFactory
152                 .createResourceUri(AAIFluentTypeBuilder.business().owningEntity(owningEntity.getOwningEntityId()));
153         AAIResourceUri serviceInstanceURI = AAIUriFactory
154                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
155         injectionHelper.getAaiClient().connect(owningEntityURI, serviceInstanceURI);
156     }
157
158     public void createOwningEntityandConnectServiceInstance(OwningEntity owningEntity,
159             ServiceInstance serviceInstance) {
160         AAIResourceUri owningEntityURI = AAIUriFactory
161                 .createResourceUri(AAIFluentTypeBuilder.business().owningEntity(owningEntity.getOwningEntityId()));
162         AAIResourceUri serviceInstanceURI = AAIUriFactory
163                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
164         org.onap.aai.domain.yang.OwningEntity aaiOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
165         injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(aaiOwningEntity))
166                 .connect(owningEntityURI, serviceInstanceURI);
167     }
168
169     public void updateOrchestrationStatusServiceInstance(ServiceInstance serviceInstance,
170             OrchestrationStatus orchestrationStatus) {
171         ServiceInstance copiedServiceInstance = serviceInstance.shallowCopyId();
172
173         copiedServiceInstance.setOrchestrationStatus(orchestrationStatus);
174         copiedServiceInstance.setServiceInstanceName(serviceInstance.getServiceInstanceName());
175         serviceInstance.setOrchestrationStatus(orchestrationStatus);
176         updateServiceInstance(copiedServiceInstance);
177     }
178
179     public void updateServiceInstance(ServiceInstance serviceInstance) {
180         AAIResourceUri serviceInstanceURI = AAIUriFactory
181                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
182         org.onap.aai.domain.yang.ServiceInstance aaiServiceInstance =
183                 aaiObjectMapper.mapServiceInstance(serviceInstance);
184         mapEmptyStringsToNull(aaiServiceInstance);
185         injectionHelper.getAaiClient().update(serviceInstanceURI, aaiServiceInstance);
186     }
187
188     /*
189      * Per serialization configurations in GraphInventoryCommonObjectMapperPatchProvider, empty strings are mapped to
190      * null and included in the payload. Null values are on the other hand excluded. Passing null values in a PATCH
191      * request to AAI will fail. We need to map empty strings to null before serialization in order to exclude these
192      * values from the payload.
193      */
194     private void mapEmptyStringsToNull(org.onap.aai.domain.yang.ServiceInstance serviceInstance) {
195         if (serviceInstance != null) {
196             if ("".equals(serviceInstance.getServiceType()))
197                 serviceInstance.setServiceType(null);
198             if ("".equals(serviceInstance.getServiceRole()))
199                 serviceInstance.setServiceRole(null);
200             if ("".equals(serviceInstance.getServiceFunction()))
201                 serviceInstance.setServiceFunction(null);
202         }
203     }
204
205     public boolean checkInstanceServiceNameInUse(ServiceInstance serviceInstance) {
206         AAIPluralResourceUri uriSI = AAIUriFactory.createNodesUri(Types.SERVICE_INSTANCES.getFragment())
207                 .queryParam("service-instance-name", serviceInstance.getServiceInstanceName());
208         return injectionHelper.getAaiClient().exists(uriSI);
209     }
210
211 }