Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.net.pi.impl; |
| 18 | |
| 19 | import org.onosproject.net.pi.runtime.PiEntity; |
| 20 | import org.onosproject.net.pi.runtime.PiHandle; |
| 21 | import org.onosproject.net.pi.service.PiTranslatable; |
| 22 | import org.onosproject.net.pi.service.PiTranslatedEntity; |
| 23 | import org.onosproject.net.pi.service.PiTranslationStore; |
| 24 | import org.onosproject.net.pi.service.PiTranslator; |
| 25 | |
| 26 | import java.util.Optional; |
| 27 | |
| 28 | /** |
| 29 | * Abstract implementation of a PI translator backed by a PI translation store. |
| 30 | * |
| 31 | * @param <T> PD entity class |
| 32 | * @param <E> PI entity class |
| 33 | */ |
| 34 | public abstract class AbstractPiTranslatorImpl |
| 35 | <T extends PiTranslatable, E extends PiEntity> |
| 36 | implements PiTranslator<T, E> { |
| 37 | |
| 38 | private final PiTranslationStore<T, E> store; |
| 39 | |
| 40 | AbstractPiTranslatorImpl(PiTranslationStore<T, E> store) { |
| 41 | this.store = store; |
| 42 | } |
| 43 | |
| 44 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 45 | public void learn(PiHandle handle, PiTranslatedEntity<T, E> entity) { |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 46 | store.addOrUpdate(handle, entity); |
| 47 | } |
| 48 | |
| 49 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 50 | public Optional<PiTranslatedEntity<T, E>> lookup(PiHandle handle) { |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 51 | return Optional.ofNullable(store.get(handle)); |
| 52 | } |
| 53 | |
| 54 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 55 | public void forget(PiHandle handle) { |
Carmelo Cascone | 326ad2d | 2017-11-28 18:09:13 -0800 | [diff] [blame] | 56 | store.remove(handle); |
| 57 | } |
| 58 | } |