2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.samples.interfaces.mapping.oper;
19 import io.fd.honeycomb.samples.interfaces.mapping.LowerLayerAccess;
20 import io.fd.honeycomb.translate.read.ReadContext;
21 import io.fd.honeycomb.translate.read.ReadFailedException;
22 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfaceId;
27 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesStateBuilder;
28 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.Interface;
29 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.InterfaceBuilder;
30 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.InterfaceKey;
31 import org.opendaylight.yangtools.concepts.Builder;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This is a customizer responsible for reading Interface operational data
40 public class InterfaceReaderCustomizer implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
42 private static final Logger LOG = LoggerFactory.getLogger(InterfaceReaderCustomizer.class);
43 private final LowerLayerAccess access;
45 public InterfaceReaderCustomizer(final LowerLayerAccess access) {
51 public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
52 @Nonnull final ReadContext context) throws ReadFailedException {
53 // context can be used to access cache (lifetime during a transaction) to store any information for
54 // subsequent invocations or for other customizers
55 // context.getModificationCache();
57 // context can also be used to access context data. Context data are stored in a persistent data store
58 // and usually are additional data required to perform the translation in customizers e.g. if underlying layer
59 // does not recognize interface-ids as string names, but only indices, the mapping between them can should
60 // be stored in the context data store
61 // Note: The context datastore is also YANG drive, so context data must be modeled in YANG prior to using them
62 // context.getMappingContext();
64 // return some sample IDs
65 return access.getAllInterfaceNames().stream()
66 .map(InterfaceId::new)
67 .map(InterfaceKey::new)
68 .collect(Collectors.toList());
72 public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<Interface> readData) {
73 // Just set the result of this customizers read into parent builder
74 // Builder has to be cast properly
75 ((InterfacesStateBuilder) builder).setInterface(readData);
80 public InterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<Interface> id) {
81 // Just providing empty builder
82 return new InterfaceBuilder();
86 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
87 @Nonnull final InterfaceBuilder builder, @Nonnull final ReadContext ctx)
88 throws ReadFailedException {
89 // This is where the actual "read" is happening, read attributes for a specific interface
90 final InterfaceKey k = id.firstKeyOf(Interface.class);
91 final String ifcId = k.getInterfaceId().getValue();
92 LOG.info("Reading data for interface: {} at {}", ifcId, id);
94 // Fill in some random values, this is actually the place where communication with lower layer
95 // would occur to get the real values
96 builder.setMtu(access.getMtuForInterface(ifcId));
97 builder.setInterfaceId(k.getInterfaceId());
98 // Counters container is not set here, instead a dedicated customizer is created for it
99 // It could be set here, if this customizer + its reader were marked as subtree reader in the ReaderFactory
100 // However its a good practice to provide a dedicated reader+customizer for every complex node