373c5dd0160a4ee6f3fb649c142a689fc16490cd
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / TestL3NetworkManager.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.google.gson.JsonObject;
19 import com.nokia.cbam.lcm.v32.model.AffectedVirtualLink;
20 import com.nokia.cbam.lcm.v32.model.ResourceHandle;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Mock;
25 import org.onap.aai.domain.yang.v11.L3Network;
26 import org.onap.aai.domain.yang.v11.ObjectFactory;
27 import org.onap.aai.domain.yang.v11.RelationshipList;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
30
31 import java.util.NoSuchElementException;
32
33 import static junit.framework.TestCase.assertEquals;
34 import static junit.framework.TestCase.assertFalse;
35 import static org.mockito.Matchers.eq;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider.AAIService.NETWORK;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.AbstractManager.buildRelationshipData;
40 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.TestGenericVnfManager.assertRelation;
41 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
42 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
43 import static org.springframework.test.util.ReflectionTestUtils.setField;
44
45 public class TestL3NetworkManager extends TestBase {
46     private ObjectFactory OBJECT_FACTORY = new ObjectFactory();
47     private ArgumentCaptor<L3Network> payload = ArgumentCaptor.forClass(L3Network.class);
48     private AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink();
49     @Mock
50     private AAIRestApiProvider aaiRestApiProvider;
51     private L3NetworkManager l3NetworkManager;
52
53     @Before
54     public void init() {
55         l3NetworkManager = new L3NetworkManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
56         setField(L3NetworkManager.class, "logger", logger);
57     }
58
59     /**
60      * test L3 network creation
61      */
62     @Test
63     public void testUpdate() throws Exception {
64         affectedVirtualLink.setId("vlId");
65         JsonObject additionalData = new JsonObject();
66         additionalData.addProperty("name", "netName");
67         additionalData.addProperty("tenantId", "myTenantId");
68         affectedVirtualLink.setResource(new ResourceHandle());
69         affectedVirtualLink.getResource().setAdditionalData(additionalData);
70         affectedVirtualLink.getResource().setResourceId("netProviderId");
71         when(aaiRestApiProvider.get(logger, NETWORK, "/l3-networks/l3-network/myVnfId_vlId", L3Network.class)).thenThrow(new NoSuchElementException());
72         when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/l3-networks/l3-network/myVnfId_vlId"), payload.capture(), eq(Void.class))).thenReturn(null);
73         //when
74         l3NetworkManager.update(VIM_ID, VNF_ID, affectedVirtualLink);
75         //verify
76         assertEquals("myVnfId_vlId", payload.getValue().getNetworkId());
77         assertEquals("netName", payload.getValue().getNetworkName());
78         assertEquals("netProviderId", payload.getValue().getNeutronNetworkId());
79         assertFalse(payload.getValue().isIsBoundToVpn());
80         assertFalse(payload.getValue().isIsProviderNetwork());
81         assertFalse(payload.getValue().isIsSharedNetwork());
82         assertFalse(payload.getValue().isIsExternalNetwork());
83         assertEquals("active", payload.getValue().getOperationalStatus());
84         assertRelation(payload.getValue().getRelationshipList(), "cloud-region", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)));
85         assertRelation(payload.getValue().getRelationshipList(), "tenant", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)), buildRelationshipData("tenant.tenant-id", "myTenantId"));
86         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
87     }
88
89     /**
90      * Test existing resource update
91      */
92     @Test
93     public void testExistingUpdate() throws Exception {
94         affectedVirtualLink.setId("vlId");
95         JsonObject additionalData = new JsonObject();
96         additionalData.addProperty("name", "netName");
97         additionalData.addProperty("tenantId", "myTenantId");
98         affectedVirtualLink.setResource(new ResourceHandle());
99         affectedVirtualLink.getResource().setAdditionalData(additionalData);
100         affectedVirtualLink.getResource().setResourceId("netProviderId");
101         L3Network l3Network = OBJECT_FACTORY.createL3Network();
102         l3Network.setResourceVersion("v3");
103         l3Network.setRelationshipList(new RelationshipList());
104         when(aaiRestApiProvider.get(logger, NETWORK, "/l3-networks/l3-network/myVnfId_vlId", L3Network.class)).thenReturn(l3Network);
105         when(aaiRestApiProvider.put(eq(logger), eq(NETWORK), eq("/l3-networks/l3-network/myVnfId_vlId"), payload.capture(), eq(Void.class))).thenReturn(null);
106         //when
107         l3NetworkManager.update(VIM_ID, VNF_ID, affectedVirtualLink);
108         //verify
109         assertEquals("myVnfId_vlId", payload.getValue().getNetworkId());
110         assertEquals("netName", payload.getValue().getNetworkName());
111         assertEquals("netProviderId", payload.getValue().getNeutronNetworkId());
112         assertFalse(payload.getValue().isIsBoundToVpn());
113         assertFalse(payload.getValue().isIsProviderNetwork());
114         assertFalse(payload.getValue().isIsSharedNetwork());
115         assertFalse(payload.getValue().isIsExternalNetwork());
116         assertEquals("active", payload.getValue().getOperationalStatus());
117         assertEquals("v3", payload.getValue().getResourceVersion());
118         assertRelation(payload.getValue().getRelationshipList(), "cloud-region", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)));
119         assertRelation(payload.getValue().getRelationshipList(), "tenant", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)), buildRelationshipData("tenant.tenant-id", "myTenantId"));
120         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
121     }
122
123     /**
124      * test L3 network deletion
125      */
126     @Test
127     public void testDelete() throws Exception {
128         affectedVirtualLink.setId("vlId");
129         //when
130         l3NetworkManager.delete(VNF_ID, affectedVirtualLink);
131         //verify
132         verify(aaiRestApiProvider).delete(logger, NETWORK, "/l3-networks/l3-network/myVnfId_vlId");
133     }
134
135     /**
136      * test inheritence
137      */
138     @Test
139     public void testInheritence() {
140         assertEquals(logger, l3NetworkManager.getLogger());
141     }
142
143
144 }