summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/DisabledInterfacesManager.java
blob: 9772923c355dc86506c51e3cf1c37b484d9a0f09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.honeycomb.translate.v3po;

import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.honeycomb.translate.read.ReaderFactory;
import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfaces;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.DisabledInterfacesBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndex;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexBuilder;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.context.rev160909.disabled.interfaces.DisabledInterfaceIndexKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;

/**
 * Facade on top of {@link MappingContext} making access to {@link DisabledInterfaces} easier.
 */
public class DisabledInterfacesManager {

    private static final InstanceIdentifier<DisabledInterfaces>
            DISABLED_IFCS_ROOT = InstanceIdentifier.create(DisabledInterfaces.class);

    /**
     * Read the list of currently disabled interfaces.
     */
    public List<Integer> getDisabledInterfaces(@Nonnull final MappingContext ctx) {
        final Optional<DisabledInterfaces> read = ctx.read(DISABLED_IFCS_ROOT);
        if (read.isPresent()) {
            return read.get().getDisabledInterfaceIndex().stream()
                    .map(DisabledInterfaceIndex::getIndex)
                    .collect(Collectors.toList());
        } else {
            return Collections.emptyList();
        }
    }

    /**
     * Check whether a specific interface is disabled.
     */
    public boolean isInterfaceDisabled(final int index, @Nonnull final MappingContext ctx) {
        return ctx.read(getKeyedId(index))
                .isPresent();
    }

    /**
     * Make a specific interface disabled.
     */
    public void disableInterface(final int index, @Nonnull final MappingContext ctx) {
        ctx.put(getKeyedId(index), getDisabledInterfaceIndex(index));
    }

    /**
     * Remove interface disability.
     */
    public void removeDisabledInterface(final int index, @Nonnull final MappingContext ctx) {
        ctx.delete(getKeyedId(index));
    }

    private static DisabledInterfaceIndex getDisabledInterfaceIndex(final int index) {
        return new DisabledInterfaceIndexBuilder().setIndex(index).build();
    }

    private static KeyedInstanceIdentifier<DisabledInterfaceIndex, DisabledInterfaceIndexKey> getKeyedId(final int id) {
        return DISABLED_IFCS_ROOT.child(DisabledInterfaceIndex.class, new DisabledInterfaceIndexKey(id));
    }

    public static final class ContextsReaderFactory implements ReaderFactory {

        @Inject
        @Named("honeycomb-context")
        private DataBroker contextBindingBrokerDependency;

        @Override
        public void init(final ModifiableReaderRegistryBuilder registry) {
            registry.add(new BindingBrokerReader<>(DISABLED_IFCS_ROOT,
                    contextBindingBrokerDependency,
                    LogicalDatastoreType.OPERATIONAL, DisabledInterfacesBuilder.class));
        }
    }

}