df22cfdc260eea1497632340f14595bdf6b56e84
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.bpmn.infrastructure.namingservice.tasks;
22
23
24 import java.util.List;
25 import java.util.Optional;
26 import org.onap.aai.domain.yang.Zone;
27 import org.onap.so.bpmn.common.BuildingBlockExecution;
28 import org.onap.so.bpmn.common.InjectionHelper;
29 import org.onap.so.bpmn.servicedecomposition.bbobjects.Configuration;
30 import org.onap.so.bpmn.servicedecomposition.bbobjects.InstanceGroup;
31 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
32 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
33 import org.onap.so.bpmn.servicedecomposition.bbobjects.VpnBinding;
34 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
36 import org.onap.so.bpmn.servicedecomposition.tasks.BBInputSetupUtils;
37 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
38 import org.onap.so.client.aai.AAIObjectType;
39 import org.onap.so.client.aai.entities.AAIResultWrapper;
40 import org.onap.so.client.aai.entities.Relationships;
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
43 import org.onap.so.client.exception.ExceptionBuilder;
44 import org.onap.so.client.namingservice.NamingRequestObject;
45 import org.onap.so.client.namingservice.NamingServiceConstants;
46 import org.onap.so.client.orchestration.NamingServiceResources;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.stereotype.Component;
49
50 @Component
51 public class NamingServiceCreateTasks {
52
53     @Autowired
54     private ExceptionBuilder exceptionUtil;
55     @Autowired
56     private ExtractPojosForBB extractPojosForBB;
57
58     @Autowired
59     private NamingServiceResources namingServiceResources;
60     @Autowired
61     protected InjectionHelper injectionHelper;
62     @Autowired
63     protected BBInputSetupUtils bbInputSetupUtils;
64
65     public void setBbInputSetupUtils(BBInputSetupUtils bbInputSetupUtils) {
66         this.bbInputSetupUtils = bbInputSetupUtils;
67     }
68
69     public void createInstanceGroupName(BuildingBlockExecution execution) throws Exception {
70         InstanceGroup instanceGroup = extractPojosForBB.extractByKey(execution, ResourceKey.INSTANCE_GROUP_ID);
71         String policyInstanceName = execution.getVariable("policyInstanceName");
72         String nfNamingCode = execution.getVariable("nfNamingCode");
73         String generatedInstanceGroupName = "";
74         try {
75             generatedInstanceGroupName =
76                     namingServiceResources.generateInstanceGroupName(instanceGroup, policyInstanceName, nfNamingCode);
77         } catch (Exception ex) {
78             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
79         }
80         instanceGroup.setInstanceGroupName(generatedInstanceGroupName);
81     }
82
83     public void createWanTransportServiceName(BuildingBlockExecution execution) throws Exception {
84         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
85         NamingRequestObject namingRequestObject = new NamingRequestObject();
86         namingRequestObject.setExternalKeyValue(serviceInstance.getServiceInstanceId());
87         namingRequestObject.setNamingTypeValue(NamingServiceConstants.NAMING_TYPE_SERVICE);
88         namingRequestObject.setResourceNameValue(NamingServiceConstants.RESOURCE_NAME_SERVICE_INSTANCE_NAME);
89         namingRequestObject.setPolicyInstanceNameValue(serviceInstance.getModelInfoServiceInstance().getNamingPolicy());
90         namingRequestObject.setServiceModelNameValue(serviceInstance.getModelInfoServiceInstance().getModelName());
91         namingRequestObject.setModelVersionValue(serviceInstance.getModelInfoServiceInstance().getModelVersion());
92         namingRequestObject.setZoneIdValue(this.getRelatedZoneId(execution));
93
94         String generatedWanTransportServiceName = "";
95         try {
96             generatedWanTransportServiceName = namingServiceResources.generateServiceInstanceName(namingRequestObject);
97         } catch (Exception ex) {
98             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
99         }
100         serviceInstance.setServiceInstanceName(generatedWanTransportServiceName);
101     }
102
103     public void createVpnBondingServiceName(BuildingBlockExecution execution) throws Exception {
104         ServiceInstance serviceInstance = extractPojosForBB.extractByKey(execution, ResourceKey.SERVICE_INSTANCE_ID);
105         L3Network network = extractPojosForBB.extractByKey(execution, ResourceKey.NETWORK_ID);
106         VpnBinding vpnBinding = extractPojosForBB.extractByKey(execution, ResourceKey.VPN_ID);
107         NamingRequestObject namingRequestObject = new NamingRequestObject();
108         namingRequestObject.setExternalKeyValue(serviceInstance.getServiceInstanceId());
109         namingRequestObject.setPolicyInstanceNameValue(serviceInstance.getModelInfoServiceInstance().getNamingPolicy());
110         namingRequestObject.setNamingTypeValue(NamingServiceConstants.NAMING_TYPE_SERVICE);
111         namingRequestObject.setServiceModelNameValue(serviceInstance.getModelInfoServiceInstance().getModelName());
112         namingRequestObject.setModelVersionValue(serviceInstance.getModelInfoServiceInstance().getModelVersion());
113         namingRequestObject.setNetworkNameValue(execution.getVariable(network.getNetworkName()));
114         namingRequestObject.setVpnNameValue(vpnBinding.getVpnName());
115         namingRequestObject.setResourceNameValue(NamingServiceConstants.RESOURCE_NAME_SERVICE_INSTANCE_NAME);
116
117         String generatedVpnBondingServiceName = "";
118         try {
119             generatedVpnBondingServiceName = namingServiceResources.generateServiceInstanceName(namingRequestObject);
120         } catch (Exception ex) {
121             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
122         }
123         serviceInstance.setServiceInstanceName(generatedVpnBondingServiceName);
124     }
125
126     protected String getRelatedZoneId(BuildingBlockExecution execution) {
127         GeneralBuildingBlock gBBInput = execution.getGeneralBuildingBlock();
128         String cloudRegionId = gBBInput.getCloudRegion().getLcpCloudRegionId();
129         String cloudOwner = gBBInput.getCloudRegion().getCloudOwner();
130         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, cloudOwner, cloudRegionId);
131         AAIResultWrapper vnfcResultsWrapper = bbInputSetupUtils.getAAIResourceDepthOne(uri);
132         Optional<Relationships> relationshipsOp = this.getRelationshipsFromWrapper(vnfcResultsWrapper);
133         Zone aaiZone = getRelatedZone(relationshipsOp);
134         if (aaiZone == null) {
135             String errMsg = "No relationships for Cloud Region in AAI to extract zone id";
136             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, errMsg);
137         }
138         return aaiZone.getZoneId();
139     }
140
141     protected Optional<Relationships> getRelationshipsFromWrapper(AAIResultWrapper wrapper) {
142         Optional<Relationships> relationshipsOp;
143         relationshipsOp = wrapper.getRelationships();
144         if (relationshipsOp.isPresent()) {
145             return relationshipsOp;
146         }
147         return Optional.empty();
148     }
149
150     protected org.onap.aai.domain.yang.Zone getRelatedZone(Optional<Relationships> relationshipsOp) {
151         Zone aaiZone = null;
152         if (relationshipsOp.isPresent()) {
153             Relationships relationships = relationshipsOp.get();
154             if (!relationships.getAll().isEmpty()) {
155                 List<AAIResultWrapper> zones = relationships.getByType(AAIObjectType.ZONE);
156                 AAIResultWrapper zoneWrapper = zones.get(0);
157                 Optional<Zone> aaiZoneOp = zoneWrapper.asBean(Zone.class);
158                 if (aaiZoneOp.isPresent()) {
159                     aaiZone = aaiZoneOp.get();
160                 }
161             }
162         }
163         return aaiZone;
164     }
165 }