blob: 475a67e4ef34ed4e00786cd161df4be28175c5d9 [file] [log] [blame]
Bharat saraswalf53b29a2016-09-27 15:35:15 +05301/*
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
17package org.onosproject.yms.app.ymsm;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.apache.felix.scr.annotations.Service;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
27import org.onosproject.core.IdGenerator;
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053028import org.onosproject.event.ListenerService;
Vidyashree Rama76faccc2016-10-17 22:06:52 +053029import org.onosproject.yms.app.yab.YangApplicationBroker;
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053030import org.onosproject.yms.app.ych.DefaultYangCodecHandler;
31import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry;
sonu gupta1bb37b82016-11-11 16:51:18 +053032import org.onosproject.yms.app.ydt.DefaultYdtWalker;
33import org.onosproject.yms.app.ydt.YangRequestWorkBench;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053034import org.onosproject.yms.app.ynh.YangNotificationExtendedService;
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053035import org.onosproject.yms.app.ynh.YangNotificationManager;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053036import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry;
37import org.onosproject.yms.app.ysr.YangSchemaRegistry;
38import org.onosproject.yms.ych.YangCodecHandler;
39import org.onosproject.yms.ych.YangDataTreeCodec;
40import org.onosproject.yms.ych.YangProtocolEncodingFormat;
41import org.onosproject.yms.ydt.YdtBuilder;
42import org.onosproject.yms.ydt.YdtResponse;
43import org.onosproject.yms.ydt.YdtWalker;
44import org.onosproject.yms.ydt.YmsOperationType;
45import org.onosproject.yms.ymsm.YmsService;
46import org.onosproject.yms.ynh.YangNotificationService;
47import org.onosproject.yms.ysr.YangModuleIdentifier;
48import org.onosproject.yms.ysr.YangModuleLibrary;
49import org.slf4j.Logger;
50import org.slf4j.LoggerFactory;
51
52import java.util.List;
53import java.util.concurrent.ExecutorService;
54import java.util.concurrent.Executors;
55
56import static org.onlab.util.Tools.groupedThreads;
57
58/**
59 * Represents implementation of YANG management system manager.
60 */
61@Service
62@Component(immediate = true)
63public class YmsManager
64 implements YmsService {
65
66 private final Logger log = LoggerFactory.getLogger(getClass());
67
68 private static final String APP_ID = "org.onosproject.app.yms";
69 private static final String MODULE_ID = "module-id";
70 private ApplicationId appId;
71 private YangSchemaRegistry schemaRegistry;
72 //module id generator should be used to generate a new module id for
73 //each YSR instance. So YCH also should generate it.
74 private IdGenerator moduleIdGenerator;
75 private ExecutorService schemaRegistryExecutor;
76 private YangNotificationExtendedService ynhExtendedService;
77
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected CoreService coreService;
80
81 @Activate
82 public void activate() {
83 appId = coreService.registerApplication(APP_ID);
84 moduleIdGenerator = coreService.getIdGenerator(MODULE_ID);
85 schemaRegistry = new DefaultYangSchemaRegistry(String.valueOf(
86 moduleIdGenerator.getNewId()));
87 schemaRegistryExecutor =
88 Executors.newSingleThreadExecutor(groupedThreads(
89 "onos/apps/yang-management-system/schema-registry",
90 "schema-registry-handler", log));
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +053091 ynhExtendedService = new YangNotificationManager(schemaRegistry);
Shankara-Huaweid5823ab2016-11-22 10:14:52 +053092 //Initilize the default codecs
93 YangCodecRegistry.initializeDefaultCodec();
94
Bharat saraswalf53b29a2016-09-27 15:35:15 +053095 log.info("Started");
96 }
97
98 @Deactivate
99 public void deactivate() {
100 ((DefaultYangSchemaRegistry) schemaRegistry).flushYsrData();
101 schemaRegistryExecutor.shutdown();
102
103 // TODO implementation for other components.
104 log.info("Stopped");
105 }
106
107 @Override
108 public YdtBuilder getYdtBuilder(String logicalRootName,
109 String rootNamespace,
110 YmsOperationType operationType) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530111 return new YangRequestWorkBench(logicalRootName, rootNamespace,
112 operationType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530113 }
114
115 @Override
116 public YdtBuilder getYdtBuilder(String logicalRootName,
117 String rootNamespace,
118 YmsOperationType operationType,
119 Object schemaRegistryForYdt) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530120 if (schemaRegistryForYdt != null) {
121 return new YangRequestWorkBench(logicalRootName, rootNamespace,
122 operationType,
123 (YangSchemaRegistry)
124 schemaRegistryForYdt,
125 false);
126 }
127 return new YangRequestWorkBench(logicalRootName, rootNamespace,
128 operationType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530129 }
130
131 @Override
132 public YdtWalker getYdtWalker() {
sonu gupta1bb37b82016-11-11 16:51:18 +0530133 return new DefaultYdtWalker();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530134 }
135
136 @Override
137 public YdtResponse executeOperation(YdtBuilder operationRequest) {
Vidyashree Rama76faccc2016-10-17 22:06:52 +0530138 YangApplicationBroker requestBroker =
139 new YangApplicationBroker(schemaRegistry);
140 switch (operationRequest.getYmsOperationType()) {
141 case EDIT_CONFIG_REQUEST:
142 try {
143 return requestBroker.processEdit(operationRequest);
144 } catch (CloneNotSupportedException e) {
145 log.error("YAB: failed to process edit request.");
146 }
147 case QUERY_CONFIG_REQUEST:
148 // TODO : to be implemented
149 case QUERY_REQUEST:
150 return requestBroker.processQuery(operationRequest);
151 case RPC_REQUEST:
152 return requestBroker.processOperation(operationRequest);
153 default:
154 // TODO : throw exception
155 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530156 return null;
157 }
158
159 @Override
160 public YangNotificationService getYangNotificationService() {
161 return ynhExtendedService;
162 }
163
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530164 @Override
165 public YangModuleLibrary getYangModuleLibrary() {
166 return ((DefaultYangSchemaRegistry) schemaRegistry).getLibrary();
167 }
168
169 @Override
170 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
171 return ((DefaultYangSchemaRegistry) schemaRegistry)
172 .getYangFile(moduleIdentifier);
173 }
174
175 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530176 public void registerDefaultCodec(YangDataTreeCodec defaultCodec,
177 YangProtocolEncodingFormat dataFormat) {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530178 YangCodecRegistry.registerDefaultCodec(defaultCodec, dataFormat);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530179 }
180
181 @Override
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530182 public void registerService(Object manager, Class<?> service,
183 List<String> features) {
184 schemaRegistryExecutor.execute(() -> {
185 schemaRegistry.registerApplication(manager, service);
186 processNotificationRegistration(service, manager);
187 });
188 // TODO implementation based on supported features.
189 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530190
Gaurav Agrawalfcc6c192016-09-20 14:29:15 +0530191 /**
192 * Process notification registration for manager class object.
193 *
194 * @param service yang service
195 * @param manager yang manager
196 */
197 private void processNotificationRegistration(Class<?> service,
198 Object manager) {
199 if (manager != null && manager instanceof ListenerService) {
200 if (((DefaultYangSchemaRegistry) schemaRegistry)
201 .verifyNotificationObject(service)) {
202 ynhExtendedService.registerAsListener((ListenerService) manager);
203 }
204 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530205 }
206
207 @Override
208 public void unRegisterService(Object appManager, Class<?> yangService) {
209 schemaRegistry.unRegisterApplication(appManager, yangService);
210 }
211
212 @Override
213 public YangCodecHandler getYangCodecHandler() {
Shankara-Huaweid5823ab2016-11-22 10:14:52 +0530214
215 YangSchemaRegistry yangSchemaRegistry =
216 new DefaultYangSchemaRegistry(
217 String.valueOf(moduleIdGenerator.getNewId()));
218 return new DefaultYangCodecHandler(yangSchemaRegistry);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530219 }
220
221 /**
222 * Returns schema registry.
223 *
224 * @return schema registry
225 */
226 public YangSchemaRegistry getSchemaRegistry() {
227 return schemaRegistry;
228 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530229}