2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Nokia. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai;
23 import com.google.common.collect.ImmutableList;
24 import org.mockito.InjectMocks;
25 import org.mockito.MockitoAnnotations;
26 import org.onap.vid.aai.util.AAITreeConverter;
27 import org.onap.vid.model.Action;
28 import org.onap.vid.model.aaiTree.AAITreeNode;
29 import org.onap.vid.model.aaiTree.Network;
30 import org.onap.vid.model.aaiTree.ServiceInstance;
31 import org.onap.vid.model.aaiTree.VfModule;
32 import org.onap.vid.model.aaiTree.Vnf;
33 import org.testng.annotations.BeforeTest;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
37 import java.util.List;
39 import static org.hamcrest.MatcherAssert.assertThat;
40 import static org.hamcrest.Matchers.emptyOrNullString;
41 import static org.hamcrest.Matchers.hasKey;
42 import static org.hamcrest.Matchers.hasSize;
43 import static org.hamcrest.Matchers.is;
44 import static org.hamcrest.Matchers.not;
45 import static org.onap.vid.asdc.parser.ToscaParserImpl2.Constants.A_LA_CARTE;
47 public class AAITreeConverterTest {
50 private AAITreeConverter aaiTreeConverter;
53 public void initMocks() {
54 MockitoAnnotations.initMocks(this);
58 public void testConvertTreeToUIModel_NoChildren() throws Exception {
60 AAITreeNode aaiTree = generateAaiTreeToConvert(0, 0);
62 ServiceInstance result = aaiTreeConverter.convertTreeToUIModel(aaiTree, "global-customer-id", "service-type", A_LA_CARTE);
64 assertService(result, 0, 0, true);
68 public void testConvertTreeToUIModel_MultipleChildren() throws Exception {
70 AAITreeNode aaiTree = generateAaiTreeToConvert(2, 2);
72 ServiceInstance serviceInstance = aaiTreeConverter.convertTreeToUIModel(aaiTree, "global-customer-id", "service-type", null);
74 assertService(serviceInstance, 2, 2, false);
77 assertThat(serviceInstance.getVnfs().entrySet(), hasSize(2));
78 assertVnf(serviceInstance.getVnfs().get("vnf-model-version-id:00"+(nodesCounter++)), 0, 0, false);
79 assertVnf(serviceInstance.getVnfs().get("vnf-model-version-id:00"+(nodesCounter++)), 0, 0, false);
81 assertThat(serviceInstance.getNetworks().entrySet(), hasSize(2));
82 assertNetwork(serviceInstance.getNetworks().get("network-model-version-id:00"+(nodesCounter++)), false);
83 assertNetwork(serviceInstance.getNetworks().get("network-model-version-id:00"+(nodesCounter)), false);
87 public static Object[][] vnfWithChildren() {
88 return new Object[][]{
89 {ImmutableList.of(), ImmutableList.of()},
90 {ImmutableList.of(true, false), ImmutableList.of()},
91 {ImmutableList.of(), ImmutableList.of(false)}
95 @Test(dataProvider = "vnfWithChildren")
96 public void testComplexVnfConversion(List<Boolean> vfModules, List<Boolean> networks) {
98 AAITreeNode vnfTreeNode = createVnf(1, false);
101 for (Boolean customizationName: vfModules) {
102 vnfTreeNode.getChildren().add(createVfModule(nodesCounter++, customizationName));
105 for (Boolean customizationName: networks) {
106 vnfTreeNode.getChildren().add(createNetwork(nodesCounter++, customizationName));
109 Vnf actualVnf = Vnf.from(vnfTreeNode);
111 assertVnf(actualVnf, vfModules.size(), networks.size(), false);
114 for (Boolean customizationName: vfModules) {
115 String key = customizationName ? "vfModule key in model" : "vfModule-model-version-id";
117 assertThat(actualVnf.getVfModules(), hasKey(key));
118 assertThat(actualVnf.getVfModules().get(key), hasKey(key + ":00" + nodesCounter));
119 VfModule actualVfModule = actualVnf.getVfModules().get(key).get(key + ":00" + nodesCounter);
120 assertVfModule(actualVfModule, customizationName);
124 for (Boolean customizationName: networks) {
125 String key = customizationName ? "network key in model" : "network-model-version-id";
127 assertThat(actualVnf.getNetworks(), hasKey(key + ":00" + nodesCounter));
128 Network actualNetwork = actualVnf.getNetworks().get(key + ":00" + nodesCounter);
129 assertNetwork(actualNetwork, customizationName);
135 public void testNetworkConversion() {
136 AAITreeNode networkTreeNode = createNetwork(1, true);
138 Network actualNetwork = Network.from(networkTreeNode);
140 assertNetwork(actualNetwork, true);
143 private AAITreeNode createVnf(int uniqueNumber, boolean hasCustomizationName) {
144 AAITreeNode vnfTreeNode = new AAITreeNode();
145 vnfTreeNode.setId("vnf-instance-id");
146 vnfTreeNode.setName("vnf-instance-name");
147 vnfTreeNode.setType("generic-vnf");
148 vnfTreeNode.setModelVersionId("vnf-model-version-id");
149 if (hasCustomizationName) {
150 vnfTreeNode.setModelCustomizationName("vnf model customization name");
151 vnfTreeNode.setKeyInModel("vnf key in model");
153 vnfTreeNode.setUniqueNumber(uniqueNumber);
157 private AAITreeNode createVfModule(int uniqueNumber, boolean hasCustomizationName) {
158 AAITreeNode vfModuleTreeNode = new AAITreeNode();
159 vfModuleTreeNode.setId("vfModule-instance-id");
160 vfModuleTreeNode.setName("vfModule-instance-name");
161 vfModuleTreeNode.setType("vf-module");
162 vfModuleTreeNode.setModelVersionId("vfModule-model-version-id");
163 if (hasCustomizationName) {
164 vfModuleTreeNode.setModelCustomizationName("vfModule model customization name");
165 vfModuleTreeNode.setKeyInModel("vfModule key in model");
167 vfModuleTreeNode.setUniqueNumber(uniqueNumber);
169 return vfModuleTreeNode;
172 private AAITreeNode createNetwork(int uniqueNumber, boolean hasCustomizationName) {
173 AAITreeNode networkTreeNode = new AAITreeNode();
174 networkTreeNode.setId("network-instance-id");
175 networkTreeNode.setName("network-instance-name");
176 networkTreeNode.setType("l3-network");
177 networkTreeNode.setModelVersionId("network-model-version-id");
178 if (hasCustomizationName) {
179 networkTreeNode.setModelCustomizationName("network model customization name");
180 networkTreeNode.setKeyInModel("network key in model");
182 networkTreeNode.setUniqueNumber(uniqueNumber);
184 return networkTreeNode;
187 private void assertService(ServiceInstance serviceInstance, int expectedVnfs, int expectedNetworks, boolean isALaCarte) {
188 assertThat(serviceInstance.getInstanceId(), is("service-instance-id"));
189 assertThat(serviceInstance.getInstanceName(), is("service-instance-name"));
190 assertThat(serviceInstance.getAction(), is(Action.None));
191 assertThat(serviceInstance.getGlobalSubscriberId(), is("global-customer-id"));
192 assertThat(serviceInstance.getSubscriptionServiceType(), is("service-type"));
193 assertThat(serviceInstance.getModelInfo().getModelType(), is("service"));
194 assertThat(serviceInstance.getVnfs().entrySet(), hasSize(expectedVnfs));
195 assertThat(serviceInstance.getNetworks().entrySet(), hasSize(expectedNetworks));
196 assertThat(serviceInstance.getIsALaCarte(), is(isALaCarte));
199 private void assertVnf(Vnf actualVnf, int expectedVfModules, int expectedNetworks, boolean hasCustomizationName) {
200 assertThat(actualVnf.getInstanceId(), is("vnf-instance-id"));
201 assertThat(actualVnf.getInstanceName(), is("vnf-instance-name"));
202 assertThat(actualVnf.getAction(), is(Action.None));
203 assertThat(actualVnf.getModelInfo().getModelType(), is("vnf"));
204 assertThat(actualVnf.getModelInfo().getModelVersionId(), is("vnf-model-version-id"));
205 assertThat(actualVnf.getVfModules().entrySet(), hasSize(expectedVfModules));
206 assertThat(actualVnf.getNetworks().entrySet(), hasSize(expectedNetworks));
207 assertThat(actualVnf.getTrackById(), is(not(emptyOrNullString())));
208 String expectedCustomizationName = hasCustomizationName ? "vnf model customization name" : null;
209 assertThat(actualVnf.getModelInfo().getModelCustomizationName(), is(expectedCustomizationName));
212 private void assertVfModule(VfModule actualVfModule, boolean hasCustomizationName) {
213 assertThat(actualVfModule.getInstanceId(), is("vfModule-instance-id"));
214 assertThat(actualVfModule.getInstanceName(), is("vfModule-instance-name"));
215 assertThat(actualVfModule.getAction(), is(Action.None));
216 assertThat(actualVfModule.getModelInfo().getModelType(), is("vfModule"));
217 assertThat(actualVfModule.getModelInfo().getModelVersionId(), is("vfModule-model-version-id"));
218 assertThat(actualVfModule.getTrackById(), is(not(emptyOrNullString())));
219 String expectedCustomizationName = hasCustomizationName ? "vfModule model customization name" : null;
220 assertThat(actualVfModule.getModelInfo().getModelCustomizationName(), is(expectedCustomizationName));
223 private void assertNetwork(Network actualNetwork, boolean hasCustomizationName) {
224 assertThat(actualNetwork.getInstanceId(), is("network-instance-id"));
225 assertThat(actualNetwork.getInstanceName(), is("network-instance-name"));
226 assertThat(actualNetwork.getAction(), is(Action.None));
227 assertThat(actualNetwork.getModelInfo().getModelType(), is("network"));
228 assertThat(actualNetwork.getModelInfo().getModelVersionId(), is("network-model-version-id"));
229 assertThat(actualNetwork.getTrackById(), is(not(emptyOrNullString())));
230 String expectedCustomizationName = hasCustomizationName ? "network model customization name" : null;
231 assertThat(actualNetwork.getModelInfo().getModelCustomizationName(), is(expectedCustomizationName));
234 private AAITreeNode generateAaiTreeToConvert(int numberOfVnfs, int numberOfNetworks) {
236 AAITreeNode aaiTree = new AAITreeNode();
237 aaiTree.setId("service-instance-id");
238 aaiTree.setName("service-instance-name");
240 for (int i = 0; i < numberOfVnfs; i++) {
241 aaiTree.getChildren().add(createVnf(counter++, false));
244 for (int i = 0; i < numberOfNetworks; i++) {
245 aaiTree.getChildren().add(createNetwork(counter++, false));