blob: 853963eab64e93c1d96e18925e6ca271ccb3c76e [file] [log] [blame]
Hyunsun Moonfb417942016-06-23 14:48:20 -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 */
16package org.opencord.cordvtn.impl.handler;
17
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;
22
23import org.opencord.cordvtn.impl.AbstractInstanceHandler;
24import org.opencord.cordvtn.api.CordVtnConfig;
25import org.opencord.cordvtn.api.Instance;
26import org.opencord.cordvtn.api.InstanceHandler;
27import org.onosproject.net.DeviceId;
28
29import org.onosproject.net.config.ConfigFactory;
30import org.onosproject.net.config.NetworkConfigEvent;
31import org.onosproject.net.config.NetworkConfigListener;
32import org.onosproject.net.config.basics.SubjectFactories;
33import org.opencord.cordconfig.access.AccessAgentConfig;
34import org.opencord.cordconfig.access.AccessAgentData;
35
36import java.util.Map;
37import java.util.Optional;
38import java.util.Set;
39
40import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.OLT_AGENT;
41
42/**
43 * Provides network connectivity for OLT agent instances.
44 */
45@Component(immediate = true)
46public class OltAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
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();
59
60 @Activate
61 protected void activate() {
62 serviceType = Optional.of(OLT_AGENT);
63 configListener = new InternalConfigListener();
64 super.activate();
65
66 configRegistry.registerConfigFactory(configFactory);
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);
77 // TODO implement
78 }
79
80 @Override
81 public void instanceRemoved(Instance instance) {
82 log.info("OLT agent instance removed {}", instance);
83 // TODO implement
84 }
85
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
97 private class InternalConfigListener implements NetworkConfigListener {
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}