Fix metadata for route, arp and overlay
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / commands / StaticArpCommand.java
1 /*
2  * Copyright (c) 2017 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 com.google.common.base.Preconditions;
12 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.General;
15 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Neighbor;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborBuilder;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.NeighborKey;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Created by Shakib Ahmed on 5/3/17.
28  */
29 public class StaticArpCommand extends AbstractConfigCommand{
30     private static final Logger LOG = LoggerFactory.getLogger(StaticArpCommand.class);
31
32     private Ipv4AddressNoZone ip;
33     private PhysAddress linkLayerAddress;
34     private InterfaceKey interfaceKey;
35
36     public StaticArpCommand(StaticArpCommandBuilder builder) {
37         this.operation = builder.getOperation();
38         this.ip = builder.getIp();
39         this.linkLayerAddress = builder.getLinkLayerAddress();
40         this.interfaceKey = builder.getInterfaceKey();
41     }
42
43     public static StaticArpCommandBuilder builder() {
44         return new StaticArpCommandBuilder();
45     }
46
47     @Override
48     public InstanceIdentifier getIid() {
49         return VppIidFactory.getNeighborIid(interfaceKey, new NeighborKey(ip));
50     }
51
52     @Override
53     void put(ReadWriteTransaction rwTx) {
54         InstanceIdentifier<Neighbor> iid = VppIidFactory.getNeighborIid(interfaceKey, new NeighborKey(ip));
55         rwTx.put(LogicalDatastoreType.CONFIGURATION, iid, getNeighborBuilder().build());
56     }
57
58     @Override
59     void merge(ReadWriteTransaction rwTx) {
60         InstanceIdentifier<Neighbor> iid = VppIidFactory.getNeighborIid(interfaceKey, new NeighborKey(ip));
61         rwTx.merge(LogicalDatastoreType.CONFIGURATION, iid, getNeighborBuilder().build());
62     }
63
64     @Override
65     void delete(ReadWriteTransaction rwTx) {
66         try {
67             InstanceIdentifier<Neighbor> iid = VppIidFactory.getNeighborIid(interfaceKey, new NeighborKey(ip));
68             rwTx.delete(LogicalDatastoreType.CONFIGURATION, iid);
69         } catch (IllegalStateException ex) {
70             LOG.debug("Proxy Range not deleted from DS {}", this, ex);
71         }
72     }
73
74     private NeighborBuilder getNeighborBuilder() {
75         return new NeighborBuilder()
76                     .setKey(new NeighborKey(this.ip))
77                     .setIp(this.ip)
78                     .setLinkLayerAddress(this.linkLayerAddress);
79     }
80
81     public static class StaticArpCommandBuilder {
82         private General.Operations operation;
83         private Ipv4AddressNoZone ip;
84         private PhysAddress linkLayerAddress;
85         private InterfaceKey interfaceKey;
86
87         public General.Operations getOperation() {
88             return operation;
89         }
90
91         public void setOperation(General.Operations operation) {
92             this.operation = operation;
93         }
94
95         public Ipv4AddressNoZone getIp() {
96             return ip;
97         }
98
99         public void setIp(Ipv4AddressNoZone ip) {
100             this.ip = ip;
101         }
102
103         public PhysAddress getLinkLayerAddress() {
104             return linkLayerAddress;
105         }
106
107         public void setLinkLayerAddress(PhysAddress linkLayerAddress) {
108             this.linkLayerAddress = linkLayerAddress;
109         }
110
111         public InterfaceKey getInterfaceKey() {
112             return interfaceKey;
113         }
114
115         public void setInterfaceKey(InterfaceKey interfaceKey) {
116             this.interfaceKey = interfaceKey;
117         }
118
119         /**
120          * StaticArpCommand build method.
121          *
122          * @return StaticArpCommand
123          * @throws IllegalArgumentException if ip or operation is null.
124          */
125         public StaticArpCommand build() {
126             Preconditions.checkNotNull(operation, "Operation must not be null!");
127             Preconditions.checkNotNull(ip, "ip must not be null!");
128
129             return new StaticArpCommand(this);
130         }
131     }
132 }