sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 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.provider.ospf.cfg.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.osgi.service.component.annotations.Activate; |
| 20 | import org.osgi.service.component.annotations.Component; |
| 21 | import org.osgi.service.component.annotations.Deactivate; |
| 22 | import org.osgi.service.component.annotations.Reference; |
| 23 | import org.osgi.service.component.annotations.ReferenceCardinality; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.config.ConfigFactory; |
| 27 | import org.onosproject.net.config.NetworkConfigEvent; |
| 28 | import org.onosproject.net.config.NetworkConfigListener; |
| 29 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.config.basics.SubjectFactories; |
| 32 | import org.onosproject.net.provider.AbstractProvider; |
| 33 | import org.onosproject.net.provider.ProviderId; |
| 34 | import org.onosproject.ospf.controller.OspfController; |
| 35 | import org.slf4j.Logger; |
| 36 | |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Provider which advertises device descriptions to the core. |
| 41 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Component(immediate = true, service = OspfCfgProvider.class) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 43 | public class OspfCfgProvider extends AbstractProvider { |
| 44 | |
| 45 | static final String PROVIDER_ID = "org.onosproject.provider.ospf.cfg"; |
| 46 | private static final Logger log = getLogger(OspfCfgProvider.class); |
| 47 | private final ConfigFactory configFactory = |
| 48 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, OspfAppConfig.class, "ospfapp") { |
| 49 | @Override |
| 50 | public OspfAppConfig createConfig() { |
| 51 | return new OspfAppConfig(); |
| 52 | } |
| 53 | }; |
| 54 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 56 | protected OspfController ospfController; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 58 | protected CoreService coreService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 60 | protected NetworkConfigRegistry configRegistry; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 62 | protected NetworkConfigService configService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame] | 64 | protected OspfController controller; |
| 65 | private ApplicationId appId; |
| 66 | |
| 67 | /** |
| 68 | * Creates an OSPF device provider. |
| 69 | */ |
| 70 | public OspfCfgProvider() { |
| 71 | super(new ProviderId("ospf", PROVIDER_ID)); |
| 72 | } |
| 73 | |
| 74 | public void setOspfController(OspfController ospfController) { |
| 75 | this.ospfController = ospfController; |
| 76 | } |
| 77 | |
| 78 | @Activate |
| 79 | public void activate() { |
| 80 | appId = coreService.registerApplication(PROVIDER_ID); |
| 81 | configService.addListener(configListener); |
| 82 | configRegistry.registerConfigFactory(configFactory); |
| 83 | log.info("activated...!!!"); |
| 84 | } |
| 85 | |
| 86 | @Deactivate |
| 87 | public void deactivate() { |
| 88 | configRegistry.unregisterConfigFactory(configFactory); |
| 89 | configService.removeListener(configListener); |
| 90 | log.info("deactivated...!!!"); |
| 91 | } |
| 92 | |
| 93 | private void updateConfig() { |
| 94 | OspfAppConfig ospfAppConfig = configRegistry.getConfig(appId, OspfAppConfig.class); |
| 95 | if ("ADD".equalsIgnoreCase(ospfAppConfig.method())) { |
| 96 | JsonNode jsonNode = ospfAppConfig.processes(); |
| 97 | ospfController.updateConfig(jsonNode); |
| 98 | } else { |
| 99 | log.debug("Please signify prop1 and prop2"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * OSPF config listener to populate the configuration. |
| 105 | */ |
| 106 | private class InternalConfigListener implements NetworkConfigListener { |
| 107 | @Override |
| 108 | public void event(NetworkConfigEvent event) { |
| 109 | log.debug("InternalConfigListener:: event is getting called"); |
| 110 | if (!event.configClass().equals(OspfAppConfig.class)) { |
| 111 | return; |
| 112 | } |
| 113 | switch (event.type()) { |
| 114 | case CONFIG_ADDED: |
| 115 | updateConfig(); |
| 116 | break; |
| 117 | case CONFIG_UPDATED: |
| 118 | updateConfig(); |
| 119 | break; |
| 120 | case CONFIG_REMOVED: |
| 121 | break; |
| 122 | default: |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |