blob: 9c27810c572e119a0d38f44ef997413e7083a7fa [file] [log] [blame]
SureshBR25058b72015-08-13 13:05:06 +05301/*
2 * Copyright 2015 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.pcep.controller.impl;
17
18import static org.onlab.util.Tools.groupedThreads;
19
20import java.lang.management.ManagementFactory;
21import java.lang.management.RuntimeMXBean;
22import java.net.InetSocketAddress;
23import java.util.HashMap;
24import java.util.Map;
25import java.util.concurrent.Executors;
26
27import org.jboss.netty.bootstrap.ServerBootstrap;
28import org.jboss.netty.channel.ChannelPipelineFactory;
29import org.jboss.netty.channel.group.ChannelGroup;
30import org.jboss.netty.channel.group.DefaultChannelGroup;
31import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
32import org.onosproject.pcep.controller.PccId;
33import org.onosproject.pcep.controller.PcepPacketStats;
34import org.onosproject.pcep.controller.driver.PcepAgent;
35import org.onosproject.pcep.controller.driver.PcepClientDriver;
36import org.onosproject.pcepio.protocol.PcepFactories;
37import org.onosproject.pcepio.protocol.PcepFactory;
38import org.onosproject.pcepio.protocol.PcepVersion;
39import org.slf4j.Logger;
40import org.slf4j.LoggerFactory;
41
42/**
43 * The main controller class. Handles all setup and network listeners -
44 * Distributed ownership control of pcc through IControllerRegistryService
45 */
46public class Controller {
47
48 private static final Logger log = LoggerFactory.getLogger(Controller.class);
49
50 private static final PcepFactory FACTORY1 = PcepFactories.getFactory(PcepVersion.PCEP_1);
51
52 private ChannelGroup cg;
53
54 // Configuration options
55 private int pcepPort = 4189;
56 private int workerThreads = 10;
57
58 // Start time of the controller
59 private long systemStartTime;
60
61 private PcepAgent agent;
62
63 private NioServerSocketChannelFactory execFactory;
64
65 // Perf. related configuration
66 private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024;
67
68 /**
69 * Returns factory version for processing pcep messages.
70 *
71 * @return instance of factory version
72 */
73 public PcepFactory getPcepMessageFactory1() {
74 return FACTORY1;
75 }
76
77 /**
78 * To get system start time.
79 *
80 * @return system start time in milliseconds
81 */
82 public long getSystemStartTime() {
83 return (this.systemStartTime);
84 }
85
86 /**
87 * Tell controller that we're ready to accept pcc connections.
88 */
89 public void run() {
90 try {
91 final ServerBootstrap bootstrap = createServerBootStrap();
92
93 bootstrap.setOption("reuseAddr", true);
94 bootstrap.setOption("child.keepAlive", true);
95 bootstrap.setOption("child.tcpNoDelay", true);
96 bootstrap.setOption("child.sendBufferSize", Controller.SEND_BUFFER_SIZE);
97
98 ChannelPipelineFactory pfact = new PcepPipelineFactory(this);
99
100 bootstrap.setPipelineFactory(pfact);
101 InetSocketAddress sa = new InetSocketAddress(pcepPort);
102 cg = new DefaultChannelGroup();
103 cg.add(bootstrap.bind(sa));
104 log.info("Listening for PCC connection on {}", sa);
105 } catch (Exception e) {
106 throw new RuntimeException(e);
107 }
108 }
109
110 /**
111 * Creates server boot strap.
112 *
113 * @return ServerBootStrap
114 */
115 private ServerBootstrap createServerBootStrap() {
116 if (workerThreads == 0) {
117 execFactory = new NioServerSocketChannelFactory(
118 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
119 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")));
120 return new ServerBootstrap(execFactory);
121 } else {
122 execFactory = new NioServerSocketChannelFactory(
123 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "boss-%d")),
124 Executors.newCachedThreadPool(groupedThreads("onos/pcep", "worker-%d")), workerThreads);
125 return new ServerBootstrap(execFactory);
126 }
127 }
128
129 /**
130 * Initialize internal data structures.
131 */
132 public void init() {
133 // These data structures are initialized here because other
134 // module's startUp() might be called before ours
135 this.systemStartTime = System.currentTimeMillis();
136 }
137
138 public Map<String, Long> getMemory() {
139 Map<String, Long> m = new HashMap<>();
140 Runtime runtime = Runtime.getRuntime();
141 m.put("total", runtime.totalMemory());
142 m.put("free", runtime.freeMemory());
143 return m;
144 }
145
146 public Long getUptime() {
147 RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
148 return rb.getUptime();
149 }
150
151 /**
152 * Creates instance of Pcep client.
153 *
154 * @param pccId pcc identifier
155 * @param sessionID session id
156 * @param pv pcep version
157 * @param pktStats pcep packet statistics
158 * @return instance of PcepClient
159 */
160 protected PcepClientDriver getPcepClientInstance(PccId pccId, int sessionID, PcepVersion pv,
161 PcepPacketStats pktStats) {
162 PcepClientDriver pcepClientDriver = new PcepClientImpl();
163 pcepClientDriver.init(pccId, pv, pktStats);
164 pcepClientDriver.setAgent(agent);
165 return pcepClientDriver;
166 }
167
168 /**
169 * Starts the pcep controller.
170 *
171 * @param ag Pcep agent
172 */
173 public void start(PcepAgent ag) {
174 log.info("Started");
175 this.agent = ag;
176 this.init();
177 this.run();
178 }
179
180 /**
181 * Stops the pcep controller.
182 */
183 public void stop() {
184 log.info("Stopped");
185 execFactory.shutdown();
186 cg.close();
187 }
188}