blob: 790ee8c0d10eb6e3236e804fb20ded008e13c33f [file] [log] [blame]
Jian Lib1a8fd02016-12-27 03:55:32 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
Jian Lib86d8ad2017-05-03 02:53:44 +090018import com.google.common.collect.Maps;
Jian Lib1a8fd02016-12-27 03:55:32 +090019import org.onlab.packet.IpAddress;
20import org.slf4j.Logger;
21
Jian Lib86d8ad2017-05-03 02:53:44 +090022import java.util.Collection;
23import java.util.Map;
24
Jian Lib1a8fd02016-12-27 03:55:32 +090025import 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 */
31public final class LispRouterFactory {
32
33 private final Logger log = getLogger(getClass());
34
35 private LispRouterAgent agent;
Jian Lib86d8ad2017-05-03 02:53:44 +090036 private Map<LispRouterId, LispRouter> routerMap = Maps.newConcurrentMap();
Jian Lib1a8fd02016-12-27 03:55:32 +090037
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 Lib86d8ad2017-05-03 02:53:44 +090073 * @param ipAddress IP address of LISP router
Jian Lib1a8fd02016-12-27 03:55:32 +090074 * @return LISP router instance
75 */
Jian Lib86d8ad2017-05-03 02:53:44 +090076 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 Lib1a8fd02016-12-27 03:55:32 +090095 }
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}