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; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 22 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 23 | import org.opencord.cordvtn.impl.AbstractInstanceHandler; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 24 | import org.opencord.cordvtn.api.CordVtnConfig; |
| 25 | import org.opencord.cordvtn.api.Instance; |
| 26 | import org.opencord.cordvtn.api.InstanceHandler; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 28 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.ConfigFactory; |
| 30 | import org.onosproject.net.config.NetworkConfigEvent; |
| 31 | import org.onosproject.net.config.NetworkConfigListener; |
| 32 | import org.onosproject.net.config.basics.SubjectFactories; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 33 | import org.opencord.cordconfig.access.AccessAgentConfig; |
| 34 | import org.opencord.cordconfig.access.AccessAgentData; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 35 | |
| 36 | import java.util.Map; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 37 | import java.util.Optional; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | |
| 40 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 41 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 42 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.OLT_AGENT; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Provides network connectivity for OLT agent instances. |
| 46 | */ |
| 47 | @Component(immediate = true) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 48 | public class OltAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 49 | |
| 50 | private static final Class<AccessAgentConfig> CONFIG_CLASS = AccessAgentConfig.class; |
| 51 | private ConfigFactory<DeviceId, AccessAgentConfig> configFactory = |
| 52 | new ConfigFactory<DeviceId, AccessAgentConfig>( |
| 53 | SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessAgent") { |
| 54 | @Override |
| 55 | public AccessAgentConfig createConfig() { |
| 56 | return new AccessAgentConfig(); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | private Map<DeviceId, AccessAgentData> oltAgentData = Maps.newConcurrentMap(); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 61 | |
| 62 | @Activate |
| 63 | protected void activate() { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 64 | serviceType = Optional.of(OLT_AGENT); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 65 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn-olt", "event-handler")); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 66 | |
| 67 | configRegistry.registerConfigFactory(configFactory); |
| 68 | configListener = new InternalConfigListener(); |
| 69 | |
| 70 | super.activate(); |
| 71 | } |
| 72 | |
| 73 | @Deactivate |
| 74 | protected void deactivate() { |
| 75 | super.deactivate(); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void instanceDetected(Instance instance) { |
| 80 | log.info("OLT agent instance detected {}", instance); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 81 | // TODO implement |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public void instanceRemoved(Instance instance) { |
| 86 | log.info("OLT agent instance removed {}", instance); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 87 | // TODO implement |
| 88 | } |
| 89 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 90 | private void readAccessAgentConfig() { |
| 91 | |
| 92 | Set<DeviceId> deviceSubjects = configRegistry.getSubjects(DeviceId.class, CONFIG_CLASS); |
| 93 | deviceSubjects.stream().forEach(subject -> { |
| 94 | AccessAgentConfig config = configRegistry.getConfig(subject, CONFIG_CLASS); |
| 95 | if (config != null) { |
| 96 | oltAgentData.put(subject, config.getAgent()); |
| 97 | } |
| 98 | }); |
| 99 | } |
| 100 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame^] | 101 | private class InternalConfigListener implements NetworkConfigListener { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 102 | |
| 103 | @Override |
| 104 | public void event(NetworkConfigEvent event) { |
| 105 | |
| 106 | switch (event.type()) { |
| 107 | case CONFIG_UPDATED: |
| 108 | case CONFIG_ADDED: |
| 109 | if (event.configClass().equals(CordVtnConfig.class)) { |
| 110 | readConfiguration(); |
| 111 | } else if (event.configClass().equals(CONFIG_CLASS)) { |
| 112 | readAccessAgentConfig(); |
| 113 | } |
| 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |