blob: c85e22a9dd07aca32fd7aa5dcb62b32745e16308 [file] [log] [blame]
SureshBR25058b72015-08-13 13:05:06 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
SureshBR25058b72015-08-13 13:05:06 +05303 *
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 */
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +053016package org.onosproject.pcep.server.impl;
SureshBR25058b72015-08-13 13:05:06 +053017
18import static org.onlab.util.Tools.groupedThreads;
19
20import java.lang.management.ManagementFactory;
21import java.lang.management.RuntimeMXBean;
22import java.net.InetSocketAddress;
SureshBR25058b72015-08-13 13:05:06 +053023import java.util.Map;
mohamedrahil00f6f262016-11-24 20:20:41 +053024import java.util.LinkedList;
25import java.util.TreeMap;
26import java.util.List;
27import java.util.HashMap;
SureshBR25058b72015-08-13 13:05:06 +053028import java.util.concurrent.Executors;
29
30import org.jboss.netty.bootstrap.ServerBootstrap;
31import org.jboss.netty.channel.ChannelPipelineFactory;
32import org.jboss.netty.channel.group.ChannelGroup;
33import org.jboss.netty.channel.group.DefaultChannelGroup;
34import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +053035import org.onosproject.pcep.server.PccId;
36import org.onosproject.pcep.server.PcepCfg;
37import org.onosproject.pcep.server.PcepPacketStats;
38import org.onosproject.pcep.server.driver.PcepAgent;
39import org.onosproject.pcep.server.driver.PcepClientDriver;
SureshBR25058b72015-08-13 13:05:06 +053040import org.onosproject.pcepio.protocol.PcepFactories;
41import org.onosproject.pcepio.protocol.PcepFactory;
42import org.onosproject.pcepio.protocol.PcepVersion;
43import org.slf4j.Logger;
44import org.slf4j.LoggerFactory;
45
46/**
47 * The main controller class. Handles all setup and network listeners -
48 * Distributed ownership control of pcc through IControllerRegistryService
49 */
50public class Controller {
51
52 private static final Logger log = LoggerFactory.getLogger(Controller.class);
53
54 private static final PcepFactory FACTORY1 = PcepFactories.getFactory(PcepVersion.PCEP_1);
mohamedrahil00f6f262016-11-24 20:20:41 +053055 private PcepCfg pcepConfig = new PcepConfig();
SureshBR25058b72015-08-13 13:05:06 +053056 private ChannelGroup cg;
57
58 // Configuration options
59 private int pcepPort = 4189;
60 private int workerThreads = 10;
61
62 // Start time of the controller
63 private long systemStartTime;
64
65 private PcepAgent agent;
66
mohamedrahil00f6f262016-11-24 20:20:41 +053067 private Map<String, String> peerMap = new TreeMap<>();
68 private Map<String, List<String>> pcepExceptionMap = new TreeMap<>();
69 private Map<Integer, Integer> pcepErrorMsg = new TreeMap<>();
70 private Map<String, Byte> sessionMap = new TreeMap<>();
71 private LinkedList<String> pcepExceptionList = new LinkedList<String>();
72
SureshBR25058b72015-08-13 13:05:06 +053073 private NioServerSocketChannelFactory execFactory;
74
75 // Perf. related configuration
76 private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
77
78 /**
mohamedrahil00f6f262016-11-24 20:20:41 +053079 * pcep session information.
80 *
81 * @param peerId id of the peer with which the session is being formed
82 * @param status pcep session status
83 * @param sessionId pcep session id
84 */
85 public void peerStatus(String peerId, String status, byte sessionId) {
86 if (peerId != null) {
87 peerMap.put(peerId, status);
88 sessionMap.put(peerId, sessionId);
89 } else {
90 log.debug("Peer Id is null");
91 }
92 }
93
94 /**
95 * Pcep session exceptions information.
96 *
97 * @param peerId id of the peer which has generated the exception
98 * @param exception pcep session exception
99 */
100 public void peerExceptions(String peerId, String exception) {
101 if (peerId != null) {
102 pcepExceptionList.add(exception);
103 pcepExceptionMap.put(peerId, pcepExceptionList);
104 } else {
105 log.debug("Peer Id is null");
106 }
107 if (pcepExceptionList.size() > 10) {
108 pcepExceptionList.clear();
109 pcepExceptionList.add(exception);
110 pcepExceptionMap.put(peerId, pcepExceptionList);
111 }
112 }
113
114 /**
115 * Create a map of pcep error messages received.
116 *
117 * @param peerId id of the peer which has sent the error message
118 * @param errorType error type of pcep error messgae
119 * @param errValue error value of pcep error messgae
120 */
121 public void peerErrorMsg(String peerId, Integer errorType, Integer errValue) {
122 if (peerId == null) {
123 pcepErrorMsg.put(errorType, errValue);
124 } else {
125 if (pcepErrorMsg.size() > 10) {
126 pcepErrorMsg.clear();
127 }
128 pcepErrorMsg.put(errorType, errValue);
129 }
130 }
131
132 /**
133 * Returns the pcep session details.
134 *
135 * @return pcep session details
136 */
137 public Map<String, Byte> mapSession() {
138 return this.sessionMap;
139 }
140
141
142
143 /**
SureshBR25058b72015-08-13 13:05:06 +0530144 * Returns factory version for processing pcep messages.
145 *
146 * @return instance of factory version
147 */
148 public PcepFactory getPcepMessageFactory1() {
149 return FACTORY1;
150 }
151
152 /**
153 * To get system start time.
154 *
155 * @return system start time in milliseconds
156 */
157 public long getSystemStartTime() {
158 return (this.systemStartTime);
159 }
160
161 /**
mohamedrahil00f6f262016-11-24 20:20:41 +0530162 * Returns the list of pcep peers with session information.
163 *
164 * @return pcep peer information
165 */
166 public Map<String, String> mapPeer() {
167 return this.peerMap;
168 }
169
170 /**
171 * Returns the list of pcep exceptions per peer.
172 *
173 * @return pcep exceptions
174 */
175 public Map<String, List<String>> exceptionsMap() {
176 return this.pcepExceptionMap;
177 }
178
179 /**
180 * Returns the type and value of pcep error messages.
181 *
182 * @return pcep error message
183 */
184 public Map<Integer, Integer> mapErrorMsg() {
185 return this.pcepErrorMsg;
186 }
187
188 /**
SureshBR25058b72015-08-13 13:05:06 +0530189 * Tell controller that we're ready to accept pcc connections.
190 */
191 public void run() {
192 try {
193 final ServerBootstrap bootstrap = createServerBootStrap();
194
195 bootstrap.setOption("reuseAddr", true);
196 bootstrap.setOption("child.keepAlive", true);
197 bootstrap.setOption("child.tcpNoDelay", true);
198 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
199
200 ChannelPipelineFactory pfact = new PcepPipelineFactory(this);
201
202 bootstrap.setPipelineFactory(pfact);
203 InetSocketAddress sa = new InetSocketAddress(pcepPort);
204 cg = new DefaultChannelGroup();
205 cg.add(bootstrap.bind(sa));
mohamedrahil00f6f262016-11-24 20:20:41 +0530206 log.debug("Listening for PCC connection on {}", sa);
SureshBR25058b72015-08-13 13:05:06 +0530207 } catch (Exception e) {
208 throw new RuntimeException(e);
209 }
210 }
211
212 /**
213 * Creates server boot strap.
214 *
215 * @return ServerBootStrap
216 */
217 private ServerBootstrap createServerBootStrap() {
218 if (workerThreads == 0) {
219 execFactory = new NioServerSocketChannelFactory(
220 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
221 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")));
222 return new ServerBootstrap(execFactory);
223 } else {
224 execFactory = new NioServerSocketChannelFactory(
225 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
226 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")), workerThreads);
227 return new ServerBootstrap(execFactory);
228 }
229 }
230
231 /**
232 * Initialize internal data structures.
233 */
234 public void init() {
235 // These data structures are initialized here because other
236 // module's startUp() might be called before ours
237 this.systemStartTime = System.currentTimeMillis();
238 }
239
240 public Map<String, Long> getMemory() {
241 Map<String, Long> m = new HashMap<>();
242 Runtime runtime = Runtime.getRuntime();
243 m.put("total", runtime.totalMemory());
244 m.put("free", runtime.freeMemory());
245 return m;
246 }
247
248 public Long getUptime() {
249 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
250 return rb.getUptime();
251 }
252
253 /**
254 * Creates instance of Pcep client.
255 *
256 * @param pccId pcc identifier
257 * @param sessionID session id
258 * @param pv pcep version
259 * @param pktStats pcep packet statistics
260 * @return instance of PcepClient
261 */
262 protected PcepClientDriver getPcepClientInstance(PccId pccId, int sessionID, PcepVersion pv,
263 PcepPacketStats pktStats) {
264 PcepClientDriver pcepClientDriver = new PcepClientImpl();
265 pcepClientDriver.init(pccId, pv, pktStats);
266 pcepClientDriver.setAgent(agent);
267 return pcepClientDriver;
268 }
269
270 /**
271 * Starts the pcep controller.
272 *
273 * @param ag Pcep agent
274 */
275 public void start(PcepAgent ag) {
276 log.info("Started");
277 this.agent = ag;
278 this.init();
279 this.run();
280 }
281
282 /**
283 * Stops the pcep controller.
284 */
285 public void stop() {
286 log.info("Stopped");
287 execFactory.shutdown();
288 cg.close();
289 }
290}