Add implementation for flat L3 overlay
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / commands / AbstractInterfaceCommand.java
1 /*
2  * Copyright (c) 2016 Cisco Systems. 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.commands;
10
11 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
12 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.interfaces.ConfigCommand;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.commands.interfaces.InterfaceCommand;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
16 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.proxy.arp.rev170315.ProxyArpInterfaceAugmentation;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.proxy.arp.rev170315.ProxyArpInterfaceAugmentationBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.proxy.arp.rev170315.interfaces._interface.ProxyArpBuilder;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public abstract class AbstractInterfaceCommand implements ConfigCommand, InterfaceCommand {
27
28     private static final Logger LOG = LoggerFactory.getLogger(AbstractInterfaceCommand.class);
29
30     protected General.Operations operation;
31     protected String name;
32     protected String description;
33     protected String bridgeDomain;
34     protected Boolean enabled;
35     protected Boolean enableProxyArp;
36     protected Long vrfId;
37
38     public General.Operations getOperation() {
39         return operation;
40     }
41
42     public String getName() {
43         return name;
44     }
45
46     public Long getVrfId() {
47         return vrfId;
48     }
49
50     public String getDescription() {
51         return description;
52     }
53
54     public AbstractInterfaceCommand setDescription(String description) {
55         this.description = description;
56         return this;
57     }
58
59     public String getBridgeDomain() {
60         return bridgeDomain;
61     }
62
63     public void execute(ReadWriteTransaction rwTx) {
64         switch (getOperation()) {
65             case PUT:
66                 LOG.debug("Executing Add operations for command: {}", this);
67                 put(rwTx);
68                 break;
69             case DELETE:
70                 LOG.debug("Executing Delete operations for command: {}", this);
71                 delete(rwTx);
72                 break;
73             case MERGE:
74                 LOG.debug("Executing Update operations for command: {}", this);
75                 merge(rwTx);
76                 break;
77             default:
78                 LOG.error("Execution failed for command: {}", this);
79                 break;
80         }
81     }
82
83     private void put(ReadWriteTransaction rwTx) {
84         InterfaceBuilder interfaceBuilder = getInterfaceBuilder();
85
86         rwTx.put(LogicalDatastoreType.CONFIGURATION, VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()),
87                 interfaceBuilder.build(), true);
88     }
89
90     private void merge(ReadWriteTransaction rwTx) {
91         InterfaceBuilder interfaceBuilder = getInterfaceBuilder();
92
93         rwTx.merge(LogicalDatastoreType.CONFIGURATION, VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()),
94                 interfaceBuilder.build());
95     }
96
97     private void delete(ReadWriteTransaction readWriteTransaction) {
98         try {
99             readWriteTransaction.delete(LogicalDatastoreType.CONFIGURATION,
100                     VppIidFactory.getInterfaceIID(new InterfaceKey(name)));
101         } catch (IllegalStateException ex) {
102             LOG.debug("Interface is not present in DS {}", this, ex);
103         }
104
105     }
106     @Override public InstanceIdentifier getIid() {
107         return VppIidFactory.getInterfaceIID(this.getInterfaceBuilder().getKey());
108     }
109
110     protected void addEnableProxyArpAugmentation(InterfaceBuilder interfaceBuilder) {
111         if (enableProxyArp != null) {
112             ProxyArpInterfaceAugmentationBuilder augmentationBuilder = new ProxyArpInterfaceAugmentationBuilder();
113             augmentationBuilder.setProxyArp((new ProxyArpBuilder()).build());
114             interfaceBuilder.addAugmentation(ProxyArpInterfaceAugmentation.class, augmentationBuilder.build());
115         }
116     }
117
118 }