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