Added PortSecurity attribute support
[neutron.git] / neutron-spi / src / test / java / org / opendaylight / neutron / spi / NeutronPortSecurityJAXBTest.java
1 /*
2  * Copyright (c) 2016 Intel Corporation  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.spi;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13
14 import java.util.List;
15
16 public class NeutronPortSecurityJAXBTest {
17
18     // JSON with PortSecurityEnabled set true
19     private static final String portSecurityEnabled_sourceJson = "{ "
20             + "\"status\": \"ACTIVE\", "
21             + "\"name\": \"net1\", "
22             + "\"admin_state_up\": true, "
23             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
24             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
25             + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
26             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
27             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
28             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
29             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
30             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
31             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", "
32             + "\"port_security_enabled\": true }";
33
34
35     // JSON with PortSecurityEnabled set false
36     private static final String portSecurityDisabled_sourceJson = "{ "
37             + "\"status\": \"ACTIVE\", "
38             + "\"name\": \"net1\", "
39             + "\"admin_state_up\": true, "
40             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
41             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
42             + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
43             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
44             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
45             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
46             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
47             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
48             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", "
49             + "\"port_security_enabled\": false }";
50
51     // JSON with PortSecurityEnabled not set for compatibility test
52     private static final String portSecurityDefault_sourceJson = "{ "
53             + "\"status\": \"ACTIVE\", "
54             + "\"name\": \"net1\", "
55             + "\"admin_state_up\": true, "
56             + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
57             + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
58             + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
59             + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
60             + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
61             + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
62             + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
63             + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
64             + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\" }";
65
66     @Test
67     public void test_NeutronPortSecurityEnabled_JAXB() {
68         test_PortSecurityEnabled_JAXB(portSecurityEnabled_sourceJson, true);
69     }
70
71     @Test
72     public void test_NeutronPortSecurityDisabled_JAXB() {
73         test_PortSecurityEnabled_JAXB(portSecurityDisabled_sourceJson, false);
74     }
75
76     /*
77         By Default PortSecurity is enabled for backward compatibility.
78         This test checks the case when the parameter is not available.
79      */
80     @Test
81     public void test_NeutronPortSecurityDefault_JAXB() {
82         test_PortSecurityEnabled_JAXB(portSecurityDefault_sourceJson, true);
83     }
84
85     private void test_PortSecurityEnabled_JAXB(String sourceJson, Boolean portSecurityEnabled) {
86         NeutronPort testObject = new NeutronPort();
87         try {
88             NeutronPort neutronObject = (NeutronPort) JaxbTestHelper.jaxbUnmarshall(testObject, sourceJson);
89             Assert.assertEquals("NeutronPort JAXB Test 1: Testing id failed", "4e8e5957-649f-477b-9e5b-f1f75b21c03c",
90                     neutronObject.getID());
91
92             Assert.assertEquals("NeutronPort JAXB Test 2: Testing tenant_id failed",
93                     "9bacb3c5d39d41a79512987f338cf177", neutronObject.getTenantID());
94
95             Assert.assertEquals("NeutronPort JAXB Test 3: Testing network_id failed",
96                     "e9330b1f-a2ef-4160-a991-169e56ab17f5", neutronObject.getNetworkUUID());
97
98             Assert.assertEquals("NeutronPort JAXB Test 4: Testing status failed", "ACTIVE", neutronObject.getStatus());
99
100             List<Neutron_IPs> fixedIPs = neutronObject.getFixedIPs();
101             Assert.assertEquals(" NeutronPort JAXB Test 5.1: Testing fixed_ips list length failed", 1, fixedIPs.size());
102
103             Assert.assertEquals(" NeutronPort JAXB Test 5.2: Testing ip_address value failed", "192.168.111.3",
104                     fixedIPs.get(0).ipAddress);
105
106             Assert.assertEquals(" NeutronPort JAXB Test 5.3: Testing subnet_id value failed",
107                     "22b44fc2-4ffb-4de4-b0f9-69d58b37ae27", fixedIPs.get(0).subnetUUID);
108
109             List<NeutronPort_ExtraDHCPOption> extraDHCPOptions = neutronObject.getExtraDHCPOptions();
110
111             Assert.assertEquals("NeutronPort JAXB Test 6: Testing extra_dhcp_opts list length failed", 1,
112                     extraDHCPOptions.size());
113
114             List<NeutronSecurityGroup> securityGroups = neutronObject.getSecurityGroups();
115             Assert.assertEquals("NeutronPort JAXB Test 7: Testing security_groups list length failed", 1,
116                     securityGroups.size());
117
118             List<NeutronPort_AllowedAddressPairs> allowedAddressPairs = neutronObject.getAllowedAddressPairs();
119             Assert.assertEquals("NeutronPort JAXB Test 8: Testing allowed_address_pairs list length failed", 1,
120                     allowedAddressPairs.size());
121
122             List<NeutronPort_VIFDetail> vifDetails = neutronObject.getVIFDetail();
123
124             Assert.assertEquals("NeutronPort JAXB Test 9.1: Testing vif_details list length failed", 1,
125                     vifDetails.size());
126
127             Assert.assertEquals("NeutronPort JAXB Test 9.2: Testing port_filter value failed", true,
128                     vifDetails.get(0).portFilter);
129
130             Assert.assertEquals("NeutronNetwork JAXB Test 9.3: Testing ovs_hybrid_plug value failed", true,
131                     vifDetails.get(0).ovsHybridPlug);
132
133             Assert.assertEquals("NeutronPort JAXB Test 10: Testing name failed", "net1", neutronObject.getName());
134
135             Assert.assertEquals("NeutronPort JAXB Test 11: Testing admin_state_up failed", true,
136                     neutronObject.getAdminStateUp());
137
138             Assert.assertEquals("NeutronPort JAXB Test 12: Testing binding:vif_type failed", "unbound",
139                     neutronObject.getBindingvifType());
140
141             Assert.assertEquals("NeutronPort JAXB Test 13: Testing binding:vnic_type failed", "normal",
142                     neutronObject.getBindingvnicType());
143
144             Assert.assertEquals("NeutronPort JAXB Test 14: Testing mac_address failed", "fa:16:3e:c9:cb:f0",
145                     neutronObject.getMacAddress());
146
147             Assert.assertEquals("NeutronPort JAXB Test 15: Testing device_id failed",
148                     "257614cc-e178-4c92-9c61-3b28d40eca44", neutronObject.getDeviceID());
149
150             Assert.assertEquals("NeutronPort JAXB Test 16: Testing device_owner failed", "",
151                     neutronObject.getDeviceOwner());
152
153             Assert.assertEquals("NeutronPort JAXB Test 17: Testing binding:host_id failed", "",
154                     neutronObject.getBindinghostID());
155
156             Assert.assertEquals("NeutronPort JAXB Test 18: Testing port_security_enabled failed", portSecurityEnabled,
157                     neutronObject.getPortSecurityEnabled());
158
159         } catch (Exception e) {
160             Assert.assertFalse("Tests Failed", true);
161         }
162     }
163 }