[SO] Macro flow for PNF-Modify operation
[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         Pnf pnfCopy = pnf.shallowCopyId();
66         if (orchestrationStatus.equals(OrchestrationStatus.REGISTER)
67                 || orchestrationStatus.equals(OrchestrationStatus.REGISTERED)) {
68             pnf.setInMaint(true);
69             pnfCopy.setInMaint(true);
70         } else {
71             pnf.setInMaint(false);
72             pnfCopy.setInMaint(false);
73         }
74
75         pnf.setOrchestrationStatus(orchestrationStatus);
76         pnfCopy.setOrchestrationStatus(orchestrationStatus);
77         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
78     }
79
80     public void checkIfPnfExistsInAaiAndCanBeUsed(Pnf pnf) throws Exception {
81         Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai =
82                 injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
83                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())));
84         if (pnfFromAai.isPresent()) {
85             checkIfPnfCanBeUsed(pnfFromAai.get());
86             updatePnfInAAI(pnf, pnfFromAai.get());
87         }
88     }
89
90     private void updatePnfInAAI(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
91         updatePnfFields(pnf, pnfFromAai);
92         injectionHelper.getAaiClient().update(
93                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())), pnfFromAai);
94         logger.debug("updatePnfInAAI: {}", pnfFromAai);
95     }
96
97
98     public void deletePnf(Pnf pnf) {
99         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName()));
100         injectionHelper.getAaiClient().delete(pnfURI);
101     }
102
103     public void updateObjectPnf(Pnf pnf) {
104         Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai =
105                 injectionHelper.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
106                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())));
107         logger.info("***in updateObjectPnf getPnfName====> {} ", pnfFromAai.get().getPnfName());
108         injectionHelper.getAaiClient().update(
109                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnf.getPnfName())),
110                 aaiObjectMapper.mapPnf((pnf)));
111     }
112
113     /**
114      * Check inMaint flag value of PNF from AAI using pnfName
115      *
116      * @param pnfName - pnf-id required pnf
117      * @return inMaint flag value
118      */
119     public boolean checkInMaintFlag(String pnfName) {
120         org.onap.aai.domain.yang.Pnf pnf = injectionHelper.getAaiClient()
121                 .get(org.onap.aai.domain.yang.Pnf.class,
122                         AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.network().pnf(pnfName)))
123                 .orElse(new org.onap.aai.domain.yang.Pnf());
124         return pnf.isInMaint();
125     }
126
127     private void updatePnfFields(Pnf pnf, org.onap.aai.domain.yang.Pnf pnfFromAai) {
128         if (pnf.getModelInfoPnf() != null
129                 && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelCustomizationUuid())) {
130             pnfFromAai.setModelCustomizationId(pnf.getModelInfoPnf().getModelCustomizationUuid());
131         }
132         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelInvariantUuid())) {
133             pnfFromAai.setModelInvariantId(pnf.getModelInfoPnf().getModelInvariantUuid());
134         }
135         if (pnf.getModelInfoPnf() != null && StringUtils.isNotBlank(pnf.getModelInfoPnf().getModelUuid())) {
136             pnfFromAai.setModelVersionId(pnf.getModelInfoPnf().getModelUuid());
137         }
138         if (pnf.getOrchestrationStatus() != null && StringUtils.isNotBlank(pnf.getOrchestrationStatus().toString())) {
139             pnfFromAai.setOrchestrationStatus(pnf.getOrchestrationStatus().toString());
140         }
141         if (StringUtils.isNotBlank(pnf.getRole())) {
142             pnfFromAai.setNfRole(pnf.getRole());
143         }
144     }
145
146     private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
147         isRelatedToService(pnfFromAai);
148         if (isOrchestrationStatusSet(pnfFromAai)) {
149             checkOrchestrationStatusOfExistingPnf(pnfFromAai);
150         }
151     }
152
153     private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
154         if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
155             logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
156                     pnfFromAai.getPnfName());
157             return false;
158         }
159         return true;
160     }
161
162     private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
163         if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
164             logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
165                     pnfFromAai.getPnfName());
166         } else {
167             String errorMessage = String.format(
168                     "pnf with name %s already exists with orchestration status %s, existing pnf can be used only "
169                             + "if status is not set or set as Inventoried",
170                     pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
171             logger.error(errorMessage);
172             throw new Exception(errorMessage);
173         }
174     }
175
176     private void isRelatedToService(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
177         if (pnfFromAai.getRelationshipList() != null) {
178             for (Relationship relationship : pnfFromAai.getRelationshipList().getRelationship()) {
179                 if (relationship.getRelatedTo().equals("service-instance")) {
180                     String errorMessage = prepareRelationErrorMessage(pnfFromAai, relationship);
181                     logger.error(errorMessage);
182                     throw new Exception(errorMessage);
183                 }
184             }
185         }
186     }
187
188     private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
189         String serviceInstanceName = "";
190         String serviceInstanceId = "";
191
192         for (RelationshipData relationshipData : relationship.getRelationshipData()) {
193             if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
194                 serviceInstanceId = relationshipData.getRelationshipValue();
195                 break;
196             }
197         }
198         for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
199             if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
200                 serviceInstanceName = relatedToProperty.getPropertyValue();
201                 break;
202             }
203         }
204         return String.format(
205                 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
206                 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus(), serviceInstanceName, serviceInstanceId);
207     }
208 }