2 * Copyright (c) 2014 Contextream, Inc. and others. 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.lisp;
12 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
13 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
14 import org.opendaylight.lispflowmapping.implementation.util.MaskUtil;
15 import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
16 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
17 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
18 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceSubscriberRLOC;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafSourceDest;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public abstract class AbstractLispComponent {
28 public static final String PASSWORD_SUBKEY = "password";
29 public static final String ADDRESS_SUBKEY = "address";
30 public static final String SUBSCRIBERS_SUBKEY = "subscribers";
31 public static final String LCAF_SRCDST_SUBKEY = "lcaf_srcdst";
32 public static final Logger LOG = LoggerFactory.getLogger(AbstractLispComponent.class);
34 protected ILispDAO dao;
35 protected volatile boolean iterateMask;
36 protected volatile boolean authenticate;
38 public AbstractLispComponent(ILispDAO dao, boolean authenticate, boolean iterateMask) {
40 this.iterateMask = iterateMask;
41 this.authenticate = authenticate;
44 public boolean shouldIterateMask() {
48 public void setShouldIterateMask(boolean shouldIterateMask) {
49 this.iterateMask = shouldIterateMask;
52 public boolean shouldAuthenticate() {
56 public void setShouldAuthenticate(boolean shouldAuthenticate) {
57 this.authenticate = shouldAuthenticate;
60 protected String getPassword(LispAddressContainer prefix, int maskLength) {
61 if (MaskUtil.isMaskable(LispAFIConvertor.toAFI(prefix))) {
62 return getPasswordForMaskable(prefix, maskLength, dao);
63 } else if (prefix.getAddress() instanceof LcafSourceDest) {
64 ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength);
65 if (srcDstDao != null) {
66 return getPasswordForMaskable(LispAFIConvertor.toContainer(getSrcForLcafSrcDst(prefix)),
67 getSrcMaskForLcafSrcDst(prefix), srcDstDao);
71 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
72 Object password = dao.getSpecific(key, PASSWORD_SUBKEY);
73 if (password != null && password instanceof String) {
74 return (String) password;
76 LOG.warn("Failed to find password!");
82 private String getPasswordForMaskable(LispAddressContainer prefix, int maskLength, ILispDAO db) {
83 while (maskLength >= 0) {
84 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
85 Object password = db.getSpecific(key, PASSWORD_SUBKEY);
86 if (password != null && password instanceof String) {
87 return (String) password;
88 } else if (shouldIterateMask()) {
91 LOG.warn("Failed to find password!");
98 @SuppressWarnings("unchecked")
99 protected Set<MappingServiceSubscriberRLOC> getSubscribers(LispAddressContainer prefix, int maskLength) {
101 if (prefix.getAddress() instanceof LcafSourceDest) {
102 IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(getSrcForLcafSrcDst(prefix),
103 getSrcMaskForLcafSrcDst(prefix));
104 ILispDAO srcDstDao = getSrcDstInnerDao(prefix, maskLength);
105 subscribers = srcDstDao.getSpecific(srcKey, SUBSCRIBERS_SUBKEY);
107 IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
108 subscribers = dao.getSpecific(key, SUBSCRIBERS_SUBKEY);
111 if (subscribers != null && subscribers instanceof Set<?>) {
112 return (Set<MappingServiceSubscriberRLOC>) subscribers;
117 protected static LispAFIAddress getSrcForLcafSrcDst(LispAddressContainer container) {
118 return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().
119 getSrcAddress().getPrimitiveAddress());
122 protected static LispAFIAddress getDstForLcafSrcDst(LispAddressContainer container) {
123 return LispAFIConvertor.toAFIfromPrimitive(((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().
124 getDstAddress().getPrimitiveAddress());
127 protected static short getSrcMaskForLcafSrcDst(LispAddressContainer container) {
128 return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getSrcMaskLength();
131 protected static short getDstMaskForLcafSrcDst(LispAddressContainer container) {
132 return ((LcafSourceDest) container.getAddress()).getLcafSourceDestAddr().getDstMaskLength();
135 // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src. This method returns the DAO
136 // associated to a dst or creates it if it doesn't exist.
137 protected ILispDAO getOrInstantiateSrcDstInnerDao(LispAddressContainer address, int maskLen) {
138 IMappingServiceKey dstKey = MappingServiceKeyUtil.generateMappingServiceKey(getDstForLcafSrcDst(address),
139 getDstMaskForLcafSrcDst(address));
140 ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, LCAF_SRCDST_SUBKEY);
141 if (srcDstDao == null) {
142 srcDstDao = new HashMapDb();
143 dao.put(dstKey, new MappingEntry<>(LCAF_SRCDST_SUBKEY, srcDstDao));
148 // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src. This method returns the DAO
149 // associated to a dst or null if it doesn't exist.
150 protected ILispDAO getSrcDstInnerDao(LispAddressContainer address, int maskLen) {
151 IMappingServiceKey dstKey = MappingServiceKeyUtil.generateMappingServiceKey(getDstForLcafSrcDst(address),
152 getDstMaskForLcafSrcDst(address));
153 return (ILispDAO) dao.getSpecific(dstKey, LCAF_SRCDST_SUBKEY);