48a1c1e55896cdb1430f428019b420287a495b82
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / orchestration / AAIVnfResources.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  * 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.client.orchestration;
24
25 import java.io.IOException;
26 import java.util.Optional;
27
28 import org.onap.so.bpmn.common.InjectionHelper;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
34 import org.onap.so.client.aai.AAIObjectType;
35 import org.onap.so.client.aai.AAIValidatorImpl;
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
38 import org.onap.so.client.aai.mapper.AAIObjectMapper;
39 import org.onap.so.db.catalog.beans.OrchestrationStatus;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 public class AAIVnfResources {
47         private static final Logger logger = LoggerFactory.getLogger(AAIVnfResources.class);
48         
49         @Autowired
50         private InjectionHelper injectionHelper;
51         
52         @Autowired
53         private AAIObjectMapper aaiObjectMapper;
54         
55         private AAIValidatorImpl aaiValidatorImpl= new AAIValidatorImpl();
56         
57         public void createVnfandConnectServiceInstance(GenericVnf vnf, ServiceInstance serviceInstance) {
58                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
59                 vnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
60                 AAIResourceUri serviceInstanceURI = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE,
61                                 serviceInstance.getServiceInstanceId());
62                 injectionHelper.getAaiClient().createIfNotExists(vnfURI, Optional.of(aaiObjectMapper.mapVnf(vnf))).connect(vnfURI, serviceInstanceURI);
63         }
64         
65         public void createPlatformandConnectVnf(Platform platform, GenericVnf vnf) {
66                 AAIResourceUri platformURI = AAIUriFactory.createResourceUri(AAIObjectType.PLATFORM, platform.getPlatformName());       
67                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
68                 injectionHelper.getAaiClient().createIfNotExists(platformURI, Optional.of(platform)).connect(vnfURI, platformURI);
69         }
70         
71         public void createLineOfBusinessandConnectVnf(LineOfBusiness lineOfBusiness, GenericVnf vnf) {
72                 AAIResourceUri lineOfBusinessURI = AAIUriFactory.createResourceUri(AAIObjectType.LINE_OF_BUSINESS, lineOfBusiness.getLineOfBusinessName());     
73                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
74                 injectionHelper.getAaiClient().createIfNotExists(lineOfBusinessURI, Optional.of(lineOfBusiness)).connect(vnfURI, lineOfBusinessURI);
75         }
76         
77         public void deleteVnf(GenericVnf vnf) {
78                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
79                 injectionHelper.getAaiClient().delete(vnfURI);
80         }
81
82         public void updateOrchestrationStatusVnf(GenericVnf vnf, OrchestrationStatus orchestrationStatus) {
83                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
84
85                 GenericVnf copiedVnf = vnf.shallowCopyId();
86
87                 vnf.setOrchestrationStatus(orchestrationStatus);
88                 copiedVnf.setOrchestrationStatus(orchestrationStatus);
89                 injectionHelper.getAaiClient().update(vnfURI, aaiObjectMapper.mapVnf(copiedVnf));
90         }
91         
92         public void updateObjectVnf(GenericVnf vnf) {
93                 AAIResourceUri vnfUri = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
94                 injectionHelper.getAaiClient().update(vnfUri, aaiObjectMapper.mapVnf(vnf));
95         }
96
97         /**
98          * Retrieve Generic VNF from AAI using vnf Id
99          * @param vnfId - vnf-id required vnf
100          * @return AAI Generic Vnf
101          */
102         public Optional<org.onap.aai.domain.yang.GenericVnf> getGenericVnf( String vnfId) {
103                 return injectionHelper.getAaiClient()
104                                 .get(org.onap.aai.domain.yang.GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId));
105         }
106         
107         /**
108          * Check inMaint flag value of Generic VNF from AAI using vnf Id
109          * @param vnfId - vnf-id required vnf
110          * @return inMaint flag value
111          */
112         public boolean checkInMaintFlag(String vnfId) {         
113                 org.onap.aai.domain.yang.GenericVnf vnf = injectionHelper.getAaiClient()
114                                 .get(org.onap.aai.domain.yang.GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId))
115                                 .orElse(new org.onap.aai.domain.yang.GenericVnf());
116                 return vnf.isInMaint();
117         }
118         
119         public void connectVnfToCloudRegion(GenericVnf vnf, CloudRegion cloudRegion) {
120                 AAIResourceUri cloudRegionURI = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION,
121                                 cloudRegion.getCloudOwner(), cloudRegion.getLcpCloudRegionId());
122                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
123                 injectionHelper.getAaiClient().connect(vnfURI,cloudRegionURI);
124         }
125         
126         public void connectVnfToTenant(GenericVnf vnf, CloudRegion cloudRegion) {
127                 AAIResourceUri tenantURI = AAIUriFactory.createResourceUri(AAIObjectType.TENANT,
128                                 cloudRegion.getCloudOwner(), cloudRegion.getLcpCloudRegionId(), cloudRegion.getTenantId());
129                 AAIResourceUri vnfURI = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnf.getVnfId());
130                 injectionHelper.getAaiClient().connect(tenantURI, vnfURI);
131         }
132         
133         public boolean checkVnfClosedLoopDisabledFlag(String vnfId) {           
134                 org.onap.aai.domain.yang.GenericVnf vnf = injectionHelper.getAaiClient()
135                                 .get(org.onap.aai.domain.yang.GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId))
136                                 .orElse(new org.onap.aai.domain.yang.GenericVnf());
137                 return vnf.isIsClosedLoopDisabled();
138         }
139         
140         public boolean checkVnfPserversLockedFlag (String vnfId) throws IOException {           
141                 org.onap.aai.domain.yang.GenericVnf vnf = injectionHelper.getAaiClient()
142                                 .get(org.onap.aai.domain.yang.GenericVnf.class, AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, vnfId))
143                                 .orElse(new org.onap.aai.domain.yang.GenericVnf());
144                         return aaiValidatorImpl.isPhysicalServerLocked(vnf.getVnfId());
145
146         }
147 }