Hyunsun Moon | 9661d64 | 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 | 9661d64 | 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 | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 33 | import java.util.concurrent.ExecutorService; |
| 34 | |
| 35 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
| 36 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Reads node information from the network config file and handles the config |
| 41 | * update events. |
| 42 | * Only a leader controller performs the node addition or deletion. |
| 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | public class CordVtnConfigManager { |
| 46 | |
| 47 | protected final Logger log = getLogger(getClass()); |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected CoreService coreService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected NetworkConfigRegistry configRegistry; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected NetworkConfigService configService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 59 | protected CordVtnService cordVtnService; |
| 60 | |
| 61 | private final ConfigFactory configFactory = |
| 62 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, CordVtnConfig.class, "cordvtn") { |
| 63 | @Override |
| 64 | public CordVtnConfig createConfig() { |
| 65 | return new CordVtnConfig(); |
| 66 | } |
| 67 | }; |
| 68 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 69 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 70 | private ApplicationId appId; |
Hyunsun Moon | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 71 | protected ExecutorService eventExecutor; |
| 72 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 73 | |
| 74 | @Activate |
| 75 | protected void active() { |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 76 | appId = coreService.getAppId(CordVtnService.CORDVTN_APP_ID); |
| 77 | |
Hyunsun Moon | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 78 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtncfg", "event-handler")); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 79 | configService.addListener(configListener); |
| 80 | configRegistry.registerConfigFactory(configFactory); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | protected void deactivate() { |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 85 | configRegistry.unregisterConfigFactory(configFactory); |
| 86 | configService.removeListener(configListener); |
Hyunsun Moon | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 87 | eventExecutor.shutdown(); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private void readConfiguration() { |
| 91 | CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class); |
| 92 | |
| 93 | if (config == null) { |
| 94 | log.warn("No configuration found"); |
| 95 | return; |
| 96 | } |
| 97 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 98 | config.cordVtnNodes().forEach(node -> { |
| 99 | CordVtnNode cordVtnNode = new CordVtnNode( |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 100 | node.hostname(), |
| 101 | node.ovsdbIp(), |
| 102 | node.ovsdbPort(), |
| 103 | node.bridgeId(), |
| 104 | node.phyPortName(), |
| 105 | node.localIp()); |
| 106 | |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 107 | cordVtnService.addNode(cordVtnNode); |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 108 | }); |
| 109 | } |
| 110 | |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 111 | private class InternalConfigListener implements NetworkConfigListener { |
| 112 | |
| 113 | @Override |
| 114 | public void event(NetworkConfigEvent event) { |
Hyunsun Moon | 61c9838 | 2015-10-27 15:40:22 -0700 | [diff] [blame] | 115 | if (!event.configClass().equals(CordVtnConfig.class)) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | switch (event.type()) { |
| 120 | case CONFIG_ADDED: |
| 121 | log.info("Network configuration added"); |
Hyunsun Moon | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 122 | eventExecutor.execute(CordVtnConfigManager.this::readConfiguration); |
Hyunsun Moon | 61c9838 | 2015-10-27 15:40:22 -0700 | [diff] [blame] | 123 | break; |
| 124 | case CONFIG_UPDATED: |
| 125 | log.info("Network configuration updated"); |
Hyunsun Moon | ca46456 | 2015-12-16 14:29:36 -0800 | [diff] [blame] | 126 | eventExecutor.execute(CordVtnConfigManager.this::readConfiguration); |
Hyunsun Moon | 61c9838 | 2015-10-27 15:40:22 -0700 | [diff] [blame] | 127 | break; |
| 128 | default: |
| 129 | break; |
| 130 | } |
Hyunsun Moon | 9661d64 | 2015-09-23 13:24:35 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | } |