Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 18 | import com.google.common.collect.Maps; |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.slf4j.Logger; |
| 21 | |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 22 | import java.util.Collection; |
| 23 | import java.util.Map; |
| 24 | |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 25 | import static org.slf4j.LoggerFactory.getLogger; |
| 26 | |
| 27 | /** |
| 28 | * LISP router factory which returns concrete router object for the physical |
| 29 | * LISP router in use. |
| 30 | */ |
| 31 | public final class LispRouterFactory { |
| 32 | |
| 33 | private final Logger log = getLogger(getClass()); |
| 34 | |
| 35 | private LispRouterAgent agent; |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 36 | private Map<LispRouterId, LispRouter> routerMap = Maps.newConcurrentMap(); |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 37 | |
| 38 | // non-instantiable (except for our Singleton) |
| 39 | private LispRouterFactory() { |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Configures LISP router agent only if it is not initialized. |
| 44 | * |
| 45 | * @param agent reference object of LISP router agent |
| 46 | */ |
| 47 | public void setAgent(LispRouterAgent agent) { |
| 48 | synchronized (agent) { |
| 49 | if (this.agent == null) { |
| 50 | this.agent = agent; |
| 51 | } else { |
| 52 | log.warn("LISP Router Agent has already been set."); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Cleans up LISP router agent. |
| 59 | */ |
| 60 | public void cleanAgent() { |
| 61 | synchronized (agent) { |
| 62 | if (this.agent != null) { |
| 63 | this.agent = null; |
| 64 | } else { |
| 65 | log.warn("LISP Router Agent is not configured."); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns a LISP router instance. |
| 72 | * |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 73 | * @param ipAddress IP address of LISP router |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 74 | * @return LISP router instance |
| 75 | */ |
Jian Li | b86d8ad | 2017-05-03 02:53:44 +0900 | [diff] [blame^] | 76 | public LispRouter getRouterInstance(IpAddress ipAddress) { |
| 77 | LispRouterId routerId = new LispRouterId(ipAddress); |
| 78 | if (!routerMap.containsKey(routerId)) { |
| 79 | LispRouter router = new DefaultLispRouter(routerId); |
| 80 | router.setAgent(agent); |
| 81 | routerMap.put(routerId, router); |
| 82 | return router; |
| 83 | } else { |
| 84 | return routerMap.get(routerId); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns all LISP routers. |
| 90 | * |
| 91 | * @return all LISP routers |
| 92 | */ |
| 93 | public Collection<LispRouter> getRouters() { |
| 94 | return routerMap.values(); |
Jian Li | b1a8fd0 | 2016-12-27 03:55:32 +0900 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Returns an instance of LISP router agent factory. |
| 99 | * |
| 100 | * @return instance of LISP router agent factory |
| 101 | */ |
| 102 | public static LispRouterFactory getInstance() { |
| 103 | return SingletonHelper.INSTANCE; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Prevents object instantiation from external. |
| 108 | */ |
| 109 | private static final class SingletonHelper { |
| 110 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 111 | private static final LispRouterFactory INSTANCE = new LispRouterFactory(); |
| 112 | |
| 113 | private SingletonHelper() { |
| 114 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 115 | } |
| 116 | } |
| 117 | } |