Kalyankumar Asangi | 6dd598c | 2016-04-27 18:56:21 +0530 | [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 | */ |
| 16 | |
| 17 | package org.onosproject.provider.isis.cfg.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.isis.controller.IsisController; |
| 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.NetworkConfigRegistry; |
| 33 | import org.onosproject.net.config.NetworkConfigService; |
| 34 | import org.onosproject.net.config.basics.SubjectFactories; |
| 35 | import org.onosproject.net.provider.AbstractProvider; |
| 36 | import org.onosproject.net.provider.ProviderId; |
| 37 | import org.slf4j.Logger; |
| 38 | |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * ISIS config provider to validate and populate the configuration. |
| 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | @Service |
| 46 | public class IsisCfgProvider extends AbstractProvider { |
| 47 | |
| 48 | private static final String PROVIDER_ID = "org.onosproject.provider.isis.cfg"; |
| 49 | private static final Logger log = getLogger(IsisCfgProvider.class); |
| 50 | private final ConfigFactory configFactory = |
| 51 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, IsisAppConfig.class, "isisapp") { |
| 52 | @Override |
| 53 | public IsisAppConfig createConfig() { |
| 54 | return new IsisAppConfig(); |
| 55 | } |
| 56 | }; |
| 57 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected IsisController isisController; |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected CoreService coreService; |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected NetworkConfigRegistry configRegistry; |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected NetworkConfigService configService; |
| 66 | private ApplicationId appId; |
| 67 | |
| 68 | /** |
| 69 | * Creates an ISIS config provider. |
| 70 | */ |
| 71 | public IsisCfgProvider() { |
| 72 | super(new ProviderId("isis", PROVIDER_ID)); |
| 73 | } |
| 74 | |
| 75 | @Activate |
| 76 | public void activate() { |
| 77 | log.debug("Activate...!!!"); |
| 78 | appId = coreService.registerApplication(PROVIDER_ID); |
| 79 | configService.addListener(configListener); |
| 80 | configRegistry.registerConfigFactory(configFactory); |
| 81 | log.debug("ISIS cfg service got started"); |
| 82 | } |
| 83 | |
| 84 | @Deactivate |
| 85 | public void deactivate() { |
| 86 | log.debug("Deactivate...!!!"); |
| 87 | configRegistry.unregisterConfigFactory(configFactory); |
| 88 | configService.removeListener(configListener); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Pushes the configuration changes to ISIS controller. |
| 93 | */ |
| 94 | private void updateConfig() { |
| 95 | IsisAppConfig isisAppConfig = configRegistry.getConfig(appId, IsisAppConfig.class); |
| 96 | log.debug("IsisAppConfig::config processes::" + isisAppConfig.processes()); |
| 97 | log.debug("IsisAppConfig::prop 1::" + isisAppConfig.method()); |
| 98 | if ("ADD".equalsIgnoreCase(isisAppConfig.method())) { |
| 99 | JsonNode jsonNode = isisAppConfig.processes(); |
| 100 | isisController.updateConfig(jsonNode); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Isis config listener to populate the configuration. |
| 106 | */ |
| 107 | private class InternalConfigListener implements NetworkConfigListener { |
| 108 | |
| 109 | @Override |
| 110 | public void event(NetworkConfigEvent event) { |
| 111 | log.debug("config event is getting called...!!!"); |
| 112 | if (!event.configClass().equals(IsisAppConfig.class)) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | switch (event.type()) { |
| 117 | case CONFIG_ADDED: |
| 118 | updateConfig(); |
| 119 | break; |
| 120 | case CONFIG_UPDATED: |
| 121 | updateConfig(); |
| 122 | break; |
| 123 | case CONFIG_REMOVED: |
| 124 | break; |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |