blob: 4e62d28b68aa64b768130a7c3627496046dc3f58 [file] [log] [blame]
sunishvkf7c56552016-07-18 16:02:39 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sunishvkf7c56552016-07-18 16:02:39 +05303 *
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.onosproject.provider.ospf.cfg.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
sunishvkf7c56552016-07-18 16:02:39 +053024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
26import org.onosproject.net.config.ConfigFactory;
27import org.onosproject.net.config.NetworkConfigEvent;
28import org.onosproject.net.config.NetworkConfigListener;
29import org.onosproject.net.config.NetworkConfigRegistry;
30import org.onosproject.net.config.NetworkConfigService;
31import org.onosproject.net.config.basics.SubjectFactories;
32import org.onosproject.net.provider.AbstractProvider;
33import org.onosproject.net.provider.ProviderId;
34import org.onosproject.ospf.controller.OspfController;
35import org.slf4j.Logger;
36
37import static org.slf4j.LoggerFactory.getLogger;
38
39/**
40 * Provider which advertises device descriptions to the core.
41 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042@Component(immediate = true, service = OspfCfgProvider.class)
sunishvkf7c56552016-07-18 16:02:39 +053043public 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 Milkeyd84f89b2018-08-17 14:54:17 -070055 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053056 protected OspfController ospfController;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070057 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053058 protected CoreService coreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070059 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053060 protected NetworkConfigRegistry configRegistry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070061 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053062 protected NetworkConfigService configService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 @Reference(cardinality = ReferenceCardinality.MANDATORY)
sunishvkf7c56552016-07-18 16:02:39 +053064 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}