b5738dcb40cf3fc8f60010749699d6815982391e
[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  * 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.client.orchestration;
22
23 import java.util.Optional;
24
25 import org.onap.so.bpmn.common.InjectionHelper;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
28 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
30 import org.onap.so.client.aai.AAIObjectPlurals;
31 import org.onap.so.client.aai.AAIObjectType;
32 import org.onap.so.client.aai.AAIResourcesClient;
33 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
34 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
35 import org.onap.so.client.aai.mapper.AAIObjectMapper;
36 import org.onap.so.db.catalog.beans.OrchestrationStatus;
37 import org.onap.so.logger.MsoLogger;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class AAIServiceInstanceResources {
43         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, AAIServiceInstanceResources.class);
44         
45         @Autowired
46         private InjectionHelper injectionHelper;
47         
48         @Autowired
49         private AAIObjectMapper aaiObjectMapper;
50
51         public boolean existsServiceInstance(ServiceInstance serviceInstance) {
52                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
53                                 serviceInstance.getServiceInstanceId());
54                 return injectionHelper.getAaiClient().exists(serviceInstanceURI);
55         }
56
57         public void createServiceInstance(ServiceInstance serviceInstance, Customer customer) {
58                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
59                                 customer.getGlobalCustomerId(), customer.getServiceSubscription().getServiceType(), serviceInstance.getServiceInstanceId());
60                 serviceInstance.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
61                 org.onap.aai.domain.yang.ServiceInstance AAIServiceInstance = aaiObjectMapper.mapServiceInstance(serviceInstance);
62                 injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(AAIServiceInstance));
63         }
64
65     /**
66      * Create ServiceSubscription in A&AI
67      * @param customer
68      */
69         public void createServiceSubscription(Customer customer) {
70         AAIResourceUri serviceSubscriptionURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_SUBSCRIPTION,
71                 customer.getGlobalCustomerId(),customer.getServiceSubscription().getServiceType());
72         org.onap.aai.domain.yang.ServiceSubscription serviceSubscription = aaiObjectMapper.mapServiceSubscription(customer.getServiceSubscription());
73         injectionHelper.getAaiClient().createIfNotExists(serviceSubscriptionURI , Optional.of(serviceSubscription));
74     }
75
76         public void deleteServiceInstance(ServiceInstance serviceInstance) {
77                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
78                                 serviceInstance.getServiceInstanceId());
79                 injectionHelper.getAaiClient().delete(serviceInstanceURI);
80         }
81
82         public void createProject(Project project) {
83                 AAIResourceUri projectURI = AAIUriFactory.createResourceUri(AAIObjectType.PROJECT, project.getProjectName());
84                 org.onap.aai.domain.yang.Project AAIProject = aaiObjectMapper.mapProject(project);
85                 injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(AAIProject));
86         }
87
88         public void createProjectandConnectServiceInstance(Project project, ServiceInstance serviceInstance) {
89                 AAIResourceUri projectURI = AAIUriFactory.createResourceUri(AAIObjectType.PROJECT, project.getProjectName());
90                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
91                                 serviceInstance.getServiceInstanceId());
92                 org.onap.aai.domain.yang.Project AAIProject = aaiObjectMapper.mapProject(project);
93                 injectionHelper.getAaiClient().createIfNotExists(projectURI, Optional.of(AAIProject)).connect(projectURI, serviceInstanceURI);
94         }
95
96         public void createOwningEntity(OwningEntity owningEntity) {
97                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY,
98                                 owningEntity.getOwningEntityId());
99                 org.onap.aai.domain.yang.OwningEntity AAIOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
100                 injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(AAIOwningEntity));
101         }
102
103         public boolean existsOwningEntity(OwningEntity owningEntity) {
104                 AAIResourceUri owningEntityUri = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY,
105                                 owningEntity.getOwningEntityId());
106                 return injectionHelper.getAaiClient().exists(owningEntityUri);
107         }
108         
109         public boolean existsOwningEntityName(String owningEntityName) {
110                 AAIResourceUri owningEntityUri = AAIUriFactory.createResourceUri(AAIObjectPlurals.OWNING_ENTITIES).queryParam("owning-entity-name", owningEntityName);
111                 AAIResourcesClient aaiRC = injectionHelper.getAaiClient();
112                 return aaiRC.exists(owningEntityUri);
113         }
114
115         public void connectOwningEntityandServiceInstance(OwningEntity owningEntity, ServiceInstance serviceInstance) {
116                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY,
117                                 owningEntity.getOwningEntityId());
118                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
119                                 serviceInstance.getServiceInstanceId());
120                 injectionHelper.getAaiClient().connect(owningEntityURI, serviceInstanceURI);
121         }
122
123         public void createOwningEntityandConnectServiceInstance(OwningEntity owningEntity,
124                         ServiceInstance serviceInstance) {
125                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY,
126                                 owningEntity.getOwningEntityId());
127                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
128                                 serviceInstance.getServiceInstanceId());
129                 org.onap.aai.domain.yang.OwningEntity AAIOwningEntity = aaiObjectMapper.mapOwningEntity(owningEntity);
130                 injectionHelper.getAaiClient().createIfNotExists(owningEntityURI, Optional.of(AAIOwningEntity)).connect(owningEntityURI,
131                                 serviceInstanceURI);
132         }
133         
134         public void updateOrchestrationStatusServiceInstance(ServiceInstance serviceInstance, OrchestrationStatus orchestrationStatus){
135                 ServiceInstance copiedServiceInstance = serviceInstance.shallowCopyId();
136
137                 copiedServiceInstance.setOrchestrationStatus(orchestrationStatus);
138                 copiedServiceInstance.setServiceInstanceName(serviceInstance.getServiceInstanceName());
139                 serviceInstance.setOrchestrationStatus(orchestrationStatus);
140                 updateServiceInstance(copiedServiceInstance);
141         }
142         
143         public void updateServiceInstance(ServiceInstance serviceInstance) {
144                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
145                 org.onap.aai.domain.yang.ServiceInstance AAIServiceInstance = aaiObjectMapper.mapServiceInstance(serviceInstance);
146                 injectionHelper.getAaiClient().update(serviceInstanceURI, AAIServiceInstance);
147         }
148         
149
150 }