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 | |
| 18 | import org.onlab.packet.IpAddress; |
| 19 | import org.slf4j.Logger; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | /** |
| 24 | * LISP router factory which returns concrete router object for the physical |
| 25 | * LISP router in use. |
| 26 | */ |
| 27 | public final class LispRouterFactory { |
| 28 | |
| 29 | private final Logger log = getLogger(getClass()); |
| 30 | |
| 31 | private LispRouterAgent agent; |
| 32 | |
| 33 | // non-instantiable (except for our Singleton) |
| 34 | private LispRouterFactory() { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Configures LISP router agent only if it is not initialized. |
| 39 | * |
| 40 | * @param agent reference object of LISP router agent |
| 41 | */ |
| 42 | public void setAgent(LispRouterAgent agent) { |
| 43 | synchronized (agent) { |
| 44 | if (this.agent == null) { |
| 45 | this.agent = agent; |
| 46 | } else { |
| 47 | log.warn("LISP Router Agent has already been set."); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Cleans up LISP router agent. |
| 54 | */ |
| 55 | public void cleanAgent() { |
| 56 | synchronized (agent) { |
| 57 | if (this.agent != null) { |
| 58 | this.agent = null; |
| 59 | } else { |
| 60 | log.warn("LISP Router Agent is not configured."); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns a LISP router instance. |
| 67 | * |
| 68 | * @param routerId LISP router identifier |
| 69 | * @return LISP router instance |
| 70 | */ |
| 71 | public LispRouter getRouterInstance(IpAddress routerId) { |
| 72 | LispRouter router = new DefaultLispRouter(new LispRouterId(routerId)); |
| 73 | router.setAgent(agent); |
| 74 | return router; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Returns an instance of LISP router agent factory. |
| 79 | * |
| 80 | * @return instance of LISP router agent factory |
| 81 | */ |
| 82 | public static LispRouterFactory getInstance() { |
| 83 | return SingletonHelper.INSTANCE; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Prevents object instantiation from external. |
| 88 | */ |
| 89 | private static final class SingletonHelper { |
| 90 | private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class."; |
| 91 | private static final LispRouterFactory INSTANCE = new LispRouterFactory(); |
| 92 | |
| 93 | private SingletonHelper() { |
| 94 | throw new IllegalAccessError(ILLEGAL_ACCESS_MSG); |
| 95 | } |
| 96 | } |
| 97 | } |