6cd0e2fd814c2504a81338cb3519536d4c8048f9
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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
17 package io.fd.hc2vpp.routing.write;
18
19 import static io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper.ROUTE_PROTOCOL_NAME;
20 import static io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper.ROUTE_PROTOCOL_NAME_2;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23 import static org.mockito.Mockito.when;
24
25 import com.google.common.base.Optional;
26 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
27 import io.fd.hc2vpp.common.translate.util.NamingContext;
28 import io.fd.hc2vpp.fib.management.FibManagementIIds;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.Direct;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.Static;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.ControlPlaneProtocols;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocol;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocolBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev180313.routing.control.plane.protocols.ControlPlaneProtocolKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.routing.rev180319.RoutingProtocolVppAttr;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.routing.rev180319.RoutingProtocolVppAttrBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.vpp.routing.rev180319.routing.control.plane.protocols.control.plane.protocol.VppProtocolAttributesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv4;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.Ipv6;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.VniReference;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.Table;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.fib.table.management.rev180521.vpp.fib.table.management.fib.tables.TableKey;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49
50 public class ControlPlaneProtocolCustomizerTest extends WriterCustomizerTest {
51
52     private static final VniReference VRF = new VniReference(1L);
53     private static final VniReference INVALID_VRF = new VniReference(3L);
54     private static final String INVALID_TABLE_PROTOCOL_1 = "invalid-table-protocol-1";
55     private InstanceIdentifier<ControlPlaneProtocol> validId;
56     private InstanceIdentifier<ControlPlaneProtocol> invalidIid;
57     private ControlPlaneProtocol validData;
58     private ControlPlaneProtocol validData2;
59     private ControlPlaneProtocol invalidData;
60     private ControlPlaneProtocol invalidTableData;
61     private ControlPlaneProtocolCustomizer customizer;
62     private NamingContext routingProtocolContext;
63
64     @Before
65     public void init() {
66         validId = InstanceIdentifier.create(ControlPlaneProtocol.class);
67         invalidIid = InstanceIdentifier.create(ControlPlaneProtocols.class)
68                 .child(ControlPlaneProtocol.class,
69                         new ControlPlaneProtocolKey(INVALID_TABLE_PROTOCOL_1, Static.class));
70         invalidTableData = new ControlPlaneProtocolBuilder()
71                 .setName(INVALID_TABLE_PROTOCOL_1)
72                 .setType(Static.class)
73                 .addAugmentation(RoutingProtocolVppAttr.class, new RoutingProtocolVppAttrBuilder()
74                         .setVppProtocolAttributes(new VppProtocolAttributesBuilder()
75                                 .setPrimaryVrf(INVALID_VRF)
76                                 .build())
77                         .build())
78                 .build();
79         validData = new ControlPlaneProtocolBuilder()
80                 .setName(ROUTE_PROTOCOL_NAME)
81                 .setType(Static.class)
82                 .addAugmentation(RoutingProtocolVppAttr.class, new RoutingProtocolVppAttrBuilder()
83                         .setVppProtocolAttributes(new VppProtocolAttributesBuilder()
84                                 .setPrimaryVrf(VRF)
85                                 .build())
86                         .build())
87                 .build();
88
89         validData2= new ControlPlaneProtocolBuilder()
90                 .setName(ROUTE_PROTOCOL_NAME_2)
91                 .setType(Static.class)
92                 .addAugmentation(RoutingProtocolVppAttr.class, new RoutingProtocolVppAttrBuilder()
93                         .setVppProtocolAttributes(new VppProtocolAttributesBuilder()
94                                 .setPrimaryVrf(VRF)
95                                 .build())
96                         .build())
97                 .build();
98
99         invalidData = new ControlPlaneProtocolBuilder()
100                 .setType(Direct.class)
101                 .build();
102
103         routingProtocolContext = new NamingContext("routing-protocol", "routing-protocol-context");
104         customizer = new ControlPlaneProtocolCustomizer(routingProtocolContext);
105         TableKey keyV4 = new TableKey(Ipv4.class, VRF);
106         TableKey keyV6 = new TableKey(Ipv6.class, VRF);
107         KeyedInstanceIdentifier<Table, TableKey> vrfIidV4 = FibManagementIIds.FM_FIB_TABLES.child(Table.class, keyV4);
108         KeyedInstanceIdentifier<Table, TableKey> vrfIidV6 = FibManagementIIds.FM_FIB_TABLES.child(Table.class, keyV6);
109         TableKey invalidKeyV4 = new TableKey(Ipv4.class, INVALID_VRF);
110         TableKey invalidKeyV6 = new TableKey(Ipv6.class, INVALID_VRF);
111         KeyedInstanceIdentifier<Table, TableKey> invalidVrfIidV4 =
112                 FibManagementIIds.FM_FIB_TABLES.child(Table.class, invalidKeyV4);
113         KeyedInstanceIdentifier<Table, TableKey> invalidVrfIidV6 =
114                 FibManagementIIds.FM_FIB_TABLES.child(Table.class, invalidKeyV6);
115         when(writeContext.readAfter(vrfIidV4)).thenReturn(Optional.of(
116                 new TableBuilder().withKey(keyV4).setAddressFamily(keyV4.getAddressFamily())
117                         .setTableId(keyV4.getTableId()).setName("VRF-IPV4-1").build()));
118         when(writeContext.readAfter(vrfIidV6)).thenReturn(Optional.of(
119                 new TableBuilder().withKey(keyV6).setAddressFamily(keyV6.getAddressFamily())
120                         .setTableId(keyV6.getTableId()).setName("VRF-IPV6-1").build()));
121         when(writeContext.readAfter(invalidVrfIidV4)).thenReturn(Optional.absent());
122         when(writeContext.readAfter(invalidVrfIidV6)).thenReturn(Optional.absent());
123     }
124
125     @Test(expected = WriteFailedException.class)
126     public void testWriteInvalid() throws WriteFailedException {
127         noMappingDefined(mappingContext, INVALID_TABLE_PROTOCOL_1, "routing-protocol-context");
128         customizer.writeCurrentAttributes(invalidIid, invalidTableData, writeContext);
129
130     }
131
132     @Test
133     public void testWriteIsStatic() throws WriteFailedException {
134         noMappingDefined(mappingContext, ROUTE_PROTOCOL_NAME, "routing-protocol-context");
135         try {
136             customizer.writeCurrentAttributes(validId, validData, writeContext);
137         } catch (Exception e) {
138             fail("Test should have passed without throwing exception");
139         }
140     }
141
142     /**
143      * Should not fail, just ignore re-mapping same name
144      * */
145     @Test
146     public void testWriteIsStaticSameAllreadyExist() throws WriteFailedException {
147         defineMapping(mappingContext, ROUTE_PROTOCOL_NAME, 1, "routing-protocol-context");
148         try {
149             customizer.writeCurrentAttributes(validId, validData, writeContext);
150         } catch (Exception e) {
151             fail("Test should have passed without throwing exception");
152         }
153     }
154
155     /**
156      * Should fail, because of attempt to map different name to same index
157      * */
158     @Test
159     public void testWriteIsStaticOtherAllreadyExist() throws WriteFailedException {
160         defineMapping(mappingContext, ROUTE_PROTOCOL_NAME, 1, "routing-protocol-context");
161         try {
162             customizer.writeCurrentAttributes(validId, validData2, writeContext);
163         } catch (Exception e) {
164             assertTrue(e instanceof IllegalStateException);
165             return;
166         }
167         fail("Test should have thrown exception");
168     }
169
170     @Test
171     public void testWriteIsntStatic() throws WriteFailedException {
172         try {
173             customizer.writeCurrentAttributes(validId, invalidData, writeContext);
174         } catch (Exception e) {
175             assertTrue(e instanceof IllegalArgumentException);
176             return;
177         }
178         fail("Test should have thrown exception");
179     }
180
181     @Test(expected = UnsupportedOperationException.class)
182     public void testUpdate() throws WriteFailedException {
183         customizer.updateCurrentAttributes(validId, validData, validData, writeContext);
184     }
185 }