blob: bf7dfb6c3c30abe8b52c09c2d1dea34f756c7265 [file] [log] [blame]
Jian Lib1a8fd02016-12-27 03:55:32 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jian Lib1a8fd02016-12-27 03:55:32 +09003 *
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() {
Ray Milkeyd6c42802018-01-16 10:18:23 -080061 if (this.agent == null) {
62 log.warn("LISP Router Agent is not configured.");
63 return;
64 }
65 LispRouterAgent existingAgent = agent;
66 synchronized (existingAgent) {
67 this.agent = null;
Jian Lib1a8fd02016-12-27 03:55:32 +090068 }
69 }
70
71 /**
72 * Returns a LISP router instance.
73 *
Jian Lib86d8ad2017-05-03 02:53:44 +090074 * @param ipAddress IP address of LISP router
Jian Lib1a8fd02016-12-27 03:55:32 +090075 * @return LISP router instance
76 */
Jian Lib86d8ad2017-05-03 02:53:44 +090077 public LispRouter getRouterInstance(IpAddress ipAddress) {
78 LispRouterId routerId = new LispRouterId(ipAddress);
79 if (!routerMap.containsKey(routerId)) {
80 LispRouter router = new DefaultLispRouter(routerId);
81 router.setAgent(agent);
82 routerMap.put(routerId, router);
83 return router;
84 } else {
85 return routerMap.get(routerId);
86 }
87 }
88
89 /**
90 * Returns all LISP routers.
91 *
92 * @return all LISP routers
93 */
94 public Collection<LispRouter> getRouters() {
95 return routerMap.values();
Jian Lib1a8fd02016-12-27 03:55:32 +090096 }
97
98 /**
99 * Returns an instance of LISP router agent factory.
100 *
101 * @return instance of LISP router agent factory
102 */
103 public static LispRouterFactory getInstance() {
104 return SingletonHelper.INSTANCE;
105 }
106
107 /**
108 * Prevents object instantiation from external.
109 */
110 private static final class SingletonHelper {
111 private static final String ILLEGAL_ACCESS_MSG = "Should not instantiate this class.";
112 private static final LispRouterFactory INSTANCE = new LispRouterFactory();
113
114 private SingletonHelper() {
115 throw new IllegalAccessError(ILLEGAL_ACCESS_MSG);
116 }
117 }
118}