Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.yms.app.ymsm; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdGenerator; |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 28 | import org.onosproject.event.ListenerService; |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 29 | import org.onosproject.yms.app.yab.YangApplicationBroker; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 30 | import org.onosproject.yms.app.ych.DefaultYangCodecHandler; |
| 31 | import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 32 | import org.onosproject.yms.app.ydt.DefaultYdtWalker; |
| 33 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 34 | import org.onosproject.yms.app.ynh.YangNotificationExtendedService; |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 35 | import org.onosproject.yms.app.ynh.YangNotificationManager; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 36 | import org.onosproject.yms.app.ysr.DefaultYangModuleLibrary; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 37 | import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry; |
| 38 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 39 | import org.onosproject.yms.ych.YangCodecHandler; |
| 40 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 41 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 42 | import org.onosproject.yms.ydt.YdtBuilder; |
| 43 | import org.onosproject.yms.ydt.YdtResponse; |
| 44 | import org.onosproject.yms.ydt.YdtWalker; |
| 45 | import org.onosproject.yms.ydt.YmsOperationType; |
| 46 | import org.onosproject.yms.ymsm.YmsService; |
| 47 | import org.onosproject.yms.ynh.YangNotificationService; |
| 48 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 49 | import org.onosproject.yms.ysr.YangModuleLibrary; |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import java.util.List; |
| 54 | import java.util.concurrent.ExecutorService; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 55 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 56 | import static java.lang.String.valueOf; |
| 57 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 58 | import static org.onlab.util.Tools.groupedThreads; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 59 | import static org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry.initializeDefaultCodec; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * Represents implementation of YANG management system manager. |
| 63 | */ |
| 64 | @Service |
| 65 | @Component(immediate = true) |
| 66 | public class YmsManager |
| 67 | implements YmsService { |
| 68 | |
| 69 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 70 | |
| 71 | private static final String APP_ID = "org.onosproject.app.yms"; |
| 72 | private static final String MODULE_ID = "module-id"; |
| 73 | private ApplicationId appId; |
| 74 | private YangSchemaRegistry schemaRegistry; |
| 75 | //module id generator should be used to generate a new module id for |
| 76 | //each YSR instance. So YCH also should generate it. |
| 77 | private IdGenerator moduleIdGenerator; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 78 | private ExecutorService executor; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 79 | private YangNotificationExtendedService ynhExtendedService; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 80 | private YangModuleLibrary library; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected CoreService coreService; |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | appId = coreService.registerApplication(APP_ID); |
| 88 | moduleIdGenerator = coreService.getIdGenerator(MODULE_ID); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 89 | schemaRegistry = new DefaultYangSchemaRegistry(); |
| 90 | library = new DefaultYangModuleLibrary(getNewModuleId()); |
| 91 | executor = newSingleThreadExecutor(groupedThreads( |
| 92 | "onos/apps/yang-management-system/schema-registry", |
| 93 | "schema-registry-handler", log)); |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 94 | ynhExtendedService = new YangNotificationManager(schemaRegistry); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 95 | //Initialize the default codec |
| 96 | initializeDefaultCodec(); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 97 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 98 | log.info("Started"); |
| 99 | } |
| 100 | |
| 101 | @Deactivate |
| 102 | public void deactivate() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 103 | schemaRegistry.flushYsrData(); |
| 104 | executor.shutdown(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 105 | |
| 106 | // TODO implementation for other components. |
| 107 | log.info("Stopped"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 112 | String rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 113 | YmsOperationType opType) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 114 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 115 | opType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 120 | String rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 121 | YmsOperationType opType, |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 122 | Object schemaRegistryForYdt) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 123 | if (schemaRegistryForYdt != null) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 124 | return new YangRequestWorkBench( |
| 125 | logicalRootName, rootNamespace, opType, |
| 126 | (YangSchemaRegistry) schemaRegistryForYdt, false); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 127 | } |
| 128 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 129 | opType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public YdtWalker getYdtWalker() { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 134 | return new DefaultYdtWalker(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public YdtResponse executeOperation(YdtBuilder operationRequest) { |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 139 | YangApplicationBroker requestBroker = |
| 140 | new YangApplicationBroker(schemaRegistry); |
| 141 | switch (operationRequest.getYmsOperationType()) { |
| 142 | case EDIT_CONFIG_REQUEST: |
| 143 | try { |
| 144 | return requestBroker.processEdit(operationRequest); |
| 145 | } catch (CloneNotSupportedException e) { |
| 146 | log.error("YAB: failed to process edit request."); |
| 147 | } |
| 148 | case QUERY_CONFIG_REQUEST: |
| 149 | // TODO : to be implemented |
| 150 | case QUERY_REQUEST: |
| 151 | return requestBroker.processQuery(operationRequest); |
| 152 | case RPC_REQUEST: |
| 153 | return requestBroker.processOperation(operationRequest); |
| 154 | default: |
| 155 | // TODO : throw exception |
| 156 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 157 | return null; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public YangNotificationService getYangNotificationService() { |
| 162 | return ynhExtendedService; |
| 163 | } |
| 164 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 165 | @Override |
| 166 | public YangModuleLibrary getYangModuleLibrary() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 167 | //TODO: get for YCH should be handled. |
| 168 | return library; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public String getYangFile(YangModuleIdentifier moduleIdentifier) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 173 | return schemaRegistry.getYangFile(moduleIdentifier); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 177 | public void registerDefaultCodec(YangDataTreeCodec defaultCodec, |
| 178 | YangProtocolEncodingFormat dataFormat) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 179 | YangCodecRegistry.registerDefaultCodec(defaultCodec, dataFormat); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 183 | public void registerService(Object manager, Class<?> service, |
| 184 | List<String> features) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 185 | //perform registration of service |
| 186 | executor.execute(() -> { |
| 187 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 188 | schemaRegistry.registerApplication(manager, service); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 189 | //process notification registration. |
| 190 | processNotificationRegistration(manager, service); |
| 191 | |
| 192 | schemaRegistry.processModuleLibrary(service.getName(), library); |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 193 | }); |
| 194 | // TODO implementation based on supported features. |
| 195 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 196 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 197 | /** |
| 198 | * Process notification registration for manager class object. |
| 199 | * |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 200 | * @param manager yang manager |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 201 | * @param service service class |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 202 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 203 | private void processNotificationRegistration(Object manager, Class<?> service) { |
| 204 | synchronized (service) { |
| 205 | if (manager != null && manager instanceof ListenerService) { |
| 206 | if (schemaRegistry.verifyNotificationObject(manager, service)) { |
| 207 | ynhExtendedService.registerAsListener((ListenerService) manager); |
| 208 | } |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 209 | } |
| 210 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void unRegisterService(Object appManager, Class<?> yangService) { |
| 215 | schemaRegistry.unRegisterApplication(appManager, yangService); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public YangCodecHandler getYangCodecHandler() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 220 | YangSchemaRegistry registry = new DefaultYangSchemaRegistry(); |
| 221 | DefaultYangCodecHandler handler = new DefaultYangCodecHandler(registry); |
| 222 | handler.setLibrary(new DefaultYangModuleLibrary(getNewModuleId())); |
| 223 | return handler; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns schema registry. |
| 228 | * |
| 229 | * @return schema registry |
| 230 | */ |
| 231 | public YangSchemaRegistry getSchemaRegistry() { |
| 232 | return schemaRegistry; |
| 233 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Returns new generated module id. |
| 237 | * |
| 238 | * @return new module id |
| 239 | */ |
| 240 | private String getNewModuleId() { |
| 241 | return valueOf(moduleIdGenerator.getNewId()); |
| 242 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 243 | } |