Merge "Add Spring Component markup to rest validator"
[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.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.so.bpmn.common.InjectionHelper;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
31 import org.onap.aaiclient.client.aai.AAIObjectType;
32 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
33 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
34 import org.onap.so.client.aai.mapper.AAIObjectMapper;
35 import org.onap.so.db.catalog.beans.OrchestrationStatus;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.stereotype.Component;
40
41 @Component
42 public class AAIPnfResources {
43
44     private static final Logger logger = LoggerFactory.getLogger(AAIPnfResources.class);
45
46     @Autowired
47     private InjectionHelper injectionHelper;
48
49     @Autowired
50     private AAIObjectMapper aaiObjectMapper;
51
52     public void createPnfAndConnectServiceInstance(Pnf pnf, ServiceInstance serviceInstance) {
53         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
54         pnf.setOrchestrationStatus(OrchestrationStatus.INVENTORIED);
55         AAIResourceUri serviceInstanceURI =
56                 AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstance.getServiceInstanceId());
57         injectionHelper.getAaiClient().createIfNotExists(pnfURI, Optional.of(aaiObjectMapper.mapPnf(pnf)))
58                 .connect(pnfURI, serviceInstanceURI);
59     }
60
61     public void updateOrchestrationStatusPnf(Pnf pnf, OrchestrationStatus orchestrationStatus) {
62         AAIResourceUri pnfURI = AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnf.getPnfName());
63
64         Pnf pnfCopy = pnf.shallowCopyId();
65
66         pnf.setOrchestrationStatus(orchestrationStatus);
67         pnfCopy.setOrchestrationStatus(orchestrationStatus);
68         injectionHelper.getAaiClient().update(pnfURI, aaiObjectMapper.mapPnf(pnfCopy));
69     }
70
71     public void checkIfPnfExistsInAaiAndCanBeUsed(String pnfName) throws Exception {
72         Optional<org.onap.aai.domain.yang.Pnf> pnfFromAai = injectionHelper.getAaiClient()
73                 .get(org.onap.aai.domain.yang.Pnf.class, AAIUriFactory.createResourceUri(AAIObjectType.PNF, pnfName));
74         if (pnfFromAai.isPresent()) {
75             checkIfPnfCanBeUsed(pnfFromAai.get());
76         }
77     }
78
79     private void checkIfPnfCanBeUsed(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
80         isRelatedToService(pnfFromAai);
81         if (isOrchestrationStatusSet(pnfFromAai)) {
82             checkOrchestrationStatusOfExistingPnf(pnfFromAai);
83         }
84     }
85
86     private boolean isOrchestrationStatusSet(org.onap.aai.domain.yang.Pnf pnfFromAai) {
87         if (Strings.isNullOrEmpty(pnfFromAai.getOrchestrationStatus())) {
88             logger.debug("pnf with name {} already exists with not set orchestration status and can be used",
89                     pnfFromAai.getPnfName());
90             return false;
91         }
92         return true;
93     }
94
95     private void checkOrchestrationStatusOfExistingPnf(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
96         if (OrchestrationStatus.INVENTORIED.toString().equals(pnfFromAai.getOrchestrationStatus())) {
97             logger.debug("pnf with name {} already exists with orchestration status Inventoried and can be used",
98                     pnfFromAai.getPnfName());
99         } else {
100             String errorMessage = String.format(
101                     "pnf with name %s already exists with orchestration status %s, only status Inventoried allows to use existing pnf",
102                     pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus());
103             logger.error(errorMessage);
104             throw new Exception(errorMessage);
105         }
106     }
107
108     private void isRelatedToService(org.onap.aai.domain.yang.Pnf pnfFromAai) throws Exception {
109         if (pnfFromAai.getRelationshipList() != null) {
110             for (Relationship relationship : pnfFromAai.getRelationshipList().getRelationship()) {
111                 if (relationship.getRelatedTo().equals("service-instance")) {
112                     String errorMessage = prepareRelationErrorMessage(pnfFromAai, relationship);
113                     logger.error(errorMessage);
114                     throw new Exception(errorMessage);
115                 }
116             }
117         }
118     }
119
120     private String prepareRelationErrorMessage(org.onap.aai.domain.yang.Pnf pnfFromAai, Relationship relationship) {
121         String serviceInstanceName = "";
122         String serviceInstanceId = "";
123
124         for (RelationshipData relationshipData : relationship.getRelationshipData()) {
125             if (relationshipData.getRelationshipKey().equals("service-instance.service-instance-id")) {
126                 serviceInstanceId = relationshipData.getRelationshipValue();
127                 break;
128             }
129         }
130         for (RelatedToProperty relatedToProperty : relationship.getRelatedToProperty()) {
131             if (relatedToProperty.getPropertyKey().equals("service-instance.service-instance-name")) {
132                 serviceInstanceName = relatedToProperty.getPropertyValue();
133                 break;
134             }
135         }
136         return String.format(
137                 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
138                 pnfFromAai.getPnfName(), pnfFromAai.getOrchestrationStatus(), serviceInstanceName, serviceInstanceId);
139     }
140 }