Tap Port implementation in VPP renderer
[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.util.General;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceBuilder;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public abstract class AbstractInterfaceCommand<T extends AbstractInterfaceCommand<T>> implements ConfigCommand {
21
22     private static final Logger LOG = LoggerFactory.getLogger(AbstractInterfaceCommand.class);
23
24     protected General.Operations operation;
25     protected String name;
26     protected String description;
27     protected Boolean enabled;
28
29     protected enum linkUpDownTrap {
30         ENABLED, DISABLED
31     }
32
33     public General.Operations getOperation() {
34         return operation;
35     }
36
37     public String getName() {
38         return name;
39     }
40
41     public String getDescription() {
42         return description;
43     }
44
45     public AbstractInterfaceCommand<T> setDescription(String description) {
46         this.description = description;
47         return this;
48     }
49
50     public Boolean getEnabled() {
51         return enabled;
52     }
53
54     public AbstractInterfaceCommand<T> setEnabled(Boolean enabled) {
55         this.enabled = enabled;
56         return this;
57     }
58
59     public void execute(ReadWriteTransaction rwTx) {
60         switch (getOperation()) {
61             case PUT:
62                 LOG.debug("Executing Add operations for command: {}", this);
63                 put(rwTx);
64                 break;
65             case DELETE:
66                 LOG.debug("Executing Delete operations for command: {}", this);
67                 delete(rwTx);
68                 break;
69             case MERGE:
70                 LOG.debug("Executing Update operations for command: {}", this);
71                 merge(rwTx);
72                 break;
73             default:
74                 LOG.error("Execution failed for command: {}", this);
75                 break;
76         }
77     }
78
79     private void put(ReadWriteTransaction rwTx) {
80         InterfaceBuilder interfaceBuilder = getInterfaceBuilder();
81
82         rwTx.put(LogicalDatastoreType.CONFIGURATION, VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()),
83                 interfaceBuilder.build(), true);
84     }
85
86     private void merge(ReadWriteTransaction rwTx) {
87         InterfaceBuilder interfaceBuilder = getInterfaceBuilder();
88
89         rwTx.merge(LogicalDatastoreType.CONFIGURATION, VppIidFactory.getInterfaceIID(interfaceBuilder.getKey()),
90                 interfaceBuilder.build());
91     }
92
93     private void delete(ReadWriteTransaction readWriteTransaction) {
94         try {
95             readWriteTransaction.delete(LogicalDatastoreType.CONFIGURATION,
96                     VppIidFactory.getInterfaceIID(new InterfaceKey(name)));
97         } catch (IllegalStateException ex) {
98             LOG.debug("Interface is not present in DS {}", this, ex);
99         }
100
101     }
102 }