Added range type to subject-feature-definition/parameter
[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 /**
38  * A context for mapping OpFlex messaging to asynchronous
39  * requests to the Endpoint Registry's list of L3 Endpoints.
40  *
41  * @author tbachman
42  *
43  */
44 public class L3EprOperation implements EprOperation, FutureCallback<Optional<EndpointL3>>{
45
46         private EprOpCallback cb;
47     private EndpointL3 ep;
48     private InstanceIdentifier<EndpointL3> iid;
49
50     private String agentId;
51     private TenantId tid;
52     private EndpointGroupId egid;
53     private MacAddress mac;
54     private List<L3Address> l3al;
55     private L2BridgeDomainId l2bdid;
56     private L3ContextId l3cid;
57     private IpAddress ip;
58     private Long timeout;
59
60     public L3EprOperation(int prr) {
61         this.timeout = Long.valueOf(prr);
62     }
63
64     public L3EprOperation() {
65     }
66
67     public void setAgentId(String agentId) {
68         this.agentId = agentId;
69     }
70
71     public void setTenantId(TenantId tid) {
72         this.tid = tid;
73     }
74
75     public void setEndpointGroupId(EndpointGroupId egid) {
76         this.egid = egid;
77     }
78
79     public void setContextId(L3ContextId l3cid) {
80         this.l3cid = l3cid;
81     }
82
83     public void setL2BridgDomainId(L2BridgeDomainId l2bdid) {
84         this.l2bdid = l2bdid;
85     }
86
87     public void setMacAddress(MacAddress mac) {
88         this.mac = mac;
89     }
90
91     public void setIpAddress(IpAddress ip) {
92         this.ip = ip;
93     }
94
95     public void setL3AddressList(List<L3Address> l3al) {
96         this.l3al = l3al;
97     }
98
99     public void addL3Address(L3Address l3a) {
100         this.l3al.add(l3a);
101     }
102     public EndpointL3 getEp() {
103         return ep;
104     }
105     public void setEp(EndpointL3 ep) {
106         this.ep = ep;
107     }
108
109     public EndpointL3 buildEp() {
110         EndpointL3Builder epBuilder = new EndpointL3Builder();
111         OpflexOverlayContextL3Builder oocb = new OpflexOverlayContextL3Builder();
112         oocb.setAgentId(this.agentId);
113
114         epBuilder.setTenant(this.tid)
115                  .setEndpointGroup(this.egid)
116                  .setL2Context(this.l2bdid)
117                  .setL3Context(this.l3cid)
118                  .setL3Address(l3al)
119                  .setMacAddress(this.mac)
120                  .setIpAddress(this.ip)
121                  .setTimestamp(this.timeout)
122                  .addAugmentation(OpflexOverlayContextL3.class, oocb.build());
123
124
125         // TODO: add support for conditions
126         //epBuilder.setCondition(new List<ConditionName>());
127
128         return epBuilder.build();
129     }
130
131     @Override
132     public void onSuccess(final Optional<EndpointL3> result) {
133         if (!result.isPresent()) {
134             /*
135              * This EP doesn't exist in the registry. If
136              * all of the data store queries have been made,
137              * and we still don't have any EPs, then provide
138              * an error result.
139              */
140             this.ep = null;
141             cb.callback(this);
142             return;
143         }
144         setEp(result.get());
145         cb.callback(this);
146     }
147
148
149     @Override
150     public void onFailure(Throwable t) {
151         // TODO: implement another callback
152     }
153
154         @Override
155         public void put(WriteTransaction wt) {
156         ep = buildEp();
157         this.iid = InstanceIdentifier.builder(Endpoints.class)
158                 .child(EndpointL3.class, ep.getKey())
159                 .build();
160         wt.put(LogicalDatastoreType.OPERATIONAL, iid, ep);
161         }
162
163         @Override
164         public void delete(WriteTransaction wt) {
165         ep = buildEp();
166         this.iid = InstanceIdentifier.builder(Endpoints.class)
167                 .child(EndpointL3.class, ep.getKey())
168                 .build();
169         wt.delete(LogicalDatastoreType.OPERATIONAL, iid);
170         }
171
172         @Override
173         public void read(ReadOnlyTransaction rot, ScheduledExecutorService executor) {
174         ep = buildEp();
175         this.iid = InstanceIdentifier.builder(Endpoints.class)
176                 .child(EndpointL3.class, ep.getKey())
177                 .build();
178
179         ListenableFuture<Optional<EndpointL3>> dao =
180                 rot.read(LogicalDatastoreType.OPERATIONAL, iid);
181         Futures.addCallback(dao, this, executor);
182
183         }
184         @Override
185         public void setCallback(EprOpCallback callback) {
186                 this.cb = callback;
187         }
188
189 }