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.groupbasedpolicy.renderer.vpp.policy.acl.AccessListUtil.ACE_DIRECTION;
17 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.GbpNetconfTransaction;
18 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.Acl;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.AclBuilder;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntries;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.AccessListEntriesBuilder;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160708.access.lists.acl.access.list.entries.Ace;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev170615.VppAcl;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public abstract class AccessListWrapper {
33 private static final Logger LOG = LoggerFactory.getLogger(AccessListWrapper.class);
34 private List<GbpAceBuilder> rules;
36 public AccessListWrapper() {
37 rules = new ArrayList<>();
40 public void writeRule(GbpAceBuilder rule) {
46 public void writeRules(List<GbpAceBuilder> rules) {
48 rules.forEach(this::writeRule);
52 public List<GbpAceBuilder> readRules() {
56 protected String resolveAclName(InterfaceKey key) {
57 return key.getName() + getDirection();
60 public abstract AccessListUtil.ACE_DIRECTION getDirection();
62 public abstract void writeAclRefOnIface(@Nonnull InstanceIdentifier<Node> vppIid,
63 @Nonnull InstanceIdentifier<Interface> ifaceIid);
65 public Acl buildVppAcl(@Nonnull InterfaceKey ifaceKey) {
66 List<Ace> aces = new ArrayList<>();
67 for (GbpAceBuilder rule : rules) {
68 aces.add(rule.build());
70 AccessListEntries entries = new AccessListEntriesBuilder().setAce(aces).build();
71 return new AclBuilder().setAclType(VppAcl.class)
72 .setAclName(resolveAclName(ifaceKey))
73 .setAccessListEntries(entries)
77 public void writeAcl(@Nonnull InstanceIdentifier<Node> vppIid, @Nonnull InterfaceKey ifaceKey) {
78 Acl builtAcl = this.buildVppAcl(ifaceKey);
79 LOG.trace("Writing access-list {}", builtAcl.getAclName());
80 boolean write = GbpNetconfTransaction.netconfSyncedWrite(vppIid,
81 VppIidFactory.getVppAcl(resolveAclName(ifaceKey)), builtAcl, GbpNetconfTransaction.RETRY_COUNT);
83 LOG.error("Failed to write rule {}", builtAcl);
87 public static void removeAclsForInterface(@Nonnull InstanceIdentifier<Node> vppIid, @Nonnull InterfaceKey ifaceKey) {
88 LOG.debug("Removing access-list {}", ifaceKey);
89 for (ACE_DIRECTION dir : new ACE_DIRECTION[] {ACE_DIRECTION.INGRESS, ACE_DIRECTION.EGRESS}) {
90 GbpNetconfTransaction.netconfSyncedDelete(vppIid,
91 VppIidFactory.getVppAcl(ifaceKey.getName() + dir), GbpNetconfTransaction.RETRY_COUNT);
95 public static void removeAclRefFromIface(@Nonnull InstanceIdentifier<Node> vppIid, @Nonnull InterfaceKey ifaceKey) {
96 LOG.debug("Removing access-lists from interface {}", ifaceKey.getName());
97 GbpNetconfTransaction.netconfSyncedDelete(vppIid,
98 VppIidFactory.getAclInterfaceRef(VppIidFactory.getInterfaceIID(ifaceKey)),
99 GbpNetconfTransaction.RETRY_COUNT);