c04c2dc15b56803f59aae429905d425527fea5e8
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / java / org / onap / so / bpmn / infrastructure / aai / groovyflows / AAICreateResources.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.bpmn.infrastructure.aai.groovyflows;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Optional;
26
27 import javax.ws.rs.NotFoundException;
28
29 import org.onap.aai.domain.yang.ServiceInstance;
30 import org.onap.aai.domain.yang.ServiceInstances;
31 import org.onap.aai.domain.yang.OwningEntities;
32 import org.onap.aai.domain.yang.OwningEntity;
33 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
34 import org.onap.so.client.aai.AAIObjectPlurals;
35 import org.onap.so.client.aai.AAIObjectType;
36 import org.onap.so.client.aai.AAIResourcesClient;
37 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
38 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
39 import org.onap.so.logger.MsoLogger;
40
41 public class AAICreateResources {
42         
43         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, AAICreateResources.class);
44
45         public void createAAIProject (String projectName, String serviceInstance){
46                 AAIResourceUri projectURI = AAIUriFactory.createResourceUri(AAIObjectType.PROJECT, projectName);
47                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance);
48                 AAIResourcesClient aaiRC = new AAIResourcesClient();      
49                 aaiRC.createIfNotExists(projectURI, Optional.empty()).connect(projectURI, serviceInstanceURI);
50                 
51         }
52         
53         public void createAAIOwningEntity(String owningEntityId, String owningEntityName,String serviceInstance){
54                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntityId);
55                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance);
56                 Map<String, String> hashMap= new HashMap<>();
57                 hashMap.put("owning-entity-name", owningEntityName);    
58                 AAIResourcesClient aaiRC = new AAIResourcesClient();
59                 aaiRC.createIfNotExists(owningEntityURI, Optional.of(hashMap)).connect(owningEntityURI, serviceInstanceURI);
60         }
61
62         public boolean existsOwningEntity(String owningEntityId){
63                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntityId);
64                 AAIResourcesClient aaiRC = new AAIResourcesClient();      
65                 return aaiRC.exists(owningEntityURI);
66         }
67         
68         protected OwningEntities getOwningEntityName(String owningEntityName){
69                 
70                 AAIResourcesClient aaiRC = new AAIResourcesClient();
71                 return aaiRC.get(OwningEntities.class,
72                                 AAIUriFactory
73                                                 .createResourceUri(AAIObjectPlurals.OWNING_ENTITIES)
74                                                 .queryParam("owning-entity-name", owningEntityName))
75                                 .orElseGet(() -> {
76                                         msoLogger.debug("No Owning Entity matched by name");
77                                         return null;
78                                 });
79                 
80         }
81         
82         public Optional<OwningEntity> getOwningEntityNames(String owningEntityName) throws Exception{
83                 OwningEntity owningEntity = null;
84                 OwningEntities owningEntities = null;
85                 owningEntities = getOwningEntityName(owningEntityName);
86
87                 if (owningEntities == null) {
88                         return Optional.empty();
89                 } else if (owningEntities.getOwningEntity().size() > 1) {
90                         throw new Exception("Multiple OwningEntities Returned");
91                 } else {
92                         owningEntity = owningEntities.getOwningEntity().get(0);
93                 }
94                 return Optional.of(owningEntity);
95         }
96         
97         public void connectOwningEntityandServiceInstance (String owningEntityId, String serviceInstance){
98                 AAIResourceUri owningEntityURI = AAIUriFactory.createResourceUri(AAIObjectType.OWNING_ENTITY, owningEntityId);
99                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance);
100                 AAIResourcesClient aaiRC = new AAIResourcesClient();
101                 aaiRC.connect(owningEntityURI, serviceInstanceURI);
102         }
103         
104         public void createAAIPlatform(String platformName,String vnfId){
105                 AAIResourceUri platformURI = AAIUriFactory.createResourceUri(AAIObjectType.PLATFORM, platformName);
106                 AAIResourceUri genericVnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF,vnfId);
107                 AAIResourcesClient aaiRC = new AAIResourcesClient();                              
108                 aaiRC.createIfNotExists(platformURI, Optional.empty()).connect(platformURI, genericVnfURI);
109         }
110         
111         public void createAAILineOfBusiness(String lineOfBusiness,String vnfId){
112                 AAIResourceUri lineOfBusinessURI = AAIUriFactory.createResourceUri(AAIObjectType.LINE_OF_BUSINESS, lineOfBusiness);
113                 AAIResourceUri genericVnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF,vnfId);
114                 AAIResourcesClient aaiRC = new AAIResourcesClient();                              
115                 aaiRC.createIfNotExists(lineOfBusinessURI, Optional.empty()).connect(lineOfBusinessURI, genericVnfURI);
116         }
117         public void createAAIServiceInstance(String globalCustomerId, String serviceType, String serviceInstanceId){
118                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustomerId,serviceType,serviceInstanceId);
119                 AAIResourcesClient aaiRC = new AAIResourcesClient();      
120                 aaiRC.createIfNotExists(serviceInstanceURI, Optional.empty());
121         }
122         
123 }