Update DAO API
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LfmMappingDatabaseRpc.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc.  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 package org.opendaylight.lispflowmapping.implementation;
9
10 import java.util.concurrent.Future;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
14 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
15 import org.opendaylight.lispflowmapping.lisp.util.MapServerMapResolverUtil;
16 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapReply;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRequest;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.AddKeyInput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.AddKeyInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.AddMappingInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.AddMappingInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetKeyInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetKeyInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetKeyOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetKeyOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetMappingInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetMappingInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetMappingOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.GetMappingOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.MappingserviceService;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.RemoveKeyInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.RemoveKeyInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.RemoveMappingInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.RemoveMappingInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.UpdateKeyInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.UpdateKeyInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.UpdateMappingInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.UpdateMappingInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.update.key.input.EidBuilder;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 import com.google.common.base.Preconditions;
48 import com.google.common.util.concurrent.Futures;
49
50 /**
51  * Implementation of the RPCs defined in lfm-mapping-database.yang
52  *
53  * @author Lorand Jakab
54  * @author Florin Coras
55  *
56  */
57 public class LfmMappingDatabaseRpc implements MappingserviceService {
58     protected static final Logger LOG = LoggerFactory.getLogger(LfmMappingDatabaseRpc.class);
59     private static final String NOT_FOUND_TAG = "data-missing";
60     private static final String DATA_EXISTS_TAG = "data-exists";
61
62     private LispMappingService lispMappingService;
63     private DataBroker dataBroker;
64     private DataStoreBackEnd dsbe;
65
66     public LfmMappingDatabaseRpc(DataBroker dataBroker) {
67         this.lispMappingService = LispMappingService.getLispMappingService();
68         this.dataBroker = dataBroker;
69         this.dsbe = new DataStoreBackEnd(this.dataBroker);
70         LOG.debug("LfmMappingDatabaseProviderRpc created!");
71     }
72
73     @Override
74     public Future<RpcResult<Void>> addKey(AddKeyInput input) {
75         Preconditions.checkNotNull(input, "add-key RPC input must be not null!");
76         LOG.trace("RPC received to add the following key: " + input.toString());
77
78         RpcResultBuilder<Void> rpcResultBuilder;
79
80         // XXX: to remove once RPC API has been updated to remove explicit mask
81         input = new AddKeyInputBuilder(input).setLispAddressContainer(
82                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
83
84         String key = lispMappingService.getAuthenticationKey(input.getLispAddressContainer());
85
86         if (key != null) {
87             String message = "Key already exists! Please use update-key if you want to change it.";
88             rpcResultBuilder = RpcResultBuilder.<Void>failed()
89                     .withError(RpcError.ErrorType.PROTOCOL, DATA_EXISTS_TAG, message);
90             return Futures.immediateFuture(rpcResultBuilder.build());
91         }
92
93         dsbe.addAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
94         rpcResultBuilder = RpcResultBuilder.success();
95
96         return Futures.immediateFuture(rpcResultBuilder.build());
97     }
98
99     @Override
100     public Future<RpcResult<Void>> addMapping(AddMappingInput input) {
101         Preconditions.checkNotNull(input, "add-mapping RPC input must be not null!");
102         LOG.trace("RPC received to add the following mapping: " + input.toString());
103
104         // XXX: to remove once RPC API has been updated to remove explicit mask
105         input = new AddMappingInputBuilder(input).setLispAddressContainer(
106                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
107
108         dsbe.addMapping(RPCInputConvertorUtil.toMapping(input));
109
110         RpcResultBuilder<Void> rpcResultBuilder;
111
112         rpcResultBuilder = RpcResultBuilder.success();
113
114         return Futures.immediateFuture(rpcResultBuilder.build());
115     }
116
117     @Override
118     public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
119         Preconditions.checkNotNull(input, "get-key RPC input must be not null!");
120         LOG.trace("RPC received to get the following key: " + input.toString());
121
122         // XXX: to remove once RPC API has been updated to remove explicit mask
123         input = new GetKeyInputBuilder(input).setLispAddressContainer(
124                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
125
126         RpcResultBuilder<GetKeyOutput> rpcResultBuilder;
127
128         String key = lispMappingService.getAuthenticationKey(input.getLispAddressContainer());
129
130         if (key == null) {
131             String message = "Key was not found in the mapping database";
132             rpcResultBuilder = RpcResultBuilder.<GetKeyOutput>failed()
133                     .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
134         } else {
135             rpcResultBuilder = RpcResultBuilder.success(new GetKeyOutputBuilder().setAuthkey(key));
136         }
137
138         return Futures.immediateFuture(rpcResultBuilder.build());
139     }
140
141     @Override
142     public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
143         Preconditions.checkNotNull(input, "get-mapping RPC input must be not null!");
144         LOG.trace("RPC received to get the following mapping: " + input.toString());
145
146         // XXX: to remove once RPC API has been updated to remove explicit mask
147         input = new GetMappingInputBuilder(input).setLispAddressContainer(
148                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
149
150         RpcResultBuilder<GetMappingOutput> rpcResultBuilder;
151
152         MapRequest request = MapServerMapResolverUtil.getMapRequest(input.getLispAddressContainer(),
153                 input.getMaskLength());
154         MapReply reply = lispMappingService.handleMapRequest(request, false);
155
156         if (reply == null) {
157             String message = "No mapping was found in the mapping database";
158             rpcResultBuilder = RpcResultBuilder.<GetMappingOutput>failed()
159                     .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
160         } else {
161             rpcResultBuilder = RpcResultBuilder.success(new GetMappingOutputBuilder()
162             .setEidToLocatorRecord(reply.getEidToLocatorRecord()));
163         }
164
165         return Futures.immediateFuture(rpcResultBuilder.build());
166     }
167
168     @Override
169     public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
170         Preconditions.checkNotNull(input, "remove-key RPC input must be not null!");
171         LOG.trace("RPC received to remove the following key: " + input.toString());
172
173         // XXX: to remove once RPC API has been updated to remove explicit mask
174         input = new RemoveKeyInputBuilder(input).setLispAddressContainer(
175                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
176
177         RpcResultBuilder<Void> rpcResultBuilder;
178
179         dsbe.removeAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
180
181         rpcResultBuilder = RpcResultBuilder.success();
182
183         return Futures.immediateFuture(rpcResultBuilder.build());
184     }
185
186     @Override
187     public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
188         Preconditions.checkNotNull(input, "remove-mapping RPC input must be not null!");
189         LOG.trace("RPC received to remove the following mapping: " + input.toString());
190
191         // XXX: to remove once RPC API has been updated to remove explicit mask
192         input = new RemoveMappingInputBuilder(input).setLispAddressContainer(
193                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
194
195         RpcResultBuilder<Void> rpcResultBuilder;
196
197         dsbe.removeMapping(RPCInputConvertorUtil.toMapping(input));
198
199         rpcResultBuilder = RpcResultBuilder.success();
200
201         return Futures.immediateFuture(rpcResultBuilder.build());
202     }
203
204     @Override
205     public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
206         Preconditions.checkNotNull(input, "update-key RPC input must be not null!");
207         LOG.trace("RPC received to update the following key: " + input.toString());
208
209         // XXX: to remove once RPC API has been updated to remove explicit mask
210         input = new UpdateKeyInputBuilder(input).setEid(
211                 new EidBuilder(input.getEid()).setLispAddressContainer(
212                         MaskUtil.setMask(input.getEid().getLispAddressContainer(), input.getEid()
213                                 .getMaskLength())).build()).build();
214
215         RpcResultBuilder<Void> rpcResultBuilder;
216
217         String key = lispMappingService.getAuthenticationKey(input.getEid().getLispAddressContainer());
218
219         if (key == null) {
220             String message = "Key doesn't exist! Please use add-key if you want to create a new authentication key.";
221             rpcResultBuilder = RpcResultBuilder.<Void>failed()
222                     .withError(RpcError.ErrorType.PROTOCOL, NOT_FOUND_TAG, message);
223             return Futures.immediateFuture(rpcResultBuilder.build());
224         }
225
226         dsbe.updateAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
227         rpcResultBuilder = RpcResultBuilder.success();
228
229         return Futures.immediateFuture(rpcResultBuilder.build());
230     }
231
232     @Override
233     public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
234         LOG.trace("RPC received to update the following mapping: " + input.toString());
235         Preconditions.checkNotNull(input, "update-mapping RPC input must be not null!");
236
237         // XXX: to remove once RPC API has been updated to remove explicit mask
238         input = new UpdateMappingInputBuilder(input).setLispAddressContainer(
239                 MaskUtil.setMask(input.getLispAddressContainer(), input.getMaskLength())).build();
240
241         RpcResultBuilder<Void> rpcResultBuilder;
242
243         dsbe.updateMapping(RPCInputConvertorUtil.toMapping(input));
244
245         rpcResultBuilder = RpcResultBuilder.success();
246
247         return Futures.immediateFuture(rpcResultBuilder.build());
248     }
249
250     public void setLispMappingService(LispMappingService lispMappingService) {
251         this.lispMappingService = lispMappingService;
252     }
253 }