BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / subobject / XROSubobjectAttributeMapping.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.protocol.pcep.impl.subobject;
9
10 import java.util.EnumMap;
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.NoSuchElementException;
14
15 import org.opendaylight.protocol.pcep.subobject.XROSubobjectAttribute;
16
17 /**
18  * Bidirectional mapping for XROSubobjectAttribute and appropriate identifier.
19  */
20 public class XROSubobjectAttributeMapping {
21         private static final XROSubobjectAttributeMapping instance = new XROSubobjectAttributeMapping();
22
23         private final Map<XROSubobjectAttribute, Integer> ofCodesMap = new EnumMap<XROSubobjectAttribute, Integer>(XROSubobjectAttribute.class);
24         private final Map<Integer, XROSubobjectAttribute> ofCodeIdsMap = new HashMap<Integer, XROSubobjectAttribute>();
25
26         private XROSubobjectAttributeMapping() {
27                 this.fillIn();
28         }
29
30         private void fillIn() {
31                 this.fillIn(0, XROSubobjectAttribute.INTERFACE);
32                 this.fillIn(1, XROSubobjectAttribute.NODE);
33                 this.fillIn(2, XROSubobjectAttribute.SRLG);
34         }
35
36         private void fillIn(int identifier, XROSubobjectAttribute ofCode) {
37                 this.ofCodesMap.put(ofCode, identifier);
38                 this.ofCodeIdsMap.put(identifier, ofCode);
39         }
40
41         public int getFromAttributeEnum(XROSubobjectAttribute ofCode) {
42                 final Integer ofci = this.ofCodesMap.get(ofCode);
43                 if (ofci == null)
44                         throw new NoSuchElementException("Unknown XROSubobjectAttribute type: " + ofCode);
45                 return ofci;
46         }
47
48         public XROSubobjectAttribute getFromAttributeIdentifier(int identifier) {
49                 final XROSubobjectAttribute ofc = this.ofCodeIdsMap.get(identifier);
50                 if (ofc == null)
51                         throw new NoSuchElementException("Unknown XROSubobjectAttribute identifier.");
52                 return ofc;
53         }
54
55         public static XROSubobjectAttributeMapping getInstance() {
56                 return instance;
57         }
58 }