acfca5d55aa72dab3cf76081f6bc8a72748b83b7
[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
91     public void deletePnf(Pnf pnf) {
92         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName()));
93         injectionHelper.getAaiClient().delete(pnfURI);
94     }
95
96     private void updatePnfFields(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
97         if (pnf.getModelInfoPnf() != null
98                 && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelCustomizationUuid())) {
99             pnfFromAai.setModelCustomizationId(pnf.getModelInfoPnf().getModelCustomizationUuid());
100         }
101         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelInvariantUuid())) {
102             pnfFromAai.setModelInvariantId(pnf.getModelInfoPnf().getModelInvariantUuid());
103         }
104         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelUuid())) {
105             pnfFromAai.setModelVersionId(pnf.getModelInfoPnf().getModelUuid());
106         }
107         if (pnf.getOrchestrationStatus() != null && StringUtils.isNotBlank(pnf.getOrchestrationStatus().toString())) {
108             pnfFromAai.setOrchestrationStatus(pnf.getOrchestrationStatus().toString());
109         }
110         if (StringUtils.isNotBlank(pnf.getRole())) {
111             pnfFromAai.setNfRole(pnf.getRole());
112         }
113     }
114
115     private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
116         isRelatedToService(pnfFromAai);
117         if (isOrchestrationStatusSet(pnfFromAai)) {
118             checkOrchestrationStatusOfExistingPnf(pnfFromAai);
119         }
120     }
121
122     private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
123         if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
124             logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
125                     pnfFromAai.getPnfName());
126             return false;
127         }
128         return true;
129     }
130
131     private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
132         if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
133             logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
134                     pnfFromAai.getPnfName());
135         } else {
136             String errorMessage = String.format(
137                     "pnf with name %s already exists with orchestration status %s, existing pnf can be used only "
138                             + "if status is not set or set as Inventoried",
139                     pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
140             logger.error(errorMessage);
141             throw new Exception(errorMessage);
142         }
143     }
144
145     private void isRelatedToService(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
146         if (pnfFromAai.getRelationshipList() != null) {
147             for (Relationship relationship : pnfFromAai.getRelationshipList().getRelationship()) {
148                 if (relationship.getRelatedTo().equals("service-instance")) {
149                     String errorMessage = prepareRelationErrorMessage(pnfFromAai, relationship);
150                     logger.error(errorMessage);
151                     throw new Exception(errorMessage);
152                 }
153             }
154         }
155     }
156
157     private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
158         String serviceInstanceName = "";
159         String serviceInstanceId = "";
160
161         for (RelationshipData relationshipData : relationship.getRelationshipData()) {
162             if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
163                 serviceInstanceId = relationshipData.getRelationshipValue();
164                 break;
165             }
166         }
167         for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
168             if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
169                 serviceInstanceName = relatedToProperty.getPropertyValue();
170                 break;
171             }
172         }
173         return String.format(
174                 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
175                 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus(), serviceInstanceName, serviceInstanceId);
176     }
177 }