2 * Copyright (c) 2017 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
9 package org.opendaylight.groupbasedpolicy.renderer.vpp.iface;
11 import java.util.Collections;
12 import java.util.List;
14 import java.util.stream.Collectors;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
22 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
23 import org.opendaylight.groupbasedpolicy.util.IidFactory;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpointKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocations;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocationsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.ExternalLocation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.ExternalLocationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.ParentEndpointChoice;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentEndpointCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpointKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.IpPrefixType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.MacAddressType;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpointKey;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import com.google.common.base.Optional;
52 public class VppLocationUtils {
54 private VppLocationUtils() {}
56 static ProviderAddressEndpointLocationKey locationProviderKey(AddressEndpointKey key) {
57 return new ProviderAddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
58 key.getContextType());
61 static ProviderAddressEndpointLocationKey providerLocationKey(AddressEndpointWithLocationKey key) {
62 return new ProviderAddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
63 key.getContextType());
66 static ProviderAddressEndpointLocationKey providerLocationKey(ParentEndpointKey key) {
67 return new ProviderAddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
68 key.getContextType());
71 static ProviderAddressEndpointLocationKey providerLocationKey(ChildEndpointKey key) {
72 return new ProviderAddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
73 key.getContextType());
76 static VppEndpointKey vppEndpointKey(AddressEndpointKey key) {
77 return new VppEndpointKey(key.getAddress(), key.getAddressType(), key.getContextId(), key.getContextType());
80 static VppEndpointKey vppEndpointKey(ParentEndpointKey key) {
81 return new VppEndpointKey(key.getAddress(), key.getAddressType(), key.getContextId(), key.getContextType());
84 static VppEndpointKey vppEndpointKey(ChildEndpointKey key) {
85 return new VppEndpointKey(key.getAddress(), key.getAddressType(), key.getContextId(), key.getContextType());
88 public static @Nonnull List<ChildEndpoint> getL2ChildEndpoints(@Nonnull AddressEndpoint addrEp) {
89 if (addrEp.getChildEndpoint() == null) {
90 return Collections.emptyList();
92 return addrEp.getChildEndpoint()
94 .filter(child -> child.getAddressType().equals(MacAddressType.class))
95 .collect(Collectors.toList());
98 static boolean validateEndpoint(AddressEndpoint addrEp) {
99 return addrEp.getAddressType().equals(IpPrefixType.class) && addrEp.getChildEndpoint() != null
100 && !addrEp.getChildEndpoint().isEmpty();
103 static AbsoluteLocation createAbsLocation(VppEndpoint vppEndpoint) {
104 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
105 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
106 return new AbsoluteLocationBuilder().setLocationType(new ExternalLocationCaseBuilder()
107 .setExternalNodeMountPoint(vppNodeIid).setExternalNodeConnector(restIfacePath).build()).build();
110 static RelativeLocations createRelLocations(List<VppEndpoint> vppEndpoints) {
111 List<ExternalLocation> extLocations = vppEndpoints.stream().map(vppEndpoint -> {
112 InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
113 String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
114 return new ExternalLocationBuilder().setExternalNodeMountPoint(vppNodeIid)
115 .setExternalNodeConnector(restIfacePath)
117 }).collect(Collectors.toList());
118 return new RelativeLocationsBuilder().setExternalLocation(extLocations).build();
121 static RelativeLocations createRelativeAddressEndpointLocation(@Nonnull AddressEndpointKey addrEp,
122 @Nonnull Map<NodeId, String> publicIntfNamesByNodes) {
123 return new RelativeLocationsBuilder()
124 .setExternalLocation(
125 publicIntfNamesByNodes.keySet()
127 .filter(nodeId -> publicIntfNamesByNodes.get(nodeId) != null)
128 .map(nodeId -> new ExternalLocationBuilder()
129 .setExternalNodeMountPoint(VppIidFactory.getNetconfNodeIid(nodeId))
130 .setExternalNodeConnector(
131 VppPathMapper.interfaceToRestPath(publicIntfNamesByNodes.get(nodeId)))
133 .collect(Collectors.toList()))
137 static ProviderAddressEndpointLocation createLocation(AddressEndpointKey addrEpKey,
138 AbsoluteLocation absoluteLocation) {
139 return new ProviderAddressEndpointLocationBuilder().setKey(createProviderAddressEndpointLocationKey(addrEpKey))
140 .setAbsoluteLocation(absoluteLocation)
144 static ProviderAddressEndpointLocation createLocation(AddressEndpointKey key, RelativeLocations relativeLocations) {
145 return new ProviderAddressEndpointLocationBuilder().setRelativeLocations(relativeLocations)
146 .setKey(createProviderAddressEndpointLocationKey(key))
150 public static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(AddressEndpointKey key) {
151 return new ProviderAddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
152 key.getContextType());
155 public static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(VppEndpoint vpp) {
156 return new ProviderAddressEndpointLocationKey(vpp.getAddress(), vpp.getAddressType(), vpp.getContextId(),
157 vpp.getContextType());
160 static boolean hasMultihomeParent(ReadTransaction rTx, @Nullable ParentEndpointChoice parentChoice) {
161 if (parentChoice == null || ((ParentEndpointCase) parentChoice).getParentEndpoint() == null) {
164 ParentEndpointCase parents = (ParentEndpointCase) parentChoice;
165 return parents.getParentEndpoint().stream().map(parent -> parent.getKey()).anyMatch(key -> {
166 AddressEndpointKey addrEpKey = new AddressEndpointKey(key.getAddress(), key.getAddressType(),
167 key.getContextId(), key.getContextType());
168 Optional<AddressEndpoint> addressEndpoint = DataStoreHelper.readFromDs(LogicalDatastoreType.OPERATIONAL,
169 IidFactory.addressEndpointIid(addrEpKey), rTx);
170 return (addressEndpoint.isPresent() && addressEndpoint.get().getChildEndpoint().size() > 1);