Merge "Update of developer info for using so monitoring"
[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.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;
41
42 @Component
43 public class AAIPnfResources {
44
45     private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
46
47     @Autowired
48     private InjectionHelper injectionHelper;
49
50     @Autowired
51     private AAIObjectMapper aaiObjectMapper;
52
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);
60     }
61
62     public void updateOrchestrationStatusPnf(Pnf pnf, OrchestrationStatus orchestrationStatus) {
63         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
64
65         Pnf pnfCopy = pnf.shallowCopyId();
66
67         pnf.setOrchestrationStatus(orchestrationStatus);
68         pnfCopy.setOrchestrationStatus(orchestrationStatus);
69         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
70     }
71
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());
79         }
80     }
81
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()),
85                 pnfFromAai);
86         logger.debug("updatePnfInAAI: {}", pnfFromAai);
87     }
88
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());
93         }
94         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelInvariantUuid())) {
95             pnfFromAai.setModelInvariantId(pnf.getModelInfoPnf().getModelInvariantUuid());
96         }
97         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelUuid())) {
98             pnfFromAai.setModelVersionId(pnf.getModelInfoPnf().getModelUuid());
99         }
100         if (pnf.getOrchestrationStatus() != null && StringUtils.isNotBlank(pnf.getOrchestrationStatus().toString())) {
101             pnfFromAai.setOrchestrationStatus(pnf.getOrchestrationStatus().toString());
102         }
103         if (StringUtils.isNotBlank(pnf.getRole())) {
104             pnfFromAai.setNfRole(pnf.getRole());
105         }
106     }
107
108     private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
109         isRelatedToService(pnfFromAai);
110         if (isOrchestrationStatusSet(pnfFromAai)) {
111             checkOrchestrationStatusOfExistingPnf(pnfFromAai);
112         }
113     }
114
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());
119             return false;
120         }
121         return true;
122     }
123
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());
128         } else {
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);
135         }
136     }
137
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);
145                 }
146             }
147         }
148     }
149
150     private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
151         String serviceInstanceName = "";
152         String serviceInstanceId = "";
153
154         for (RelationshipData relationshipData : relationship.getRelationshipData()) {
155             if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
156                 serviceInstanceId = relationshipData.getRelationshipValue();
157                 break;
158             }
159         }
160         for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
161             if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
162                 serviceInstanceName = relatedToProperty.getPropertyValue();
163                 break;
164             }
165         }
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);
169     }
170 }