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 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 19 | import org.onosproject.core.ApplicationId; |
| 20 | import org.onosproject.core.CoreService; |
| 21 | import org.onosproject.core.IdGenerator; |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 22 | import org.onosproject.event.ListenerService; |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 23 | import org.onosproject.yms.app.yab.YangApplicationBroker; |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 24 | import org.onosproject.yms.app.ych.DefaultYangCodecHandler; |
| 25 | import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 26 | import org.onosproject.yms.app.ydt.DefaultYdtWalker; |
| 27 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 28 | import org.onosproject.yms.app.ynh.YangNotificationExtendedService; |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 29 | import org.onosproject.yms.app.ynh.YangNotificationManager; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 30 | import org.onosproject.yms.app.ysr.DefaultYangModuleLibrary; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 31 | import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry; |
| 32 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 33 | import org.onosproject.yms.ych.YangCodecHandler; |
| 34 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 35 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 36 | import org.onosproject.yms.ydt.YdtBuilder; |
| 37 | import org.onosproject.yms.ydt.YdtResponse; |
| 38 | import org.onosproject.yms.ydt.YdtWalker; |
| 39 | import org.onosproject.yms.ydt.YmsOperationType; |
| 40 | import org.onosproject.yms.ymsm.YmsService; |
| 41 | import org.onosproject.yms.ynh.YangNotificationService; |
| 42 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 43 | import org.onosproject.yms.ysr.YangModuleLibrary; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 44 | import org.osgi.service.component.annotations.Activate; |
| 45 | import org.osgi.service.component.annotations.Component; |
| 46 | import org.osgi.service.component.annotations.Deactivate; |
| 47 | import org.osgi.service.component.annotations.Reference; |
| 48 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | import java.util.List; |
| 53 | import java.util.concurrent.ExecutorService; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 54 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 55 | import static java.lang.String.valueOf; |
| 56 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 57 | import static org.onlab.util.Tools.groupedThreads; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 58 | import static org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry.initializeDefaultCodec; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Represents implementation of YANG management system manager. |
| 62 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 63 | @Component(immediate = true, service = YmsService.class) |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 64 | public 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 76 | private ExecutorService executor; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 77 | private YangNotificationExtendedService ynhExtendedService; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 78 | private YangModuleLibrary library; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 79 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 81 | protected CoreService coreService; |
| 82 | |
| 83 | @Activate |
| 84 | public void activate() { |
| 85 | appId = coreService.registerApplication(APP_ID); |
| 86 | moduleIdGenerator = coreService.getIdGenerator(MODULE_ID); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 87 | 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 Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 92 | ynhExtendedService = new YangNotificationManager(schemaRegistry); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 93 | //Initialize the default codec |
| 94 | initializeDefaultCodec(); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 95 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 96 | log.info("Started"); |
| 97 | } |
| 98 | |
| 99 | @Deactivate |
| 100 | public void deactivate() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 101 | schemaRegistry.flushYsrData(); |
| 102 | executor.shutdown(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 103 | |
| 104 | // TODO implementation for other components. |
| 105 | log.info("Stopped"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 110 | String rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 111 | YmsOperationType opType) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 112 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 113 | opType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 118 | String rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 119 | YmsOperationType opType, |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 120 | Object schemaRegistryForYdt) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 121 | if (schemaRegistryForYdt != null) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 122 | return new YangRequestWorkBench( |
| 123 | logicalRootName, rootNamespace, opType, |
| 124 | (YangSchemaRegistry) schemaRegistryForYdt, false); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 125 | } |
| 126 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 127 | opType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public YdtWalker getYdtWalker() { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 132 | return new DefaultYdtWalker(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public YdtResponse executeOperation(YdtBuilder operationRequest) { |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 137 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 155 | return null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public YangNotificationService getYangNotificationService() { |
| 160 | return ynhExtendedService; |
| 161 | } |
| 162 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 163 | @Override |
| 164 | public YangModuleLibrary getYangModuleLibrary() { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 165 | //TODO: get for YCH should be handled. |
| 166 | return library; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public String getYangFile(YangModuleIdentifier moduleIdentifier) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 171 | return schemaRegistry.getYangFile(moduleIdentifier); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 175 | public void registerDefaultCodec(YangDataTreeCodec defaultCodec, |
| 176 | YangProtocolEncodingFormat dataFormat) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 177 | YangCodecRegistry.registerDefaultCodec(defaultCodec, dataFormat); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 181 | public void registerService(Object manager, Class<?> service, |
| 182 | List<String> features) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 183 | //perform registration of service |
| 184 | executor.execute(() -> { |
| 185 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 186 | schemaRegistry.registerApplication(manager, service); |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 187 | //process notification registration. |
| 188 | processNotificationRegistration(manager, service); |
| 189 | |
| 190 | schemaRegistry.processModuleLibrary(service.getName(), library); |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 191 | }); |
| 192 | // TODO implementation based on supported features. |
| 193 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 194 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 195 | /** |
| 196 | * Process notification registration for manager class object. |
| 197 | * |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 198 | * @param manager yang manager |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 199 | * @param service service class |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 200 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 201 | 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 Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 207 | } |
| 208 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 209 | } |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 218 | YangSchemaRegistry registry = new DefaultYangSchemaRegistry(); |
| 219 | DefaultYangCodecHandler handler = new DefaultYangCodecHandler(registry); |
| 220 | handler.setLibrary(new DefaultYangModuleLibrary(getNewModuleId())); |
| 221 | return handler; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Returns schema registry. |
| 226 | * |
| 227 | * @return schema registry |
| 228 | */ |
| 229 | public YangSchemaRegistry getSchemaRegistry() { |
| 230 | return schemaRegistry; |
| 231 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 232 | |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 241 | } |