Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl.service; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.Maps; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.IpPrefix; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 24 | |
| 25 | import org.opencord.cordvtn.api.CordVtnConfig; |
| 26 | import org.opencord.cordvtn.api.Instance; |
| 27 | import org.opencord.cordvtn.api.InstanceHandler; |
| 28 | import org.opencord.cordvtn.impl.CordVtnInstanceHandler; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.PortNumber; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 31 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 32 | import org.onosproject.net.config.ConfigFactory; |
| 33 | import org.onosproject.net.config.NetworkConfigEvent; |
| 34 | import org.onosproject.net.config.NetworkConfigListener; |
| 35 | import org.onosproject.net.config.basics.SubjectFactories; |
| 36 | import org.onosproject.net.flow.DefaultFlowRule; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.FlowRule; |
| 40 | import org.onosproject.net.flow.TrafficSelector; |
| 41 | import org.onosproject.net.flow.TrafficTreatment; |
| 42 | import org.onosproject.xosclient.api.VtnService; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 43 | import org.opencord.cordconfig.access.AccessAgentConfig; |
| 44 | import org.opencord.cordconfig.access.AccessAgentData; |
| 45 | import org.opencord.cordvtn.impl.CordVtnPipeline; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 46 | |
| 47 | import java.util.Map; |
| 48 | import java.util.Set; |
| 49 | |
| 50 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 51 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Provides network connectivity for OLT agent instances. |
| 55 | */ |
| 56 | @Component(immediate = true) |
| 57 | public class OltAgentInstanceHandler extends CordVtnInstanceHandler implements InstanceHandler { |
| 58 | |
| 59 | private static final Class<AccessAgentConfig> CONFIG_CLASS = AccessAgentConfig.class; |
| 60 | private ConfigFactory<DeviceId, AccessAgentConfig> configFactory = |
| 61 | new ConfigFactory<DeviceId, AccessAgentConfig>( |
| 62 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessAgent") { |
| 63 | @Override |
| 64 | public AccessAgentConfig createConfig() { |
| 65 | return new AccessAgentConfig(); |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | private Map<DeviceId, AccessAgentData> oltAgentData = Maps.newConcurrentMap(); |
| 70 | private IpPrefix mgmtIpRange = null; |
| 71 | |
| 72 | @Activate |
| 73 | protected void activate() { |
| 74 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn-olt", "event-handler")); |
| 75 | serviceType = VtnService.ServiceType.OLT_AGENT; |
| 76 | |
| 77 | configRegistry.registerConfigFactory(configFactory); |
| 78 | configListener = new InternalConfigListener(); |
| 79 | |
| 80 | super.activate(); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | protected void deactivate() { |
| 85 | super.deactivate(); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void instanceDetected(Instance instance) { |
| 90 | log.info("OLT agent instance detected {}", instance); |
| 91 | |
| 92 | managementAccessRule(instance.deviceId(), true); |
| 93 | // TODO implement |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void instanceRemoved(Instance instance) { |
| 98 | log.info("OLT agent instance removed {}", instance); |
| 99 | |
| 100 | if (getInstances(instance.serviceId()).isEmpty()) { |
| 101 | nodeManager.completeNodes().stream().forEach(node -> |
| 102 | managementAccessRule(node.intBrId(), false)); |
| 103 | } |
| 104 | |
| 105 | // TODO implement |
| 106 | } |
| 107 | |
| 108 | private void managementAccessRule(DeviceId deviceId, boolean install) { |
| 109 | // TODO remove this rule after long term management network is done |
| 110 | if (mgmtIpRange != null) { |
| 111 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 112 | .matchEthType(Ethernet.TYPE_IPV4) |
| 113 | .matchIPDst(mgmtIpRange) |
| 114 | .build(); |
| 115 | |
| 116 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 117 | .setOutput(PortNumber.LOCAL) |
| 118 | .build(); |
| 119 | |
| 120 | FlowRule flowRule = DefaultFlowRule.builder() |
| 121 | .fromApp(appId) |
| 122 | .withSelector(selector) |
| 123 | .withTreatment(treatment) |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 124 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 125 | .forDevice(deviceId) |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 126 | .forTable(CordVtnPipeline.TABLE_ACCESS_TYPE) |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 127 | .makePermanent() |
| 128 | .build(); |
| 129 | |
| 130 | pipeline.processFlowRule(install, flowRule); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private void readAccessAgentConfig() { |
| 135 | |
| 136 | Set<DeviceId> deviceSubjects = configRegistry.getSubjects(DeviceId.class, CONFIG_CLASS); |
| 137 | deviceSubjects.stream().forEach(subject -> { |
| 138 | AccessAgentConfig config = configRegistry.getConfig(subject, CONFIG_CLASS); |
| 139 | if (config != null) { |
| 140 | oltAgentData.put(subject, config.getAgent()); |
| 141 | } |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected void readConfiguration() { |
| 147 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 148 | if (config == null) { |
| 149 | log.debug("No configuration found"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | osAccess = config.openstackAccess(); |
| 154 | xosAccess = config.xosAccess(); |
| 155 | mgmtIpRange = config.managementIpRange(); |
| 156 | } |
| 157 | |
| 158 | public class InternalConfigListener implements NetworkConfigListener { |
| 159 | |
| 160 | @Override |
| 161 | public void event(NetworkConfigEvent event) { |
| 162 | |
| 163 | switch (event.type()) { |
| 164 | case CONFIG_UPDATED: |
| 165 | case CONFIG_ADDED: |
| 166 | if (event.configClass().equals(CordVtnConfig.class)) { |
| 167 | readConfiguration(); |
| 168 | } else if (event.configClass().equals(CONFIG_CLASS)) { |
| 169 | readAccessAgentConfig(); |
| 170 | } |
| 171 | break; |
| 172 | default: |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |