Prepare the YANG models for RESTCONF
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / AbstractLispComponent.java
1 /*
2  * Copyright (c) 2014 Contextream, 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 package org.opendaylight.lispflowmapping.implementation.lisp;
9
10 import java.util.HashSet;
11
12 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
13 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
14 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
15 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceSubscriberRLOC;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
17
18 public abstract class AbstractLispComponent {
19
20     public static final String PASSWORD_SUBKEY = "password";
21     public static final String ADDRESS_SUBKEY = "address";
22     public static final String SUBSCRIBERS_SUBKEY = "subscribers";
23
24     protected ILispDAO dao;
25     protected volatile boolean iterateMask;
26     protected volatile boolean authenticate;
27
28     public AbstractLispComponent(ILispDAO dao, boolean authenticate, boolean iterateMask) {
29         this.dao = dao;
30         this.iterateMask = iterateMask;
31         this.authenticate = authenticate;
32     }
33
34     public boolean shouldIterateMask() {
35         return iterateMask;
36     }
37
38     public void setShouldIterateMask(boolean shouldIterateMask) {
39         this.iterateMask = shouldIterateMask;
40     }
41
42     public boolean shouldAuthenticate() {
43         return authenticate;
44     }
45
46     public void setShouldAuthenticate(boolean shouldAuthenticate) {
47         this.authenticate = shouldAuthenticate;
48     }
49
50     protected String getPassword(LispAddressContainer prefix, int maskLength) {
51         while (maskLength >= 0) {
52             IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
53             Object password = dao.getSpecific(key, PASSWORD_SUBKEY);
54             if (password != null && password instanceof String) {
55                 return (String) password;
56             } else if (shouldIterateMask()) {
57                 maskLength -= 1;
58             } else {
59                 return null;
60             }
61         }
62         return null;
63     }
64
65     @SuppressWarnings("unchecked")
66     protected HashSet<MappingServiceSubscriberRLOC> getSubscribers(LispAddressContainer prefix, int maskLength) {
67         IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(prefix, maskLength);
68         Object subscribers = dao.getSpecific(key, SUBSCRIBERS_SUBKEY);
69         if (subscribers != null && subscribers instanceof HashSet<?>) {
70             return (HashSet<MappingServiceSubscriberRLOC>) subscribers;
71         }
72         return null;
73     }
74 }