blob: 618c0f0f8c7022ff4a6548a3078cfbd2350e312a [file] [log] [blame]
Charles Chan35fd1a72016-06-13 18:54:31 -07001/*
2 * Copyright 2016-present 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.segmentrouting;
18
19import com.google.common.collect.ImmutableSet;
20import org.onlab.packet.Ip4Prefix;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.DefaultHost;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.Host;
27import org.onosproject.net.HostId;
28import org.onosproject.net.HostLocation;
29import org.onosproject.net.provider.ProviderId;
30import org.opencord.cordconfig.CordConfigEvent;
31import org.opencord.cordconfig.access.AccessAgentData;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Optional;
36
37/**
38 * Handles access agent config event which is required for CORD integration.
39 */
40public class CordConfigHandler {
41 private static Logger log = LoggerFactory.getLogger(CordConfigHandler.class);
42 private final SegmentRoutingManager srManager;
43
44 /**
45 * Constructs the CordConfigHandler.
46 *
47 * @param srManager Segment Routing manager
48 */
49 public CordConfigHandler(SegmentRoutingManager srManager) {
50 this.srManager = srManager;
51 }
52
53 /**
54 * Read initial access agent config for given device.
55 *
56 * @param deviceId ID of the device to be initialized
57 */
58 public void init(DeviceId deviceId) {
59 // Try to read access agent config
60 Optional<AccessAgentData> accessAgent =
61 srManager.cordConfigService.getAccessAgent(deviceId);
62
63 if (!accessAgent.isPresent()) {
64 log.debug("No access agent config on {}. Skip.", deviceId);
65 return;
66 }
67
68 processAccessAgentAdded(accessAgent.get());
69 }
70
71 // TODO javadoc
72 protected void processAccessAgentAddedEvent(CordConfigEvent event) {
73 log.debug("processAccessAgentAdded: {}, {}", event.subject(), event.prevSubject());
74 processAccessAgentAdded((AccessAgentData) event.subject());
75 }
76
77 protected void processAccessAgentUpdatedEvent(CordConfigEvent event) {
78 log.debug("processAccessAgentUpdated: {}, {}", event.subject(), event.prevSubject());
79 processAccessAgentRemoved((AccessAgentData) event.prevSubject());
80 processAccessAgentAdded((AccessAgentData) event.subject());
81 }
82
83 protected void processAccessAgentRemovedEvent(CordConfigEvent event) {
84 log.debug("processAccessAgentRemoved: {}, {}", event.subject(), event.prevSubject());
85 processAccessAgentRemoved((AccessAgentData) event.prevSubject());
86 }
87
88 protected void processAccessAgentAdded(AccessAgentData accessAgentData) {
89 if (!srManager.mastershipService.isLocalMaster(accessAgentData.deviceId())) {
90 log.debug("Not the master of {}. Abort.", accessAgentData.deviceId());
91 return;
92 }
93
94 // Do not proceed if vtn location is missing
95 if (!accessAgentData.getVtnLocation().isPresent()) {
96 log.warn("accessAgentData does not contain vtn location. Abort.");
97 return;
98 }
99
100 MacAddress agentMac = accessAgentData.getAgentMac();
101 ConnectPoint agentLocation = accessAgentData.getVtnLocation().get();
102
103 // Do not proceed if agent port doesn't have subnet configured
104 Ip4Prefix agentSubnet = srManager.deviceConfiguration
Pier Ventre10bd8d12016-11-26 21:05:22 -0800105 .getPortIPv4Subnet(agentLocation.deviceId(), agentLocation.port());
Charles Chan35fd1a72016-06-13 18:54:31 -0700106 if (agentSubnet == null) {
107 log.warn("Agent port does not have subnet configuration. Abort.");
108 return;
109 }
110
111 // Add host information for agent
112 log.info("push host info for agent {}", agentMac);
113 srManager.hostHandler.processHostAdded(createHost(agentMac, agentLocation));
114
115 accessAgentData.getOltMacInfo().forEach((connectPoint, macAddress) -> {
116 // Do not proceed if olt port has subnet configured
117 Ip4Prefix oltSubnet = srManager.deviceConfiguration
Pier Ventre10bd8d12016-11-26 21:05:22 -0800118 .getPortIPv4Subnet(connectPoint.deviceId(), connectPoint.port());
Charles Chan35fd1a72016-06-13 18:54:31 -0700119 if (oltSubnet != null) {
120 log.warn("OLT port has subnet configuration. Abort.");
121 return;
122 }
123
124 // Add olt to the subnet of agent
125 log.info("push subnet for olt {}", agentSubnet);
126 srManager.deviceConfiguration.addSubnet(connectPoint, agentSubnet);
127 srManager.routingRulePopulator.populateRouterMacVlanFilters(connectPoint.deviceId());
128
129 // Add host information for olt
130 log.info("push host info for olt {}", macAddress);
131 srManager.hostHandler.processHostAdded(createHost(macAddress, connectPoint));
132 });
133 }
134
135 protected void processAccessAgentRemoved(AccessAgentData accessAgentData) {
136 if (!srManager.mastershipService.isLocalMaster(accessAgentData.deviceId())) {
137 log.debug("Not the master of {}. Abort.", accessAgentData.deviceId());
138 return;
139 }
140
141 // Do not proceed if vtn location is missing
142 if (!accessAgentData.getVtnLocation().isPresent()) {
143 log.warn("accessAgentData does not contain vtn location. Abort.");
144 return;
145 }
146
147 MacAddress agentMac = accessAgentData.getAgentMac();
148 ConnectPoint agentLocation = accessAgentData.getVtnLocation().get();
149
150 // Do not proceed if olt port doesn't have subnet configured
151 Ip4Prefix agentSubnet = srManager.deviceConfiguration
Pier Ventre10bd8d12016-11-26 21:05:22 -0800152 .getPortIPv4Subnet(agentLocation.deviceId(), agentLocation.port());
Charles Chan35fd1a72016-06-13 18:54:31 -0700153 if (agentSubnet == null) {
154 log.warn("Agent port does not have subnet configuration. Abort.");
155 return;
156 }
157
158 // Remove host information for agent
159 log.info("delete host info for agent {}", agentMac);
160 srManager.hostHandler.processHostRemoved(createHost(agentMac, agentLocation));
161
162 accessAgentData.getOltMacInfo().forEach((connectPoint, macAddress) -> {
163 // Do not proceed if agent port doesn't have subnet configured
164 Ip4Prefix oltSubnet = srManager.deviceConfiguration
Pier Ventre10bd8d12016-11-26 21:05:22 -0800165 .getPortIPv4Subnet(connectPoint.deviceId(), connectPoint.port());
Charles Chan35fd1a72016-06-13 18:54:31 -0700166 if (oltSubnet == null) {
167 log.warn("OLT port does not have subnet configuration. Abort.");
168 return;
169 }
170
171 // Remove host information for olt
172 log.info("delete host info for olt {}", macAddress);
173 srManager.hostHandler.processHostRemoved(createHost(macAddress, connectPoint));
174
175 // Remove olt to the subnet of agent
176 log.info("delete subnet for olt {}", agentSubnet);
177 srManager.deviceConfiguration.removeSubnet(connectPoint, agentSubnet);
178 srManager.routingRulePopulator.populateRouterMacVlanFilters(connectPoint.deviceId());
179 });
180 }
181
182 private Host createHost(MacAddress macAddress, ConnectPoint location) {
183 return new DefaultHost(
184 new ProviderId("host", "org.onosproject.segmentrouting"),
185 HostId.hostId(macAddress),
186 macAddress,
187 VlanId.NONE,
188 new HostLocation(location, System.currentTimeMillis()),
189 ImmutableSet.of());
190 }
191}