BUG-47: more subobject models
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPOFCodesMapping.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;
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.PCEPOFCodes;
16
17 /**
18  * Mapping of enumerable objective function codes to integral identifiers and
19  * vice-versa.
20  */
21 public class PCEPOFCodesMapping {
22         private static final PCEPOFCodesMapping instance = new PCEPOFCodesMapping();
23
24         private final Map<PCEPOFCodes, Integer> ofCodesMap = new EnumMap<PCEPOFCodes, Integer>(PCEPOFCodes.class);
25         private final Map<Integer, PCEPOFCodes> ofCodeIdsMap = new HashMap<Integer, PCEPOFCodes>();
26
27         private PCEPOFCodesMapping() {
28                 this.fillIn();
29         }
30
31         private void fillIn() {
32                 this.fillIn(1, PCEPOFCodes.MCP);
33                 this.fillIn(2, PCEPOFCodes.MLP);
34                 this.fillIn(3, PCEPOFCodes.MBP);
35                 this.fillIn(4, PCEPOFCodes.MBC);
36                 this.fillIn(5, PCEPOFCodes.MLL);
37                 this.fillIn(6, PCEPOFCodes.MCC);
38                 this.fillIn(7, PCEPOFCodes.SPT);
39                 this.fillIn(8, PCEPOFCodes.MCT);
40         }
41
42         private void fillIn(int identifier, PCEPOFCodes ofCode) {
43                 this.ofCodesMap.put(ofCode, identifier);
44                 this.ofCodeIdsMap.put(identifier, ofCode);
45         }
46
47         public int getFromOFCodesEnum(PCEPOFCodes ofCode) {
48                 final Integer ofci = this.ofCodesMap.get(ofCode);
49                 if (ofci == null)
50                         throw new NoSuchElementException("Unknown PCEPOFCodes type: " + ofCode);
51                 return ofci;
52         }
53
54         public PCEPOFCodes getFromCodeIdentifier(int identifier) {
55                 final PCEPOFCodes ofc = this.ofCodeIdsMap.get(identifier);
56                 if (ofc == null)
57                         throw new NoSuchElementException("Unknown PCEPOFCode identifier.");
58                 return ofc;
59         }
60
61         public static PCEPOFCodesMapping getInstance() {
62                 return instance;
63         }
64 }