2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.frm.compatibility;
10 import java.text.MessageFormat;
11 import java.util.Iterator;
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;
27 public class FlowConfigMapping {
29 private final static Logger LOG = LoggerFactory.getLogger(FlowConfigMapping.class);
31 /* nodes/node/flow -> 0 / 1 / 2 */
32 private static final int FLOW_KEY_IDENTIFIER_DEEP = 2;
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());
45 new FlowKey(Long.valueOf(sourceCfg.getName()), flowBuilder.getNode());
46 flowBuilder.setKey(flowKey);
47 return flowBuilder.build();
50 public static FlowConfig toFlowConfig(final Flow sourceCfg) {
52 final FlowConfig flowConfig = new FlowConfig();
53 flowConfig.setName(String.valueOf(sourceCfg.getId()));
54 flowConfig.setNode(NodeMapping.toADNode(sourceCfg.getNode()));
56 } catch (Exception e) {
57 String errMsg = MessageFormat.format("Convert from Flow {} to FlowConfig fail", sourceCfg);
59 throw new RuntimeException(errMsg, e);
63 public static FlowConfig toFlowConfig(final InstanceIdentifier<? extends Object> identifier) {
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()));
74 } catch (Exception e) {
75 String errMsg = MessageFormat.format("Convert from InstanceIdentifier {} to FlowConfig fail", identifier);
77 throw new RuntimeException(errMsg, e);
81 public static boolean isFlowPath(final InstanceIdentifier<? extends Object> path) {
82 PathArgument pathArg = FlowConfigMapping.getSecondPathArgumentFromPath(path);
83 if (pathArg == null) {
86 if (pathArg instanceof IdentifiableItem<?,?>) {
87 final Identifiable<?> key = ((IdentifiableItem<?, ? extends Identifiable<?>>) pathArg).getKey();
88 if ((key instanceof FlowKey)) {
95 private static PathArgument getSecondPathArgumentFromPath(final InstanceIdentifier<? extends Object> path) {
96 if (path != null && path.getPathArguments() != null) {
97 Iterator<PathArgument> iterator = path.getPathArguments().iterator();
99 while (iterator.hasNext()) {
100 PathArgument pathArg = iterator.next();
101 if (deep == FlowConfigMapping.FLOW_KEY_IDENTIFIER_DEEP) {