Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +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 | |
| 17 | package org.onosproject.ospf.controller.impl; |
| 18 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 21 | import org.onosproject.net.driver.DriverService; |
| 22 | import org.onosproject.ospf.controller.OspfAgent; |
| 23 | import org.onosproject.ospf.controller.OspfController; |
| 24 | import org.onosproject.ospf.controller.OspfLinkListener; |
| 25 | import org.onosproject.ospf.controller.OspfLinkTed; |
| 26 | import org.onosproject.ospf.controller.OspfProcess; |
| 27 | import org.onosproject.ospf.controller.OspfRouter; |
| 28 | import org.onosproject.ospf.controller.OspfRouterListener; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 37 | import java.util.HashSet; |
| 38 | import java.util.List; |
| 39 | import java.util.Set; |
| 40 | |
| 41 | /** |
| 42 | * Representation of an OSPF controller implementation. |
| 43 | * Serves as a one stop shop for obtaining OSPF devices and (un)register listeners on OSPF events |
| 44 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 45 | @Component(immediate = true, service = OspfController.class) |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 46 | public class OspfControllerImpl implements OspfController { |
| 47 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 48 | private static final Logger log = LoggerFactory.getLogger(OspfControllerImpl.class); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 49 | private final Controller ctrl = new Controller(); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 50 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 51 | protected DriverService driverService; |
| 52 | protected Set<OspfRouterListener> ospfRouterListener = new HashSet<>(); |
| 53 | protected Set<OspfLinkListener> ospfLinkListener = Sets.newHashSet(); |
| 54 | protected OspfAgent agent = new InternalDeviceConfig(); |
| 55 | |
| 56 | @Activate |
| 57 | public void activate() { |
| 58 | log.info("OSPFControllerImpl activate...!!!"); |
| 59 | ctrl.start(agent, driverService); |
| 60 | log.info("Started"); |
| 61 | } |
| 62 | |
| 63 | @Deactivate |
| 64 | public void deactivate() { |
| 65 | ctrl.stop(); |
| 66 | log.info("Stopped"); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | @Override |
| 71 | public void addRouterListener(OspfRouterListener listener) { |
| 72 | if (!ospfRouterListener.contains(listener)) { |
| 73 | this.ospfRouterListener.add(listener); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void removeRouterListener(OspfRouterListener listener) { |
| 79 | this.ospfRouterListener.remove(listener); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void addLinkListener(OspfLinkListener listener) { |
| 84 | ospfLinkListener.add(listener); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void removeLinkListener(OspfLinkListener listener) { |
| 90 | ospfLinkListener.remove(listener); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Set<OspfRouterListener> listener() { |
| 96 | return ospfRouterListener; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Set<OspfLinkListener> linkListener() { |
| 101 | return ospfLinkListener; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | @Override |
| 106 | public List<OspfProcess> getAllConfiguredProcesses() { |
| 107 | List<OspfProcess> processes = ctrl.getAllConfiguredProcesses(); |
| 108 | return processes; |
| 109 | } |
| 110 | |
| 111 | @Override |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 112 | public void updateConfig(JsonNode processesNode) { |
| 113 | try { |
| 114 | List<OspfProcess> ospfProcesses = OspfConfigUtil.processes(processesNode); |
| 115 | //if there is interface details then update configuration |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 116 | if (!ospfProcesses.isEmpty() && |
| 117 | ospfProcesses.get(0).areas() != null && !ospfProcesses.get(0).areas().isEmpty() && |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 118 | ospfProcesses.get(0).areas().get(0) != null && |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 119 | !ospfProcesses.get(0).areas().get(0).ospfInterfaceList().isEmpty()) { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 120 | ctrl.updateConfig(ospfProcesses); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 121 | } |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 122 | } catch (Exception e) { |
| 123 | log.debug("Error::updateConfig::{}", e.getMessage()); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 124 | } |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void deleteConfig(List<OspfProcess> processes, String attribute) { |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Notifier for internal OSPF device and link changes. |
| 133 | */ |
| 134 | private class InternalDeviceConfig implements OspfAgent { |
| 135 | |
| 136 | @Override |
| 137 | public boolean addConnectedRouter(OspfRouter ospfRouter) { |
| 138 | for (OspfRouterListener l : listener()) { |
| 139 | l.routerAdded(ospfRouter); |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public void removeConnectedRouter(OspfRouter ospfRouter) { |
| 146 | for (OspfRouterListener l : listener()) { |
| 147 | l.routerRemoved(ospfRouter); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void addLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) { |
| 153 | for (OspfLinkListener l : linkListener()) { |
| 154 | l.addLink(ospfRouter, ospfLinkTed); |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | @Override |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 160 | public void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) { |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 161 | for (OspfLinkListener l : linkListener()) { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 162 | l.deleteLink(ospfRouter, ospfLinkTed); |
Dhruv Dhody | 4d8943a | 2016-02-17 16:36:08 +0530 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |