Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Kalyankumar Asangi | 27728f2 | 2016-02-17 15:46:28 +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 | */ |
| 16 | package org.onosproject.ospf.controller.impl; |
| 17 | |
| 18 | import org.jboss.netty.bootstrap.ServerBootstrap; |
| 19 | import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; |
| 20 | import org.jboss.netty.channel.ChannelPipelineFactory; |
| 21 | import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory; |
| 22 | import org.jboss.netty.channel.group.ChannelGroup; |
| 23 | import org.jboss.netty.channel.group.DefaultChannelGroup; |
| 24 | import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; |
| 25 | import org.onosproject.net.driver.DriverService; |
| 26 | import org.onosproject.ospf.controller.OspfAgent; |
| 27 | import org.onosproject.ospf.controller.OspfArea; |
| 28 | import org.onosproject.ospf.controller.OspfInterface; |
| 29 | import org.onosproject.ospf.controller.OspfLinkTed; |
| 30 | import org.onosproject.ospf.controller.OspfProcess; |
| 31 | import org.onosproject.ospf.controller.OspfRouter; |
| 32 | import org.slf4j.Logger; |
| 33 | import org.slf4j.LoggerFactory; |
| 34 | |
| 35 | import java.net.InetAddress; |
| 36 | import java.net.InetSocketAddress; |
| 37 | import java.net.NetworkInterface; |
| 38 | import java.net.SocketException; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Collections; |
| 41 | import java.util.Enumeration; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Iterator; |
| 44 | import java.util.List; |
| 45 | import java.util.Set; |
| 46 | import java.util.concurrent.Executor; |
| 47 | import java.util.concurrent.Executors; |
| 48 | |
| 49 | /** |
| 50 | * Representation of the main controller class. Handles all setup and network listeners. |
| 51 | */ |
| 52 | public class Controller { |
| 53 | |
| 54 | protected static final Logger log = LoggerFactory.getLogger(Controller.class); |
| 55 | protected static final int BUFFER_SIZE = 4 * 1024 * 1024; |
| 56 | private static final String PROCESS = "process"; |
| 57 | private static final String AREA = "area"; |
| 58 | private static final String INTERFACE = "interface"; |
| 59 | |
| 60 | protected int ospfPort = 7000; |
| 61 | protected int workerThreads = 16; |
| 62 | protected long systemStartTime; |
| 63 | private DriverService driverService; |
| 64 | private OspfAgent agent; |
| 65 | private List<ChannelGroup> cgList = new ArrayList(); |
| 66 | private List<NioServerSocketChannelFactory> execFactoryLst = new ArrayList<>(); |
| 67 | private List<OspfProcess> processes; |
| 68 | |
| 69 | /** |
| 70 | * Gets all configured processes. |
| 71 | * |
| 72 | * @return all configured processes |
| 73 | */ |
| 74 | public List<OspfProcess> getAllConfiguredProcesses() { |
| 75 | return processes; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Adds device details. |
| 80 | * |
| 81 | * @param ospfRouter OSPF router instance |
| 82 | */ |
| 83 | public void addDeviceDetails(OspfRouter ospfRouter) { |
| 84 | agent.addConnectedRouter(ospfRouter); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Removes device details. |
| 89 | * |
| 90 | * @param ospfRouter OSPF router instance |
| 91 | */ |
| 92 | public void removeDeviceDetails(OspfRouter ospfRouter) { |
| 93 | agent.removeConnectedRouter(ospfRouter); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Adds link details. |
| 98 | * |
| 99 | * @param ospfRouter OSPF router instance |
| 100 | * @param ospfLinkTed OSPF link ted instance |
| 101 | */ |
| 102 | public void addLinkDetails(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) { |
| 103 | agent.addLink(ospfRouter, ospfLinkTed); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Removes link details. |
| 108 | * |
| 109 | * @param ospfRouter OSPF router instance |
| 110 | */ |
| 111 | public void removeLinkDetails(OspfRouter ospfRouter) { |
| 112 | agent.deleteLink(ospfRouter); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Creates a server bootstrap. |
| 117 | * |
| 118 | * @return ServerBootstrap bootstrap instance |
| 119 | */ |
| 120 | private ServerBootstrap createServerBootStrap() { |
| 121 | |
| 122 | Executor bossPool = Executors.newCachedThreadPool(); |
| 123 | Executor workerPool = Executors.newCachedThreadPool(); |
| 124 | NioServerSocketChannelFactory executerFactory; |
| 125 | |
| 126 | if (workerThreads == 0) { |
| 127 | executerFactory = new NioServerSocketChannelFactory(bossPool, workerPool); |
| 128 | execFactoryLst.add(executerFactory); |
| 129 | } else { |
| 130 | executerFactory = new NioServerSocketChannelFactory(bossPool, workerPool, workerThreads); |
| 131 | execFactoryLst.add(executerFactory); |
| 132 | } |
| 133 | |
| 134 | return new ServerBootstrap(executerFactory); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** |
| 139 | * Initializes internal data structures. |
| 140 | */ |
| 141 | public void init() { |
| 142 | this.systemStartTime = System.currentTimeMillis(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Starts the controller. |
| 147 | * |
| 148 | * @param ag OSPF agent instance |
| 149 | * @param driverService driver service instance |
| 150 | */ |
| 151 | public void start(OspfAgent ag, DriverService driverService) { |
| 152 | log.info("Starting OSPF Controller...!!!"); |
| 153 | this.agent = ag; |
| 154 | this.driverService = driverService; |
| 155 | this.init(); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Stops the Controller. |
| 160 | */ |
| 161 | public void stop() { |
| 162 | log.info("Stopping OSPF Controller...!!!"); |
| 163 | |
| 164 | for (ChannelGroup cg : cgList) { |
| 165 | cg.close(); |
| 166 | } |
| 167 | |
| 168 | for (NioServerSocketChannelFactory execFactory : execFactoryLst) { |
| 169 | execFactory.shutdown(); |
| 170 | } |
| 171 | |
| 172 | processes.clear(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Deletes configured interface from the area. |
| 177 | * |
| 178 | * @param processId process id |
| 179 | * @param areaId area id |
| 180 | * @param interfaceToDelete interface to delete |
| 181 | * @return true if operation success else false |
| 182 | */ |
| 183 | public boolean deleteInterfaceFromArea(String processId, String areaId, String interfaceToDelete) { |
| 184 | Iterator<OspfProcess> processItr = processes.iterator(); |
| 185 | |
| 186 | while (processItr.hasNext()) { |
| 187 | OspfProcess process = processItr.next(); |
| 188 | if (processId.equalsIgnoreCase(process.processId())) { |
| 189 | Iterator<OspfArea> areaItr = process.areas().iterator(); |
| 190 | while (areaItr.hasNext()) { |
| 191 | OspfArea area = areaItr.next(); |
| 192 | Iterator<OspfInterface> ospfIntrItr = area.getInterfacesLst().iterator(); |
| 193 | if (area.areaId().toString().equalsIgnoreCase(areaId)) { |
| 194 | while (ospfIntrItr.hasNext()) { |
| 195 | OspfInterface ospfIntr = ospfIntrItr.next(); |
| 196 | if (interfaceToDelete.equalsIgnoreCase(ospfIntr.ipAddress().toString())) { |
| 197 | ospfIntrItr.remove(); |
| 198 | log.debug("Interface With Id {} is removed from Area {}", |
| 199 | ospfIntr.ipAddress(), ospfIntr.areaId()); |
| 200 | return true; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Checks area with area id exists in process. |
| 213 | * |
| 214 | * @param processId process id |
| 215 | * @param areaId area id |
| 216 | * @return true if exist else false |
| 217 | */ |
| 218 | public boolean checkArea(String processId, String areaId) { |
| 219 | for (OspfProcess process : processes) { |
| 220 | if (processId.equalsIgnoreCase(process.processId())) { |
| 221 | for (OspfArea area : process.areas()) { |
| 222 | if (area.areaId().toString().equalsIgnoreCase(areaId)) { |
| 223 | return true; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Checks process with process id exists or not. |
| 233 | * |
| 234 | * @param processId process id |
| 235 | * @return true if exist else false |
| 236 | */ |
| 237 | public boolean checkProcess(String processId) { |
| 238 | log.debug("CheckProcess,Process Id ={} processes={}", processId, processes); |
| 239 | for (OspfProcess process : processes) { |
| 240 | if (processId.equalsIgnoreCase(process.processId())) { |
| 241 | return true; |
| 242 | } |
| 243 | } |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * Checks interface exists in given area. |
| 249 | * |
| 250 | * @param processId process id |
| 251 | * @param areaId area id |
| 252 | * @param ipAddress interface |
| 253 | * @return true if exist else false |
| 254 | */ |
| 255 | public boolean checkInterface(String processId, String areaId, String interfaceIp) { |
| 256 | for (OspfProcess process : processes) { |
| 257 | if (processId.equalsIgnoreCase(process.processId())) { |
| 258 | for (OspfArea area : process.areas()) { |
| 259 | if (area.areaId().toString().equalsIgnoreCase(areaId)) { |
| 260 | for (OspfInterface ospfInterface : area.getInterfacesLst()) { |
| 261 | if (ospfInterface.ipAddress().toString().equalsIgnoreCase(interfaceIp)) { |
| 262 | return true; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Create processes first time when no process exist. |
| 274 | * Create server bootstraps for all the interfaces in the processes. |
| 275 | * |
| 276 | * @param ospfProcesses list of OSPF processes to create |
| 277 | */ |
| 278 | private void createProcessWhenNoProcessesExists(List<OspfProcess> ospfProcesses) { |
| 279 | Set<String> interfaceIpList = new HashSet<>(); |
| 280 | Set<String> areaIdList = new HashSet<>(); |
| 281 | if (processes != null) { |
| 282 | if (processes.size() == 0) { |
| 283 | |
| 284 | processes.addAll(ospfProcesses); |
| 285 | for (OspfProcess process : ospfProcesses) { |
| 286 | for (OspfArea area : process.areas()) { |
| 287 | areaIdList.add(area.areaId().toString()); |
| 288 | for (OspfInterface intrfc : area.getInterfacesLst()) { |
| 289 | interfaceIpList.add(intrfc.ipAddress().toString()); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | createBootStrapForCreatedInterface(interfaceIpList, areaIdList); |
| 294 | } |
| 295 | } else { |
| 296 | processes = new ArrayList<>(); |
| 297 | processes.addAll(ospfProcesses); |
| 298 | |
| 299 | for (OspfProcess process : ospfProcesses) { |
| 300 | for (OspfArea area : process.areas()) { |
| 301 | areaIdList.add(area.areaId().toString()); |
| 302 | for (OspfInterface intrfc : area.getInterfacesLst()) { |
| 303 | interfaceIpList.add(intrfc.ipAddress().toString()); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | createBootStrapForCreatedInterface(interfaceIpList, areaIdList); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Creates processes when already process exist. |
| 313 | * It can be modifying existing process or adding a new process. |
| 314 | * |
| 315 | * @param ospfProcesses list of processes |
| 316 | */ |
| 317 | private void createProcessWhenProcessesExists(List<OspfProcess> ospfProcesses) { |
| 318 | if (ospfProcesses != null) { |
| 319 | for (OspfProcess process : ospfProcesses) { |
| 320 | if (!checkProcess(process.processId())) { |
| 321 | createNewProcess(process.processId(), process); |
| 322 | } else { |
| 323 | List<OspfArea> areas = process.areas(); |
| 324 | for (OspfArea area : areas) { |
| 325 | if (!checkArea(process.processId(), area.areaId().toString())) { |
| 326 | createAreaInProcess(process.processId(), |
| 327 | area.areaId().toString(), area); |
| 328 | } else { |
| 329 | updateAreaInProcess(process.processId(), area.areaId().toString(), area); |
| 330 | for (OspfInterface interfc : area.getInterfacesLst()) { |
| 331 | if (!checkInterface(process.processId(), |
| 332 | area.areaId().toString(), interfc.ipAddress().toString())) { |
| 333 | createInterfaceInAreaInProcess(process.processId(), |
| 334 | area.areaId().toString(), interfc); |
| 335 | } else { |
| 336 | updateInterfaceParameters(process.processId(), |
| 337 | area.areaId().toString(), |
| 338 | interfc.ipAddress().toString(), interfc); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Updates the area information in already started OSPF processes. |
| 350 | * |
| 351 | * @param processId process id |
| 352 | * @param areaId area id |
| 353 | * @param areaFrmConfig area to update |
| 354 | */ |
| 355 | public void updateAreaInProcess(String processId, String areaId, OspfArea areaFrmConfig) { |
| 356 | if (processes != null) { |
| 357 | Iterator<OspfProcess> processItr = processes.iterator(); |
| 358 | while (processItr.hasNext()) { |
| 359 | OspfProcess process = processItr.next(); |
| 360 | if (processId.equalsIgnoreCase(process.processId())) { |
| 361 | Iterator<OspfArea> area = process.areas().iterator(); |
| 362 | while (area.hasNext()) { |
| 363 | OspfArea ospfArea = area.next(); |
| 364 | if (areaId.equalsIgnoreCase(ospfArea.areaId().toString())) { |
| 365 | ospfArea.setAddressRanges(areaFrmConfig.addressRanges()); |
| 366 | ospfArea.setRouterId(areaFrmConfig.routerId()); |
| 367 | ospfArea.setTransitCapability(areaFrmConfig.isTransitCapability()); |
| 368 | ospfArea.setExternalRoutingCapability(areaFrmConfig.isExternalRoutingCapability()); |
| 369 | ospfArea.setStubCost(areaFrmConfig.stubCost()); |
| 370 | ospfArea.setOptions(areaFrmConfig.options()); |
| 371 | ospfArea.setIsOpaqueEnabled(areaFrmConfig.isOpaqueEnabled()); |
| 372 | log.debug("updateAreaInProcess::Process Id::{}::Ospf Area with Id::{}::is " + |
| 373 | "updated", processId, areaId); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * Updates the processes configuration. |
| 383 | * |
| 384 | * @param ospfProcesses list of OSPF processes |
| 385 | */ |
| 386 | public void updateConfig(List<OspfProcess> ospfProcesses) { |
| 387 | log.info("Controller::UpdateConfig called"); |
| 388 | if (processes != null) { |
| 389 | if (processes.size() == 0) { |
| 390 | createProcessWhenNoProcessesExists(ospfProcesses); |
| 391 | } else { |
| 392 | createProcessWhenProcessesExists(ospfProcesses); |
| 393 | } |
| 394 | } else { |
| 395 | createProcessWhenNoProcessesExists(ospfProcesses); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Deletes configuration. |
| 401 | * |
| 402 | * @param ospfProcesses OSPF processes |
| 403 | * @param attribute attribute to delete |
| 404 | */ |
| 405 | public void deleteConfig(List<OspfProcess> ospfProcesses, String attribute) { |
| 406 | log.info("Controller::UpdateConfig called"); |
| 407 | if (processes != null) { |
| 408 | if (processes.size() == 0) { |
| 409 | log.debug("DeleteConfig:: No process exists"); |
| 410 | } else { |
| 411 | deleteProcessWhenExists(ospfProcesses, attribute); |
| 412 | } |
| 413 | } else { |
| 414 | log.debug("DeleteConfig:: No process exists"); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Creates a new process. |
| 420 | * |
| 421 | * @param processId process id |
| 422 | * @param process OSPF process instance |
| 423 | */ |
| 424 | private void createNewProcess(String processId, OspfProcess process) { |
| 425 | Set<String> interfaceIpList = new HashSet<>(); |
| 426 | Set<String> areaIdList = new HashSet<>(); |
| 427 | |
| 428 | processes.add(process); |
| 429 | for (OspfArea area : process.areas()) { |
| 430 | areaIdList.add(area.areaId().toString()); |
| 431 | for (OspfInterface interfc : area.getInterfacesLst()) { |
| 432 | interfaceIpList.add(interfc.ipAddress().toString()); |
| 433 | } |
| 434 | } |
| 435 | log.debug("createNewProcess::List of areas in process::{} areas::{}", processId, areaIdList); |
| 436 | |
| 437 | createBootStrapForCreatedInterface(interfaceIpList, areaIdList); |
| 438 | |
| 439 | log.debug("createNewProcess:: all processes::{}", processes); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Creates a new area information in the process. |
| 444 | * |
| 445 | * @param processId process id |
| 446 | * @param areaId area id |
| 447 | * @param area OSPF area instance |
| 448 | */ |
| 449 | private void createAreaInProcess(String processId, String areaId, OspfArea area) { |
| 450 | Set<String> interfaceIpList = new HashSet<>(); |
| 451 | Set<String> areaIdList = new HashSet<>(); |
| 452 | |
| 453 | Iterator<OspfProcess> processItr = processes.iterator(); |
| 454 | while (processItr.hasNext()) { |
| 455 | OspfProcess process = processItr.next(); |
| 456 | List<OspfArea> areasInProcess = process.areas(); |
| 457 | if (processId.equalsIgnoreCase(process.processId())) { |
| 458 | areasInProcess.add(area); |
| 459 | |
| 460 | for (OspfInterface intrfc : area.getInterfacesLst()) { |
| 461 | interfaceIpList.add(intrfc.ipAddress().toString()); |
| 462 | } |
| 463 | areaIdList.add(area.areaId().toString()); |
| 464 | log.debug("createAreaInProcess::List of areas in process Id::{} " + |
| 465 | "AreaId ::{} update process::{}", |
| 466 | processId, areaId, process); |
| 467 | } |
| 468 | } |
| 469 | createBootStrapForCreatedInterface(interfaceIpList, areaIdList); |
| 470 | log.debug("createAreaInProcess:: all processes::{}", processes); |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Creates an interface in the given area and process. |
| 475 | * |
| 476 | * @param processId process id |
| 477 | * @param areaId area id |
| 478 | * @param ospfInterface OSPF interface instance |
| 479 | */ |
| 480 | private void createInterfaceInAreaInProcess(String processId, |
| 481 | String areaId, OspfInterface ospfInterface) { |
| 482 | Set<String> interfaceIpList = new HashSet<>(); |
| 483 | Set<String> areaIdList = new HashSet<>(); |
| 484 | |
| 485 | Iterator<OspfProcess> processItr = processes.iterator(); |
| 486 | while (processItr.hasNext()) { |
| 487 | OspfProcess process = processItr.next(); |
| 488 | List<OspfArea> areasInProcess = process.areas(); |
| 489 | if (processId.equalsIgnoreCase(process.processId())) { |
| 490 | Iterator<OspfArea> areaItr = areasInProcess.iterator(); |
| 491 | while (areaItr.hasNext()) { |
| 492 | OspfArea area = areaItr.next(); |
| 493 | if (areaId.equalsIgnoreCase(area.areaId().toString())) { |
| 494 | area.getInterfacesLst().add(ospfInterface); |
| 495 | interfaceIpList.add(ospfInterface.ipAddress().toString()); |
| 496 | |
| 497 | log.debug("createInterfaceInAreaInProcess::Interface " + |
| 498 | "updated in process Id::{} AreaId ::{} Interface List{}", |
| 499 | processId, areaId, area.getInterfacesLst()); |
| 500 | |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | createBootStrapForCreatedInterface(interfaceIpList, areaIdList); |
| 506 | log.debug("createInterfaceInAreaInProcess:: all processes::{}", processes); |
| 507 | } |
| 508 | |
| 509 | /** |
| 510 | * Updates interface parameters. |
| 511 | * |
| 512 | * @param processId process id |
| 513 | * @param areaId area id |
| 514 | * @param interfaceId interface id |
| 515 | * @param ospfInterface OSPF interface instance |
| 516 | */ |
| 517 | private void updateInterfaceParameters(String processId, String areaId, String interfaceId, |
| 518 | OspfInterface ospfInterface) { |
| 519 | Iterator<OspfProcess> processItr = processes.iterator(); |
| 520 | while (processItr.hasNext()) { |
| 521 | OspfProcess process = processItr.next(); |
| 522 | if (processId.equalsIgnoreCase(process.processId())) { |
| 523 | Iterator<OspfArea> areItr = process.areas().iterator(); |
| 524 | while (areItr.hasNext()) { |
| 525 | OspfArea area = (OspfArea) areItr.next(); |
| 526 | if (area.areaId().toString().equalsIgnoreCase(areaId)) { |
| 527 | Iterator<OspfInterface> intfcList = area.getInterfacesLst().iterator(); |
| 528 | while (intfcList.hasNext()) { |
| 529 | OspfInterface intrfcObj = intfcList.next(); |
| 530 | if (interfaceId.equalsIgnoreCase(intrfcObj.ipAddress().toString())) { |
| 531 | intrfcObj.setPollInterval(ospfInterface.pollInterval()); |
| 532 | intrfcObj.setTransmitDelay(ospfInterface.transmitDelay()); |
| 533 | intrfcObj.setBdr(ospfInterface.bdr()); |
| 534 | intrfcObj.setDr(ospfInterface.dr()); |
| 535 | intrfcObj.setAuthKey(ospfInterface.authKey()); |
| 536 | intrfcObj.setAuthType(ospfInterface.authType()); |
| 537 | intrfcObj.setHelloIntervalTime(ospfInterface.helloIntervalTime()); |
| 538 | intrfcObj.setReTransmitInterval(ospfInterface.reTransmitInterval()); |
| 539 | intrfcObj.setMtu(ospfInterface.mtu()); |
| 540 | intrfcObj.setInterfaceCost(ospfInterface.interfaceCost()); |
| 541 | intrfcObj.setInterfaceType(ospfInterface.interfaceType()); |
| 542 | intrfcObj.setRouterDeadIntervalTime(ospfInterface.routerDeadIntervalTime()); |
| 543 | intrfcObj.setRouterPriority(ospfInterface.routerPriority()); |
| 544 | intrfcObj.setIpNetworkMask(ospfInterface.ipNetworkMask()); |
| 545 | log.debug("updateInterfaceParameters::Interface updated in " + |
| 546 | "process Id::{} AreaId ::{} Interface Id:{} " + |
| 547 | "Updated Interface List: {}", processId, areaId, |
| 548 | interfaceId, intfcList); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | log.debug("updateInterfaceParameters:: all processes::{}", processes); |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Creates server bootstrap for interface. |
| 560 | * |
| 561 | * @param interfaceIPs set of interfaces |
| 562 | * @param areaIds set of area id's |
| 563 | */ |
| 564 | private void createBootStrapForCreatedInterface(Set<String> interfaceIPs, Set<String> areaIds) { |
| 565 | |
| 566 | log.debug("createBootStrapForCreatedInterface:: List of new Interfaces::{}, " + |
| 567 | "List of new areas::{}", interfaceIPs, areaIds); |
| 568 | List<String> networkInterfaces = new ArrayList(); |
| 569 | //get the connected interfaces |
| 570 | Enumeration<NetworkInterface> nets = null; |
| 571 | try { |
| 572 | nets = NetworkInterface.getNetworkInterfaces(); |
| 573 | // Check NetworkInterfaces and add the IP's |
| 574 | for (NetworkInterface netInt : Collections.list(nets)) { |
| 575 | // if the interface is up & not loopback |
| 576 | if (!netInt.isUp() && !netInt.isLoopback()) { |
| 577 | continue; |
| 578 | } |
| 579 | //get all the InetAddresses |
| 580 | Enumeration<InetAddress> inetAddresses = netInt.getInetAddresses(); |
| 581 | for (InetAddress inetAddress : Collections.list(inetAddresses)) { |
| 582 | String ipAddress = inetAddress.getHostAddress(); |
| 583 | networkInterfaces.add(ipAddress); |
| 584 | } |
| 585 | } |
| 586 | //Search for the address in all configured areas interfaces |
| 587 | for (OspfProcess process : processes) { |
| 588 | for (OspfArea area : process.areas()) { |
| 589 | for (OspfInterface ospfIf : area.getInterfacesLst()) { |
| 590 | String ipFromConfig = ospfIf.ipAddress().toString(); |
| 591 | if (interfaceIPs.contains(ipFromConfig)) { |
| 592 | log.debug("Ip address::{} for area {} is newly created" + ipFromConfig); |
| 593 | if (networkInterfaces.contains(ipFromConfig)) { |
| 594 | log.debug("Both Config and Interface have ipAddress {} for area {}", |
| 595 | ipFromConfig, area.areaId()); |
| 596 | // if same IP address create |
| 597 | try { |
| 598 | log.debug("Creating ServerBootstrap for {} @ {}", ipFromConfig, ospfPort); |
| 599 | |
| 600 | final ServerBootstrap bootstrap = createServerBootStrap(); |
| 601 | |
| 602 | bootstrap.setOption("receiveBufferSize", Controller.BUFFER_SIZE); |
| 603 | bootstrap.setOption("receiveBufferSizePredictorFactory", |
| 604 | new FixedReceiveBufferSizePredictorFactory( |
| 605 | Controller.BUFFER_SIZE)); |
| 606 | bootstrap.setOption("reuseAddress", true); |
| 607 | bootstrap.setOption("tcpNoDelay", true); |
| 608 | bootstrap.setOption("keepAlive", true); |
| 609 | |
| 610 | bootstrap.setOption("child.receiveBufferSize", Controller.BUFFER_SIZE); |
| 611 | bootstrap.setOption("child.receiveBufferSizePredictorFactory", |
| 612 | new FixedReceiveBufferSizePredictorFactory( |
| 613 | Controller.BUFFER_SIZE)); |
| 614 | bootstrap.setOption("child.reuseAddress", true); |
| 615 | bootstrap.setOption("child.tcpNoDelay", true); |
| 616 | bootstrap.setOption("child.keepAlive", true); |
| 617 | bootstrap.setOption("receiveBufferSizePredictorFactory", |
| 618 | new FixedReceiveBufferSizePredictorFactory( |
| 619 | Controller.BUFFER_SIZE)); |
| 620 | bootstrap.setOption("receiveBufferSizePredictor", |
| 621 | new AdaptiveReceiveBufferSizePredictor(64, 1024, 65536)); |
| 622 | |
| 623 | ChannelPipelineFactory pfact = new OspfPipelineFactory(this, area, ospfIf); |
| 624 | bootstrap.setPipelineFactory(pfact); |
| 625 | InetSocketAddress sa = new InetSocketAddress(InetAddress.getByName(ipFromConfig), |
| 626 | ospfPort); |
| 627 | |
| 628 | ChannelGroup cg = new DefaultChannelGroup(); |
| 629 | cg.add(bootstrap.bind(sa)); |
| 630 | cgList.add(cg); |
| 631 | |
| 632 | log.debug("Listening for connections on {}", sa); |
| 633 | |
| 634 | } catch (Exception e) { |
| 635 | throw new RuntimeException(e); |
| 636 | } |
| 637 | } |
| 638 | } else { |
| 639 | log.debug("Ip address::{} for area {} is not newly created" + ipFromConfig); |
| 640 | } |
| 641 | } |
| 642 | if (areaIds.contains(area.areaId().toString())) { |
| 643 | area.initializeDb(); |
| 644 | } |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | } catch (SocketException e) { |
| 649 | log.error("Error occured due to SocketException::Class::{},Line::{},Method::{}", |
| 650 | e.getStackTrace()[0].getFileName(), e.getStackTrace()[0].getLineNumber(), |
| 651 | e.getStackTrace()[0].getMethodName()); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Deletes given process. |
| 657 | * |
| 658 | * @param ospfProcesses list of OSPF process instance. |
| 659 | * @param attribute attribute to delete |
| 660 | */ |
| 661 | public void deleteProcessWhenExists(List<OspfProcess> ospfProcesses, String attribute) { |
| 662 | if (ospfProcesses != null) { |
| 663 | for (OspfProcess process : ospfProcesses) { |
| 664 | if (checkProcess(process.processId())) { |
| 665 | if (PROCESS.equalsIgnoreCase(attribute)) { |
| 666 | deleteProcess(process.processId(), process); |
| 667 | } else { |
| 668 | List<OspfArea> areas = process.areas(); |
| 669 | for (OspfArea area : areas) { |
| 670 | if (checkArea(process.processId(), area.areaId().toString())) { |
| 671 | if (AREA.equalsIgnoreCase(attribute)) { |
| 672 | deleteAreaFromProcess(process.processId(), |
| 673 | area.areaId().toString(), area); |
| 674 | } else { |
| 675 | for (OspfInterface interfc : area.getInterfacesLst()) { |
| 676 | if (checkInterface(process.processId(), |
| 677 | area.areaId().toString(), |
| 678 | interfc.ipAddress().toString())) { |
| 679 | if (INTERFACE.equalsIgnoreCase(attribute)) { |
| 680 | deleteInterfaceFromAreaProcess(process.processId(), |
| 681 | area.areaId().toString(), |
| 682 | interfc); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | } |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Deletes given process. |
| 697 | * |
| 698 | * @param processId process id |
| 699 | * @param process OSPF process instance |
| 700 | */ |
| 701 | private void deleteProcess(String processId, OspfProcess process) { |
| 702 | if (processes != null) { |
| 703 | Iterator<OspfProcess> itrProcess = processes.iterator(); |
| 704 | while (itrProcess.hasNext()) { |
| 705 | OspfProcess ospfPrs = itrProcess.next(); |
| 706 | if (processId.equalsIgnoreCase(ospfPrs.processId())) { |
| 707 | itrProcess.remove(); |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Deletes area from process. |
| 715 | * |
| 716 | * @param processId process id |
| 717 | * @param areaId area id |
| 718 | * @param area OSPF area instance |
| 719 | */ |
| 720 | private void deleteAreaFromProcess(String processId, String areaId, OspfArea area) { |
| 721 | if (processes != null) { |
| 722 | Iterator<OspfProcess> itrProcess = processes.iterator(); |
| 723 | while (itrProcess.hasNext()) { |
| 724 | OspfProcess ospfPrs = itrProcess.next(); |
| 725 | if (processId.equalsIgnoreCase(ospfPrs.processId())) { |
| 726 | if (ospfPrs.areas() != null) { |
| 727 | Iterator<OspfArea> itrArea = ospfPrs.areas().iterator(); |
| 728 | while (itrArea.hasNext()) { |
| 729 | OspfArea ospfArea = itrArea.next(); |
| 730 | if (areaId.equalsIgnoreCase(ospfArea.areaId().toString())) { |
| 731 | itrArea.remove(); |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Deletes interface from area. |
| 742 | * |
| 743 | * @param processId process id |
| 744 | * @param areaId area id |
| 745 | * @param interfaceToDelete interface to delete |
| 746 | */ |
| 747 | private void deleteInterfaceFromAreaProcess(String processId, String areaId, OspfInterface interfaceToDelete) { |
| 748 | if (processes != null) { |
| 749 | Iterator<OspfProcess> itrProcess = processes.iterator(); |
| 750 | while (itrProcess.hasNext()) { |
| 751 | OspfProcess ospfPrs = itrProcess.next(); |
| 752 | if (processId.equalsIgnoreCase(ospfPrs.processId())) { |
| 753 | if (ospfPrs.areas() != null) { |
| 754 | Iterator<OspfArea> itrArea = ospfPrs.areas().iterator(); |
| 755 | while (itrArea.hasNext()) { |
| 756 | OspfArea ospfArea = itrArea.next(); |
| 757 | if (areaId.equalsIgnoreCase(ospfArea.areaId().toString())) { |
| 758 | if (ospfArea.getInterfacesLst() != null) { |
| 759 | Iterator<OspfInterface> intrfcList = ospfArea.getInterfacesLst().iterator(); |
| 760 | while (intrfcList.hasNext()) { |
| 761 | OspfInterface ospfItrfc = intrfcList.next(); |
| 762 | if (interfaceToDelete.ipAddress().equals(ospfItrfc.ipAddress())) { |
| 763 | intrfcList.remove(); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | } |