a426c3964650edd8ffa5ea886c3ed58d8e1e4348
[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 java.util.Optional;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.aai.domain.yang.RelatedToProperty;
26 import org.onap.aai.domain.yang.Relationship;
27 import org.onap.aai.domain.yang.RelationshipData;
28 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
29 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
30 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
31 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder.Types;
32 import org.onap.so.bpmn.common.InjectionHelper;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
34 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
35 import org.onap.so.client.aai.mapper.AAIObjectMapper;
36 import org.onap.so.db.catalog.beans.OrchestrationStatus;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41 import com.google.common.base.Strings;
42
43 @Component
44 public class AAIPnfResources {
45
46     private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
47
48     @Autowired
49     private InjectionHelper injectionHelper;
50
51     @Autowired
52     private AAIObjectMapper aaiObjectMapper;
53
54     public void createPnfAndConnectServiceInstance(Pnf pnf, ServiceInstance serviceInstance) {
55         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName()));
56         pnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
57         AAIResourceUri serviceInstanceURI = AAIUriFactory
58                 .createResourceUri(Types.SERVICE_INSTANCE.getFragment(serviceInstance.getServiceInstanceId()));
59         injectionHelper.getAaiClient().createIfNotExists(pnfURI, Optional.of(aaiObjectMapper.mapPnf(pnf)))
60                 .connect(pnfURI, serviceInstanceURI);
61     }
62
63     public void updateOrchestrationStatusPnf(Pnf pnf, OrchestrationStatus orchestrationStatus) {
64         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName()));
65
66         Pnf pnfCopy = pnf.shallowCopyId();
67
68         pnf.setOrchestrationStatus(orchestrationStatus);
69         pnfCopy.setOrchestrationStatus(orchestrationStatus);
70         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
71     }
72
73     public void checkIfPnfExistsInAaiAndCanBeUsed(Pnf pnf) throws Exception {
74         Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai =
75                 injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
76                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())));
77         if (pnfFromAai.isPresent()) {
78             checkIfPnfCanBeUsed(pnfFromAai.get());
79             updatePnfInAAI(pnf, pnfFromAai.get());
80         }
81     }
82
83     private void updatePnfInAAI(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
84         updatePnfFields(pnf, pnfFromAai);
85         injectionHelper.getAaiClient().update(
86                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())), pnfFromAai);
87         logger.debug("updatePnfInAAI: {}", pnfFromAai);
88     }
89
90     private void updatePnfFields(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
91         if (pnf.getModelInfoPnf() != null
92                 && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelCustomizationUuid())) {
93             pnfFromAai.setModelCustomizationId(pnf.getModelInfoPnf().getModelCustomizationUuid());
94         }
95         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelInvariantUuid())) {
96             pnfFromAai.setModelInvariantId(pnf.getModelInfoPnf().getModelInvariantUuid());
97         }
98         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelUuid())) {
99             pnfFromAai.setModelVersionId(pnf.getModelInfoPnf().getModelUuid());
100         }
101         if (pnf.getOrchestrationStatus() != null && StringUtils.isNotBlank(pnf.getOrchestrationStatus().toString())) {
102             pnfFromAai.setOrchestrationStatus(pnf.getOrchestrationStatus().toString());
103         }
104         if (StringUtils.isNotBlank(pnf.getRole())) {
105             pnfFromAai.setNfRole(pnf.getRole());
106         }
107     }
108
109     private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
110         isRelatedToService(pnfFromAai);
111         if (isOrchestrationStatusSet(pnfFromAai)) {
112             checkOrchestrationStatusOfExistingPnf(pnfFromAai);
113         }
114     }
115
116     private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
117         if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
118             logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
119                     pnfFromAai.getPnfName());
120             return false;
121         }
122         return true;
123     }
124
125     private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
126         if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
127             logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
128                     pnfFromAai.getPnfName());
129         } else {
130             String errorMessage = String.format(
131                     "pnf with name %s already exists with orchestration status %s, existing pnf can be used only "
132                             + "if status is not set or set as Inventoried",
133                     pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
134             logger.error(errorMessage);
135             throw new Exception(errorMessage);
136         }
137     }
138
139     private void isRelatedToService(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
140         if (pnfFromAai.getRelationshipList() != null) {
141             for (Relationship relationship : pnfFromAai.getRelationshipList().getRelationship()) {
142                 if (relationship.getRelatedTo().equals("service-instance")) {
143                     String errorMessage = prepareRelationErrorMessage(pnfFromAai, relationship);
144                     logger.error(errorMessage);
145                     throw new Exception(errorMessage);
146                 }
147             }
148         }
149     }
150
151     private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
152         String serviceInstanceName = "";
153         String serviceInstanceId = "";
154
155         for (RelationshipData relationshipData : relationship.getRelationshipData()) {
156             if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
157                 serviceInstanceId = relationshipData.getRelationshipValue();
158                 break;
159             }
160         }
161         for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
162             if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
163                 serviceInstanceName = relatedToProperty.getPropertyValue();
164                 break;
165             }
166         }
167         return String.format(
168                 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
169                 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus(), serviceInstanceName, serviceInstanceId);
170     }
171 }