blob: dc63617b24d0f3228bcccdf4af5e7390fc43a7ee [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05303 *
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
17package org.onosproject.ospf.controller.impl;
18
sunishvkf7c56552016-07-18 16:02:39 +053019import com.fasterxml.jackson.databind.JsonNode;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053020import com.google.common.collect.Sets;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053021import org.onosproject.net.driver.DriverService;
22import org.onosproject.ospf.controller.OspfAgent;
23import org.onosproject.ospf.controller.OspfController;
24import org.onosproject.ospf.controller.OspfLinkListener;
25import org.onosproject.ospf.controller.OspfLinkTed;
26import org.onosproject.ospf.controller.OspfProcess;
27import org.onosproject.ospf.controller.OspfRouter;
28import org.onosproject.ospf.controller.OspfRouterListener;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053037import java.util.HashSet;
38import java.util.List;
39import 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 Milkeyd84f89b2018-08-17 14:54:17 -070045@Component(immediate = true, service = OspfController.class)
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053046public class OspfControllerImpl implements OspfController {
47
Ray Milkey9c9cde42018-01-12 14:22:06 -080048 private static final Logger log = LoggerFactory.getLogger(OspfControllerImpl.class);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053049 private final Controller ctrl = new Controller();
Ray Milkeyd84f89b2018-08-17 14:54:17 -070050 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053051 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
sunishvkf7c56552016-07-18 16:02:39 +0530112 public void updateConfig(JsonNode processesNode) {
113 try {
114 List<OspfProcess> ospfProcesses = OspfConfigUtil.processes(processesNode);
115 //if there is interface details then update configuration
Jon Hallcbd1b392017-01-18 20:15:44 -0800116 if (!ospfProcesses.isEmpty() &&
117 ospfProcesses.get(0).areas() != null && !ospfProcesses.get(0).areas().isEmpty() &&
sunishvkf7c56552016-07-18 16:02:39 +0530118 ospfProcesses.get(0).areas().get(0) != null &&
Jon Hallcbd1b392017-01-18 20:15:44 -0800119 !ospfProcesses.get(0).areas().get(0).ospfInterfaceList().isEmpty()) {
sunishvkf7c56552016-07-18 16:02:39 +0530120 ctrl.updateConfig(ospfProcesses);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530121 }
sunishvkf7c56552016-07-18 16:02:39 +0530122 } catch (Exception e) {
123 log.debug("Error::updateConfig::{}", e.getMessage());
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530124 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530125 }
126
127 @Override
128 public void deleteConfig(List<OspfProcess> processes, String attribute) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530129 }
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
sunishvkf7c56552016-07-18 16:02:39 +0530160 public void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530161 for (OspfLinkListener l : linkListener()) {
sunishvkf7c56552016-07-18 16:02:39 +0530162 l.deleteLink(ospfRouter, ospfLinkTed);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530163 }
164 }
165 }
166}