Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 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; |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 36 | import org.onosproject.yms.app.ysr.DefaultYangSchemaRegistry; |
| 37 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 38 | import org.onosproject.yms.ych.YangCodecHandler; |
| 39 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 40 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 41 | import org.onosproject.yms.ydt.YdtBuilder; |
| 42 | import org.onosproject.yms.ydt.YdtResponse; |
| 43 | import org.onosproject.yms.ydt.YdtWalker; |
| 44 | import org.onosproject.yms.ydt.YmsOperationType; |
| 45 | import org.onosproject.yms.ymsm.YmsService; |
| 46 | import org.onosproject.yms.ynh.YangNotificationService; |
| 47 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 48 | import org.onosproject.yms.ysr.YangModuleLibrary; |
| 49 | import org.slf4j.Logger; |
| 50 | import org.slf4j.LoggerFactory; |
| 51 | |
| 52 | import java.util.List; |
| 53 | import java.util.concurrent.ExecutorService; |
| 54 | import java.util.concurrent.Executors; |
| 55 | |
| 56 | import static org.onlab.util.Tools.groupedThreads; |
| 57 | |
| 58 | /** |
| 59 | * Represents implementation of YANG management system manager. |
| 60 | */ |
| 61 | @Service |
| 62 | @Component(immediate = true) |
| 63 | public 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 Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 91 | ynhExtendedService = new YangNotificationManager(schemaRegistry); |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 92 | //Initilize the default codecs |
| 93 | YangCodecRegistry.initializeDefaultCodec(); |
| 94 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 95 | 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 gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 111 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
| 112 | operationType, schemaRegistry, true); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 117 | String rootNamespace, |
| 118 | YmsOperationType operationType, |
| 119 | Object schemaRegistryForYdt) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 120 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public YdtWalker getYdtWalker() { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 133 | return new DefaultYdtWalker(); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public YdtResponse executeOperation(YdtBuilder operationRequest) { |
Vidyashree Rama | 76faccc | 2016-10-17 22:06:52 +0530 | [diff] [blame] | 138 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 156 | return null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public YangNotificationService getYangNotificationService() { |
| 161 | return ynhExtendedService; |
| 162 | } |
| 163 | |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 164 | @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 Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 176 | public void registerDefaultCodec(YangDataTreeCodec defaultCodec, |
| 177 | YangProtocolEncodingFormat dataFormat) { |
Shankara-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 178 | YangCodecRegistry.registerDefaultCodec(defaultCodec, dataFormat); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 182 | 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 190 | |
Gaurav Agrawal | fcc6c19 | 2016-09-20 14:29:15 +0530 | [diff] [blame] | 191 | /** |
| 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 saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 205 | } |
| 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-Huawei | d5823ab | 2016-11-22 10:14:52 +0530 | [diff] [blame] | 214 | |
| 215 | YangSchemaRegistry yangSchemaRegistry = |
| 216 | new DefaultYangSchemaRegistry( |
| 217 | String.valueOf(moduleIdGenerator.getNewId())); |
| 218 | return new DefaultYangCodecHandler(yangSchemaRegistry); |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Returns schema registry. |
| 223 | * |
| 224 | * @return schema registry |
| 225 | */ |
| 226 | public YangSchemaRegistry getSchemaRegistry() { |
| 227 | return schemaRegistry; |
| 228 | } |
Bharat saraswal | f53b29a | 2016-09-27 15:35:15 +0530 | [diff] [blame] | 229 | } |