summaryrefslogtreecommitdiffstats
path: root/acl/acl-impl/src/main/java/io/fd/hc2vpp/acl/util/iface/acl/AclInterfaceAssignmentRequest.java
blob: bf1f1497e5c441afb510416ef3dc23a826b6c152 (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
/*
 * 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.util.iface.acl;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import io.fd.hc2vpp.acl.util.AclContextManager;
import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.acl.dto.AclInterfaceSetAclList;
import io.fd.vpp.jvpp.acl.future.FutureJVppAclFacade;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.acl.rev161214._interface.acl.attributes.Acl;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Multi-assignment single-request taking advantage from acl_interface_set_acl_list api
 */
public class AclInterfaceAssignmentRequest implements JvppReplyConsumer, ByteDataTranslator {

    private static final Logger LOG = LoggerFactory.getLogger(AclInterfaceAssignmentRequest.class);

    private final MappingContext mappingContext;
    private InstanceIdentifier<Acl> identifier;
    private List<String> inputAclNames = Collections.emptyList();
    private List<String> outputAclNames = Collections.emptyList();
    private AclContextManager standardAclContext;
    private NamingContext interfaceContext;


    private AclInterfaceAssignmentRequest(final MappingContext mappingContext) {
        this.mappingContext = checkNotNull(mappingContext, "Mapping context cannot be null");
    }

    public static AclInterfaceAssignmentRequest create(@Nonnull final MappingContext mappingContext) {
        return new AclInterfaceAssignmentRequest(mappingContext);
    }

    public AclInterfaceAssignmentRequest identifier(
            @Nonnull final InstanceIdentifier<Acl> identifier) {
        this.identifier = identifier;
        return this;
    }

    public AclInterfaceAssignmentRequest inputAclNames(@Nonnull final List<String> inputAclNames) {
        checkNotNull(inputAclNames, "Input ACL names cannot be null");
        this.inputAclNames = ImmutableList.copyOf(inputAclNames);
        return this;
    }

    public AclInterfaceAssignmentRequest outputAclNames(@Nonnull final List<String> outputAclNames) {
        checkNotNull(outputAclNames, "Output ACL names cannot be null");
        this.outputAclNames = ImmutableList.copyOf(outputAclNames);
        return this;
    }

    public AclInterfaceAssignmentRequest standardAclContext(@Nonnull final AclContextManager standardAclContext) {
        this.standardAclContext = standardAclContext;
        return this;
    }

    public AclInterfaceAssignmentRequest interfaceContext(@Nonnull final NamingContext interfaceContext) {
        this.interfaceContext = interfaceContext;
        return this;
    }

    private void checkValidRequest() {
        checkNotNull(identifier, "Identifier cannot be null");
        checkNotNull(standardAclContext, "ACL context cannot be null");
        checkNotNull(interfaceContext, "Interface context cannot be null");
    }

    public void executeAsCreate(@Nonnull final FutureJVppAclFacade api) throws WriteFailedException {
        checkValidRequest();
        final String interfaceName = identifier.firstKeyOf(Interface.class).getName();

        // locking on mapping context, to prevent modifying of mappings (for both contexts) during binding/execution of request
        synchronized (mappingContext) {
            LOG.debug(
                    "Executing acl interface assignment write request for interface={}, input ACL's={},output ACL's={}",
                    interfaceName, inputAclNames, outputAclNames);

            getReplyForWrite(api.aclInterfaceSetAclList(createRequest(interfaceName)).toCompletableFuture(),
                    identifier);
            LOG.debug(
                    "Acl interface assignment write request for interface={}, input ACL's={},output ACL's={} successfully passed",
                    interfaceName, inputAclNames, outputAclNames);
        }
    }

    public void executeAsUpdate(@Nonnull final FutureJVppAclFacade api, final Acl before, final Acl after)
            throws WriteFailedException {
        checkValidRequest();
        final String interfaceName = identifier.firstKeyOf(Interface.class).getName();

        // locking on mapping context, to prevent modifying of mappings (for both contexts) during binding/execution of request
        synchronized (mappingContext) {
            LOG.debug(
                    "Executing acl interface assignment update request for interface={}, input ACL's={},output ACL's={}",
                    interfaceName, inputAclNames, outputAclNames);

            getReplyForUpdate(api.aclInterfaceSetAclList(createRequest(interfaceName)).toCompletableFuture(),
                    identifier, before, after);
            LOG.debug(
                    "Acl interface assignment update request for interface={}, input ACL's={},output ACL's={} successfully passed",
                    interfaceName, inputAclNames, outputAclNames);
        }
    }

    public void executeAsDelete(@Nonnull final FutureJVppAclFacade api) throws WriteFailedException {
        checkValidRequest();
        final String interfaceName = identifier.firstKeyOf(Interface.class).getName();

        // locking on mapping context, to prevent modifying of mappings (for both contexts) during binding/execution of request
        synchronized (mappingContext) {
            LOG.debug(
                    "Executing acl interface assignment delete request for interface={}, input ACL's={},output ACL's={}",
                    interfaceName, inputAclNames, outputAclNames);

            // remove all ACLs, just in case they were set by AclInterfaceAssignmentRequest user
            inputAclNames = Collections.emptyList();
            outputAclNames = Collections.emptyList();

            getReplyForDelete(api.aclInterfaceSetAclList(createRequest(interfaceName)).toCompletableFuture(),
                    identifier);
            LOG.debug(
                    "Acl interface assignment delete request for interface={}, input ACL's={},output ACL's={} successfully passed",
                    interfaceName, inputAclNames, outputAclNames);
        }
    }

    // synchronized on higher layer
    private AclInterfaceSetAclList createRequest(final String interfaceName) {

        AclInterfaceSetAclList request = new AclInterfaceSetAclList();
        request.swIfIndex = interfaceContext.getIndex(interfaceName, mappingContext);
        // FIXME (HC2VPP-201): possible overflow
        request.nInput = (byte) inputAclNames.size();
        request.count = (byte) (inputAclNames.size() + outputAclNames.size());
        request.acls = Stream.concat(inputAclNames.stream(), outputAclNames.stream())
                .mapToInt(aclName -> standardAclContext.getAclIndex(aclName, mappingContext))
                .toArray();
        return request;
    }
}