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