e897ff351f1b06b0a48629a9b3a2cc47f0cabcab
[groupbasedpolicy.git] / renderers / vpp / src / main / java / org / opendaylight / groupbasedpolicy / renderer / vpp / iface / VppEndpointLocationProvider.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 static com.google.common.base.Preconditions.checkNotNull;
12
13 import javax.annotation.Nonnull;
14
15 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.CloseOnFailTransactionChain;
20 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.KeyFactory;
21 import org.opendaylight.groupbasedpolicy.renderer.vpp.util.VppIidFactory;
22 import org.opendaylight.groupbasedpolicy.util.IidFactory;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.ProviderName;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.vpp_renderer.rev160425.config.VppEndpoint;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.base.Function;
41 import com.google.common.util.concurrent.FutureCallback;
42 import com.google.common.util.concurrent.Futures;
43 import com.google.common.util.concurrent.ListenableFuture;
44
45 public class VppEndpointLocationProvider implements AutoCloseable {
46
47     private static final Logger LOG = LoggerFactory.getLogger(VppEndpointLocationProvider.class);
48     public static final ProviderName VPP_ENDPOINT_LOCATION_PROVIDER =
49             new ProviderName("VPP endpoint location provider");
50     public static final long PROVIDER_PRIORITY = 10L;
51     private final BindingTransactionChain txChain;
52
53     public VppEndpointLocationProvider(DataBroker dataProvider) {
54         LocationProvider locationProvider = new LocationProviderBuilder().setProvider(VPP_ENDPOINT_LOCATION_PROVIDER)
55             .setPriority(PROVIDER_PRIORITY)
56             .build();
57         txChain = checkNotNull(dataProvider)
58             .createTransactionChain(new CloseOnFailTransactionChain(VppEndpointLocationProvider.class.getSimpleName()));
59         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
60         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER),
61                 locationProvider, true);
62
63         Futures.addCallback(wTx.submit(), new FutureCallback<Void>() {
64
65             @Override
66             public void onSuccess(Void result) {
67                 LOG.debug("{} was created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
68             }
69
70             @Override
71             public void onFailure(Throwable t) {
72                 LOG.error("{} was NOT created", VPP_ENDPOINT_LOCATION_PROVIDER.getValue());
73             }
74         });
75     }
76
77     public ListenableFuture<Void> createLocationForVppEndpoint(VppEndpoint vppEndpoint) {
78         ProviderAddressEndpointLocation providerAddressEndpointLocation = createProviderAddressEndpointLocation(vppEndpoint);
79         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
80         wTx.put(LogicalDatastoreType.CONFIGURATION, IidFactory.providerAddressEndpointLocationIid(
81                 VPP_ENDPOINT_LOCATION_PROVIDER, providerAddressEndpointLocation.getKey()),
82                 providerAddressEndpointLocation);
83         LOG.debug("Creating location for {}", providerAddressEndpointLocation.getKey());
84         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
85
86             @Override
87             public Void apply(Void input) {
88                 LOG.debug("{} provided location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
89                         providerAddressEndpointLocation);
90                 return null;
91             }
92         });
93     }
94
95     public static ProviderAddressEndpointLocation createProviderAddressEndpointLocation(VppEndpoint vppEndpoint) {
96         InstanceIdentifier<Node> vppNodeIid = VppIidFactory.getNetconfNodeIid(vppEndpoint.getVppNodeId());
97         String restIfacePath = VppPathMapper.interfaceToRestPath(vppEndpoint.getVppInterfaceName());
98         AbsoluteLocation absoluteLocation = new AbsoluteLocationBuilder()
99             .setLocationType(new ExternalLocationCaseBuilder().setExternalNodeMountPoint(vppNodeIid)
100                 .setExternalNodeConnector(restIfacePath)
101                 .build())
102             .build();
103         return new ProviderAddressEndpointLocationBuilder()
104             .setKey(createProviderAddressEndpointLocationKey(vppEndpoint))
105             .setAbsoluteLocation(absoluteLocation)
106             .build();
107     }
108
109     public ListenableFuture<Void> deleteLocationForVppEndpoint(VppEndpoint vppEndpoint) {
110         ProviderAddressEndpointLocationKey provAddrEpLocKey = createProviderAddressEndpointLocationKey(vppEndpoint);
111         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
112         wTx.delete(LogicalDatastoreType.CONFIGURATION,
113                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER, provAddrEpLocKey));
114         LOG.debug("Deleting location for {}", provAddrEpLocKey);
115         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
116
117             @Override
118             public Void apply(Void input) {
119                 LOG.debug("{} removed location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(), provAddrEpLocKey);
120                 return null;
121             }
122         });
123     }
124
125     private static ProviderAddressEndpointLocationKey createProviderAddressEndpointLocationKey(
126             VppEndpoint vppEndpoint) {
127         return new ProviderAddressEndpointLocationKey(vppEndpoint.getAddress(), vppEndpoint.getAddressType(),
128                 vppEndpoint.getContextId(), vppEndpoint.getContextType());
129     }
130
131     public ListenableFuture<Void> replaceLocationForEndpoint(@Nonnull ExternalLocationCase location, @Nonnull AddressEndpointWithLocationKey addrEpWithLocKey) {
132         ProviderAddressEndpointLocationKey provAddrEpLocKey =
133                 KeyFactory.providerAddressEndpointLocationKey(addrEpWithLocKey);
134         AbsoluteLocation absoluteLocation =
135                 new AbsoluteLocationBuilder().setLocationType(location).build();
136         ProviderAddressEndpointLocation providerAddressEndpointLocation = new ProviderAddressEndpointLocationBuilder()
137             .setKey(provAddrEpLocKey).setAbsoluteLocation(absoluteLocation).build();
138         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
139         wTx.put(LogicalDatastoreType.CONFIGURATION,
140                 IidFactory.providerAddressEndpointLocationIid(VPP_ENDPOINT_LOCATION_PROVIDER,
141                         providerAddressEndpointLocation.getKey()),
142                 providerAddressEndpointLocation);
143         LOG.debug("Updating location for {}", provAddrEpLocKey);
144         return Futures.transform(wTx.submit(), new Function<Void, Void>() {
145
146             @Override
147             public Void apply(Void input) {
148                 LOG.debug("{} replaced location: {}", VPP_ENDPOINT_LOCATION_PROVIDER.getValue(),
149                         providerAddressEndpointLocation);
150                 return null;
151             }
152         });
153     }
154
155     @Override
156     public void close() throws Exception {
157         WriteTransaction wTx = txChain.newWriteOnlyTransaction();
158         wTx.delete(LogicalDatastoreType.CONFIGURATION, IidFactory.locationProviderIid(VPP_ENDPOINT_LOCATION_PROVIDER));
159         wTx.submit();
160     }
161 }