Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 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 | */ |
| 16 | package org.onosproject.cordvtn; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.config.ConfigFactory; |
| 26 | import org.onosproject.net.config.NetworkConfigEvent; |
| 27 | import org.onosproject.net.config.NetworkConfigListener; |
| 28 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 29 | import org.onosproject.net.config.NetworkConfigService; |
| 30 | import org.onosproject.net.config.basics.SubjectFactories; |
| 31 | import org.slf4j.Logger; |
| 32 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | |
| 35 | /** |
| 36 | * Reads node information from the network config file and handles the config |
| 37 | * update events. |
| 38 | * Only a leader controller performs the node addition or deletion. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class CordVtnConfigManager { |
| 42 | |
| 43 | protected final Logger log = getLogger(getClass()); |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected CoreService coreService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected NetworkConfigRegistry configRegistry; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected NetworkConfigService configService; |
| 53 | |
| 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 55 | protected CordVtnService cordVtnService; |
| 56 | |
| 57 | private final ConfigFactory configFactory = |
| 58 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") { |
| 59 | @Override |
| 60 | public CordVtnConfig createConfig() { |
| 61 | return new CordVtnConfig(); |
| 62 | } |
| 63 | }; |
| 64 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 65 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
| 66 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 67 | private ApplicationId appId; |
| 68 | |
| 69 | @Activate |
| 70 | protected void active() { |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 71 | appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID); |
| 72 | |
| 73 | configService.addListener(configListener); |
| 74 | configRegistry.registerConfigFactory(configFactory); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Deactivate |
| 78 | protected void deactivate() { |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 79 | configRegistry.unregisterConfigFactory(configFactory); |
| 80 | configService.removeListener(configListener); |
| 81 | } |
| 82 | |
| 83 | private void readConfiguration() { |
| 84 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 85 | |
| 86 | if (config == null) { |
| 87 | log.warn("No configuration found"); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | config.ovsdbNodes().forEach(node -> { |
Hyunsun Moon | 1f14555 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 92 | DefaultOvsdbNode ovsdb = new DefaultOvsdbNode( |
| 93 | node.host(), node.ip(), node.port(), node.bridgeId()); |
| 94 | cordVtnService.addNode(ovsdb); |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 95 | }); |
| 96 | } |
| 97 | |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 98 | private class InternalConfigListener implements NetworkConfigListener { |
| 99 | |
| 100 | @Override |
| 101 | public void event(NetworkConfigEvent event) { |
Hyunsun Moon | fae11ee | 2015-10-27 15:40:22 -0700 | [diff] [blame] | 102 | if (!event.configClass().equals(CordVtnConfig.class)) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | switch (event.type()) { |
| 107 | case CONFIG_ADDED: |
| 108 | log.info("Network configuration added"); |
| 109 | readConfiguration(); |
| 110 | break; |
| 111 | case CONFIG_UPDATED: |
| 112 | log.info("Network configuration updated"); |
Hyunsun Moon | d772f34 | 2015-10-28 20:28:16 -0700 | [diff] [blame] | 113 | readConfiguration(); |
Hyunsun Moon | fae11ee | 2015-10-27 15:40:22 -0700 | [diff] [blame] | 114 | break; |
| 115 | default: |
| 116 | break; |
| 117 | } |
Hyunsun Moon | 2b53032 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |