SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 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 | */ |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame] | 16 | package org.onosproject.pcep.server.impl; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 17 | |
| 18 | import static org.onlab.util.Tools.groupedThreads; |
| 19 | |
| 20 | import java.lang.management.ManagementFactory; |
| 21 | import java.lang.management.RuntimeMXBean; |
| 22 | import java.net.InetSocketAddress; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 23 | import java.util.Map; |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 24 | import java.util.LinkedList; |
| 25 | import java.util.TreeMap; |
| 26 | import java.util.List; |
| 27 | import java.util.HashMap; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 28 | import java.util.concurrent.Executors; |
| 29 | |
| 30 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 31 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 32 | import org.jboss.netty.channel.group.ChannelGroup; |
| 33 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 34 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame] | 35 | import org.onosproject.pcep.server.PccId; |
| 36 | import org.onosproject.pcep.server.PcepCfg; |
| 37 | import org.onosproject.pcep.server.PcepPacketStats; |
| 38 | import org.onosproject.pcep.server.driver.PcepAgent; |
| 39 | import org.onosproject.pcep.server.driver.PcepClientDriver; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 40 | import org.onosproject.pcepio.protocol.PcepFactories; |
| 41 | import org.onosproject.pcepio.protocol.PcepFactory; |
| 42 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 43 | import org.slf4j.Logger; |
| 44 | import 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 | */ |
| 50 | public 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); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 55 | private PcepCfg pcepConfig = new PcepConfig(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 56 | 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 | |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 67 | 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 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 73 | private NioServerSocketChannelFactory execFactory; |
| 74 | |
| 75 | // Perf. related configuration |
| 76 | private static final int SEND_BUFFER_SIZE = 4 * 1024 * 1024; |
| 77 | |
| 78 | /** |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 79 | * 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 | /** |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 144 | * 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 | /** |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 162 | * 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 | /** |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 189 | * 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)); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 206 | log.debug("Listening for PCC connection on {}", sa); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 207 | } 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 | } |