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.translate.v3po;
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;
41 * Facade on top of {@link MappingContext} making access to {@link DisabledInterfaces} easier.
43 public class DisabledInterfacesManager {
45 private static final InstanceIdentifier<DisabledInterfaces>
46 DISABLED_IFCS_ROOT = InstanceIdentifier.create(DisabledInterfaces.class);
49 * Read the list of currently disabled interfaces.
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());
58 return Collections.emptyList();
63 * Check whether a specific interface is disabled.
65 public boolean isInterfaceDisabled(final int index, @Nonnull final MappingContext ctx) {
66 return ctx.read(getKeyedId(index))
71 * Make a specific interface disabled.
73 public void disableInterface(final int index, @Nonnull final MappingContext ctx) {
74 ctx.put(getKeyedId(index), getDisabledInterfaceIndex(index));
78 * Remove interface disability.
80 public void removeDisabledInterface(final int index, @Nonnull final MappingContext ctx) {
81 ctx.delete(getKeyedId(index));
84 private static DisabledInterfaceIndex getDisabledInterfaceIndex(final int index) {
85 return new DisabledInterfaceIndexBuilder().setIndex(index).build();
88 private static KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> getKeyedId(final int id) {
89 return DISABLED_IFCS_ROOT.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(id));
92 public static final class ContextsReaderFactory implements ReaderFactory {
95 @Named("honeycomb-context")
96 private DataBroker contextBindingBrokerDependency;
99 public void init(final ModifiableReaderRegistryBuilder registry) {
100 registry.add(new BindingBrokerReader<>(DISABLED_IFCS_ROOT,
101 contextBindingBrokerDependency,
102 LogicalDatastoreType.OPERATIONAL, DisabledInterfacesBuilder.class));