1 package org.onap.vid.asdc.parser;
3 import com.fasterxml.jackson.core.JsonProcessingException;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.fasterxml.jackson.databind.SerializationFeature;
6 import com.google.common.collect.ImmutableList;
7 import net.javacrumbs.jsonunit.JsonAssert;
8 import org.apache.commons.io.IOUtils;
9 import org.apache.log4j.Logger;
10 import org.json.JSONObject;
11 import org.json.JSONTokener;
12 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
13 import org.onap.sdc.toscaparser.api.Group;
14 import org.onap.sdc.toscaparser.api.NodeTemplate;
15 import org.onap.vid.asdc.AsdcCatalogException;
16 import org.onap.vid.asdc.AsdcClient;
17 import org.onap.vid.asdc.local.LocalAsdcClient;
18 import org.onap.vid.controllers.ToscaParserMockHelper;
19 import org.onap.vid.model.*;
20 import org.testng.Assert;
21 import org.testng.annotations.BeforeClass;
22 import org.testng.annotations.Test;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.charset.StandardCharsets;
27 import java.nio.file.Path;
30 import static org.hamcrest.Matchers.*;
31 import static org.junit.Assert.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34 import static org.onap.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls;
37 public class ToscaParserImpl2Test {
39 private final String myUUID = "myUUID";
40 private static final Logger log = Logger.getLogger(ToscaParserImpl2Test.class);
42 private ToscaParserImpl2 toscaParserImpl2 = new ToscaParserImpl2();
44 private AsdcClient asdcClient;
45 private ObjectMapper om = new ObjectMapper();
48 void init() throws IOException {
50 final InputStream asdcServicesFile = this.getClass().getClassLoader().getResourceAsStream("sdcservices.json");
52 final JSONTokener jsonTokener = new JSONTokener(IOUtils.toString(asdcServicesFile));
53 final JSONObject sdcServicesCatalog = new JSONObject(jsonTokener);
55 asdcClient = new LocalAsdcClient.Builder().catalog(sdcServicesCatalog).build();
60 public void assertEqualsBetweenServices() throws Exception {
61 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
62 Service expectedService = mockHelper.getNewServiceModel().getService();
63 Service actualService = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getService();
64 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedService), om.writeValueAsString(actualService));
69 public void assertEqualBetweenObjects() throws Exception {
70 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
71 final Path csarPath = getCsarPath(mockHelper.getUuid());
72 System.out.println("Comparing for csar " + csarPath);
73 ServiceModel actualServiceModel = toscaParserImpl2.makeServiceModel(csarPath, getServiceByUuid(mockHelper.getUuid()));
74 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(mockHelper.getNewServiceModel()), om.writeValueAsString(actualServiceModel));
79 public void assertEqualsBetweenNetworkNodes() throws Exception {
80 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
81 Map<String, Network> expectedNetworksMap = mockHelper.getNewServiceModel().getNetworks();
82 Map<String, Network> actualNetworksMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getNetworks();
83 for (Map.Entry<String, Network> entry : expectedNetworksMap.entrySet()) {
84 Network expectedNetwork = entry.getValue();
85 Network actualNetwork = actualNetworksMap.get(entry.getKey());
86 Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName());
87 verifyBaseNodeProperties(expectedNetwork, actualNetwork);
88 compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties());
93 //Because we are not supporting the old flow, the JSON are different by definition.
95 public void assertEqualsBetweenVnfsOfTosca() throws Exception {
96 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
97 Map<String, VNF> expectedVnfsMap = mockHelper.getNewServiceModel().getVnfs();
98 Map<String, VNF> actualVnfsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVnfs();
99 for (Map.Entry<String, VNF> entry : expectedVnfsMap.entrySet()) {
100 VNF expectedVnf = entry.getValue();
101 VNF actualVnf = actualVnfsMap.get(entry.getKey());
102 verifyBaseNodeProperties(expectedVnf, actualVnf);
103 Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName());
104 compareProperties(expectedVnf.getProperties(), actualVnf.getProperties());
105 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf));
111 public void assertEqualsBetweenCollectionResourcesOfTosca() throws Exception {
112 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
113 Map<String, CR> expectedVnfsMap = mockHelper.getNewServiceModel().getCollectionResource();
114 Map<String, CR> actualCRsMap = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getCollectionResource();
115 if(!actualCRsMap.isEmpty()) {
116 for (Map.Entry<String, CR> entry : expectedVnfsMap.entrySet()) {
117 CR expectedCR = entry.getValue();
118 CR actualCR = actualCRsMap.get(entry.getKey());
119 verifyCollectionResource(expectedCR, actualCR);
120 Assert.assertEquals(expectedCR.getName(), actualCR.getName());
121 compareProperties(expectedCR.getProperties(), actualCR.getProperties());
122 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedCR), om.writeValueAsString(actualCR));
128 private void verifyCollectionResource(CR expectedCR, CR actualCR) {
129 verifyBaseNodeProperties(expectedCR, actualCR);
130 Assert.assertEquals(expectedCR.getCategory(), actualCR.getCategory());
131 Assert.assertEquals(expectedCR.getSubcategory(), actualCR.getSubcategory());
132 Assert.assertEquals(expectedCR.getResourceVendor(), actualCR.getResourceVendor());
133 Assert.assertEquals(expectedCR.getResourceVendorRelease(), actualCR.getResourceVendorRelease());
134 Assert.assertEquals(expectedCR.getResourceVendorModelNumber(), actualCR.getResourceVendorModelNumber());
135 Assert.assertEquals(expectedCR.getCustomizationUUID(), actualCR.getCustomizationUUID());
136 verifyNetworkCollections(expectedCR.getNetworksCollection(), actualCR.getNetworksCollection());
139 private void verifyNetworkCollections(Map<String, NetworkCollection> expectedNetworksCollection, Map<String, NetworkCollection> actualNetworksCollection) {
140 for (Map.Entry<String, NetworkCollection> property : expectedNetworksCollection.entrySet()) {
141 NetworkCollection expectedValue = property.getValue();
142 String key = property.getKey();
143 NetworkCollection actualValue = actualNetworksCollection.get(key);
144 verifyNetworkCollection(expectedValue, actualValue);
148 private void verifyNetworkCollection(NetworkCollection expectedValue, NetworkCollection actualValue) {
149 Assert.assertEquals(expectedValue.getInvariantUuid(), actualValue.getInvariantUuid());
150 Assert.assertEquals(expectedValue.getName(), actualValue.getName());
151 Assert.assertEquals(expectedValue.getUuid(), actualValue.getUuid());
152 Assert.assertEquals(expectedValue.getVersion(), actualValue.getVersion());
153 Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionDescription(), actualValue.getNetworkCollectionProperties().getNetworkCollectionDescription());
154 Assert.assertEquals(expectedValue.getNetworkCollectionProperties().getNetworkCollectionFunction(), actualValue.getNetworkCollectionProperties().getNetworkCollectionFunction());
159 public void assertEqualsBetweenVolumeGroups() throws Exception {
160 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
161 Map<String, VolumeGroup> actualVolumeGroups = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVolumeGroups();
162 Map<String, VolumeGroup> expectedVolumeGroups = mockHelper.getNewServiceModel().getVolumeGroups();
163 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVolumeGroups), om.writeValueAsString(actualVolumeGroups));
168 public void assertEqualsBetweenVfModules() throws Exception {
169 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
170 Map<String, VfModule> actualVfModules = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getVfModules();
171 Map<String, VfModule> expectedVfModules = mockHelper.getNewServiceModel().getVfModules();
172 assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVfModules), om.writeValueAsString(actualVfModules));
177 public void assertEqualsBetweenPolicyConfigurationNodes() throws Exception {
178 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
179 Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
180 Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations();
181 JsonAssert.assertJsonEquals(actualConfigurations, expectedConfigurations);
185 public void assertEqualsBetweenPolicyConfigurationByPolicyFalse() throws Exception {
186 ToscaParserMockHelper mockHelper = new ToscaParserMockHelper(Constants.configurationByPolicyFalseUuid, Constants.configurationByPolicyFalseFilePath);
187 InputStream jsonFile = this.getClass().getClassLoader().getResourceAsStream(mockHelper.getFilePath());
188 String expectedJsonAsString = IOUtils.toString(jsonFile, StandardCharsets.UTF_8.name());
189 NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
190 mockHelper.setNewServiceModel(newServiceModel1);
191 Map<String, PortMirroringConfig> expectedConfigurations = mockHelper.getNewServiceModel().getConfigurations();
192 Map<String, PortMirroringConfig> actualConfigurations = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getConfigurations();
194 setPprobeServiceProxy(expectedConfigurations);
196 JsonAssert.assertJsonEquals(expectedConfigurations, actualConfigurations);
200 public void modelWithAnnotatedInputWithTwoProperties_vfModuleGetsTheInput() throws Exception {
201 final ToscaParserMockHelper mockHelper = new ToscaParserMockHelper("90fe6842-aa76-4b68-8329-5c86ff564407", "empty.json");
202 final ServiceModel serviceModel = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
204 assertJsonStringEqualsIgnoreNulls("{ vfModules: { 201712488_adiodvpe10..201712488AdiodVpe1..ADIOD_vRE_BV..module-1: { inputs: { 201712488_adiodvpe10_availability_zone_0: { } } } } }", om.writeValueAsString(serviceModel));
208 public void modelWithNfNamingWithToValues_ecompGeneratedNamingIsExtracted() throws Exception {
209 final ToscaParserMockHelper mockHelper = new ToscaParserMockHelper("90fe6842-aa76-4b68-8329-5c86ff564407", "empty.json");
210 final ServiceModel serviceModel = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid()));
212 assertJsonStringEqualsIgnoreNulls("" +
214 " { \"201712-488_ADIOD-vPE-1 0\": " +
215 " { properties: { " +
216 " ecomp_generated_naming: \"true\", " +
217 " nf_naming: \"{naming_policy=SDNC_Policy.Config_MS_1806SRIOV_VPE_ADIoDJson, ecomp_generated_naming=true}\" " +
218 "} } } }", om.writeValueAsString(serviceModel));
221 private void setPprobeServiceProxy(Map<String, PortMirroringConfig> expectedConfigurations){
222 //Port Mirroring Configuration By Policy 0 doesn't contains pProbe.
223 // But due to sdc design if pProbe not exists parser expects to get it from other source.
224 // In a follow implementation provided the expected pProbe.
225 PortMirroringConfig pmconfig = expectedConfigurations.get("Port Mirroring Configuration By Policy 0");
226 pmconfig.setCollectorNodes(new ArrayList<>(Arrays.asList("pprobeservice_proxy 4")));
230 public void assertEqualsBetweenServiceProxyNodes() throws Exception {
231 for (ToscaParserMockHelper mockHelper : getExpectedServiceModel()) {
232 Map<String, ServiceProxy> actualServiceProxies = toscaParserImpl2.makeServiceModel(getCsarPath(mockHelper.getUuid()), getServiceByUuid(mockHelper.getUuid())).getServiceProxies();
233 Map<String, ServiceProxy> expectedServiceProxies = mockHelper.getNewServiceModel().getServiceProxies();
234 JsonAssert.assertJsonEquals(actualServiceProxies, expectedServiceProxies);
238 private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) {
239 Assert.assertEquals(expectedNode.getName(), actualNode.getName());
240 Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid());
241 Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription());
242 Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid());
243 Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid());
244 Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion());
247 private void compareProperties(Map<String, String> expectedProperties, Map<String, String> actualProperties) {
248 for (Map.Entry<String, String> property : expectedProperties.entrySet()) {
249 String expectedValue = property.getValue();
250 String key = property.getKey();
251 String actualValue = actualProperties.get(key);
252 Assert.assertEquals(expectedValue, actualValue);
256 private ToscaParserMockHelper[] getExpectedServiceModel() throws IOException {
257 ToscaParserMockHelper[] mockHelpers = {
258 new ToscaParserMockHelper(Constants.vlUuid, Constants.vlFilePath),
259 new ToscaParserMockHelper(Constants.vfUuid, Constants.vfFilePath),
260 new ToscaParserMockHelper(Constants.crUuid, Constants.crFilePath),
261 new ToscaParserMockHelper(Constants.vfWithAnnotationUuid, Constants.vfWithAnnotationFilePath),
262 new ToscaParserMockHelper(Constants.vfWithVfcGroup, Constants.vfWithVfcGroupFilePath),
263 new ToscaParserMockHelper(Constants.configurationUuid, Constants.configurationFilePath)
265 for (ToscaParserMockHelper mockHelper : mockHelpers) {
266 InputStream jsonFile = this.getClass().getClassLoader().getResourceAsStream(mockHelper.getFilePath());
267 System.out.println(jsonFile);
268 String expectedJsonAsString = IOUtils.toString(jsonFile, StandardCharsets.UTF_8.name());
269 NewServiceModel newServiceModel1 = om.readValue(expectedJsonAsString, NewServiceModel.class);
270 mockHelper.setNewServiceModel(newServiceModel1);
276 private Path getCsarPath(String uuid) throws AsdcCatalogException {
277 return asdcClient.getServiceToscaModel(UUID.fromString(uuid));
280 private org.onap.vid.asdc.beans.Service getServiceByUuid(String uuid) throws AsdcCatalogException {
281 return asdcClient.getService(UUID.fromString(uuid));
284 public class Constants {
285 public static final String configurationUuid = "ee6d61be-4841-4f98-8f23-5de9da846ca7";
286 public static final String configurationFilePath = "policy-configuration-csar.JSON";
287 static final String vfUuid = "48a52540-8772-4368-9cdb-1f124ea5c931";
288 static final String vfWithAnnotationUuid = "f4d84bb4-a416-4b4e-997e-0059973630b9";
289 static final String vlUuid = "cb49608f-5a24-4789-b0f7-2595473cb997";
290 static final String crUuid = "76f27dfe-33e5-472f-8e0b-acf524adc4f0";
291 static final String vfWithVfcGroup = "6bce7302-70bd-4057-b48e-8d5b99e686ca";
292 // public static final String PNFUuid = "68101369-6f08-4e99-9a28-fa6327d344f3";
293 static final String vfFilePath = "vf-csar.JSON";
294 static final String vlFilePath = "vl-csar.JSON";
295 static final String crFilePath = "cr-csar.JSON";
296 static final String vfWithAnnotationFilePath = "vf-with-annotation-csar.json";
297 static final String vfWithVfcGroupFilePath = "vf-with-vfcInstanceGroups.json";
298 public static final String configurationByPolicyFalseUuid = "ee6d61be-4841-4f98-8f23-5de9da845544";
299 public static final String configurationByPolicyFalseFilePath = "policy-configuration-by-policy-false.JSON";
307 public void testGetNFModuleFromVf() {
308 ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
310 Map<String, VfModule> vfModulesFromVF = toscaParserImpl2.getVfModulesFromVF(csarHelper, myUUID);
312 assertThat(vfModulesFromVF, allOf(
314 hasKey("withoutVol"),
320 public void testGetVolumeGroupsFromVF() {
321 ISdcCsarHelper csarHelper = getMockedSdcCsarHelper();
323 Map<String, VolumeGroup> volumeGroupsFromVF = toscaParserImpl2.getVolumeGroupsFromVF(csarHelper, myUUID);
325 assertThat(volumeGroupsFromVF, allOf(
331 private ISdcCsarHelper getMockedSdcCsarHelper() {
332 ISdcCsarHelper csarHelper = mock(ISdcCsarHelper.class);
334 Group withVol = createMinimalGroup("withVol", true);
335 Group withoutVol = createMinimalGroup("withoutVol", false);
337 when(csarHelper.getVfModulesByVf(myUUID))
338 .thenReturn(ImmutableList.of(withVol, withoutVol));
343 private static Group createMinimalGroup(String name, boolean isVolumeGroup) {
344 LinkedHashMap<String, Object>
353 templates = new LinkedHashMap<>();
354 templates.put("type", "org.onap.groups.VfModule");
356 properties = addNewNamedMap(templates, "properties");
357 properties.put("volume_group", isVolumeGroup);
359 metadata = addNewNamedMap(templates, "metadata");
361 ArrayList<NodeTemplate> memberNodes = new ArrayList<>();
363 customDef = new LinkedHashMap<>();
364 vfModule = addNewNamedMap(customDef, "org.onap.groups.VfModule");
365 vfModuleProperties = addNewNamedMap(vfModule, "properties");
367 volumeGroup = addNewNamedMap(vfModuleProperties, "volume_group");
368 volumeGroup.put("type", "boolean");
369 volumeGroup.put("default", false);
370 volumeGroup.put("required", true);
373 Group group = new Group(
381 log.info(String.format("Built a group: %s",
382 (new com.fasterxml.jackson.databind.ObjectMapper())
383 .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
384 .writeValueAsString(group)
386 } catch (JsonProcessingException e) {
387 throw new RuntimeException(e);
393 private static LinkedHashMap<String, Object> addNewNamedMap(LinkedHashMap<String, Object> root, String key) {
394 LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
395 root.put(key, properties);