Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [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.yab; |
| 18 | |
| 19 | import static java.lang.String.valueOf; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.drivers.netconf.MockCoreService; |
| 29 | import org.onosproject.yms.app.ych.DefaultYangCodecHandler; |
| 30 | import org.onosproject.yms.app.ych.defaultcodecs.YangCodecRegistry; |
| 31 | import org.onosproject.yms.app.ydt.YangRequestWorkBench; |
| 32 | import org.onosproject.yms.app.ysr.DefaultYangModuleLibrary; |
| 33 | import org.onosproject.yms.app.ysr.MockYangSchemaRegistry; |
| 34 | import org.onosproject.yms.app.ysr.YangSchemaRegistry; |
| 35 | import org.onosproject.yms.ych.YangCodecHandler; |
| 36 | import org.onosproject.yms.ych.YangDataTreeCodec; |
| 37 | import org.onosproject.yms.ych.YangProtocolEncodingFormat; |
| 38 | import org.onosproject.yms.ydt.YdtBuilder; |
| 39 | import org.onosproject.yms.ydt.YdtResponse; |
| 40 | import org.onosproject.yms.ydt.YdtWalker; |
| 41 | import org.onosproject.yms.ydt.YmsOperationType; |
| 42 | import org.onosproject.yms.ymsm.YmsService; |
| 43 | import org.onosproject.yms.ynh.YangNotificationService; |
| 44 | import org.onosproject.yms.ysr.YangModuleIdentifier; |
| 45 | import org.onosproject.yms.ysr.YangModuleLibrary; |
| 46 | |
| 47 | /** |
| 48 | * Represents implementation of YANG application management system manager. |
| 49 | */ |
| 50 | public class MockYmsManager |
| 51 | implements YmsService { |
| 52 | private static final String MODULE_ID = "module-id"; |
| 53 | |
| 54 | YangSchemaRegistry schemaRegistry; |
| 55 | private YangModuleLibrary library; |
| 56 | private IdGenerator moduleIdGenerator; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | public MockYmsManager() |
| 62 | throws InstantiationException, IllegalAccessException, IOException { |
| 63 | coreService = new MockCoreService(); |
| 64 | moduleIdGenerator = coreService.getIdGenerator(MODULE_ID); |
| 65 | library = new DefaultYangModuleLibrary(getNewModuleId()); |
| 66 | YangCodecRegistry.initializeDefaultCodec(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 71 | String rootNamespace, |
| 72 | YmsOperationType operationType) { |
| 73 | return new YangRequestWorkBench(logicalRootName, rootNamespace, |
| 74 | operationType, schemaRegistry, false); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public YdtBuilder getYdtBuilder(String logicalRootName, |
| 79 | String rootNamespace, |
| 80 | YmsOperationType operationType, |
| 81 | Object schemaRegistryForYdt) { |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public YdtWalker getYdtWalker() { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public YdtResponse executeOperation(YdtBuilder operationRequest) { |
| 92 | YangApplicationBroker requestBroker = |
| 93 | new YangApplicationBroker(schemaRegistry); |
| 94 | switch (operationRequest.getYmsOperationType()) { |
| 95 | case EDIT_CONFIG_REQUEST: |
| 96 | try { |
| 97 | return requestBroker.processEdit(operationRequest); |
| 98 | } catch (CloneNotSupportedException e) { |
| 99 | } |
| 100 | break; |
| 101 | case QUERY_CONFIG_REQUEST: |
| 102 | case QUERY_REQUEST: |
| 103 | return requestBroker.processQuery(operationRequest); |
| 104 | case RPC_REQUEST: |
| 105 | return requestBroker.processOperation(operationRequest); |
| 106 | default: |
| 107 | } |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public YangNotificationService getYangNotificationService() { |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void registerService(Object appManager, Class<?> yangService, |
| 118 | List<String> supportedFeatureList) { |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void unRegisterService(Object appManager, Class<?> yangService) { |
| 123 | |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public YangModuleLibrary getYangModuleLibrary() { |
| 128 | return library; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String getYangFile(YangModuleIdentifier moduleIdentifier) { |
| 133 | return null; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void registerDefaultCodec(YangDataTreeCodec defaultCodec, |
| 138 | YangProtocolEncodingFormat dataFormat) { |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public YangCodecHandler getYangCodecHandler() { |
| 143 | YangSchemaRegistry registry = new MockYangSchemaRegistry(); |
| 144 | DefaultYangCodecHandler handler = new DefaultYangCodecHandler(registry); |
| 145 | handler.setLibrary(new DefaultYangModuleLibrary(getNewModuleId())); |
| 146 | return handler; |
| 147 | } |
| 148 | |
| 149 | private String getNewModuleId() { |
| 150 | return valueOf(moduleIdGenerator.getNewId()); |
| 151 | } |
| 152 | } |