Thomas Vachuska | 73436b5 | 2017-03-22 19:50:47 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.yang; |
| 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.onosproject.yang.model.YangModel; |
| 25 | import org.onosproject.yang.runtime.DefaultModelRegistrationParam; |
| 26 | import org.onosproject.yang.runtime.ModelRegistrationParam; |
| 27 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.getYangModel; |
| 32 | |
| 33 | /** |
| 34 | * Abstract base for self-registering YANG models. |
| 35 | */ |
| 36 | @Component |
| 37 | public abstract class AbstractYangModelRegistrator { |
| 38 | |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | private final Class<?> loaderClass; |
| 41 | |
| 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 43 | protected YangModelRegistry modelRegistry; |
| 44 | |
| 45 | private YangModel model; |
| 46 | private ModelRegistrationParam registrationParam; |
| 47 | |
| 48 | /** |
| 49 | * Creates a model registrator primed with the class-loader of the specified |
| 50 | * class. |
| 51 | * |
| 52 | * @param loaderClass class whose class loader is to be used for locating schema data |
| 53 | */ |
| 54 | protected AbstractYangModelRegistrator(Class<?> loaderClass) { |
| 55 | this.loaderClass = loaderClass; |
| 56 | } |
| 57 | |
| 58 | @Activate |
| 59 | protected void activate() { |
| 60 | model = getYangModel(loaderClass); |
| 61 | registrationParam = DefaultModelRegistrationParam.builder() |
| 62 | .setYangModel(model).build(); |
| 63 | modelRegistry.registerModel(registrationParam); |
| 64 | log.info("Started"); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | protected void deactivate() { |
| 69 | modelRegistry.unregisterModel(registrationParam); |
| 70 | log.info("Stopped"); |
| 71 | } |
| 72 | } |