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