Merge "Bug 809: Enhancements to the toaster example"
[controller.git] / opendaylight / md-sal / compatibility / flow-management-compatibility / src / main / java / org / opendaylight / controller / md / frm / compatibility / FlowConfigMapping.java
1 /**
2  * Copyright (c) 2014 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.controller.md.frm.compatibility;
9
10 import java.text.MessageFormat;
11 import java.util.Iterator;
12
13 import org.opendaylight.controller.forwardingrulesmanager.FlowConfig;
14 import org.opendaylight.controller.sal.compatibility.MDFlowMapping;
15 import org.opendaylight.controller.sal.compatibility.NodeMapping;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.Flow;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.config.rev130819.flows.FlowKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAdded;
20 import org.opendaylight.yangtools.yang.binding.Identifiable;
21 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 public class FlowConfigMapping {
28
29     private final static Logger LOG = LoggerFactory.getLogger(FlowConfigMapping.class);
30
31     /* nodes/node/flow  -> 0 / 1 / 2  */
32     private static final int FLOW_KEY_IDENTIFIER_DEEP = 2;
33
34     public static Flow toConfigurationFlow(final FlowConfig sourceCfg) {
35         final FlowAdded source = MDFlowMapping.flowAdded(sourceCfg.getFlow());
36         final FlowBuilder flowBuilder = new FlowBuilder();
37         flowBuilder.setInstructions(source.getInstructions());
38         flowBuilder.setCookie(source.getCookie());
39         flowBuilder.setHardTimeout(source.getHardTimeout());
40         flowBuilder.setIdleTimeout(source.getIdleTimeout());
41         flowBuilder.setMatch(source.getMatch());
42         flowBuilder.setNode(source.getNode());
43
44         FlowKey flowKey =
45                 new FlowKey(Long.valueOf(sourceCfg.getName()), flowBuilder.getNode());
46         flowBuilder.setKey(flowKey);
47         return flowBuilder.build();
48     }
49
50     public static FlowConfig toFlowConfig(final Flow sourceCfg) {
51         try {
52             final FlowConfig flowConfig = new FlowConfig();
53             flowConfig.setName(String.valueOf(sourceCfg.getId()));
54             flowConfig.setNode(NodeMapping.toADNode(sourceCfg.getNode()));
55             return flowConfig;
56         } catch (Exception e) {
57             String errMsg = MessageFormat.format("Convert from Flow {} to FlowConfig fail", sourceCfg);
58             LOG.error(errMsg, e);
59             throw new RuntimeException(errMsg, e);
60         }
61     }
62
63     public static FlowConfig toFlowConfig(final InstanceIdentifier<? extends Object> identifier) {
64         try {
65             PathArgument pathArg = FlowConfigMapping.getSecondPathArgumentFromPath(identifier);
66             if (pathArg != null) {
67                 final FlowConfig flowConfig = new FlowConfig();
68                 FlowKey key = ((IdentifiableItem<Flow, FlowKey>) pathArg).getKey();
69                 flowConfig.setName(String.valueOf(key.getId()));
70                 flowConfig.setNode(NodeMapping.toADNode(key.getNode()));
71                 return flowConfig;
72             }
73             return null;
74         } catch (Exception e) {
75             String errMsg = MessageFormat.format("Convert from InstanceIdentifier {} to FlowConfig fail", identifier);
76             LOG.error(errMsg, e);
77             throw new RuntimeException(errMsg, e);
78         }
79     }
80
81     public static boolean isFlowPath(final InstanceIdentifier<? extends Object> path) {
82         PathArgument pathArg = FlowConfigMapping.getSecondPathArgumentFromPath(path);
83         if (pathArg == null) {
84             return false;
85         }
86         if (pathArg instanceof IdentifiableItem<?,?>) {
87             final Identifiable<?> key = ((IdentifiableItem<?, ? extends Identifiable<?>>) pathArg).getKey();
88             if ((key instanceof FlowKey)) {
89                 return true;
90             }
91         }
92         return false;
93     }
94
95     private static PathArgument getSecondPathArgumentFromPath(final InstanceIdentifier<? extends Object> path) {
96         if (path != null && path.getPathArguments() != null) {
97             Iterator<PathArgument> iterator = path.getPathArguments().iterator();
98             int deep = 0;
99             while (iterator.hasNext()) {
100                 PathArgument pathArg = iterator.next();
101                 if (deep == FlowConfigMapping.FLOW_KEY_IDENTIFIER_DEEP) {
102                     return pathArg;
103                 }
104                 deep++;
105             }
106         }
107         return null;
108     }
109 }