blob: f7b4a73462a6ef6589b00144afab33951d982460 [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
2 * Copyright 2016 Open Networking Laboratory
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
17package org.onosproject.ospf.controller.impl;
18
19import com.google.common.collect.Sets;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
26import org.onosproject.net.driver.DriverService;
27import org.onosproject.ospf.controller.OspfAgent;
28import org.onosproject.ospf.controller.OspfController;
29import org.onosproject.ospf.controller.OspfLinkListener;
30import org.onosproject.ospf.controller.OspfLinkTed;
31import org.onosproject.ospf.controller.OspfProcess;
32import org.onosproject.ospf.controller.OspfRouter;
33import org.onosproject.ospf.controller.OspfRouterListener;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
37import java.util.ArrayList;
38import java.util.HashSet;
39import java.util.List;
40import 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
48public 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
114 public void updateConfig(List processes) {
115 List<OspfProcess> ospfProcesses = new ArrayList<>();
116 if (processes != null) {
117 for (Object process : processes) {
118 ospfProcesses.add((OspfProcess) process);
119 }
120 }
121 log.debug("updateConfig::OspfList::processes::{}", ospfProcesses);
122 ctrl.updateConfig(ospfProcesses);
123 }
124
125 @Override
126 public void deleteConfig(List<OspfProcess> processes, String attribute) {
127 List<OspfProcess> ospfProcesses = new ArrayList<>();
128 if (processes != null) {
129 for (Object process : processes) {
130 ospfProcesses.add((OspfProcess) process);
131 }
132 }
133 log.debug("deleteConfig::OspfList::processes::{}", ospfProcesses);
134 ctrl.deleteConfig(ospfProcesses, attribute);
135 }
136
137 /**
138 * Notifier for internal OSPF device and link changes.
139 */
140 private class InternalDeviceConfig implements OspfAgent {
141
142 @Override
143 public boolean addConnectedRouter(OspfRouter ospfRouter) {
144 for (OspfRouterListener l : listener()) {
145 l.routerAdded(ospfRouter);
146 }
147 return true;
148 }
149
150 @Override
151 public void removeConnectedRouter(OspfRouter ospfRouter) {
152 for (OspfRouterListener l : listener()) {
153 l.routerRemoved(ospfRouter);
154 }
155 }
156
157 @Override
158 public void addLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) {
159 for (OspfLinkListener l : linkListener()) {
160 l.addLink(ospfRouter, ospfLinkTed);
161 }
162
163 }
164
165 @Override
166 public void deleteLink(OspfRouter ospfRouter) {
167 for (OspfLinkListener l : linkListener()) {
168 l.deleteLink(ospfRouter);
169 }
170 }
171 }
172}