possibility to use inventoried PNF instance in new service instance
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / orchestration / AAIPnfResources.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020 Nokia 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.client.orchestration;
22
23 import com.google.common.base.Strings;
24 import java.util.Optional;
25 import org.onap.so.bpmn.common.InjectionHelper;
26 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
27 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
28 import org.onap.aaiclient.client.aai.AAIObjectType;
29 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
30 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
31 import org.onap.so.client.aai.mapper.AAIObjectMapper;
32 import org.onap.so.db.catalog.beans.OrchestrationStatus;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 @Component
39 public class AAIPnfResources {
40
41     private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
42
43     @Autowired
44     private InjectionHelper injectionHelper;
45
46     @Autowired
47     private AAIObjectMapper aaiObjectMapper;
48
49     public void createPnfAndConnectServiceInstance(Pnf pnf, ServiceInstance serviceInstance) {
50         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
51         pnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
52         AAIResourceUri serviceInstanceURI =
53                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
54         injectionHelper.getAaiClient().createIfNotExists(pnfURI, Optional.of(aaiObjectMapper.mapPnf(pnf)))
55                 .connect(pnfURI, serviceInstanceURI);
56     }
57
58     public void updateOrchestrationStatusPnf(Pnf pnf, OrchestrationStatus orchestrationStatus) {
59         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
60
61         Pnf pnfCopy = pnf.shallowCopyId();
62
63         pnf.setOrchestrationStatus(orchestrationStatus);
64         pnfCopy.setOrchestrationStatus(orchestrationStatus);
65         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
66     }
67
68     public void checkIfPnfExistsInAaiAndCanBeUsed(String pnfName) throws Exception {
69         Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai = injectionHelper.getAaiClient()
70                 .get(org.onap.aai.domain.yang.Pnf.class, AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfName));
71         if (pnfFromAai.isPresent() && isOrchestrationStatusSet(pnfFromAai.get())) {
72             checkOrchestrationStatusOfExistingPnf(pnfFromAai.get());
73         }
74     }
75
76     private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
77         if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
78             logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
79                     pnfFromAai.getPnfName());
80             return false;
81         }
82         return true;
83     }
84
85     private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
86         if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
87             logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
88                     pnfFromAai.getPnfName());
89         } else {
90             String errorMessage = String.format(
91                     "pnf with name %s already exists with orchestration status %s, only status Inventoried allows to use existing pnf",
92                     pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
93             logger.error(errorMessage);
94             throw new Exception(errorMessage);
95
96         }
97     }
98 }