Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / L3NetworkManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.nokia.cbam.lcm.v32.model.AffectedVirtualLink;
19 import io.reactivex.Observable;
20 import java.util.ArrayList;
21 import org.onap.aai.model.L3Network;
22 import org.onap.aai.model.Relationship;
23 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
24 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.spring.Conditions;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamRestApiProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties;
27 import org.slf4j.Logger;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.annotation.Conditional;
30 import org.springframework.stereotype.Component;
31
32 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.SEPARATOR;
33 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
35
36 /**
37  * Responsible for managing the {@link L3Network} in AAI
38  */
39 @Component
40 @Conditional(value = Conditions.UseForDirect.class)
41 class L3NetworkManager extends AbstractManager {
42     private static Logger logger = org.slf4j.LoggerFactory.getLogger(L3NetworkManager.class);
43
44     @Autowired
45     L3NetworkManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
46         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
47     }
48
49     @Override
50     protected Logger getLogger() {
51         return logger;
52     }
53
54     void update(String vimId, String vnfId, AffectedVirtualLink affectedVirtualLink) {
55         L3Network l3Network = createOrGet(getNetwork(vnfId, affectedVirtualLink), new L3Network());
56         updateNetworkFields(vimId, vnfId, affectedVirtualLink, l3Network);
57     }
58
59     private Observable<L3Network> getNetwork(String vnfId, AffectedVirtualLink affectedVirtualLink) {
60         return aaiRestApiProvider.getNetworkApi().getNetworkL3NetworksL3Network(buildNetworkId(vnfId, affectedVirtualLink), null, null, null, null, null, null, null, null, null);
61     }
62
63     void delete(String vnfId, AffectedVirtualLink removedVl) {
64         L3Network l3Network = getNetwork(vnfId, removedVl).blockingFirst();
65         aaiRestApiProvider.getNetworkApi().deleteNetworkL3NetworksL3Network(l3Network.getNetworkId(), l3Network.getResourceVersion()).blockingFirst();
66     }
67
68     private void updateNetworkFields(String vimId, String vnfId, AffectedVirtualLink affectedVirtualLink, L3Network network) {
69         network.setNetworkId(buildNetworkId(vnfId, affectedVirtualLink));
70         network.setNetworkName(extractMandatoryValue(affectedVirtualLink.getResource().getAdditionalData(), "name"));
71         network.setNeutronNetworkId(affectedVirtualLink.getResource().getResourceId());
72         network.setIsBoundToVpn(false);
73         network.setIsExternalNetwork(false);
74         network.setIsProviderNetwork(false);
75         network.setIsSharedNetwork(false);
76         network.setOperationalStatus("active");
77         network.setOrchestrationStatus("active");
78         if (network.getRelationshipList() == null) {
79             network.setRelationshipList(new ArrayList<>());
80         }
81         addMissingRelation(network.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
82         addSingletonRelation(network.getRelationshipList(), getRegionLink(vimId));
83         addSingletonRelation(network.getRelationshipList(), getTenantLink(vimId, extractMandatoryValue(affectedVirtualLink.getResource().getAdditionalData(), "tenantId")));
84         aaiRestApiProvider.getNetworkApi().createOrUpdateNetworkL3NetworksL3Network(network.getNetworkId(), network).blockingFirst();
85     }
86
87     private String buildNetworkId(String vnfId, AffectedVirtualLink affectedVirtualLink) {
88         return vnfId + SEPARATOR + affectedVirtualLink.getId();
89     }
90
91     private Relationship getRegionLink(String vimId) {
92         Relationship relationship = new Relationship();
93         relationship.setRelatedTo("cloud-region");
94         relationship.setRelationshipData(new ArrayList<>());
95         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(vimId)));
96         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-region-id", getRegionName(vimId)));
97         return relationship;
98     }
99
100     private Relationship getTenantLink(String vimId, String tenantId) {
101         Relationship relationship = new Relationship();
102         relationship.setRelatedTo("tenant");
103         relationship.setRelationshipData(new ArrayList<>());
104         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(vimId)));
105         relationship.getRelationshipData().add(buildRelationshipData("cloud-region.cloud-region-id", getRegionName(vimId)));
106         relationship.getRelationshipData().add(buildRelationshipData("tenant.tenant-id", tenantId));
107         return relationship;
108     }
109 }