cb9ff68901457d700c3116f6c51d338e78073d4a
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SwitchFeaturesUtil.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.openflowplugin.openflow.md.core.sal;
9
10 import java.util.HashMap;
11 import java.util.Map;
12
13 import org.opendaylight.openflowplugin.api.openflow.md.core.sal.BuildSwitchFeatures;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18
19 /**
20  * @author jsebin
21  *
22  */
23 public class SwitchFeaturesUtil {
24
25     protected static final Logger LOG = LoggerFactory.getLogger(SwitchFeaturesUtil.class);
26     
27     private static SwitchFeaturesUtil instance = new SwitchFeaturesUtil();
28     private Map<Short, BuildSwitchFeatures> swFeaturesBuilders;
29     
30     private SwitchFeaturesUtil() {
31         swFeaturesBuilders = new HashMap<>();
32         swFeaturesBuilders.put((short) 1, BuildSwitchCapabilitiesOF10.getInstance());
33         swFeaturesBuilders.put((short) 4, BuildSwitchCapabilitiesOF13.getInstance());
34     }
35     
36     /**
37      * Get singleton instance
38      * 
39      * @return instance
40      */
41     public static SwitchFeaturesUtil getInstance() {
42         return instance;
43     }
44     
45     /**
46      * @param features {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput}
47      * @return switch features
48      */
49     public SwitchFeatures buildSwitchFeatures(GetFeaturesOutput features) {
50
51         if(swFeaturesBuilders.containsKey(features.getVersion()) == true) {
52             LOG.debug("map contains version {}", features.getVersion());
53             try {
54                 return swFeaturesBuilders.get(features.getVersion()).build(features);
55             } catch (NullPointerException e) {
56                 LOG.error("error while building switch features {}", e);
57             }
58         }
59         else {
60             LOG.warn("unknown version: {}", features.getVersion());            
61         }                
62         
63         return null;
64     }
65     
66 }