Merge "Reorder modifiers"
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / openecomp / mso / client / orchestration / AAIOrchestrator.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.openecomp.mso.client.orchestration;
22
23 import java.util.Optional;
24 import java.util.logging.Logger;
25
26 import org.modelmapper.ModelMapper;
27 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition;
28 import org.openecomp.mso.client.aai.AAIResourcesClient;
29 import org.openecomp.mso.client.aai.entities.AAIEntityObject;
30 import org.openecomp.mso.client.aai.objects.AAIOwningEntity;
31 import org.openecomp.mso.client.aai.objects.AAIProject;
32 import org.openecomp.mso.client.aai.objects.AAIServiceInstance;
33
34 public class AAIOrchestrator {
35         
36         private static Logger LOGGER = Logger.getLogger("AAIOrchestrator");
37         
38         public void createServiceInstance(ServiceDecomposition serviceDecomp) {
39                 try{
40                         ModelMapper modelMapper = new ModelMapper();
41                         AAIEntityObject serviceInstance = modelMapper.map(serviceDecomp.getServiceInstance(), AAIServiceInstance.class);
42                         AAIResourcesClient aaiRC = this.getClient();
43                         aaiRC.createIfNotExists(serviceInstance.getUri(), Optional.of(serviceInstance));
44                 }catch(Exception ex) {
45                         String msg = "Failed to create service instance in A&AI.";
46                         throw new IllegalStateException(msg);
47                 }
48         }
49         
50         public void deleteServiceInstance(ServiceDecomposition serviceDecomp) {
51                 try{
52                         ModelMapper modelMapper = new ModelMapper();
53                         AAIEntityObject serviceInstance = modelMapper.map(serviceDecomp.getServiceInstance(), AAIServiceInstance.class);
54                         AAIResourcesClient aaiRC = this.getClient();
55                         aaiRC.delete(serviceInstance.getUri());
56                 } catch (Exception ex) {
57                         String msg = "Failed to delete service instance in A&AI.";
58                         throw new IllegalStateException(msg);
59                 }
60         }
61         
62         public void createProject(ServiceDecomposition serviceDecomp) {
63                 try{
64                         ModelMapper modelMapper = new ModelMapper();
65                         AAIEntityObject project = modelMapper.map(serviceDecomp.getProject(), AAIProject.class);
66                         AAIResourcesClient aaiRC = this.getClient();
67                         aaiRC.createIfNotExists(project.getUri(), Optional.of(project));
68                 }catch(Exception ex) {
69                         String msg = "Failed to create project in A&AI.";
70                         throw new IllegalStateException(msg);           }
71         }
72         
73         public void createProjectandConnectServiceInstance(ServiceDecomposition serviceDecomp) {
74                 try{
75                         ModelMapper modelMapper = new ModelMapper();
76                         AAIEntityObject project = modelMapper.map(serviceDecomp.getProject(), AAIProject.class);
77                         AAIEntityObject serviceInstance = modelMapper.map(serviceDecomp.getServiceInstance(), AAIServiceInstance.class);
78                         AAIResourcesClient aaiRC = this.getClient();
79                         aaiRC.createIfNotExists(project.getUri(), Optional.of(project)).connect(project.getUri(), serviceInstance.getUri());
80                 } catch(Exception ex) {
81                         String msg = "Failed to create project and connect service instance in A&AI.";
82                         throw new IllegalStateException(msg);
83                 }
84         }
85         
86         public void createOwningEntity(ServiceDecomposition serviceDecomp) {
87                 try{
88                         ModelMapper modelMapper = new ModelMapper();
89                         AAIEntityObject owningEntity = modelMapper.map(serviceDecomp.getOwningEntity(), AAIOwningEntity.class);
90                         AAIResourcesClient aaiRC = this.getClient();
91                         aaiRC.createIfNotExists(owningEntity.getUri(), Optional.of(owningEntity));
92                 }catch(Exception ex) {
93                         String msg = "Failed to create owning entity in A&AI.";
94                         throw new IllegalStateException(msg);
95                 }
96         }
97         
98         public void createOwningEntityandConnectServiceInstance(ServiceDecomposition serviceDecomp) {
99                 try{
100                         ModelMapper modelMapper = new ModelMapper();
101                         AAIEntityObject owningEntity = modelMapper.map(serviceDecomp.getOwningEntity(), AAIOwningEntity.class);
102                         AAIEntityObject serviceInstance = modelMapper.map(serviceDecomp.getServiceInstance(), AAIServiceInstance.class);
103                         AAIResourcesClient aaiRC = this.getClient();
104                         aaiRC.createIfNotExists(owningEntity.getUri(), Optional.of(owningEntity)).connect(owningEntity.getUri(), serviceInstance.getUri());
105                 }catch(Exception ex) {
106                         String msg = "Failed to create owning entity and connect service instance in A&AI.";
107                         throw new IllegalStateException(msg);           }
108         }
109         
110         protected AAIResourcesClient getClient() {
111                 return new AAIResourcesClient();
112         }
113
114 }