911350562963de44bf13652127e9022d2bb54c40
[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.vpp.classifier.write.acl.egress;
18
19 import static org.mockito.Matchers.any;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22
23 import com.google.common.base.Optional;
24 import io.fd.hc2vpp.common.test.write.WriterCustomizerTest;
25 import io.fd.hc2vpp.vpp.classifier.write.acl.common.AclTableContextManager;
26 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelSessionReply;
27 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTable;
28 import io.fd.vpp.jvpp.core.dto.ClassifyAddDelTableReply;
29 import io.fd.vpp.jvpp.core.dto.ClassifySetInterfaceL2Tables;
30 import io.fd.vpp.jvpp.core.dto.ClassifySetInterfaceL2TablesReply;
31 import java.util.Collections;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.AclBase;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.EthAcl;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.AceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.ActionsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.MatchesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.actions.packet.handling.PermitBuilder;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIpBuilder;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.ace.matches.ace.type.ace.ip.ace.ip.version.AceIpv4Builder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.context.rev161214.mapping.entry.context.attributes.acl.mapping.entry.context.mapping.table.MappingEntryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.InterfaceMode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.AccessLists;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.AccessListsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.access.lists.AclBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.ietf.acl.Egress;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.ietf.acl.EgressBuilder;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51
52 public class EgressIetfAclWriterTest extends WriterCustomizerTest {
53
54     private static final int IF_INDEX = 1;
55     private static final String ACL_NAME = "acl1";
56     private static final Class<? extends AclBase> ACL_TYPE = EthAcl.class;
57
58     private EgressIetfAclWriter writer;
59     @Mock
60     private AclTableContextManager aclCtx;
61     @Mock
62     private InstanceIdentifier<?> id;
63
64     private static ClassifyAddDelTable classifyAddDelTable(final int tableIndex) {
65         final ClassifyAddDelTable reply = new ClassifyAddDelTable();
66         reply.tableIndex = tableIndex;
67         reply.delChain = 1;
68         return reply;
69     }
70
71     @Override
72     protected void setUpTest() throws Exception {
73         writer = new EgressIetfAclWriter(api, aclCtx);
74     }
75
76     private ClassifySetInterfaceL2Tables classifySetInterfaceL2TablesRequest() {
77         final ClassifySetInterfaceL2Tables request = new ClassifySetInterfaceL2Tables();
78         request.isInput = 0;
79         request.ip4TableIndex = -1;
80         request.ip6TableIndex = -1;
81         request.otherTableIndex = -1;
82         request.swIfIndex = IF_INDEX;
83         return request;
84     }
85
86     @Test
87     public void testDeleteAcl() throws Exception {
88         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
89         when(api.classifySetInterfaceL2Tables(any())).thenReturn(future(new ClassifySetInterfaceL2TablesReply()));
90         when(aclCtx.getEntry(IF_INDEX, mappingContext)).thenReturn(Optional.of(
91             new MappingEntryBuilder()
92                 .setIndex(IF_INDEX)
93                 .setL2TableId(1)
94                 .setIp4TableId(2)
95                 .setIp6TableId(3)
96                 .build()));
97
98         writer.deleteAcl(id, IF_INDEX, mappingContext);
99
100         verify(api).classifySetInterfaceL2Tables(classifySetInterfaceL2TablesRequest());
101         verify(api).classifyAddDelTable(classifyAddDelTable(1));
102         verify(api).classifyAddDelTable(classifyAddDelTable(2));
103         verify(api).classifyAddDelTable(classifyAddDelTable(3));
104         verify(aclCtx).removeEntry(IF_INDEX, mappingContext);
105     }
106
107     @Test
108     public void testWrite() throws Exception {
109         when(api.classifyAddDelTable(any())).thenReturn(future(new ClassifyAddDelTableReply()));
110         when(api.classifyAddDelSession(any())).thenReturn(future(new ClassifyAddDelSessionReply()));
111         when(api.classifySetInterfaceL2Tables(any())).thenReturn(future(new ClassifySetInterfaceL2TablesReply()));
112
113         final Egress
114             acl = new EgressBuilder().setAccessLists(
115             new AccessListsBuilder().setAcl(
116                 Collections.singletonList(new AclBuilder()
117                     .setName(ACL_NAME)
118                     .setType(ACL_TYPE)
119                     .build())
120             ).setMode(InterfaceMode.L2).build()
121         ).build();
122
123         final AccessLists accessLists = acl.getAccessLists();
124
125         when(writeContext.readAfter(any())).thenReturn(Optional.of(
126             new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder()
127                 .setAccessListEntries(
128                     new AccessListEntriesBuilder().setAce(Collections.singletonList(new AceBuilder()
129                         .setMatches(new MatchesBuilder().setAceType(
130                             new AceIpBuilder()
131                                 .setAceIpVersion(new AceIpv4Builder().build())
132                                 .setProtocol((short) 1)
133                                 .build()
134                         ).build())
135                         .setActions(new ActionsBuilder().setPacketHandling(new PermitBuilder().build()).build())
136                         .build())).build()
137                 ).build()
138
139         ));
140
141         writer.write(id, IF_INDEX, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
142             writeContext, mappingContext);
143
144         final ClassifySetInterfaceL2Tables request = new ClassifySetInterfaceL2Tables();
145         request.isInput = 0;
146         request.swIfIndex = IF_INDEX;
147         request.otherTableIndex = -1;
148         request.ip4TableIndex = 0;
149         request.ip6TableIndex = -1;
150         verify(api).classifySetInterfaceL2Tables(request);
151     }
152 }