Add implementation for flat L3 overlay
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / VppPathMapper.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
10
11 import javax.annotation.Nullable;
12
13 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
16 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
17
18 import com.google.common.base.Optional;
19 import com.google.common.base.Strings;
20
21 public class VppPathMapper {
22
23     private static final String INTERFACE_PATH_BEFORE_KEY =
24             "/ietf-interfaces:interfaces/ietf-interfaces:interface[ietf-interfaces:name='";
25     private static final String INTERFACE_PATH_AFTER_KEY = "']";
26     private static final int INTERFACE_PATH_MIN_LENGTH =
27             INTERFACE_PATH_BEFORE_KEY.length() + INTERFACE_PATH_AFTER_KEY.length() + 1;
28     private static final String BD_PATH_BEFORE_KEY = "/v3po:vpp/v3po:bridge-domains/v3po:bridge-domain[v3po:name='";
29     private static final String BD_PATH_AFTER_KEY = "']";
30     private static final int BD_PATH_MIN_LENGTH = BD_PATH_BEFORE_KEY.length() + BD_PATH_AFTER_KEY.length() + 1;
31
32     private VppPathMapper() {}
33
34     public static String interfaceToRestPath(String interfaceName) {
35         return INTERFACE_PATH_BEFORE_KEY + interfaceName + INTERFACE_PATH_AFTER_KEY;
36     }
37
38     public static Optional<InstanceIdentifier<Interface>> interfaceToInstanceIdentifier(@Nullable String restPath) {
39         if (Strings.isNullOrEmpty(restPath)) {
40             return Optional.absent();
41         }
42         if (restPath.length() < INTERFACE_PATH_MIN_LENGTH) {
43             return Optional.absent();
44         }
45         if (!restPath.startsWith(INTERFACE_PATH_BEFORE_KEY)) {
46             return Optional.absent();
47         }
48         if (!restPath.endsWith(INTERFACE_PATH_AFTER_KEY)) {
49             return Optional.absent();
50         }
51         int endIndexInterfaceName = restPath.length() - INTERFACE_PATH_AFTER_KEY.length();
52         String interfaceName = restPath.substring(INTERFACE_PATH_BEFORE_KEY.length(), endIndexInterfaceName);
53         return Optional.of(VppIidFactory.getInterfaceIID(new InterfaceKey(interfaceName)));
54     }
55
56     public static Optional<String> interfacePathToInterfaceName(@Nullable String restPath) {
57         if (Strings.isNullOrEmpty(restPath)) {
58             return Optional.absent();
59         }
60         if (restPath.length() < INTERFACE_PATH_MIN_LENGTH) {
61             return Optional.absent();
62         }
63         if (!restPath.startsWith(INTERFACE_PATH_BEFORE_KEY)) {
64             return Optional.absent();
65         }
66         if (!restPath.endsWith(INTERFACE_PATH_AFTER_KEY)) {
67             return Optional.absent();
68         }
69         int endIndexInterfaceName = restPath.length() - INTERFACE_PATH_AFTER_KEY.length();
70         String interfaceName = restPath.substring(INTERFACE_PATH_BEFORE_KEY.length(), endIndexInterfaceName);
71         return Optional.of(interfaceName);
72     }
73
74     public static String bridgeDomainToRestPath(String bridgeDomainName) {
75         return BD_PATH_BEFORE_KEY + bridgeDomainName + BD_PATH_AFTER_KEY;
76     }
77
78 }