2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl;
11 import java.util.ArrayList;
12 import java.util.List;
14 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl.AccessListUtil.ACE_DIRECTION;
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntries;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.VppAcl;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public abstract class AccessListWrapper {
34 private static final Logger LOG = LoggerFactory.getLogger(AccessListWrapper.class);
35 private List<GbpAceBuilder> rules;
37 public AccessListWrapper() {
38 rules = new ArrayList<>();
41 public void writeRule(GbpAceBuilder rule) {
47 public void writeRules(List<GbpAceBuilder> rules) {
49 rules.forEach(this::writeRule);
53 public List<GbpAceBuilder> readRules() {
57 protected String resolveAclName(InterfaceKey key) {
58 return key.getName() + getDirection();
61 public abstract AccessListUtil.ACE_DIRECTION getDirection();
63 public abstract void writeAclRefOnIface(@Nonnull DataBroker mountPoint,
64 @Nonnull InstanceIdentifier<Interface> ifaceIid);
66 public Acl buildVppAcl(@Nonnull InterfaceKey ifaceKey) {
67 List<Ace> aces = new ArrayList<>();
68 for (GbpAceBuilder rule : rules) {
69 aces.add(rule.build());
71 AccessListEntries entries = new AccessListEntriesBuilder().setAce(aces).build();
72 return new AclBuilder().setAclType(VppAcl.class)
73 .setAclName(resolveAclName(ifaceKey))
74 .setAccessListEntries(entries)
78 public void writeAcl(@Nonnull DataBroker mountPoint, @Nonnull InterfaceKey ifaceKey) {
79 Acl builtAcl = this.buildVppAcl(ifaceKey);
80 LOG.info("Writing access-list {}", builtAcl.getAclName());
81 boolean write = GbpNetconfTransaction.netconfSyncedWrite(mountPoint,
82 VppIidFactory.getVppAcl(resolveAclName(ifaceKey)), builtAcl, GbpNetconfTransaction.RETRY_COUNT);
84 LOG.error("Failed to write rule {}", builtAcl);
88 public static void removeAclsForInterface(@Nonnull DataBroker mountPoint, @Nonnull InterfaceKey ifaceKey) {
89 LOG.debug("Removing access-list {}", ifaceKey);
90 for (ACE_DIRECTION dir : new ACE_DIRECTION[] {ACE_DIRECTION.INGRESS, ACE_DIRECTION.EGRESS}) {
91 GbpNetconfTransaction.netconfSyncedDelete(mountPoint,
92 VppIidFactory.getVppAcl(ifaceKey.getName() + dir), GbpNetconfTransaction.RETRY_COUNT);
96 public static void removeAclRefFromIface(@Nonnull DataBroker mountPoint, @Nonnull InterfaceKey ifaceKey) {
97 LOG.debug("Removing access-lists from interface {}", ifaceKey.getName());
98 GbpNetconfTransaction.netconfSyncedDelete(mountPoint, VppIidFactory.getInterfaceIetfAcl(ifaceKey),
99 GbpNetconfTransaction.RETRY_COUNT);