summaryrefslogtreecommitdiffstats
path: root/nokiav2/driver/src/test/java/org/onap/vfc/nfvo/driver/vnfm/svnfm/nokia/onap/direct/notification/TestAAINotificationProcessor.java
blob: d183e38eba3ebe9a9d3b4ddbdc4eb8d04505a3d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
 * Copyright 2016-2017, Nokia Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;

import com.google.gson.JsonObject;
import com.nokia.cbam.lcm.v32.model.*;
import java.util.ArrayList;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedConnectionPoints;
import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.notification.ReportedAffectedCp;

import static java.util.Optional.empty;
import static java.util.Optional.of;

import static com.nokia.cbam.lcm.v32.model.ChangeType.*;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.springframework.test.util.ReflectionTestUtils.setField;

public class TestAAINotificationProcessor extends TestBase {
    @Mock
    private GenericVnfManager genericVnfManager;
    @Mock
    private L3NetworkManager l3NetworkManager;
    @Mock
    private LInterfaceManager lInterfaceManager;
    @Mock
    private VnfcManager vnfcManager;
    @Mock
    private VserverManager vserverManager;
    private AAINotificationProcessor aaiNotificationProcessor;

    @Before
    public void init() {
        aaiNotificationProcessor = new AAINotificationProcessor(genericVnfManager, l3NetworkManager, lInterfaceManager, vnfcManager, vserverManager);
        setField(AAINotificationProcessor.class, "logger", logger);
    }

    /**
     * test objects are manipulated in correct order in AAA
     * (other is dependency between the objects though relations)
     */
    @Test
    public void testObjectManipulationOrder() throws Exception {
        VnfLifecycleChangeNotification recievenNotification = new VnfLifecycleChangeNotification();
        recievenNotification.setAffectedVnfcs(new ArrayList<>());
        recievenNotification.setAffectedVirtualLinks(new ArrayList<>());
        ArrayList<AffectedVirtualStorage> affectedVirtualStorages = new ArrayList<>();
        recievenNotification.setAffectedVirtualStorages(affectedVirtualStorages);
        recievenNotification.setVnfInstanceId(VNF_ID);
        recievenNotification.setStatus(OperationStatus.STARTED);
        AffectedVirtualLink addedLink = buildVl(recievenNotification, ADDED);
        AffectedVirtualLink removedLink = buildVl(recievenNotification, REMOVED);
        AffectedVirtualLink modifiedLink = buildVl(recievenNotification, MODIFIED);
        AffectedVnfc addedVnfc = buildVnfc(recievenNotification, ADDED, "tenantId1");
        AffectedVnfc removedVnfc = buildVnfc(recievenNotification, REMOVED, "tenantId2");
        AffectedVnfc modifedVnfc = buildVnfc(recievenNotification, MODIFIED, "tenantId3");
        boolean inMaintenance = true;
        ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints();

        ReportedAffectedCp removedCp = buildCp();
        removedCp.setServerProviderId("serverId");
        affectedConnectionPoints.getPre().add(removedCp);

        ReportedAffectedCp removedCpWithoutServer = buildCp();
        removedCpWithoutServer.setServerProviderId(null);
        affectedConnectionPoints.getPre().add(removedCpWithoutServer);

        ReportedAffectedCp addedCp = buildCp();
        addedCp.setServerProviderId("serverId");
        affectedConnectionPoints.getPost().add(addedCp);
        //when
        aaiNotificationProcessor.processNotification(recievenNotification, null, of(affectedConnectionPoints), VIM_ID);
        //verify
        InOrder inOrder = Mockito.inOrder(genericVnfManager, l3NetworkManager, lInterfaceManager, vnfcManager, vserverManager);
        inOrder.verify(l3NetworkManager).update(VIM_ID, VNF_ID, addedLink);
        inOrder.verify(l3NetworkManager).update(VIM_ID, VNF_ID, modifiedLink);
        inOrder.verify(vserverManager).update(VIM_ID, VNF_ID, addedVnfc, affectedVirtualStorages, inMaintenance);
        inOrder.verify(vnfcManager).update(VIM_ID, "tenantId1", VNF_ID, addedVnfc, inMaintenance);
        inOrder.verify(vserverManager).update(VIM_ID, VNF_ID, modifedVnfc, affectedVirtualStorages, inMaintenance);
        inOrder.verify(vnfcManager).update(VIM_ID, "tenantId3", VNF_ID, modifedVnfc, inMaintenance);
        inOrder.verify(lInterfaceManager).delete(VIM_ID, removedCp);
        inOrder.verify(lInterfaceManager).update(VNF_ID, VIM_ID, addedCp, inMaintenance);
        inOrder.verify(vnfcManager).delete(VNF_ID, removedVnfc);
        inOrder.verify(vserverManager).delete(VIM_ID, removedVnfc);
        inOrder.verify(l3NetworkManager).delete(VNF_ID, removedLink);
        verify(lInterfaceManager, never()).update(VNF_ID, VIM_ID, removedCpWithoutServer, inMaintenance);
        verify(lInterfaceManager, never()).delete(VIM_ID, removedCpWithoutServer);
    }

