blob: 853963eab64e93c1d96e18925e6ca271ccb3c76e [file] [log] [blame]
Hyunsun Moone7e4bb32016-05-16 04:32:45 -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 */
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070016package org.opencord.cordvtn.impl.handler;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070017
18import com.google.common.collect.Maps;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
alshabibb4d31712016-06-01 18:51:03 -070022
Hyunsun Moon60a10672016-06-12 17:39:12 -070023import org.opencord.cordvtn.impl.AbstractInstanceHandler;
alshabibb4d31712016-06-01 18:51:03 -070024import org.opencord.cordvtn.api.CordVtnConfig;
25import org.opencord.cordvtn.api.Instance;
26import org.opencord.cordvtn.api.InstanceHandler;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070027import org.onosproject.net.DeviceId;
alshabibb4d31712016-06-01 18:51:03 -070028
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070029import org.onosproject.net.config.ConfigFactory;
30import org.onosproject.net.config.NetworkConfigEvent;
31import org.onosproject.net.config.NetworkConfigListener;
32import org.onosproject.net.config.basics.SubjectFactories;
alshabibb4d31712016-06-01 18:51:03 -070033import org.opencord.cordconfig.access.AccessAgentConfig;
34import org.opencord.cordconfig.access.AccessAgentData;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070035
36import java.util.Map;
Hyunsun Moon60a10672016-06-12 17:39:12 -070037import java.util.Optional;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070038import java.util.Set;
39
Hyunsun Moon60a10672016-06-12 17:39:12 -070040import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.OLT_AGENT;
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070041
42/**
43 * Provides network connectivity for OLT agent instances.
44 */
45@Component(immediate = true)
Hyunsun Moon60a10672016-06-12 17:39:12 -070046public class OltAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070047
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 Moone7e4bb32016-05-16 04:32:45 -070059
60 @Activate
61 protected void activate() {
Hyunsun Moon60a10672016-06-12 17:39:12 -070062 serviceType = Optional.of(OLT_AGENT);
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070063 configListener = new InternalConfigListener();
64 super.activate();
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070065
66 configRegistry.registerConfigFactory(configFactory);
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070067 }
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 Moone7e4bb32016-05-16 04:32:45 -070077 // TODO implement
78 }
79
80 @Override
81 public void instanceRemoved(Instance instance) {
82 log.info("OLT agent instance removed {}", instance);
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070083 // TODO implement
84 }
85
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070086 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 Moon60a10672016-06-12 17:39:12 -070097 private class InternalConfigListener implements NetworkConfigListener {
Hyunsun Moone7e4bb32016-05-16 04:32:45 -070098
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}