2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. All rights reserved.
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
8 package org.opendaylight.lispflowmapping.implementation;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.Futures;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.concurrent.Future;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
17 import org.opendaylight.lispflowmapping.config.ConfigIni;
18 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
19 import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener;
20 import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener;
21 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
22 import org.opendaylight.lispflowmapping.implementation.util.RPCInputConvertorUtil;
23 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
24 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
25 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
26 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeyInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddKeysInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.AddMappingsInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetAllKeysOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetAllMappingsOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeyOutputBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeysInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetKeysOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingOutputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingWithXtrIdInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingWithXtrIdOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingWithXtrIdOutputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingsInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.GetMappingsOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceService;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeyInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveKeysInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.RemoveMappingsInput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeyInput;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateKeysInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingInput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.UpdateMappingsInput;
64 import org.opendaylight.yangtools.yang.common.RpcError;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
71 * Dispatcher of API calls that implements the RPC and Java APIs in mappingservice.yang and IMappingService
72 * respectively. It also coordinates and acts as container for all objects in charge of
73 * - saving and updating md-sal datastore stored mappings
74 * - monitoring md-sal datastore mapping updates and pushing them to the in memory mapping-system
75 * - in memory mapping-system
77 * @author Lorand Jakab
78 * @author Florin Coras
81 public class MappingService implements OdlMappingserviceService, IMappingService, AutoCloseable {
82 protected static final Logger LOG = LoggerFactory.getLogger(MappingService.class);
83 private static final String NOT_FOUND_TAG = "data-missing";
84 private static final String DATA_EXISTS_TAG = "data-exists";
86 private MappingSystem mappingSystem;
87 private DataStoreBackEnd dsbe;
88 private AuthenticationKeyDataListener keyListener;
89 private MappingDataListener mappingListener;
90 private final ILispDAO dao;
92 private final DataBroker dataBroker;
93 private final NotificationPublishService notificationPublishService;
95 private boolean mappingMergePolicy = ConfigIni.getInstance().mappingMergeIsSet();
96 private boolean notificationPolicy = ConfigIni.getInstance().smrIsSet();
97 private boolean iterateMask = true;
98 private boolean isMaster = false;
100 public MappingService(final DataBroker broker,
101 final NotificationPublishService notificationPublishService,
102 final ILispDAO lispDAO) {
103 this.dataBroker = broker;
104 this.notificationPublishService = notificationPublishService;
107 LOG.debug("MappingService created!");
112 public void setMappingMerge(boolean mergeMapping) {
113 this.mappingMergePolicy = mergeMapping;
114 if (mappingSystem != null) {
115 mappingSystem.setMappingMerge(mergeMapping);
116 ConfigIni.getInstance().setMappingMerge(mappingMergePolicy);
121 public void setLookupPolicy(IMappingService.LookupPolicy policy) {
122 ConfigIni.getInstance().setLookupPolicy(policy);
125 public void initialize() {
126 LOG.info("Mapping Service initializing...");
127 dsbe = new DataStoreBackEnd(dataBroker);
129 mappingSystem = new MappingSystem(dao, iterateMask, notificationPublishService, mappingMergePolicy);
130 mappingSystem.setDataStoreBackEnd(dsbe);
131 mappingSystem.initialize();
133 keyListener = new AuthenticationKeyDataListener(dataBroker, mappingSystem);
134 mappingListener = new MappingDataListener(dataBroker, mappingSystem, notificationPublishService);
135 LOG.info("Mapping Service loaded.");
139 public Future<RpcResult<Void>> addKey(AddKeyInput input) {
140 Preconditions.checkNotNull(input, "add-key RPC input must be not null!");
141 LOG.trace("RPC received to add the following key: " + input.toString());
143 RpcResultBuilder<Void> rpcResultBuilder;
145 MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
148 String message = "Key already exists! Please use update-key if you want to change it.";
149 rpcResultBuilder = RpcResultBuilder.<Void>failed()
150 .withError(RpcError.ErrorType.PROTOCOL, DATA_EXISTS_TAG, message);
151 return Futures.immediateFuture(rpcResultBuilder.build());
154 dsbe.addAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
155 rpcResultBuilder = RpcResultBuilder.success();
157 return Futures.immediateFuture(rpcResultBuilder.build());
161 public Future<RpcResult<Void>> addMapping(AddMappingInput input) {
162 Preconditions.checkNotNull(input, "add-mapping RPC input must be not null!");
163 LOG.trace("RPC received to add the following mapping: " + input.toString());
165 dsbe.addMapping(RPCInputConvertorUtil.toMapping(input));
167 RpcResultBuilder<Void> rpcResultBuilder;
169 rpcResultBuilder = RpcResultBuilder.success();
171 return Futures.immediateFuture(rpcResultBuilder.build());
175 public void addMapping(MappingOrigin origin, Eid key, SiteId siteId, MappingData mappingData) {
176 // SB registrations are first written to the MappingSystem and only afterwards are persisted to the datastore
177 if (origin.equals(MappingOrigin.Southbound)) {
178 // Store data first in MapCache and only afterwards persist to datastore. This should be used only for SB
180 mappingSystem.addMapping(origin, key, mappingData);
181 dsbe.addMapping(DSBEInputUtil.toMapping(origin, key, siteId, mappingData));
182 if (mappingData.getXtrId() != null) {
183 dsbe.addXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
186 dsbe.addMapping(DSBEInputUtil.toMapping(origin, key, siteId, mappingData));
191 public MappingData addNegativeMapping(Eid key) {
192 return mappingSystem.addNegativeMapping(key);
196 public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
197 mappingSystem.refreshMappingRegistration(key, xtrId, timestamp);
201 public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
202 Preconditions.checkNotNull(input, "get-key RPC input must be not null!");
203 LOG.trace("RPC received to get the following key: " + input.toString());
205 RpcResultBuilder<GetKeyOutput> rpcResultBuilder;
207 MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
210 String message = "Key was not found in the mapping database";
211 rpcResultBuilder = RpcResultBuilder.<GetKeyOutput>failed()
212 .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
214 rpcResultBuilder = RpcResultBuilder.success(new GetKeyOutputBuilder().setMappingAuthkey(key));
217 return Futures.immediateFuture(rpcResultBuilder.build());
221 public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
222 Preconditions.checkNotNull(input, "get-mapping RPC input must be not null!");
223 LOG.trace("RPC received to get the following mapping: " + input.toString());
225 RpcResultBuilder<GetMappingOutput> rpcResultBuilder;
227 MappingData reply = mappingSystem.getMapping(convertToBinaryIfNecessary(input.getEid()));
230 String message = "No mapping was found in the mapping database";
231 rpcResultBuilder = RpcResultBuilder.<GetMappingOutput>failed()
232 .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
234 final MappingRecord convertedReply = convertFromBinaryIfNecessary(reply.getRecord());
235 rpcResultBuilder = RpcResultBuilder.success(new GetMappingOutputBuilder().setMappingRecord(convertedReply));
238 return Futures.immediateFuture(rpcResultBuilder.build());
242 public MappingData getMapping(MappingOrigin origin, Eid key) {
243 return mappingSystem.getMapping(origin, key);
247 public MappingData getMapping(Eid key) {
248 return mappingSystem.getMapping(key);
252 public MappingData getMapping(Eid srcKey, Eid dstKey) {
253 return mappingSystem.getMapping(srcKey, dstKey);
257 public Future<RpcResult<GetMappingWithXtrIdOutput>> getMappingWithXtrId(GetMappingWithXtrIdInput input) {
258 Preconditions.checkNotNull(input, "get-mapping RPC input must be not null!");
259 LOG.trace("RPC received to get the following mapping: " + input.toString());
261 RpcResultBuilder<GetMappingWithXtrIdOutput> rpcResultBuilder;
263 MappingData reply = mappingSystem.getMapping(null, convertToBinaryIfNecessary(input.getEid()),
267 String message = "No mapping was found in the mapping database";
268 rpcResultBuilder = RpcResultBuilder.<GetMappingWithXtrIdOutput>failed()
269 .withError(RpcError.ErrorType.APPLICATION, NOT_FOUND_TAG, message);
271 final MappingRecord convertedReply = convertFromBinaryIfNecessary(reply.getRecord());
272 rpcResultBuilder = RpcResultBuilder.success(new GetMappingWithXtrIdOutputBuilder()
273 .setMappingRecord(convertedReply));
276 return Futures.immediateFuture(rpcResultBuilder.build());
280 public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
281 Preconditions.checkNotNull(input, "remove-key RPC input must be not null!");
282 LOG.trace("RPC received to remove the following key: " + input.toString());
284 RpcResultBuilder<Void> rpcResultBuilder;
286 dsbe.removeAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
288 rpcResultBuilder = RpcResultBuilder.success();
290 return Futures.immediateFuture(rpcResultBuilder.build());
294 public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
295 Preconditions.checkNotNull(input, "remove-mapping RPC input must be not null!");
296 LOG.trace("RPC received to remove the following mapping: " + input.toString());
298 RpcResultBuilder<Void> rpcResultBuilder;
300 dsbe.removeMapping(RPCInputConvertorUtil.toMapping(input));
302 rpcResultBuilder = RpcResultBuilder.success();
304 return Futures.immediateFuture(rpcResultBuilder.build());
308 public void removeMapping(MappingOrigin origin, Eid key) {
309 dsbe.removeMapping(DSBEInputUtil.toMapping(origin, key));
313 public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
314 Preconditions.checkNotNull(input, "update-key RPC input must be not null!");
315 LOG.trace("RPC received to update the following key: " + input.toString());
317 RpcResultBuilder<Void> rpcResultBuilder;
319 MappingAuthkey key = mappingSystem.getAuthenticationKey(convertToBinaryIfNecessary(input.getEid()));
322 String message = "Key doesn't exist! Please use add-key if you want to create a new authentication key.";
323 rpcResultBuilder = RpcResultBuilder.<Void>failed()
324 .withError(RpcError.ErrorType.PROTOCOL, NOT_FOUND_TAG, message);
325 return Futures.immediateFuture(rpcResultBuilder.build());
328 dsbe.updateAuthenticationKey(RPCInputConvertorUtil.toAuthenticationKey(input));
329 rpcResultBuilder = RpcResultBuilder.success();
331 return Futures.immediateFuture(rpcResultBuilder.build());
335 public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
336 LOG.trace("RPC received to update the following mapping: " + input.toString());
337 Preconditions.checkNotNull(input, "update-mapping RPC input must be not null!");
339 RpcResultBuilder<Void> rpcResultBuilder;
341 dsbe.updateMapping(RPCInputConvertorUtil.toMapping(input));
343 rpcResultBuilder = RpcResultBuilder.success();
345 return Futures.immediateFuture(rpcResultBuilder.build());
349 public Future<RpcResult<Void>> removeKeys(RemoveKeysInput input) {
350 // TODO Auto-generated method stub
355 public Future<RpcResult<Void>> removeMappings(RemoveMappingsInput input) {
356 // TODO Auto-generated method stub
361 public Future<RpcResult<GetKeysOutput>> getKeys(GetKeysInput input) {
362 // TODO Auto-generated method stub
367 public Future<RpcResult<Void>> addMappings(AddMappingsInput input) {
368 // TODO Auto-generated method stub
373 public Future<RpcResult<Void>> updateKeys(UpdateKeysInput input) {
374 // TODO Auto-generated method stub
379 public Future<RpcResult<Void>> removeAllMappings() {
380 // TODO Auto-generated method stub
385 public Future<RpcResult<Void>> removeAllKeys() {
386 // TODO Auto-generated method stub
391 public Future<RpcResult<GetAllKeysOutput>> getAllKeys() {
392 // TODO Auto-generated method stub
397 public Future<RpcResult<Void>> updateMappings(UpdateMappingsInput input) {
398 // TODO Auto-generated method stub
403 public Future<RpcResult<Void>> addKeys(AddKeysInput input) {
404 // TODO Auto-generated method stub
409 public Future<RpcResult<GetAllMappingsOutput>> getAllMappings() {
410 // TODO Auto-generated method stub
415 public Future<RpcResult<GetMappingsOutput>> getMappings(
416 GetMappingsInput input) {
417 // TODO Auto-generated method stub
422 public Future<RpcResult<Void>> removeAllOperationalContent() {
423 RpcResultBuilder<Void> rpcResultBuilder;
426 * Since master nodes ignore datastore changes for southbound originated mappings, they need to be removed
430 mappingSystem.cleanSBMappings();
432 dsbe.removeAllOperationalDatastoreContent();
434 rpcResultBuilder = RpcResultBuilder.success();
436 return Futures.immediateFuture(rpcResultBuilder.build());
440 public Eid getWidestNegativePrefix(Eid key) {
441 return mappingSystem.getWidestNegativePrefix(key);
445 public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
446 dsbe.addAuthenticationKey(DSBEInputUtil.toAuthenticationKey(key, authKey));
450 public MappingAuthkey getAuthenticationKey(Eid key) {
451 return mappingSystem.getAuthenticationKey(key);
455 public void removeAuthenticationKey(Eid key) {
456 dsbe.removeAuthenticationKey(DSBEInputUtil.toAuthenticationKey(key, null));
460 public void addData(MappingOrigin origin, Eid key, String subKey, Object data) {
461 mappingSystem.addData(origin, key, subKey, data);
465 public Object getData(MappingOrigin origin, Eid key, String subKey) {
466 return mappingSystem.getData(origin, key, subKey);
470 public void removeData(MappingOrigin origin, Eid key, String subKey) {
471 mappingSystem.removeData(origin, key, subKey);
475 public Eid getParentPrefix(Eid key) {
476 return mappingSystem.getParentPrefix(key);
480 public String printMappings() {
481 return mappingSystem.printMappings();
485 public String prettyPrintMappings() {
486 return mappingSystem.prettyPrintMappings();
490 public String printKeys() {
491 return mappingSystem.printKeys();
495 public String prettyPrintKeys() {
496 return mappingSystem.prettyPrintKeys();
500 public void close() throws Exception {
501 LOG.info("Mapping Service is being destroyed!");
502 keyListener.closeDataChangeListener();
503 mappingListener.closeDataChangeListener();
504 mappingSystem.destroy();
508 public void cleanCachedMappings() {
509 mappingSystem.cleanCaches();
510 dsbe.removeAllDatastoreContent();
513 private static Eid convertToBinaryIfNecessary(Eid eid) {
514 if (LispAddressUtil.addressNeedsConversionToBinary(eid.getAddress())) {
515 return LispAddressUtil.convertToBinary(eid);
520 private static MappingRecord convertFromBinaryIfNecessary(MappingRecord originalRecord) {
521 List<LocatorRecord> originalLocators = originalRecord.getLocatorRecord();
523 List<LocatorRecord> convertedLocators = null;
524 if (originalLocators != null) {
525 // If convertedLocators is non-null, while originalLocators is also non-null, conversion has been made
526 convertedLocators = convertFromBinaryIfNecessary(originalLocators);
529 if (LispAddressUtil.addressNeedsConversionFromBinary(originalRecord.getEid().getAddress())
530 || (originalLocators != null && convertedLocators != null)) {
531 MappingRecordBuilder mrb = new MappingRecordBuilder(originalRecord);
532 mrb.setEid(LispAddressUtil.convertFromBinary(originalRecord.getEid()));
533 if (convertedLocators != null) {
534 mrb.setLocatorRecord(convertedLocators);
538 return originalRecord;
541 private static List<LocatorRecord> convertFromBinaryIfNecessary(List<LocatorRecord> originalLocators) {
542 List<LocatorRecord> convertedLocators = null;
543 for (LocatorRecord record : originalLocators) {
544 if (LispAddressUtil.addressNeedsConversionFromBinary(record.getRloc().getAddress())) {
545 LocatorRecordBuilder lrb = new LocatorRecordBuilder(record);
546 lrb.setRloc(LispAddressUtil.convertFromBinary(record.getRloc()));
547 if (convertedLocators == null) {
548 convertedLocators = new ArrayList<LocatorRecord>();
550 convertedLocators.add(lrb.build());
553 if (convertedLocators != null) {
554 return convertedLocators;
556 return originalLocators;
560 public void setIsMaster(boolean isMaster) {
561 this.isMaster = isMaster;
562 mappingSystem.setIsMaster(isMaster);
566 public boolean isMaster() {