summaryrefslogtreecommitdiffstats
path: root/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/write/InterfaceAclCustomizer.java
blob: 622a84d93b7beb670c102330c3a562e438a85fb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.acl.write;

import static io.fd.hc2vpp.acl.write.request.MacIpInterfaceAssignmentRequest.deleteExisting;
import static java.util.stream.Collectors.toList;

import io.fd.hc2vpp.acl.util.AclContextManager;
import io.fd.hc2vpp.acl.util.FutureJVppAclCustomizer;
import io.fd.hc2vpp.acl.write.request.AclInterfaceAssignmentRequest;
import io.fd.hc2vpp.acl.write.request.MacIpInterfaceAssignmentRequest;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.acl.future.FutureJVppAclFacade;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev181001.acls.attachment.points.Interface;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev181001.acls.attachment.points._interface.acl.AclSets;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev181001.acls.attachment.points._interface.acl.acl.sets.AclSet;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

/**
 * Handles acl assignments(only standard ones, mac-ip have dedicated customizer)
 */
public class InterfaceAclCustomizer extends FutureJVppAclCustomizer implements WriterCustomizer<Interface> {

    private final NamingContext interfaceContext;
    private final AclContextManager standardAclContext;
    private final AclContextManager macIpAclContext;

    public InterfaceAclCustomizer(@Nonnull final FutureJVppAclFacade jVppAclFacade,
                                  @Nonnull final NamingContext interfaceContext,
                                  @Nonnull final AclContextManager standardAclContext,
                                  @Nonnull final AclContextManager macIpAclContext) {
        super(jVppAclFacade);
        this.interfaceContext = interfaceContext;
        this.standardAclContext = standardAclContext;
        this.macIpAclContext = macIpAclContext;
    }

    private static List<String> getAclNames(final AclSets acls) {
        if (acls == null || acls.getAclSet() == null) {
            return Collections.emptyList();
        } else {
            return acls.getAclSet().stream().map(AclSet::getName).collect(toList());
        }
    }

    @Override
    public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
                                       @Nonnull final Interface dataAfter,
                                       @Nonnull final WriteContext writeContext) throws WriteFailedException {
        AclSets egress = dataAfter.getEgress() != null ? dataAfter.getEgress().getAclSets() : null;
        AclSets ingress = dataAfter.getIngress() != null ? dataAfter.getIngress().getAclSets() : null;
        List<String> macIngress = parseMacRules(getAclNames(ingress), writeContext.getMappingContext());
        List<String> standardIngress = parseStandardRules(getAclNames(ingress), writeContext.getMappingContext());
        List<String> standardEgress = parseStandardRules(getAclNames(egress), writeContext.getMappingContext());

        // Process standard ACLs
        if (!standardIngress.isEmpty() || !standardEgress.isEmpty()) {
            AclInterfaceAssignmentRequest.create(writeContext.getMappingContext())
                    .standardAclContext(standardAclContext)
                    .interfaceContext(interfaceContext)
                    .identifier(id)
                    .inputAclNames(standardIngress)
                    .outputAclNames(standardEgress)
                    .executeAsCreate(getjVppAclFacade());
        }
        // Process mac ACLs
        if (!macIngress.isEmpty()) {
            addMacAcls(id, writeContext, macIngress);
        }
    }

    @Override
    public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
                                        @Nonnull final Interface dataBefore,
                                        @Nonnull final Interface dataAfter, @Nonnull final WriteContext writeContext)
            throws WriteFailedException {
        AclSets egress = dataAfter.getEgress() != null ? dataAfter.getEgress().getAclSets() : null;
        AclSets ingress = dataAfter.getIngress() != null ? dataAfter.getIngress().getAclSets() : null;
        List<String> standardIngress = parseStandardRules(getAclNames(ingress), writeContext.getMappingContext());
        List<String> standardEgress = parseStandardRules(getAclNames(egress), writeContext.getMappingContext());

        // update standard ACLs
        AclInterfaceAssignmentRequest.create(writeContext.getMappingContext())
                .standardAclContext(standardAclContext)
                .interfaceContext(interfaceContext)
                .identifier(id)
                .inputAclNames(standardIngress)
                .outputAclNames(standardEgress)
                .executeAsUpdate(getjVppAclFacade(), dataBefore, dataAfter);

        // Process mac ACLs
        AclSets ingressBefore = dataBefore.getIngress() != null ? dataBefore.getIngress().getAclSets() : null;
        List<String> macIngressAfter = parseMacRules(getAclNames(ingress), writeContext.getMappingContext());
        List<String> macIngressBefore = parseMacRules(getAclNames(ingressBefore), writeContext.getMappingContext());
        List<String> added =
                macIngressAfter.stream().filter(acl -> !macIngressBefore.contains(acl)).collect(Collectors.toList());
        List<String> removed =
                macIngressBefore.stream().filter(acl -> !macIngressAfter.contains(acl)).collect(Collectors.toList());

        if (!removed.isEmpty()) {
            deleteMacACLs(id, writeContext, removed);
        }

        if (!added.isEmpty()) {
            addMacAcls(id, writeContext, added);
        }
    }

    @Override
    public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
                                        @Nonnull final Interface dataBefore, @Nonnull final WriteContext writeContext)
            throws WriteFailedException {
        AclSets ingress = dataBefore.getIngress() != null ? dataBefore.getIngress().getAclSets() : null;
        List<String> standardIngress = parseStandardRules(getAclNames(ingress), writeContext.getMappingContext());
        List<String> macIngress = parseMacRules(getAclNames(ingress), writeContext.getMappingContext());

        //Process standard ACLs
        if (!standardIngress.isEmpty()) {
            AclInterfaceAssignmentRequest.create(writeContext.getMappingContext())
                    .standardAclContext(standardAclContext)
                    .interfaceContext(interfaceContext)
                    .identifier(id)
                    .executeAsDelete(getjVppAclFacade());
        }

        // Process mac ACLs
        if (!macIngress.isEmpty()) {
            deleteMacACLs(id, writeContext, macIngress);
        }
    }

    private List<String> parseMacRules(final List<String> ingress, final MappingContext mappingContext) {
        return ingress.stream()
                .filter(aclName -> macIpAclContext.containsAcl(aclName, mappingContext)).collect(Collectors.toList());
    }

    private List<String> parseStandardRules(final List<String> ingress, final MappingContext mappingContext) {
        return ingress.stream()
                .filter(aclName -> standardAclContext.containsAcl(aclName, mappingContext))
                .collect(Collectors.toList());
    }


    private void addMacAcls(@Nonnull final InstanceIdentifier<Interface> id,
                            @Nonnull final WriteContext writeContext, final List<String> added)
            throws WriteFailedException {
        for (String macAcl : added) {
            MacIpInterfaceAssignmentRequest.addNew(writeContext.getMappingContext())
                    .identifier(id)
                    .aclName(macAcl)
                    .macIpAclContext(macIpAclContext)
                    .interfaceContext(interfaceContext)
                    .execute(getjVppAclFacade());
        }
    }

    private void deleteMacACLs(@Nonnull final InstanceIdentifier<Interface> id,
                               @Nonnull final WriteContext writeContext, final List<String> macAcls)
            throws WriteFailedException {
        for (String macAcl : macAcls) {
            deleteExisting(writeContext.getMappingContext())
                    .identifier(id)
                    .aclName(macAcl)
                    .macIpAclContext(macIpAclContext)
                    .interfaceContext(interfaceContext)
                    .execute(getjVppAclFacade());
        }
    }

}