850c1396b53661fc4a831e8f071277e87502dac5
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
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 io.fd.honeycomb.samples.interfaces.mapping.oper;
18
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.Initialized;
23 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
24 import java.util.List;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfaceId;
28 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.Interfaces;
29 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesStateBuilder;
30 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.Interface;
31 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.InterfaceBuilder;
32 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.state.InterfaceKey;
33 import org.opendaylight.yangtools.concepts.Builder;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * This is a customizer responsible for reading Interface operational data
41  */
42 public class InterfaceReaderCustomizer implements
43         InitializingListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(InterfaceReaderCustomizer.class);
46     private final LowerLayerAccess access;
47
48     public InterfaceReaderCustomizer(final LowerLayerAccess access) {
49         this.access = access;
50     }
51
52     @Nonnull
53     @Override
54     public List<InterfaceKey> getAllIds(@Nonnull final InstanceIdentifier<Interface> id,
55                                         @Nonnull final ReadContext context) throws ReadFailedException {
56         // context can be used to access cache (lifetime during a transaction) to store any information for
57         // subsequent invocations or for other customizers
58         // context.getModificationCache();
59
60         // context can also be used to access context data. Context data are stored in a persistent data store
61         // and usually are additional data required to perform the translation in customizers e.g. if underlying layer
62         // does not recognize interface-ids as string names, but only indices, the mapping between them can should
63         // be stored in the context data store
64         // Note: The context datastore is also YANG drive, so context data must be modeled in YANG prior to using them
65         // context.getMappingContext();
66
67         // return some sample IDs
68         return access.getAllInterfaceNames().stream()
69                 .map(InterfaceId::new)
70                 .map(InterfaceKey::new)
71                 .collect(Collectors.toList());
72     }
73
74     @Override
75     public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<Interface> readData) {
76         // Just set the result of this customizers read into parent builder
77         // Builder has to be cast properly
78         ((InterfacesStateBuilder) builder).setInterface(readData);
79     }
80
81     @Nonnull
82     @Override
83     public InterfaceBuilder getBuilder(@Nonnull final InstanceIdentifier<Interface> id) {
84         // Just providing empty builder
85         return new InterfaceBuilder();
86     }
87
88     @Override
89     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id,
90                                       @Nonnull final InterfaceBuilder builder, @Nonnull final ReadContext ctx)
91             throws ReadFailedException {
92         // This is where the actual "read" is happening, read attributes for a specific interface
93         final InterfaceKey k = id.firstKeyOf(Interface.class);
94         final String ifcId = k.getInterfaceId().getValue();
95         LOG.info("Reading data for interface: {} at {}", ifcId, id);
96
97         // Fill in some random values, this is actually the place where communication with lower layer
98         // would occur to get the real values
99         builder.setMtu(access.getMtuForInterface(ifcId));
100         builder.setInterfaceId(k.getInterfaceId());
101         // Counters container is not set here, instead a dedicated customizer is created for it
102         // It could be set here, if this customizer + its reader were marked as subtree reader in the ReaderFactory
103         // However its a good practice to provide a dedicated reader+customizer for every complex node
104     }
105
106     @Override
107     public Initialized<org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.Interface> init(
108             @Nonnull final InstanceIdentifier<Interface> id,
109             @Nonnull final Interface readValue,
110             @Nonnull final ReadContext ctx) {
111         return Initialized.create(InstanceIdentifier.create(Interfaces.class)
112                         .child(org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.Interface.class,
113                                 new org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceKey(
114                                         id.firstKeyOf(Interface.class).getInterfaceId())),
115                 new org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceBuilder()
116                         .setMtu(readValue.getMtu())
117                         .setInterfaceId(readValue.getInterfaceId())
118                         .build());
119     }
120 }