2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.orchestration;
23 import com.google.common.base.Strings;
24 import java.util.Optional;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.aai.domain.yang.RelatedToProperty;
27 import org.onap.aai.domain.yang.Relationship;
28 import org.onap.aai.domain.yang.RelationshipData;
29 import org.onap.so.bpmn.common.InjectionHelper;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
32 import org.onap.aaiclient.client.aai.AAIObjectType;
33 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
34 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
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;
43 public class AAIPnfResources {
45 private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
48 private InjectionHelper injectionHelper;
51 private AAIObjectMapper aaiObjectMapper;
53 public void createPnfAndConnectServiceInstance(Pnf pnf, ServiceInstance serviceInstance) {
54 AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
55 pnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
56 AAIResourceUri serviceInstanceURI =
57 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
58 injectionHelper.getAaiClient().createIfNotExists(pnfURI, Optional.of(aaiObjectMapper.mapPnf(pnf)))
59 .connect(pnfURI, serviceInstanceURI);
62 public void updateOrchestrationStatusPnf(Pnf pnf, OrchestrationStatus orchestrationStatus) {
63 AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
65 Pnf pnfCopy = pnf.shallowCopyId();
67 pnf.setOrchestrationStatus(orchestrationStatus);
68 pnfCopy.setOrchestrationStatus(orchestrationStatus);
69 injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
72 public void checkIfPnfExistsInAaiAndCanBeUsed(Pnf pnf) throws Exception {
73 Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai =
74 injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
75 AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName()));
76 if (pnfFromAai.isPresent()) {
77 checkIfPnfCanBeUsed(pnfFromAai.get());
78 updatePnfInAAI(pnf, pnfFromAai.get());
82 private void updatePnfInAAI(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
83 updatePnfFields(pnf, pnfFromAai);
84 injectionHelper.getAaiClient().update(AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName()),
86 logger.debug("updatePnfInAAI: {}", pnfFromAai);
89 private void updatePnfFields(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
90 if (pnf.getModelInfoPnf() != null
91 && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelCustomizationUuid())) {
92 pnfFromAai.setModelCustomizationId(pnf.getModelInfoPnf().getModelCustomizationUuid());
94 if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelInvariantUuid())) {
95 pnfFromAai.setModelInvariantId(pnf.getModelInfoPnf().getModelInvariantUuid());
97 if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelUuid())) {
98 pnfFromAai.setModelVersionId(pnf.getModelInfoPnf().getModelUuid());
100 if (pnf.getOrchestrationStatus() != null && StringUtils.isNotBlank(pnf.getOrchestrationStatus().toString())) {
101 pnfFromAai.setOrchestrationStatus(pnf.getOrchestrationStatus().toString());
103 if (StringUtils.isNotBlank(pnf.getRole())) {
104 pnfFromAai.setNfRole(pnf.getRole());
108 private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
109 isRelatedToService(pnfFromAai);
110 if (isOrchestrationStatusSet(pnfFromAai)) {
111 checkOrchestrationStatusOfExistingPnf(pnfFromAai);
115 private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
116 if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
117 logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
118 pnfFromAai.getPnfName());
124 private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
125 if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
126 logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
127 pnfFromAai.getPnfName());
129 String errorMessage = String.format(
130 "pnf with name %s already exists with orchestration status %s, existing pnf can be used only "
131 + "if status is not set or set as Inventoried",
132 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
133 logger.error(errorMessage);
134 throw new Exception(errorMessage);
138 private void isRelatedToService(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
139 if (pnfFromAai.getRelationshipList() != null) {
140 for (Relationship relationship : pnfFromAai.getRelationshipList().getRelationship()) {
141 if (relationship.getRelatedTo().equals("service-instance")) {
142 String errorMessage = prepareRelationErrorMessage(pnfFromAai, relationship);
143 logger.error(errorMessage);
144 throw new Exception(errorMessage);
150 private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
151 String serviceInstanceName = "";
152 String serviceInstanceId = "";
154 for (RelationshipData relationshipData : relationship.getRelationshipData()) {
155 if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
156 serviceInstanceId = relationshipData.getRelationshipValue();
160 for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
161 if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
162 serviceInstanceName = relatedToProperty.getPropertyValue();
166 return String.format(
167 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
168 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus(), serviceInstanceName, serviceInstanceId);