blob: ba0554b6205373d5b7bbb42374a654659b228b95 [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;
Vidyashree Rama76faccc2016-10-17 22:06:52 +053028import org.onosproject.yms.app.yab.YangApplicationBroker;
sonu gupta1bb37b82016-11-11 16:51:18 +053029import org.onosproject.yms.app.ydt.DefaultYdtWalker;
30import org.onosproject.yms.app.ydt.YangRequestWorkBench;
Bharat saraswalf53b29a2016-09-27 15:35:15 +053031import org.onosproject.yms.app.ynh.YangNotificationExtendedService;
32import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry;
33import org.onosproject.yms.app.ysr.YangSchemaRegistry;
34import org.onosproject.yms.ych.YangCodecHandler;
35import org.onosproject.yms.ych.YangDataTreeCodec;
36import org.onosproject.yms.ych.YangProtocolEncodingFormat;
37import org.onosproject.yms.ydt.YdtBuilder;
38import org.onosproject.yms.ydt.YdtResponse;
39import org.onosproject.yms.ydt.YdtWalker;
40import org.onosproject.yms.ydt.YmsOperationType;
41import org.onosproject.yms.ymsm.YmsService;
42import org.onosproject.yms.ynh.YangNotificationService;
43import org.onosproject.yms.ysr.YangModuleIdentifier;
44import org.onosproject.yms.ysr.YangModuleLibrary;
45import org.slf4j.Logger;
46import org.slf4j.LoggerFactory;
47
48import java.util.List;
49import java.util.concurrent.ExecutorService;
50import java.util.concurrent.Executors;
51
52import static org.onlab.util.Tools.groupedThreads;
53
54/**
55 * Represents implementation of YANG management system manager.
56 */
57@Service
58@Component(immediate = true)
59public class YmsManager
60 implements YmsService {
61
62 private final Logger log = LoggerFactory.getLogger(getClass());
63
64 private static final String APP_ID = "org.onosproject.app.yms";
65 private static final String MODULE_ID = "module-id";
66 private ApplicationId appId;
67 private YangSchemaRegistry schemaRegistry;
68 //module id generator should be used to generate a new module id for
69 //each YSR instance. So YCH also should generate it.
70 private IdGenerator moduleIdGenerator;
71 private ExecutorService schemaRegistryExecutor;
72 private YangNotificationExtendedService ynhExtendedService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected CoreService coreService;
76
77 @Activate
78 public void activate() {
79 appId = coreService.registerApplication(APP_ID);
80 moduleIdGenerator = coreService.getIdGenerator(MODULE_ID);
81 schemaRegistry = new DefaultYangSchemaRegistry(String.valueOf(
82 moduleIdGenerator.getNewId()));
83 schemaRegistryExecutor =
84 Executors.newSingleThreadExecutor(groupedThreads(
85 "onos/apps/yang-management-system/schema-registry",
86 "schema-registry-handler", log));
87 log.info("Started");
88 }
89
90 @Deactivate
91 public void deactivate() {
92 ((DefaultYangSchemaRegistry) schemaRegistry).flushYsrData();
93 schemaRegistryExecutor.shutdown();
94
95 // TODO implementation for other components.
96 log.info("Stopped");
97 }
98
99 @Override
100 public YdtBuilder getYdtBuilder(String logicalRootName,
101 String rootNamespace,
102 YmsOperationType operationType) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530103 return new YangRequestWorkBench(logicalRootName, rootNamespace,
104 operationType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530105 }
106
107 @Override
108 public YdtBuilder getYdtBuilder(String logicalRootName,
109 String rootNamespace,
110 YmsOperationType operationType,
111 Object schemaRegistryForYdt) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530112 if (schemaRegistryForYdt != null) {
113 return new YangRequestWorkBench(logicalRootName, rootNamespace,
114 operationType,
115 (YangSchemaRegistry)
116 schemaRegistryForYdt,
117 false);
118 }
119 return new YangRequestWorkBench(logicalRootName, rootNamespace,
120 operationType, schemaRegistry, true);
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530121 }
122
123 @Override
124 public YdtWalker getYdtWalker() {
sonu gupta1bb37b82016-11-11 16:51:18 +0530125 return new DefaultYdtWalker();
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530126 }
127
128 @Override
129 public YdtResponse executeOperation(YdtBuilder operationRequest) {
Vidyashree Rama76faccc2016-10-17 22:06:52 +0530130 YangApplicationBroker requestBroker =
131 new YangApplicationBroker(schemaRegistry);
132 switch (operationRequest.getYmsOperationType()) {
133 case EDIT_CONFIG_REQUEST:
134 try {
135 return requestBroker.processEdit(operationRequest);
136 } catch (CloneNotSupportedException e) {
137 log.error("YAB: failed to process edit request.");
138 }
139 case QUERY_CONFIG_REQUEST:
140 // TODO : to be implemented
141 case QUERY_REQUEST:
142 return requestBroker.processQuery(operationRequest);
143 case RPC_REQUEST:
144 return requestBroker.processOperation(operationRequest);
145 default:
146 // TODO : throw exception
147 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530148 return null;
149 }
150
151 @Override
152 public YangNotificationService getYangNotificationService() {
153 return ynhExtendedService;
154 }
155
156 /**
157 * Returns YANG notification extended service.
158 *
159 * @return YANG notification extended service
160 */
161 private YangNotificationExtendedService getYnhExtendedService() {
162 return ynhExtendedService;
163 }
164
165 @Override
166 public YangModuleLibrary getYangModuleLibrary() {
167 return ((DefaultYangSchemaRegistry) schemaRegistry).getLibrary();
168 }
169
170 @Override
171 public String getYangFile(YangModuleIdentifier moduleIdentifier) {
172 return ((DefaultYangSchemaRegistry) schemaRegistry)
173 .getYangFile(moduleIdentifier);
174 }
175
176 @Override
177 public void registerDefaultCodec(YangDataTreeCodec defaultCodec,
178 YangProtocolEncodingFormat dataFormat) {
179
180 }
181
182 @Override
183 public void registerService(Object yangManager, Class<?> yangService,
184 List<String> supportedFeatureList) {
185
186 //perform registration of service
187 schemaRegistryExecutor.execute(() -> schemaRegistry
188 .registerApplication(yangManager, yangService,
189 getYnhExtendedService()));
190 }
191
192 @Override
193 public void unRegisterService(Object appManager, Class<?> yangService) {
194 schemaRegistry.unRegisterApplication(appManager, yangService);
195 }
196
197 @Override
198 public YangCodecHandler getYangCodecHandler() {
199 return null;
200 }
201
202 /**
203 * Returns schema registry.
204 *
205 * @return schema registry
206 */
207 public YangSchemaRegistry getSchemaRegistry() {
208 return schemaRegistry;
209 }
Bharat saraswalf53b29a2016-09-27 15:35:15 +0530210}