From: Michal Cmarada Date: Mon, 9 May 2016 16:38:05 +0000 (+0200) Subject: Implementing base endpoint rpc registry X-Git-Tag: release/boron~188 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=84003e8b9ec9629818908583797f2ecaac7637d0;p=groupbasedpolicy.git Implementing base endpoint rpc registry Change-Id: Ie571b9b06921cbecd1fd39b8b0ecefbfc9a42e45 Signed-off-by: Michal Cmarada --- diff --git a/groupbasedpolicy/src/main/config/default-config.xml b/groupbasedpolicy/src/main/config/default-config.xml index bf4ae2c43..0abe8d3a0 100755 --- a/groupbasedpolicy/src/main/config/default-config.xml +++ b/groupbasedpolicy/src/main/config/default-config.xml @@ -59,6 +59,22 @@ binding-rpc-broker + + + groupbasedpolicy:base-endpoint-renderer-augmentation-registry-impl + + base-endpoint-renderer-augmentation-registry + + + binding:binding-async-data-broker + binding-data-broker + + + + binding:binding-rpc-registry + binding-rpc-broker + + groupbasedpolicy:statistics-manager-impl @@ -93,6 +109,16 @@ /modules/module[type='ep-renderer-augmentation-registry-impl'][name='ep-renderer-augmentation-registry'] + + + groupbasedpolicy:base-endpoint-renderer-augmentation-registry + + + + base-endpoint-renderer-augmentation-registry + /modules/module[type='base-endpoint-renderer-augmentation-registry-impl'][name='base-endpoint-renderer-augmentation-registry'] + + groupbasedpolicy:statistics-manager diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModule.java b/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModule.java new file mode 100644 index 000000000..57c82a08f --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModule.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.config.yang.config.groupbasedpolicy; + +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.base_endpoint.BaseEndpointRpcRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BaseEndpointRendererAugmentationRegistryImplModule extends org.opendaylight.controller.config.yang.config.groupbasedpolicy.AbstractBaseEndpointRendererAugmentationRegistryImplModule { + + private static final Logger LOG = LoggerFactory.getLogger(BaseEndpointRendererAugmentationRegistryImplModule.class); + + public BaseEndpointRendererAugmentationRegistryImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + super(identifier, dependencyResolver); + } + + public BaseEndpointRendererAugmentationRegistryImplModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.controller.config.yang.config.groupbasedpolicy.BaseEndpointRendererAugmentationRegistryImplModule oldModule, java.lang.AutoCloseable oldInstance) { + super(identifier, dependencyResolver, oldModule, oldInstance); + } + + @Override + public void customValidation() { + // add custom validation form module attributes here. + } + + @Override + public java.lang.AutoCloseable createInstance() { + final DataBroker dataProvider = getDataBrokerDependency(); + final RpcProviderRegistry rpcRegistry = getRpcRegistryDependency(); + + BaseEndpointRpcRegistry baseEndpointRpcRegistry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry); + LOG.info("{} successfully started.", BaseEndpointRendererAugmentationRegistryImplModule.class.getCanonicalName()); + return baseEndpointRpcRegistry; + } + +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModuleFactory.java b/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModuleFactory.java new file mode 100644 index 000000000..4ffe06f4f --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/controller/config/yang/config/groupbasedpolicy/BaseEndpointRendererAugmentationRegistryImplModuleFactory.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +/* +* Generated file +* +* Generated from: yang module name: groupbasedpolicy-cfg yang module local name: base-endpoint-renderer-augmentation-registry-impl +* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator +* Generated at: Mon May 09 16:19:05 CEST 2016 +* +* Do not modify this file unless it is present under src/main directory +*/ +package org.opendaylight.controller.config.yang.config.groupbasedpolicy; +public class BaseEndpointRendererAugmentationRegistryImplModuleFactory extends org.opendaylight.controller.config.yang.config.groupbasedpolicy.AbstractBaseEndpointRendererAugmentationRegistryImplModuleFactory { + +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentation.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentation.java new file mode 100644 index 000000000..c9607c292 --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentation.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.api; + +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointReg; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointReg; +import org.opendaylight.yangtools.yang.binding.Augmentation; + +import javax.annotation.Nullable; +import java.util.Map; + +public interface BaseEndpointRendererAugmentation { + + /** + * Creates pair of AddressEndpoint augmentation, specific for renderer and augmentation type + * (class + * name). + * + * @param input input data for AddressEndpoint creation + * @return pair of augmentation type and augmentation + */ + @Nullable + Map.Entry>, Augmentation> buildAddressEndpointAugmentation( + AddressEndpointReg input); + + /** + * Creates pair of AddressEndpoint augmentation, specific for renderer and augmentation type + * (class + * name). + * + * @param input input data for AddressEndpoint creation + * @return pair of augmentation type and augmentation + */ + @Nullable + Map.Entry>, Augmentation> buildContainmentEndpointAugmentation( + ContainmentEndpointReg input); +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentationRegistry.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentationRegistry.java new file mode 100644 index 000000000..18bc120d1 --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/api/BaseEndpointRendererAugmentationRegistry.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.api; + +public interface BaseEndpointRendererAugmentationRegistry { + + /** + * Registers renderer's endpoints augmentation. + * + * @param baseEndpointRendererAugmentation cannot be {@code null} + * @throws NullPointerException + */ + void register(BaseEndpointRendererAugmentation baseEndpointRendererAugmentation); + + /** + * Unregisters renderer's endpoints augmentation. + * + * @param baseEndpointRendererAugmentation cannot be {@code null} + * @throws NullPointerException + */ + void unregister(BaseEndpointRendererAugmentation baseEndpointRendererAugmentation); +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistry.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistry.java new file mode 100644 index 000000000..d92edd196 --- /dev/null +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistry.java @@ -0,0 +1,251 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.base_endpoint; + +import com.google.common.base.Function; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.sal.binding.api.BindingAwareBroker; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.api.BaseEndpointRendererAugmentation; +import org.opendaylight.groupbasedpolicy.api.BaseEndpointRendererAugmentationRegistry; +import org.opendaylight.groupbasedpolicy.util.IidFactory; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.*; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointReg; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointReg; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnreg; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.ContainmentEndpointUnreg; +import org.opendaylight.yangtools.yang.binding.Augmentation; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.Future; + +public class BaseEndpointRpcRegistry + implements BaseEndpointService, BaseEndpointRendererAugmentationRegistry, AutoCloseable { + + static final ConcurrentMap registeredRenderers = new ConcurrentHashMap<>(); + private static final Logger LOG = LoggerFactory.getLogger(BaseEndpointRpcRegistry.class); + private final DataBroker dataProvider; + private final BindingAwareBroker.RpcRegistration rpcRegistration; + + private Function> futureTrans = new Function>() { + + @Override + public RpcResult apply(Void input) { + return RpcResultBuilder.success().build(); + } + }; + + public BaseEndpointRpcRegistry(DataBroker dataProvider, RpcProviderRegistry rpcRegistry) { + this.dataProvider = dataProvider; + + if (rpcRegistry != null) { + rpcRegistration = rpcRegistry.addRpcImplementation(BaseEndpointService.class, this); + LOG.debug("Added Endpoints RPC Implementation Correctly"); + } else { + rpcRegistration = null; + } + + if (dataProvider != null) { + InstanceIdentifier iid = InstanceIdentifier.builder(Endpoints.class).build(); + WriteTransaction t = this.dataProvider.newWriteOnlyTransaction(); + t.put(LogicalDatastoreType.OPERATIONAL, iid, new EndpointsBuilder().build()); + CheckedFuture f = t.submit(); + Futures.addCallback(f, new FutureCallback() { + + @Override + public void onFailure(Throwable t) { + LOG.error("Could not write Endpoints base container", t); + } + + @Override + public void onSuccess(Void result) { + LOG.info("Endpoints container write successful"); + } + }); + } + } + + /** + * Registers renderer's endpoints augmentation. + * + * @param baseEndpointRendererAugmentation cannot be {@code null} + * @throws NullPointerException + */ + @Override + public void register(BaseEndpointRendererAugmentation baseEndpointRendererAugmentation) { + if (baseEndpointRendererAugmentation != null) { + registeredRenderers.putIfAbsent(baseEndpointRendererAugmentation.getClass().getName(), + baseEndpointRendererAugmentation); + LOG.info("Registered {}", baseEndpointRendererAugmentation.getClass().getName()); + } + } + + /** + * Unregisters renderer's endpoints augmentation. + * + * @param baseEndpointRendererAugmentation cannot be {@code null} + * @throws NullPointerException + */ + @Override + public void unregister(BaseEndpointRendererAugmentation baseEndpointRendererAugmentation) { + if (baseEndpointRendererAugmentation == null + || !registeredRenderers.containsKey(baseEndpointRendererAugmentation.getClass().getName())) { + return; + } + registeredRenderers.remove(baseEndpointRendererAugmentation.getClass().getName()); + LOG.info("Unregistered {}", baseEndpointRendererAugmentation.getClass().getName()); + } + + /** + * Register a new endpoint into the registry. If there is already an existing + * endpoint with the same keys, they will be overwritten with the new information. + * + * @param input Endpoint to register + */ + @Override + public Future> registerEndpoint(RegisterEndpointInput input) { + long timestamp = System.currentTimeMillis(); + + WriteTransaction t = dataProvider.newWriteOnlyTransaction(); + + List endpoints = input.getContainmentEndpointReg(); + for (ContainmentEndpointReg ce : nullToEmpty(endpoints)) { + long stamp = (ce.getTimestamp() == null || ce.getTimestamp() == 0) ? timestamp : ce.getTimestamp(); + ContainmentEndpoint endpoint = buildContainmentEndpoint(ce).setTimestamp(stamp).build(); + t.put(LogicalDatastoreType.OPERATIONAL, IidFactory.containmentEndpointIid(endpoint.getKey()), endpoint, + true); + } + + List addressEndpoints = input.getAddressEndpointReg(); + for (AddressEndpointReg ae : nullToEmpty(addressEndpoints)) { + long stamp = (ae.getTimestamp() == null || ae.getTimestamp() == 0) ? timestamp : ae.getTimestamp(); + AddressEndpoint endpoint = buildAddressEndpoint(ae).setTimestamp(stamp).build(); + t.put(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(endpoint.getKey()), endpoint, true); + } + + ListenableFuture r = t.submit(); + return Futures.transform(r, futureTrans); + } + + private ContainmentEndpointBuilder buildContainmentEndpoint(ContainmentEndpointReg input) { + ContainmentEndpointBuilder eb = new ContainmentEndpointBuilder().setChildEndpoint(input.getChildEndpoint()) + .setCondition(input.getCondition()) + .setContextType(input.getContextType()) + .setContextId(input.getContextId()) + .setEndpointGroup(input.getEndpointGroup()) + .setKey(new ContainmentEndpointKey(input.getContextId(), input.getContextType())) + .setNetworkContainment(input.getNetworkContainment()) + .setTenant(input.getTenant()) + .setTimestamp(input.getTimestamp()); + + for (Map.Entry entry : registeredRenderers.entrySet()) { + try { + Map.Entry>, Augmentation> augmentationEntry = + entry.getValue().buildContainmentEndpointAugmentation(input); + if (augmentationEntry != null) { + eb.addAugmentation(augmentationEntry.getKey(), augmentationEntry.getValue()); + } + } catch (Exception e) { + LOG.warn("AddressEndpoint Augmentation error while processing " + entry.getKey() + ". Reason: ", e); + } + } + return eb; + } + + private AddressEndpointBuilder buildAddressEndpoint(AddressEndpointReg ae) { + AddressEndpointBuilder builder = new AddressEndpointBuilder().setTenant(ae.getTenant()) + .setNetworkContainment(ae.getNetworkContainment()) + .setEndpointGroup(ae.getEndpointGroup()) + .setAddress(ae.getAddress()) + .setAddressType(ae.getAddressType()) + .setChildEndpoint(ae.getChildEndpoint()) + .setCondition(ae.getCondition()) + .setKey(new AddressEndpointKey(ae.getAddress(), ae.getAddressType(), ae.getContextId(), + ae.getContextType())) + .setParentEndpointChoice(ae.getParentEndpointChoice()) + .setTimestamp(ae.getTimestamp()) + .setContextId(ae.getContextId()) + .setContextType(ae.getContextType()) + .setTenant(ae.getTenant()); + + for (Map.Entry entry : registeredRenderers.entrySet()) { + try { + Map.Entry>, Augmentation> augmentationEntry = + entry.getValue().buildAddressEndpointAugmentation(ae); + if (augmentationEntry != null) { + builder.addAugmentation(augmentationEntry.getKey(), augmentationEntry.getValue()); + } + } catch (Exception e) { + LOG.warn("AddressEndpoint Augmentation error while processing " + entry.getKey() + ". Reason: ", e); + } + } + return builder; + } + + /** + * Unregister an endpoint or endpoints from the registry. + * + * @param input Endpoint/endpoints to unregister + */ + @Override + public Future> unregisterEndpoint(UnregisterEndpointInput input) { + WriteTransaction t = dataProvider.newWriteOnlyTransaction(); + + List addressEndpoints = input.getAddressEndpointUnreg(); + for (AddressEndpointUnreg ae : nullToEmpty(addressEndpoints)) { + AddressEndpointKey key = new AddressEndpointKey(ae.getAddress(), ae.getAddressType(), ae.getContextId(), + ae.getContextType()); + t.delete(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(key)); + } + + List endpoints = input.getContainmentEndpointUnreg(); + for (ContainmentEndpointUnreg ce : nullToEmpty(endpoints)) { + ContainmentEndpointKey key = new ContainmentEndpointKey(ce.getContextId(), ce.getContextType()); + t.delete(LogicalDatastoreType.OPERATIONAL, IidFactory.containmentEndpointIid(key)); + } + + ListenableFuture r = t.submit(); + return Futures.transform(r, futureTrans); + } + + @Override + public void close() throws Exception { + if (rpcRegistration != null) { + rpcRegistration.close(); + } + } + + private List nullToEmpty(@Nullable List list) { + return list == null ? Collections.emptyList() : list; + } + +} diff --git a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java index 0855c152d..7cb3f3be3 100644 --- a/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java +++ b/groupbasedpolicy/src/main/java/org/opendaylight/groupbasedpolicy/util/IidFactory.java @@ -11,6 +11,10 @@ package org.opendaylight.groupbasedpolicy.util; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.AddressEndpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.ContainmentEndpoints; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionDefinitionId; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierDefinitionId; @@ -33,6 +37,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.r import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Key; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.SubjectFeatureDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Tenants; import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef; @@ -359,4 +365,22 @@ public class IidFactory { .build(); } + public static InstanceIdentifier containmentEndpointIid(ContainmentEndpointKey key) { + return InstanceIdentifier + .builder( + org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints.class) + .child(ContainmentEndpoints.class) + .child(ContainmentEndpoint.class, key) + .build(); + } + + public static InstanceIdentifier addressEndpointIid(AddressEndpointKey addressEndpointKey) { + return InstanceIdentifier + .builder( + org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.Endpoints.class) + .child(AddressEndpoints.class) + .child(AddressEndpoint.class, addressEndpointKey) + .build(); + } + } diff --git a/groupbasedpolicy/src/main/yang/groupbasedpolicy-cfg.yang b/groupbasedpolicy/src/main/yang/groupbasedpolicy-cfg.yang index f67909879..5af40ce00 100644 --- a/groupbasedpolicy/src/main/yang/groupbasedpolicy-cfg.yang +++ b/groupbasedpolicy/src/main/yang/groupbasedpolicy-cfg.yang @@ -31,6 +31,14 @@ module groupbasedpolicy-cfg { config:java-class "org.opendaylight.groupbasedpolicy.api.EpRendererAugmentationRegistry"; } + identity base-endpoint-renderer-augmentation-registry { + description + "base-endpoint-renderer-augmentation-registry service definition"; + + base "config:service-type"; + config:java-class "org.opendaylight.groupbasedpolicy.api.BaseEndpointRendererAugmentationRegistry"; + } + identity policy-validator-registry { description "policy-validator-registry service definition"; @@ -61,6 +69,13 @@ module groupbasedpolicy-cfg { config:java-name-prefix EpRendererAugmentationRegistryImpl; } + identity base-endpoint-renderer-augmentation-registry-impl { + base "config:module-type"; + + config:provided-service base-endpoint-renderer-augmentation-registry; + config:java-name-prefix BaseEndpointRendererAugmentationRegistryImpl; + } + identity statistics-manager-impl { base "config:module-type"; @@ -141,6 +156,28 @@ module groupbasedpolicy-cfg { } } } + case base-endpoint-renderer-augmentation-registry-impl { + when "/config:modules/config:module/config:type = 'base-endpoint-renderer-augmentation-registry-impl'"; + + //wires in the data-broker service + container data-broker { + uses config:service-ref { + refine type { + mandatory true; + config:required-identity mdsal:binding-async-data-broker; + } + } + } + //RPC Registry + container rpc-registry { + uses config:service-ref { + refine type { + mandatory true; + config:required-identity mdsal:binding-rpc-registry; + } + } + } + } case statistics-manager-impl { when "/config:modules/config:module/config:type = 'statistics-manager-impl'"; diff --git a/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistryTest.java b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistryTest.java new file mode 100644 index 000000000..c26576f57 --- /dev/null +++ b/groupbasedpolicy/src/test/java/org/opendaylight/groupbasedpolicy/base_endpoint/BaseEndpointRpcRegistryTest.java @@ -0,0 +1,168 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.groupbasedpolicy.base_endpoint; + +import com.google.common.util.concurrent.CheckedFuture; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry; +import org.opendaylight.groupbasedpolicy.api.BaseEndpointRendererAugmentation; +import org.opendaylight.groupbasedpolicy.base_endpoint.BaseEndpointRpcRegistry; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointRegKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointRegKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.*; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.RegisterEndpointInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.UnregisterEndpointInputBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.common.endpoint.fields.NetworkContainmentBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.AddressEndpointRegBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.register.endpoint.input.ContainmentEndpointRegBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.AddressEndpointUnregBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.unregister.endpoint.input.ContainmentEndpointUnregBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.L2FloodDomain; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.MacAddressType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev160427.Subnet; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.AddressType; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +import java.util.ArrayList; + +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; +import static org.mockito.internal.verification.VerificationModeFactory.times; + +public class BaseEndpointRpcRegistryTest { + + private static final String MAC_ADDRESS = "01:23:45:67:89:AB"; + private static final String TENANT = "admin"; + private static final String DOMAIN = "test.domain"; + private static final String CONTEXT_ID = "testContext"; + private static final String FLOOD_DOMAIN = "testFloodDomain"; + + private DataBroker dataProvider; + private BaseEndpointRendererAugmentation baseEndpointRendererAugmentation; + private BaseEndpointRpcRegistry baseEndpointRpcRegistry; + + @Before + public void init() { + dataProvider = mock(DataBroker.class); + baseEndpointRendererAugmentation = mock(BaseEndpointRendererAugmentation.class); + WriteTransaction wt = newWriteTransactionMock(); + RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class); + + baseEndpointRpcRegistry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry); + } + + @Test + public void testConstructor() throws Exception { + RpcProviderRegistry rpcRegistry = mock(RpcProviderRegistry.class); + BaseEndpointRpcRegistry registry = new BaseEndpointRpcRegistry(dataProvider, rpcRegistry); + registry.close(); + } + + @Test + public void testRegister() throws Exception { + baseEndpointRpcRegistry.register(baseEndpointRendererAugmentation); + Assert.assertEquals(1, BaseEndpointRpcRegistry.registeredRenderers.size()); + + baseEndpointRpcRegistry.unregister(baseEndpointRendererAugmentation); + Assert.assertEquals(0, BaseEndpointRpcRegistry.registeredRenderers.size()); + } + + @Test + public void testRegisterEndpoint() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + baseEndpointRpcRegistry.registerEndpoint(createRegisterEndpointInputVariablesForTest()); + + verify(wt, times(2)).put(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), + any(DataObject.class), eq(true)); + } + + private RegisterEndpointInput createRegisterEndpointInputVariablesForTest() throws Exception { + RegisterEndpointInputBuilder registerEndpointInputBuilder = new RegisterEndpointInputBuilder(); + long timestamp = System.currentTimeMillis(); + + registerEndpointInputBuilder.setAddressEndpointReg(new ArrayList<>()); + registerEndpointInputBuilder.setContainmentEndpointReg(new ArrayList<>()); + + registerEndpointInputBuilder.getAddressEndpointReg().add( + new AddressEndpointRegBuilder().setTimestamp(timestamp) + .setContextId(new ContextId(CONTEXT_ID)) + .setContextType(L2FloodDomain.class) + .setTenant(new TenantId(TENANT)) + .setAddress(MAC_ADDRESS) + .setAddressType(MacAddressType.class) + .setAddressType(AddressType.class) + .setNetworkContainment(new NetworkContainmentBuilder() + .setNetworkDomainId(new NetworkDomainId(DOMAIN)).setNetworkDomainType(Subnet.class).build()) + .setKey(new AddressEndpointRegKey(MAC_ADDRESS,MacAddressType.class, new ContextId(CONTEXT_ID), L2FloodDomain.class)) + .setTimestamp(timestamp).build()); + + registerEndpointInputBuilder.getContainmentEndpointReg().add( + new ContainmentEndpointRegBuilder() + .setTimestamp(timestamp) + .setContextId(new L2FloodDomainId(FLOOD_DOMAIN)) + .setContextType(L2FloodDomain.class) + .setTenant(new TenantId(TENANT)) + .setNetworkContainment(new NetworkContainmentBuilder() + .setNetworkDomainId(new NetworkDomainId(DOMAIN)).setNetworkDomainType(Subnet.class).build()) + .setKey(new ContainmentEndpointRegKey(new L2FloodDomainId(FLOOD_DOMAIN),L2FloodDomain.class)) + .build()); + + return registerEndpointInputBuilder.build(); + } + + @Test + public void testUnregisterEndpoint() throws Exception { + WriteTransaction wt = newWriteTransactionMock(); + + UnregisterEndpointInput unregisterEndpointInput = unregisterEndpointInput(); + + baseEndpointRpcRegistry.unregisterEndpoint(unregisterEndpointInput); + + verify(wt, times(2)).delete(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class)); + } + + private UnregisterEndpointInput unregisterEndpointInput() { + UnregisterEndpointInputBuilder builder = new UnregisterEndpointInputBuilder(); + + builder.setAddressEndpointUnreg(new ArrayList<>()); + builder.setContainmentEndpointUnreg(new ArrayList<>()); + + builder.getAddressEndpointUnreg().add(new AddressEndpointUnregBuilder().setContextId(new ContextId(CONTEXT_ID)) + .setContextType(L2FloodDomain.class) + .setAddress(MAC_ADDRESS) + .setAddressType(MacAddressType.class) + .build()); + + builder.getContainmentEndpointUnreg().add(new ContainmentEndpointUnregBuilder() + .setContextId(new ContextId(CONTEXT_ID)).setContextType(L2FloodDomain.class).build()); + + return builder.build(); + } + + private WriteTransaction newWriteTransactionMock() { + WriteTransaction wt = mock(WriteTransaction.class); + CheckedFuture f = mock(CheckedFuture.class); + + when(dataProvider.newWriteOnlyTransaction()).thenReturn(wt); + when(wt.submit()).thenReturn(f); + return wt; + } + +}