blob: f25d00d0b7e07ac1b60946fd005128c2731847c9 [file] [log] [blame]
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +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 */
16
17package org.onosproject.provider.isis.cfg.impl;
18
19import com.fasterxml.jackson.databind.JsonNode;
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053020import org.onosproject.core.ApplicationId;
21import org.onosproject.core.CoreService;
22import org.onosproject.isis.controller.IsisController;
23import org.onosproject.net.config.ConfigFactory;
24import org.onosproject.net.config.NetworkConfigEvent;
25import org.onosproject.net.config.NetworkConfigListener;
26import org.onosproject.net.config.NetworkConfigRegistry;
27import org.onosproject.net.config.NetworkConfigService;
28import org.onosproject.net.config.basics.SubjectFactories;
29import org.onosproject.net.provider.AbstractProvider;
30import org.onosproject.net.provider.ProviderId;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053036import org.slf4j.Logger;
37
38import static org.slf4j.LoggerFactory.getLogger;
39
40/**
41 * ISIS config provider to validate and populate the configuration.
42 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070043@Component(immediate = true, service = IsisCfgProvider.class)
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053044public class IsisCfgProvider extends AbstractProvider {
45
46 private static final String PROVIDER_ID = "org.onosproject.provider.isis.cfg";
47 private static final Logger log = getLogger(IsisCfgProvider.class);
48 private final ConfigFactory configFactory =
49 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, IsisAppConfig.class, "isisapp") {
50 @Override
51 public IsisAppConfig createConfig() {
52 return new IsisAppConfig();
53 }
54 };
55 private final NetworkConfigListener configListener = new InternalConfigListener();
Ray Milkeyd84f89b2018-08-17 14:54:17 -070056 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053057 protected IsisController isisController;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053059 protected CoreService coreService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053061 protected NetworkConfigRegistry configRegistry;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070062 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Kalyankumar Asangi6dd598c2016-04-27 18:56:21 +053063 protected NetworkConfigService configService;
64 private ApplicationId appId;
65
66 /**
67 * Creates an ISIS config provider.
68 */
69 public IsisCfgProvider() {
70 super(new ProviderId("isis", PROVIDER_ID));
71 }
72
73 @Activate
74 public void activate() {
75 log.debug("Activate...!!!");
76 appId = coreService.registerApplication(PROVIDER_ID);
77 configService.addListener(configListener);
78 configRegistry.registerConfigFactory(configFactory);
79 log.debug("ISIS cfg service got started");
80 }
81
82 @Deactivate
83 public void deactivate() {
84 log.debug("Deactivate...!!!");
85 configRegistry.unregisterConfigFactory(configFactory);
86 configService.removeListener(configListener);
87 }
88
89 /**
90 * Pushes the configuration changes to ISIS controller.
91 */
92 private void updateConfig() {
93 IsisAppConfig isisAppConfig = configRegistry.getConfig(appId, IsisAppConfig.class);
94 log.debug("IsisAppConfig::config processes::" + isisAppConfig.processes());
95 log.debug("IsisAppConfig::prop 1::" + isisAppConfig.method());
96 if ("ADD".equalsIgnoreCase(isisAppConfig.method())) {
97 JsonNode jsonNode = isisAppConfig.processes();
98 isisController.updateConfig(jsonNode);
99 }
100 }
101
102 /**
103 * Isis config listener to populate the configuration.
104 */
105 private class InternalConfigListener implements NetworkConfigListener {
106
107 @Override
108 public void event(NetworkConfigEvent event) {
109 log.debug("config event is getting called...!!!");
110 if (!event.configClass().equals(IsisAppConfig.class)) {
111 return;
112 }
113
114 switch (event.type()) {
115 case CONFIG_ADDED:
116 updateConfig();
117 break;
118 case CONFIG_UPDATED:
119 updateConfig();
120 break;
121 case CONFIG_REMOVED:
122 break;
123 default:
124 break;
125 }
126 }
127 }
128}