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