9aea508c719bf3f2d1117afdb1da13d04635cc3f
[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.init;
18
19 import com.google.inject.Inject;
20 import com.google.inject.name.Named;
21 import io.fd.honeycomb.data.init.AbstractDataTreeConverter;
22 import java.util.stream.Collectors;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.Interfaces;
25 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesBuilder;
26 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.InterfacesState;
27 import org.opendaylight.yang.gen.v1.io.fd.honeycomb.samples.interfaces.rev160810.interfaces.InterfaceBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * This is an initializer for interfaces plugin. Its main goal is to revers-engineer configuration data (config "true")
34  * for interfaces model from operational data. In this case, we are trying to recreate Interfaces container from InterfacesState
35  * container. Thanks to symmetrical nature of the model, it's pretty straightforward.
36  *
37  * This is very useful when the lower layer already contains some data that should be revers-engineer to config data
38  * in honeycomb in order to get HC and lower layer to sync... It makes life of upper layers much easier
39  *
40  * However it's not always possible to perform this task so the initializers are optional for plugins
41  */
42 public class InterfacesInitializer extends AbstractDataTreeConverter<InterfacesState, Interfaces> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(InterfacesInitializer.class);
45
46     @Inject
47     public InterfacesInitializer(@Named("honeycomb-initializer") final DataBroker bindingDataBroker) {
48         super(bindingDataBroker,
49                 InstanceIdentifier.create(InterfacesState.class), InstanceIdentifier.create(Interfaces.class));
50     }
51
52     @Override
53     protected Interfaces convert(final InterfacesState operationalData) {
54         // Just convert operational data into config data
55         // The operational data are queried from lower layer using readerCustomizers from this plugin
56
57         LOG.info("Initializing interfaces config data from: {}", operationalData);
58
59         return new InterfacesBuilder()
60                 .setInterface(operationalData.getInterface().stream()
61                         .map(oper -> new InterfaceBuilder()
62                                 .setMtu(oper.getMtu())
63                                 .setInterfaceId(oper.getInterfaceId())
64                                 .build())
65                         .collect(Collectors.toList()))
66                 .build();
67     }
68 }