17691b6ead3f6b7c9e832152cc3f3617172e110f
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.v3po.interfacesstate.span;
18
19 import io.fd.hc2vpp.common.translate.util.NamingContext;
20 import io.fd.hc2vpp.v3po.interfacesstate.InterfaceCustomizer;
21 import io.fd.honeycomb.translate.read.ReadContext;
22 import io.fd.honeycomb.translate.spi.read.Initialized;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.jvpp.core.future.FutureJVppCore;
25 import java.util.Collections;
26 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.VppInterfaceAugmentation;
30 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces.state._interface.SpanBuilder;
31 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfacesBuilder;
32 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.mirrored.interfaces.MirroredInterfaceBuilder;
33 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.mirrored.interfaces.MirroredInterfaceKey;
34 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.state.attributes.MirroredInterfaces;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yangtools.concepts.Builder;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 /**
41  * Provides interface-specific logic to read/init port mirroring configuration
42  */
43 public class InterfaceMirroredInterfacesCustomizer extends AbstractMirroredInterfacesCustomizer {
44
45     public InterfaceMirroredInterfacesCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
46                                                  @Nonnull final NamingContext ifcContext) {
47         super(futureJVppCore, ifcContext, id -> id.firstKeyOf(Interface.class).getName());
48     }
49
50     @Nonnull
51     @Override
52     public Initialized<? extends DataObject> init(@Nonnull final InstanceIdentifier<MirroredInterfaces> id,
53                                                   @Nonnull final MirroredInterfaces readValue,
54                                                   @Nonnull final ReadContext ctx) {
55         final InstanceIdentifier<org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfaces> cfgId =
56                 InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
57                         .augmentation(VppInterfaceAugmentation.class)
58                         .child(org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.interfaces._interface.Span.class)
59                         .child(org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfaces.class);
60         final org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190502.span.attributes.MirroredInterfaces
61                 cfgValue = new MirroredInterfacesBuilder()
62                 .setMirroredInterface(Optional.ofNullable(readValue.getMirroredInterface()).orElse(Collections.emptyList())
63                         .stream()
64                         .map(mirroredInterface -> new MirroredInterfaceBuilder()
65                                 .setState(mirroredInterface.getState())
66                                 .withKey(new MirroredInterfaceKey(mirroredInterface.key().getIfaceRef()))
67                                 .setIfaceRef(mirroredInterface.getIfaceRef())
68                                 .build())
69                         .collect(Collectors.toList()))
70                 .build();
71         return Initialized.create(cfgId, cfgValue);
72     }
73
74     @Override
75     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull MirroredInterfaces mirroredInterfaces) {
76         ((SpanBuilder) builder).setMirroredInterfaces(mirroredInterfaces);
77     }
78 }