Policy exclusions & parallel netconf transactions
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / policy / acl / AccessListWrapper.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.policy.acl;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.annotation.Nonnull;
15
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;
30
31 public abstract class AccessListWrapper {
32
33     private static final Logger LOG = LoggerFactory.getLogger(AccessListWrapper.class);
34     private List<GbpAceBuilder> rules;
35
36     public AccessListWrapper() {
37         rules = new ArrayList<>();
38     }
39
40     public void writeRule(GbpAceBuilder rule) {
41         if (rule != null) {
42             this.rules.add(rule);
43         }
44     }
45
46     public void writeRules(List<GbpAceBuilder> rules) {
47         if (rules != null) {
48             rules.forEach(this::writeRule);
49         }
50     }
51
52     public List<GbpAceBuilder> readRules() {
53         return rules;
54     }
55
56     protected String resolveAclName(InterfaceKey key) {
57         return key.getName() + getDirection();
58     }
59
60     public abstract AccessListUtil.ACE_DIRECTION getDirection();
61
62     public abstract void writeAclRefOnIface(@Nonnull InstanceIdentifier<Node> vppIid,
63             @Nonnull InstanceIdentifier<Interface> ifaceIid);
64
65     public Acl buildVppAcl(@Nonnull InterfaceKey ifaceKey) {
66         List<Ace> aces = new ArrayList<>();
67         for (GbpAceBuilder rule : rules) {
68             aces.add(rule.build());
69         }
70         AccessListEntries entries = new AccessListEntriesBuilder().setAce(aces).build();
71         return new AclBuilder().setAclType(VppAcl.class)
72             .setAclName(resolveAclName(ifaceKey))
73             .setAccessListEntries(entries)
74             .build();
75     }
76
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);
82         if (!write) {
83             LOG.error("Failed to write rule {}", builtAcl);
84         }
85     }
86
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);
92         }
93     }
94
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);
100     }
101 }