BUG-6647 Increase code coverage and clean up IV
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / PeerRoleUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.protocol.bgp.rib.spi;
10
11 import com.google.common.base.Optional;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.bgp.rib.rib.Peer;
14 import org.opendaylight.yangtools.yang.binding.BindingMapping;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
19
20 public final class PeerRoleUtil {
21     public static final NodeIdentifier PEER_ROLE_NID = new NodeIdentifier(QName.create(Peer.QNAME, "peer-role").intern());
22
23     private PeerRoleUtil() {
24         throw new UnsupportedOperationException();
25     }
26
27     public static PeerRole roleForChange(final Optional<NormalizedNode<?, ?>> maybePeerRole) {
28         if (maybePeerRole.isPresent()) {
29             final LeafNode<?> peerRoleLeaf = (LeafNode<?>) maybePeerRole.get();
30             return PeerRole.valueOf(BindingMapping.getClassName((String) peerRoleLeaf.getValue()));
31         }
32         return null;
33     }
34
35     public static String roleForString(final PeerRole role) {
36         switch (role) {
37         case Ebgp:
38             return "ebgp";
39         case Ibgp:
40             return "ibgp";
41         case RrClient:
42             return "rr-client";
43         case Internal:
44             return "internal";
45         default:
46             throw new IllegalArgumentException("Unhandled role " + role);
47         }
48     }
49 }