Feature uses features-parent as parent
[groupbasedpolicy.git] / renderers / opflex / src / main / java / org / opendaylight / groupbasedpolicy / renderer / opflex / L3EprOperation.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others. 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.opflex;
10
11 import java.util.List;
12 import java.util.concurrent.ScheduledExecutorService;
13
14 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.Endpoints;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.OpflexOverlayContextL3;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.opflex.rev140528.OpflexOverlayContextL3Builder;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 import com.google.common.base.Optional;
32 import com.google.common.util.concurrent.FutureCallback;
33 import com.google.common.util.concurrent.Futures;
34 import com.google.common.util.concurrent.ListenableFuture;
35
36 /**
37  * A context for mapping OpFlex messaging to asynchronous
38  * requests to the Endpoint Registry's list of L3 Endpoints.
39  *
40  * @author tbachman
41  */
42 public class L3EprOperation implements EprOperation, FutureCallback<Optional<EndpointL3>> {
43
44     private EprOpCallback cb;
45     private EndpointL3 ep;
46     private InstanceIdentifier<EndpointL3> iid;
47
48     private String agentId;
49     private TenantId tid;
50     private EndpointGroupId egid;
51     private MacAddress mac;
52     private List<L3Address> l3al;
53     private L2BridgeDomainId l2bdid;
54     private L3ContextId l3cid;
55     private IpAddress ip;
56     private Long timeout;
57
58     public L3EprOperation(int prr) {
59         this.timeout = Long.valueOf(prr);
60     }
61
62     public L3EprOperation() {}
63
64     public void setAgentId(String agentId) {
65         this.agentId = agentId;
66     }
67
68     public void setTenantId(TenantId tid) {
69         this.tid = tid;
70     }
71
72     public void setEndpointGroupId(EndpointGroupId egid) {
73         this.egid = egid;
74     }
75
76     public void setContextId(L3ContextId l3cid) {
77         this.l3cid = l3cid;
78     }
79
80     public void setL2BridgDomainId(L2BridgeDomainId l2bdid) {
81         this.l2bdid = l2bdid;
82     }
83
84     public void setMacAddress(MacAddress mac) {
85         this.mac = mac;
86     }
87
88     public void setIpAddress(IpAddress ip) {
89         this.ip = ip;
90     }
91
92     public void setL3AddressList(List<L3Address> l3al) {
93         this.l3al = l3al;
94     }
95
96     public void addL3Address(L3Address l3a) {
97         this.l3al.add(l3a);
98     }
99
100     public EndpointL3 getEp() {
101         return ep;
102     }
103
104     public void setEp(EndpointL3 ep) {
105         this.ep = ep;
106     }
107
108     public EndpointL3 buildEp() {
109         EndpointL3Builder epBuilder = new EndpointL3Builder();
110         OpflexOverlayContextL3Builder oocb = new OpflexOverlayContextL3Builder();
111         oocb.setAgentId(this.agentId);
112
113         epBuilder.setTenant(this.tid)
114             .setEndpointGroup(this.egid)
115             .setL2Context(this.l2bdid)
116             .setL3Context(this.l3cid)
117             .setL3Address(l3al)
118             .setMacAddress(this.mac)
119             .setIpAddress(this.ip)
120             .setTimestamp(this.timeout)
121             .addAugmentation(OpflexOverlayContextL3.class, oocb.build());
122
123         // TODO: add support for conditions
124         // epBuilder.setCondition(new List<ConditionName>());
125
126         return epBuilder.build();
127     }
128
129     @Override
130     public void onSuccess(final Optional<EndpointL3> result) {
131         if (!result.isPresent()) {
132             /*
133              * This EP doesn't exist in the registry. If
134              * all of the data store queries have been made,
135              * and we still don't have any EPs, then provide
136              * an error result.
137              */
138             this.ep = null;
139             cb.callback(this);
140             return;
141         }
142         setEp(result.get());
143         cb.callback(this);
144     }
145
146     @Override
147     public void onFailure(Throwable t) {
148         // TODO: implement another callback
149     }
150
151     @Override
152     public void put(WriteTransaction wt) {
153         ep = buildEp();
154         this.iid = InstanceIdentifier.builder(Endpoints.class).child(EndpointL3.class, ep.getKey()).build();
155         wt.put(LogicalDatastoreType.OPERATIONAL, iid, ep);
156     }
157
158     @Override
159     public void delete(WriteTransaction wt) {
160         ep = buildEp();
161         this.iid = InstanceIdentifier.builder(Endpoints.class).child(EndpointL3.class, ep.getKey()).build();
162         wt.delete(LogicalDatastoreType.OPERATIONAL, iid);
163     }
164
165     @Override
166     public void read(ReadOnlyTransaction rot, ScheduledExecutorService executor) {
167         ep = buildEp();
168         this.iid = InstanceIdentifier.builder(Endpoints.class).child(EndpointL3.class, ep.getKey()).build();
169
170         ListenableFuture<Optional<EndpointL3>> dao = rot.read(LogicalDatastoreType.OPERATIONAL, iid);
171         Futures.addCallback(dao, this, executor);
172
173     }
174
175     @Override
176     public void setCallback(EprOpCallback callback) {
177         this.cb = callback;
178     }
179
180 }