    /**
     * - unchanged CP is updated
     * - changed CP is updated
     */
    @Test
    public void testCps() throws Exception {
        VnfLifecycleChangeNotification recievenNotification = new VnfLifecycleChangeNotification();
        recievenNotification.setAffectedVnfcs(new ArrayList<>());
        recievenNotification.setAffectedVirtualLinks(new ArrayList<>());
        recievenNotification.setVnfInstanceId(VNF_ID);
        recievenNotification.setStatus(OperationStatus.STARTED);
        ArrayList<AffectedVirtualStorage> affectedVirtualStorages = new ArrayList<>();
        recievenNotification.setAffectedVirtualStorages(affectedVirtualStorages);
        boolean inMaintenance = true;
        ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints();

        ReportedAffectedCp unchangedCp = buildCp();
        unchangedCp.setCpId("unchanged");
        affectedConnectionPoints.getPre().add(unchangedCp);
        affectedConnectionPoints.getPost().add(unchangedCp);

        ReportedAffectedCp changedCpBefore = buildCp();
        changedCpBefore.setCpId("changedBefore");
        ReportedAffectedCp changedCpAfter = buildCp();
        changedCpAfter.setCpId("changedAfter");
        changedCpAfter.setCpId(changedCpBefore.getCpId());
        affectedConnectionPoints.getPre().add(changedCpBefore);
        affectedConnectionPoints.getPost().add(changedCpAfter);

        //when
        aaiNotificationProcessor.processNotification(recievenNotification, null, of(affectedConnectionPoints), VIM_ID);
        //verify
        verify(lInterfaceManager).update(VNF_ID, VIM_ID, unchangedCp, inMaintenance);
        verify(lInterfaceManager, never()).update(VNF_ID, VIM_ID, changedCpBefore, inMaintenance);
        verify(lInterfaceManager).update(VNF_ID, VIM_ID, changedCpAfter, inMaintenance);
    }

