9772923c355dc86506c51e3cf1c37b484d9a0f09
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / DisabledInterfacesManager.java
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.translate.v3po;
18
19 import com.google.common.base.Optional;
20 import com.google.inject.Inject;
21 import com.google.inject.name.Named;
22 import io.fd.honeycomb.translate.MappingContext;
23 import io.fd.honeycomb.translate.read.ReaderFactory;
24 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
25 import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfacesBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndex;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
39
40 /**
41  * Facade on top of {@link MappingContext} making access to {@link DisabledInterfaces} easier.
42  */
43 public class DisabledInterfacesManager {
44
45     private static final InstanceIdentifier<DisabledInterfaces>
46             DISABLED_IFCS_ROOT = InstanceIdentifier.create(DisabledInterfaces.class);
47
48     /**
49      * Read the list of currently disabled interfaces.
50      */
51     public List<Integer> getDisabledInterfaces(@Nonnull final MappingContext ctx) {
52         final Optional<DisabledInterfaces> read = ctx.read(DISABLED_IFCS_ROOT);
53         if (read.isPresent()) {
54             return read.get().getDisabledInterfaceIndex().stream()
55                     .map(DisabledInterfaceIndex::getIndex)
56                     .collect(Collectors.toList());
57         } else {
58             return Collections.emptyList();
59         }
60     }
61
62     /**
63      * Check whether a specific interface is disabled.
64      */
65     public boolean isInterfaceDisabled(final int index, @Nonnull final MappingContext ctx) {
66         return ctx.read(getKeyedId(index))
67                 .isPresent();
68     }
69
70     /**
71      * Make a specific interface disabled.
72      */
73     public void disableInterface(final int index, @Nonnull final MappingContext ctx) {
74         ctx.put(getKeyedId(index), getDisabledInterfaceIndex(index));
75     }
76
77     /**
78      * Remove interface disability.
79      */
80     public void removeDisabledInterface(final int index, @Nonnull final MappingContext ctx) {
81         ctx.delete(getKeyedId(index));
82     }
83
84     private static DisabledInterfaceIndex getDisabledInterfaceIndex(final int index) {
85         return new DisabledInterfaceIndexBuilder().setIndex(index).build();
86     }
87
88     private static KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> getKeyedId(final int id) {
89         return DISABLED_IFCS_ROOT.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(id));
90     }
91
92     public static final class ContextsReaderFactory implements ReaderFactory {
93
94         @Inject
95         @Named("honeycomb-context")
96         private DataBroker contextBindingBrokerDependency;
97
98         @Override
99         public void init(final ModifiableReaderRegistryBuilder registry) {
100             registry.add(new BindingBrokerReader<>(DISABLED_IFCS_ROOT,
101                     contextBindingBrokerDependency,
102                     LogicalDatastoreType.OPERATIONAL, DisabledInterfacesBuilder.class));
103         }
104     }
105
106 }