blob: 01f52dcbfa770f413f07551cbdcc2433e81222ec [file] [log] [blame]
Dhruv Dhody4d8943a2016-02-17 16:36:08 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onosproject.net.driver.DriverService;
28import org.onosproject.ospf.controller.OspfAgent;
29import org.onosproject.ospf.controller.OspfController;
30import org.onosproject.ospf.controller.OspfLinkListener;
31import org.onosproject.ospf.controller.OspfLinkTed;
32import org.onosproject.ospf.controller.OspfProcess;
33import org.onosproject.ospf.controller.OspfRouter;
34import org.onosproject.ospf.controller.OspfRouterListener;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
Dhruv Dhody4d8943a2016-02-17 16:36:08 +053038import 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
sunishvkf7c56552016-07-18 16:02:39 +0530114 public void updateConfig(JsonNode processesNode) {
115 try {
116 List<OspfProcess> ospfProcesses = OspfConfigUtil.processes(processesNode);
117 //if there is interface details then update configuration
Jon Hallcbd1b392017-01-18 20:15:44 -0800118 if (!ospfProcesses.isEmpty() &&
119 ospfProcesses.get(0).areas() != null && !ospfProcesses.get(0).areas().isEmpty() &&
sunishvkf7c56552016-07-18 16:02:39 +0530120 ospfProcesses.get(0).areas().get(0) != null &&
Jon Hallcbd1b392017-01-18 20:15:44 -0800121 !ospfProcesses.get(0).areas().get(0).ospfInterfaceList().isEmpty()) {
sunishvkf7c56552016-07-18 16:02:39 +0530122 ctrl.updateConfig(ospfProcesses);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530123 }
sunishvkf7c56552016-07-18 16:02:39 +0530124 } catch (Exception e) {
125 log.debug("Error::updateConfig::{}", e.getMessage());
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530126 }
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530127 }
128
129 @Override
130 public void deleteConfig(List<OspfProcess> processes, String attribute) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530131 }
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
sunishvkf7c56552016-07-18 16:02:39 +0530162 public void deleteLink(OspfRouter ospfRouter, OspfLinkTed ospfLinkTed) {
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530163 for (OspfLinkListener l : linkListener()) {
sunishvkf7c56552016-07-18 16:02:39 +0530164 l.deleteLink(ospfRouter, ospfLinkTed);
Dhruv Dhody4d8943a2016-02-17 16:36:08 +0530165 }
166 }
167 }
168}