ab216bd75c662a67b5c275c02f5a898c6237cb23
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / LInterfaceManager.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 io.reactivex.Observable;
19 import java.util.ArrayList;
20 import org.onap.aai.model.L3InterfaceIpv4AddressList;
21 import org.onap.aai.model.L3InterfaceIpv6AddressList;
22 import org.onap.aai.model.LInterface;
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.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;
28 import org.slf4j.Logger;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Conditional;
31 import org.springframework.stereotype.Component;
32
33 import static java.lang.String.format;
34
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
37
38 /**
39  * Responsible for managing the {@link LInterface} in AAI
40  */
41 @Component
42 @Conditional(value = Conditions.UseForDirect.class)
43 class LInterfaceManager extends AbstractManager {
44     private static Logger logger = org.slf4j.LoggerFactory.getLogger(LInterfaceManager.class);
45
46     @Autowired
47     LInterfaceManager(AAIRestApiProvider aaiRestApiProvider, CbamRestApiProvider cbamRestApiProvider, DriverProperties driverProperties) {
48         super(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
49     }
50
51     static String buildUrl(String vimId, ReportedAffectedCp affectedCp) {
52         String cloudOwner = getCloudOwner(vimId);
53         String regionName = getRegionName(vimId);
54         String tenantId = affectedCp.getTenantId();
55         String vServerId = affectedCp.getServerProviderId();
56         String cpId = affectedCp.getCpId();
57         return format("/cloud-regions/cloud-region/%s/%s/tenants/tenant/%s/vservers/vserver/%s/l-interfaces/l-interface/%s", cloudOwner, regionName, tenantId, vServerId, cpId);
58     }
59
60     @Override
61     protected Logger getLogger() {
62         return logger;
63     }
64
65     void update(String vnfId, String vimId, ReportedAffectedCp affectedCp, boolean inMaintenance) {
66         LInterface lInterface = createOrGet(getLinterface(vimId, affectedCp), new LInterface());
67         updateFields(vimId, lInterface, affectedCp, vnfId, inMaintenance);
68     }
69
70     void delete(String vimId, ReportedAffectedCp removedCp) {
71         LInterface linterface = getLinterface(vimId, removedCp).blockingFirst();
72         String cloudOwner = getCloudOwner(vimId);
73         String regionName = getRegionName(vimId);
74         String tenantId = removedCp.getTenantId();
75         String vServerId = removedCp.getServerProviderId();
76         String cpId = removedCp.getCpId();
77         aaiRestApiProvider.getCloudInfrastructureApi().deleteCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserverLInterfacesLInterface(cloudOwner, regionName, tenantId, vServerId, cpId, linterface.getResourceVersion());
78     }
79
80     private Observable<LInterface> getLinterface(String vimId, ReportedAffectedCp cp) {
81         String cloudOwner = getCloudOwner(vimId);
82         String regionName = getRegionName(vimId);
83         String tenantId = cp.getTenantId();
84         String vServerId = cp.getServerProviderId();
85         String cpId = cp.getCpId();
86         return aaiRestApiProvider.getCloudInfrastructureApi().getCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserverLInterfacesLInterface(cloudOwner, regionName, tenantId, vServerId, cpId, null, null, null, null, null, null, null, null, null, null, null, null);
87     }
88
89     private void updateFields(String vimId, LInterface logicalInterface, ReportedAffectedCp affectedCp, String vnfId, boolean inMaintenance) {
90         logicalInterface.setInMaint(inMaintenance);
91         logicalInterface.setIsIpUnnumbered(false);
92         logicalInterface.setIsPortMirrored(false);
93         logicalInterface.setInterfaceName(affectedCp.getName());
94         logicalInterface.setInterfaceId(affectedCp.getCpId());
95         logicalInterface.setInterfaceRole(affectedCp.getCpdId());
96         logicalInterface.setMacaddr(affectedCp.getMacAddress());
97         logicalInterface.setProvStatus("active");
98         if (affectedCp.getIpAddress() != null) {
99             if (affectedCp.getIpAddress().contains(":")) {
100                 L3InterfaceIpv6AddressList ipv6Address = new L3InterfaceIpv6AddressList();
101                 ipv6Address.setL3InterfaceIpv6Address(affectedCp.getIpAddress());
102                 ipv6Address.setNeutronNetworkId(affectedCp.getNetworkProviderId());
103                 if (logicalInterface.getL3InterfaceIpv6AddressList() == null) {
104                     logicalInterface.setL3InterfaceIpv6AddressList(new ArrayList<>());
105                 }
106                 logicalInterface.getL3InterfaceIpv6AddressList().add(ipv6Address);
107             } else {
108                 L3InterfaceIpv4AddressList ipv4Address = new L3InterfaceIpv4AddressList();
109                 ipv4Address.setL3InterfaceIpv4Address(affectedCp.getIpAddress());
110                 ipv4Address.setNeutronNetworkId(affectedCp.getNetworkProviderId());
111                 if (logicalInterface.getL3InterfaceIpv4AddressList() == null) {
112                     logicalInterface.setL3InterfaceIpv4AddressList(new ArrayList<>());
113                 }
114                 logicalInterface.getL3InterfaceIpv4AddressList().add(ipv4Address);
115             }
116         }
117         if (logicalInterface.getRelationshipList() == null) {
118             logicalInterface.setRelationshipList(new ArrayList<>());
119         }
120         addSingletonRelation(logicalInterface.getRelationshipList(), GenericVnfManager.linkTo(vnfId));
121         String cloudOwner = getCloudOwner(vimId);
122         String regionName = getRegionName(vimId);
123         String tenantId = affectedCp.getTenantId();
124         String vServerId = affectedCp.getServerProviderId();
125         String cpId = affectedCp.getCpId();
126         aaiRestApiProvider.getCloudInfrastructureApi().createOrUpdateCloudInfrastructureCloudRegionsCloudRegionTenantsTenantVserversVserverLInterfacesLInterface(cloudOwner, regionName, tenantId, vServerId, cpId, logicalInterface).blockingFirst();
127     }
128 }