531b55628fae215d36f348946e1189053997b3ca
[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.yangtools.yang.binding.InstanceIdentifier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Preconditions;
26
27 import java.util.List;
28
29 public class DhcpRelayCommand extends AbstractConfigCommand {
30
31     private static final Logger LOG = LoggerFactory.getLogger(DhcpRelayCommand.class);
32     private Long rxVrfId;
33     private IpAddress gatewayIpAddress;
34     private List<Server> serverIpAddresses;
35     private Class<? extends AddressFamily> addressType;
36
37     private DhcpRelayCommand(DhcpRelayBuilder builder) {
38         operation = builder.getOperation();
39         rxVrfId = builder.getRxVrfId();
40         gatewayIpAddress = builder.getGatewayIpAddress();
41         serverIpAddresses = builder.getServerIpAddresses();
42         addressType = builder.getAddressType();
43     }
44
45     public static DhcpRelayBuilder builder() {
46         return new DhcpRelayBuilder();
47     }
48
49     Long getRxVrfId() {
50         return rxVrfId;
51     }
52
53     public IpAddress getGatewayIpAddress() {
54         return gatewayIpAddress;
55     }
56
57     public List<Server>  getServerIpAddresses() {
58         return serverIpAddresses;
59     }
60
61     public Class<? extends AddressFamily> getAddressType() {
62         return addressType;
63     }
64
65     @Override public InstanceIdentifier<Relay> getIid() {
66         return VppIidFactory.getDhcpRelayIid(getDhcpBuilder().getKey());
67     }
68
69     void put(ReadWriteTransaction rwTx) {
70         rwTx.put(LogicalDatastoreType.CONFIGURATION, getIid(), getDhcpBuilder().build(), true);
71     }
72
73     void merge(ReadWriteTransaction rwTx) {
74         rwTx.merge(LogicalDatastoreType.CONFIGURATION, getIid(), getDhcpBuilder().build(), true);
75     }
76
77     void delete(ReadWriteTransaction rwTx) {
78         try {
79             rwTx.delete(LogicalDatastoreType.CONFIGURATION, getIid());
80         } catch (IllegalStateException ex) {
81             LOG.debug("Routing protocol not deleted from DS {}", this, ex);
82         }
83     }
84
85     @Override public String toString() {
86         return "DhcpProxyCommand [ rxVrfId=" + rxVrfId + ", gatewayIpAddress=" + gatewayIpAddress
87             + ", serverIpAddresses=" + serverIpAddresses +  ", addressType=" + addressType
88             + ", operations=" + operation + "]";
89     }
90
91     public RelayBuilder getDhcpBuilder() {
92         return new RelayBuilder()
93             .setAddressType(addressType)
94             .setGatewayAddress(gatewayIpAddress)
95             .setKey(new RelayKey(addressType, rxVrfId))
96             .setRxVrfId(rxVrfId)
97             .setServer(serverIpAddresses);
98     }
99
100     public static class DhcpRelayBuilder {
101
102         private General.Operations operation;
103         private Long rxVrfId;
104         private IpAddress gatewayIpAddress;
105         private List<Server>  serverIpAddress;
106         private Class<? extends AddressFamily> addressType;
107
108         public General.Operations getOperation() {
109             return operation;
110         }
111
112         public DhcpRelayBuilder setOperation(General.Operations operation) {
113             this.operation = operation;
114             return this;
115         }
116
117         public Long getRxVrfId() {
118             return rxVrfId;
119         }
120
121         public DhcpRelayBuilder setRxVrfId(Long rxVrfId) {
122             this.rxVrfId = rxVrfId;
123             return this;
124         }
125
126         public IpAddress getGatewayIpAddress() {
127             return gatewayIpAddress;
128         }
129
130         public DhcpRelayBuilder setGatewayIpAddress(IpAddress gatewayIpAddress) {
131             this.gatewayIpAddress = gatewayIpAddress;
132             return this;
133         }
134
135         public List<Server>  getServerIpAddresses() {
136             return serverIpAddress;
137         }
138
139         public DhcpRelayBuilder setServerIpAddresses(List<Server>  serverIpAddress) {
140             this.serverIpAddress = serverIpAddress;
141             return this;
142         }
143
144         public Class<? extends AddressFamily> getAddressType() {
145             return addressType;
146         }
147
148         public DhcpRelayBuilder setAddressType(Class<? extends AddressFamily> addressType) {
149             this.addressType = addressType;
150             return this;
151         }
152
153         /**
154          * RoutingCommand build method.
155          *
156          * @return RoutingCommand
157          * @throws IllegalArgumentException if routerProtocol, operation or rxVrfId is null.
158          */
159         public DhcpRelayCommand build() {
160             Preconditions.checkArgument(this.operation != null);
161             Preconditions.checkArgument(this.rxVrfId != null);
162             Preconditions.checkArgument(this.addressType != null);
163
164             return new DhcpRelayCommand(this);
165         }
166     }
167 }