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