blob: b3e37c1023cfd68d678540cd16bbccdce59727e0 [file] [log] [blame]
Bharat saraswalf53b29a2016-09-27 15:35:15 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Bharat saraswalf53b29a2016-09-27 15:35:15 +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.yms.app.ymsm;
18
Bharat saraswalf53b29a2016-09-27 15:35:15 +053019import org.onosproject.core.ApplicationId;
20import org.onosproject.core.CoreService;
21import org.onosproject.core.IdGenerator;
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053022import org.onosproject.event.ListenerService;
Vidyashree Rama76faccc2016-10-17 22:06:52 +053023import org.onosproject.yms.app.yab.YangApplicationBroker;
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053024import org.onosproject.yms.app.ych.DefaultYangCodecHandler;
25import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry;
sonu gupta1bb37b82016-11-11 16:51:18 +053026import org.onosproject.yms.app.ydt.DefaultYdtWalker;
27import org.onosproject.yms.app.ydt.YangRequestWorkBench;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053028import org.onosproject.yms.app.ynh.YangNotificationExtendedService;
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053029import org.onosproject.yms.app.ynh.YangNotificationManager;
sonu guptaeff184b2016-11-24 12:43:49 +053030import org.onosproject.yms.app.ysr.DefaultYangModuleLibrary;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053031import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry;
32import org.onosproject.yms.app.ysr.YangSchemaRegistry;
33import org.onosproject.yms.ych.YangCodecHandler;
34import org.onosproject.yms.ych.YangDataTreeCodec;
35import org.onosproject.yms.ych.YangProtocolEncodingFormat;
36import org.onosproject.yms.ydt.YdtBuilder;
37import org.onosproject.yms.ydt.YdtResponse;
38import org.onosproject.yms.ydt.YdtWalker;
39import org.onosproject.yms.ydt.YmsOperationType;
40import org.onosproject.yms.ymsm.YmsService;
41import org.onosproject.yms.ynh.YangNotificationService;
42import org.onosproject.yms.ysr.YangModuleIdentifier;
43import org.onosproject.yms.ysr.YangModuleLibrary;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070044import org.osgi.service.component.annotations.Activate;
45import org.osgi.service.component.annotations.Component;
46import org.osgi.service.component.annotations.Deactivate;
47import org.osgi.service.component.annotations.Reference;
48import org.osgi.service.component.annotations.ReferenceCardinality;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053049import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import java.util.List;
53import java.util.concurrent.ExecutorService;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053054
sonu guptaeff184b2016-11-24 12:43:49 +053055import static java.lang.String.valueOf;
56import static java.util.concurrent.Executors.newSingleThreadExecutor;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053057import static org.onlab.util.Tools.groupedThreads;
sonu guptaeff184b2016-11-24 12:43:49 +053058import static org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry.initializeDefaultCodec;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053059
60/**
61 * Represents implementation of YANG management system manager.
62 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063@Component(immediate = true, service = YmsService.class)
Bharat saraswalf53b29a2016-09-27 15:35:15 +053064public class YmsManager
65 implements YmsService {
66
67 private final Logger log = LoggerFactory.getLogger(getClass());
68
69 private static final String APP_ID = "org.onosproject.app.yms";
70 private static final String MODULE_ID = "module-id";
71 private ApplicationId appId;
72 private YangSchemaRegistry schemaRegistry;
73 //module id generator should be used to generate a new module id for
74 //each YSR instance. So YCH also should generate it.
75 private IdGenerator moduleIdGenerator;
sonu guptaeff184b2016-11-24 12:43:49 +053076 private ExecutorService executor;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053077 private YangNotificationExtendedService ynhExtendedService;
sonu guptaeff184b2016-11-24 12:43:49 +053078 private YangModuleLibrary library;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053079
Ray Milkeyd84f89b2018-08-17 14:54:17 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Bharat saraswalf53b29a2016-09-27 15:35:15 +053081 protected CoreService coreService;
82
83 @Activate
84 public void activate() {
85 appId = coreService.registerApplication(APP_ID);
86 moduleIdGenerator = coreService.getIdGenerator(MODULE_ID);
sonu guptaeff184b2016-11-24 12:43:49 +053087 schemaRegistry = new DefaultYangSchemaRegistry();
88 library = new DefaultYangModuleLibrary(getNewModuleId());
89 executor = newSingleThreadExecutor(groupedThreads(
90 "onos/apps/yang-management-system/schema-registry",
91 "schema-registry-handler", log));
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053092 ynhExtendedService = new YangNotificationManager(schemaRegistry);
sonu guptaeff184b2016-11-24 12:43:49 +053093 //Initialize the default codec
94 initializeDefaultCodec();
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053095
Bharat saraswalf53b29a2016-09-27 15:35:15 +053096 log.info("Started");
97 }
98
99 @Deactivate
100 public void deactivate() {
sonu guptaeff184b2016-11-24 12:43:49 +0530101 schemaRegistry.flushYsrData();
102 executor.shutdown();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530103
104 // TODO implementation for other components.
105 log.info("Stopped");
106 }
107
108 @Override
109 public YdtBuilder getYdtBuilder(String logicalRootName,
110 String rootNamespace,
sonu guptaeff184b2016-11-24 12:43:49 +0530111 YmsOperationType opType) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530112 return new YangRequestWorkBench(logicalRootName, rootNamespace,
sonu guptaeff184b2016-11-24 12:43:49 +0530113 opType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530114 }
115
116 @Override
117 public YdtBuilder getYdtBuilder(String logicalRootName,
118 String rootNamespace,
sonu guptaeff184b2016-11-24 12:43:49 +0530119 YmsOperationType opType,
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530120 Object schemaRegistryForYdt) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530121 if (schemaRegistryForYdt != null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530122 return new YangRequestWorkBench(
123 logicalRootName, rootNamespace, opType,
124 (YangSchemaRegistry) schemaRegistryForYdt, false);
sonu gupta1bb37b82016-11-11 16:51:18 +0530125 }
126 return new YangRequestWorkBench(logicalRootName, rootNamespace,
sonu guptaeff184b2016-11-24 12:43:49 +0530127 opType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530128 }
129
130 @Override
131 public YdtWalker getYdtWalker() {
sonu gupta1bb37b82016-11-11 16:51:18 +0530132 return new DefaultYdtWalker();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530133 }
134
135 @Override
136 public YdtResponse executeOperation(YdtBuilder operationRequest) {
Vidyashree Rama76faccc2016-10-17 22:06:52 +0530137 YangApplicationBroker requestBroker =
138 new YangApplicationBroker(schemaRegistry);
139 switch (operationRequest.getYmsOperationType()) {
140 case EDIT_CONFIG_REQUEST:
141 try {
142 return requestBroker.processEdit(operationRequest);
143 } catch (CloneNotSupportedException e) {
144 log.error("YAB: failed to process edit request.");
145 }
146 case QUERY_CONFIG_REQUEST:
147 // TODO : to be implemented
148 case QUERY_REQUEST:
149 return requestBroker.processQuery(operationRequest);
150 case RPC_REQUEST:
151 return requestBroker.processOperation(operationRequest);
152 default:
153 // TODO : throw exception
154 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530155 return null;
156 }
157
158 @Override
159 public YangNotificationService getYangNotificationService() {
160 return ynhExtendedService;
161 }
162
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530163 @Override
164 public YangModuleLibrary getYangModuleLibrary() {
sonu guptaeff184b2016-11-24 12:43:49 +0530165 //TODO: get for YCH should be handled.
166 return library;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530167 }
168
169 @Override
170 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
sonu guptaeff184b2016-11-24 12:43:49 +0530171 return schemaRegistry.getYangFile(moduleIdentifier);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530172 }
173
174 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530175 public void registerDefaultCodec(YangDataTreeCodec defaultCodec,
176 YangProtocolEncodingFormat dataFormat) {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530177 YangCodecRegistry.registerDefaultCodec(defaultCodec, dataFormat);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530178 }
179
180 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530181 public void registerService(Object manager, Class<?> service,
182 List<String> features) {
sonu guptaeff184b2016-11-24 12:43:49 +0530183 //perform registration of service
184 executor.execute(() -> {
185
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530186 schemaRegistry.registerApplication(manager, service);
sonu guptaeff184b2016-11-24 12:43:49 +0530187 //process notification registration.
188 processNotificationRegistration(manager, service);
189
190 schemaRegistry.processModuleLibrary(service.getName(), library);
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530191 });
192 // TODO implementation based on supported features.
193 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530194
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530195 /**
196 * Process notification registration for manager class object.
197 *
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530198 * @param manager yang manager
sonu guptaeff184b2016-11-24 12:43:49 +0530199 * @param service service class
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530200 */
sonu guptaeff184b2016-11-24 12:43:49 +0530201 private void processNotificationRegistration(Object manager, Class<?> service) {
202 synchronized (service) {
203 if (manager != null && manager instanceof ListenerService) {
204 if (schemaRegistry.verifyNotificationObject(manager, service)) {
205 ynhExtendedService.registerAsListener((ListenerService) manager);
206 }
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530207 }
208 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530209 }
210
211 @Override
212 public void unRegisterService(Object appManager, Class<?> yangService) {
213 schemaRegistry.unRegisterApplication(appManager, yangService);
214 }
215
216 @Override
217 public YangCodecHandler getYangCodecHandler() {
sonu guptaeff184b2016-11-24 12:43:49 +0530218 YangSchemaRegistry registry = new DefaultYangSchemaRegistry();
219 DefaultYangCodecHandler handler = new DefaultYangCodecHandler(registry);
220 handler.setLibrary(new DefaultYangModuleLibrary(getNewModuleId()));
221 return handler;
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530222 }
223
224 /**
225 * Returns schema registry.
226 *
227 * @return schema registry
228 */
229 public YangSchemaRegistry getSchemaRegistry() {
230 return schemaRegistry;
231 }
sonu guptaeff184b2016-11-24 12:43:49 +0530232
233 /**
234 * Returns new generated module id.
235 *
236 * @return new module id
237 */
238 private String getNewModuleId() {
239 return valueOf(moduleIdGenerator.getNewId());
240 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530241}