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