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