Optimize DHCP relay processing for VPP
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / commands / DhcpRelayCommand.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 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.inet.types.rev130715.IpAddress;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.AddressFamily;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.Relay;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.dhcp.attributes.relays.RelayKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.dhcp.rev170315.relay.attributes.Server;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Preconditions;
27
28 import java.util.List;
29
30 public class DhcpRelayCommand extends AbstractConfigCommand {
31
32     private static final Logger LOG = LoggerFactory.getLogger(DhcpRelayCommand.class);
33     private Long rxVrfId;
34     private IpAddress gatewayIpAddress;
35     private List<Server> serverIpAddresses;
36     private Class<? extends AddressFamily> addressType;
37     private NodeId vppNodeId;
38
39     private DhcpRelayCommand(DhcpRelayBuilder builder) {
40         operation = builder.getOperation();
41         rxVrfId = builder.getRxVrfId();
42         gatewayIpAddress = builder.getGatewayIpAddress();
43         serverIpAddresses = builder.getServerIpAddresses();
44         addressType = builder.getAddressType();
45         vppNodeId = builder.getVppNodeId();
46     }
47
48     public static DhcpRelayBuilder builder() {
49         return new DhcpRelayBuilder();
50     }
51
52     Long getRxVrfId() {
53         return rxVrfId;
54     }
55
56     public IpAddress getGatewayIpAddress() {
57         return gatewayIpAddress;
58     }
59
60     public List<Server>  getServerIpAddresses() {
61         return serverIpAddresses;
62     }
63
64     public Class<? extends AddressFamily> getAddressType() {
65         return addressType;
66     }
67
68     @Override public InstanceIdentifier<Relay> getIid() {
69         return VppIidFactory.getDhcpRelayIid(getDhcpBuilder().getKey());
70     }
71
72     public NodeId getVppNodeId() {
73         return vppNodeId;
74     }
75
76     void put(ReadWriteTransaction rwTx) {
77         rwTx.put(LogicalDatastoreType.CONFIGURATION, getIid(), getDhcpBuilder().build(), true);
78     }
79
80     void merge(ReadWriteTransaction rwTx) {
81         rwTx.merge(LogicalDatastoreType.CONFIGURATION, getIid(), getDhcpBuilder().build(), true);
82     }
83
84     void delete(ReadWriteTransaction rwTx) {
85         try {
86             rwTx.delete(LogicalDatastoreType.CONFIGURATION, getIid());
87         } catch (IllegalStateException ex) {
88             LOG.debug("Routing protocol not deleted from DS {}", this, ex);
89         }
90     }
91
92     @Override public String toString() {
93         return "DhcpProxyCommand [ rxVrfId=" + rxVrfId + ", gatewayIpAddress=" + gatewayIpAddress
94             + ", serverIpAddresses=" + serverIpAddresses +  ", addressType=" + addressType
95             + ", operations=" + operation + "]";
96     }
97
98     /**
99      * Compares two DhcpRelayCommands without checking operation status.
100      * @param compareTo DhcpRelayCommand to compare with.
101      * @return true if commands match, false otherwise.
102      */
103     @Override public boolean equals(Object compareTo) {
104         if (compareTo == null || !compareTo.getClass().equals(this.getClass())) {
105             return false;
106         }
107
108         DhcpRelayCommand command = (DhcpRelayCommand) compareTo;
109
110         if (!this.getVppNodeId().equals(command.getVppNodeId())) {
111             return false;
112         } else if (!this.getAddressType().equals(command.getAddressType())) {
113             return false;
114         } else if (!this.getGatewayIpAddress().equals(command.getGatewayIpAddress())) {
115             return false;
116         } else if (!this.getIid().equals(command.getIid())) {
117             return false;
118         } else if (!this.getRxVrfId().equals(command.getRxVrfId())) {
119             return false;
120         } else if (this.getServerIpAddresses() != null && !this.getServerIpAddresses()
121             .containsAll(command.getServerIpAddresses())) {
122             return false;
123         } else if (command.getServerIpAddresses() != null && !command.getServerIpAddresses()
124             .containsAll(this.getServerIpAddresses())) {
125             return false;
126         }
127
128         return true;
129     }
130
131     public RelayBuilder getDhcpBuilder() {
132         return new RelayBuilder()
133             .setAddressType(addressType)
134             .setGatewayAddress(gatewayIpAddress)
135             .setKey(new RelayKey(addressType, rxVrfId))
136             .setRxVrfId(rxVrfId)
137             .setServer(serverIpAddresses);
138     }
139
140     public static class DhcpRelayBuilder {
141
142         private General.Operations operation;
143         private Long rxVrfId;
144         private IpAddress gatewayIpAddress;
145         private List<Server>  serverIpAddress;
146         private Class<? extends AddressFamily> addressType;
147         private NodeId VppNodeId;
148
149         public General.Operations getOperation() {
150             return operation;
151         }
152
153         public DhcpRelayBuilder setOperation(General.Operations operation) {
154             this.operation = operation;
155             return this;
156         }
157
158         public Long getRxVrfId() {
159             return rxVrfId;
160         }
161
162         public DhcpRelayBuilder setRxVrfId(Long rxVrfId) {
163             this.rxVrfId = rxVrfId;
164             return this;
165         }
166
167         public IpAddress getGatewayIpAddress() {
168             return gatewayIpAddress;
169         }
170
171         public DhcpRelayBuilder setGatewayIpAddress(IpAddress gatewayIpAddress) {
172             this.gatewayIpAddress = gatewayIpAddress;
173             return this;
174         }
175
176         public List<Server>  getServerIpAddresses() {
177             return serverIpAddress;
178         }
179
180         public DhcpRelayBuilder setServerIpAddresses(List<Server>  serverIpAddress) {
181             this.serverIpAddress = serverIpAddress;
182             return this;
183         }
184
185         public Class<? extends AddressFamily> getAddressType() {
186             return addressType;
187         }
188
189         public DhcpRelayBuilder setAddressType(Class<? extends AddressFamily> addressType) {
190             this.addressType = addressType;
191             return this;
192         }
193
194         public NodeId getVppNodeId() {
195             return VppNodeId;
196         }
197
198         public DhcpRelayBuilder setVppNodeId(NodeId vppNodeId) {
199             VppNodeId = vppNodeId;
200             return this;
201         }
202
203         /**
204          * RoutingCommand build method.
205          *
206          * @return RoutingCommand
207          * @throws IllegalArgumentException if routerProtocol, operation or rxVrfId is null.
208          */
209         public DhcpRelayCommand build() {
210             Preconditions.checkArgument(this.operation != null);
211             Preconditions.checkArgument(this.rxVrfId != null);
212             Preconditions.checkArgument(this.addressType != null);
213
214             return new DhcpRelayCommand(this);
215         }
216     }
217 }