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