    /**
     * the end notification calls resource managers with not in maintenance state
     */
    @Test
    public void testEndNotification() throws Exception {
        VnfLifecycleChangeNotification recievenNotification = new VnfLifecycleChangeNotification();
        recievenNotification.setAffectedVnfcs(new ArrayList<>());
        recievenNotification.setAffectedVirtualLinks(new ArrayList<>());
        ArrayList<AffectedVirtualStorage> affectedVirtualStorages = new ArrayList<>();
        recievenNotification.setAffectedVirtualStorages(affectedVirtualStorages);
        recievenNotification.setVnfInstanceId(VNF_ID);
        recievenNotification.setStatus(OperationStatus.FINISHED);
        AffectedVirtualLink addedLink = buildVl(recievenNotification, ADDED);
        AffectedVirtualLink removedLink = buildVl(recievenNotification, REMOVED);
        AffectedVirtualLink modifiedLink = buildVl(recievenNotification, MODIFIED);
        AffectedVnfc addedVnfc = buildVnfc(recievenNotification, ADDED, "tenantId1");
        AffectedVnfc removedVnfc = buildVnfc(recievenNotification, REMOVED, "tenantId2");
        AffectedVnfc modifedVnfc = buildVnfc(recievenNotification, MODIFIED, "tenantId3");
        boolean inMaintenance = false;
        ReportedAffectedConnectionPoints affectedConnectionPoints = new ReportedAffectedConnectionPoints();

        ReportedAffectedCp removedCp = buildCp();
        removedCp.setServerProviderId("serverId");
        affectedConnectionPoints.getPre().add(removedCp);

        ReportedAffectedCp removedCpWithoutServer = buildCp();
        removedCpWithoutServer.setServerProviderId(null);
        affectedConnectionPoints.getPre().add(removedCpWithoutServer);

        ReportedAffectedCp addedCp = buildCp();
        addedCp.setServerProviderId("serverId");
        affectedConnectionPoints.getPost().add(addedCp);

        ReportedAffectedCp cpWithoutServer = buildCp();
        cpWithoutServer.setServerProviderId(null);
        affectedConnectionPoints.getPost().add(cpWithoutServer);

        //when
        aaiNotificationProcessor.processNotification(recievenNotification, null, of(affectedConnectionPoints), VIM_ID);
        //verify
        InOrder inOrder = Mockito.inOrder(genericVnfManager, l3NetworkManager, lInterfaceManager, vnfcManager, vserverManager);
        inOrder.verify(l3NetworkManager).update(VIM_ID, VNF_ID, addedLink);
        inOrder.verify(l3NetworkManager).update(VIM_ID, VNF_ID, modifiedLink);
        inOrder.verify(vserverManager).update(VIM_ID, VNF_ID, addedVnfc, affectedVirtualStorages, inMaintenance);
        inOrder.verify(vnfcManager).update(VIM_ID, "tenantId1", VNF_ID, addedVnfc, inMaintenance);
        inOrder.verify(vserverManager).update(VIM_ID, VNF_ID, modifedVnfc, affectedVirtualStorages, inMaintenance);
        inOrder.verify(vnfcManager).update(VIM_ID, "tenantId3", VNF_ID, modifedVnfc, inMaintenance);
        inOrder.verify(lInterfaceManager).delete(VIM_ID, removedCp);
        inOrder.verify(lInterfaceManager).update(VNF_ID, VIM_ID, addedCp, inMaintenance);
        inOrder.verify(vnfcManager).delete(VNF_ID, removedVnfc);
        inOrder.verify(vserverManager).delete(VIM_ID, removedVnfc);
        inOrder.verify(l3NetworkManager).delete(VNF_ID, removedLink);
        verify(lInterfaceManager, never()).update(VNF_ID, VIM_ID, removedCpWithoutServer, inMaintenance);
        verify(lInterfaceManager, never()).delete(VIM_ID, removedCpWithoutServer);
        verify(logger).warn("The changed {} connection point is not linked to any server", cpWithoutServer.getCpId());
    }


    /**
     * if changes connection points are not present a warning is logged
     */
    @Test
    public void testMissingChangedConnectionPoints() throws Exception {
        VnfLifecycleChangeNotification recievenNotification = new VnfLifecycleChangeNotification();
        recievenNotification.setAffectedVnfcs(new ArrayList<>());
        recievenNotification.setAffectedVirtualLinks(new ArrayList<>());
        recievenNotification.setVnfInstanceId(VNF_ID);
        //when
        aaiNotificationProcessor.processNotification(recievenNotification, null, empty(), VIM_ID);
        //verify
        verify(logger).warn("The changed connection points are not present in VNF with {} identifier", VNF_ID);
    }

    private ReportedAffectedCp buildCp() {
        ReportedAffectedCp cp = new ReportedAffectedCp();
        cp.setServerProviderId(UUID.randomUUID().toString());
        cp.setName(UUID.randomUUID().toString());
        cp.setEcpdId(UUID.randomUUID().toString());
        cp.setMacAddress(UUID.randomUUID().toString());
        cp.setNetworkProviderId(UUID.randomUUID().toString());
        cp.setCpdId(UUID.randomUUID().toString());
        cp.setCpId(UUID.randomUUID().toString());
        cp.setTenantId(UUID.randomUUID().toString());
        return cp;
    }

    private AffectedVirtualLink buildVl(VnfLifecycleChangeNotification recievenNotification, ChangeType changeType) {
        AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink();
        affectedVirtualLink.setChangeType(changeType);
        recievenNotification.getAffectedVirtualLinks().add(affectedVirtualLink);
        return affectedVirtualLink;
    }

    private AffectedVnfc buildVnfc(VnfLifecycleChangeNotification recievenNotification, ChangeType changeType, String tenantId) {
        AffectedVnfc addedVnfc = new AffectedVnfc();
        addedVnfc.setChangeType(changeType);
        JsonObject additionalData = new JsonObject();
        additionalData.addProperty("tenantId", tenantId);
        addedVnfc.setComputeResource(new ResourceHandle());
        addedVnfc.getComputeResource().setAdditionalData(additionalData);
        recievenNotification.getAffectedVnfcs().add(addedVnfc);
        return addedVnfc;
    }
}