From: Devin Avery Date: Wed, 9 Jul 2014 20:36:32 +0000 (+0000) Subject: Merge "Deprecate model-flow-management as it was never used and is confusing people." X-Git-Tag: release/helium~501 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=46b6284fd4dca701ca6927b4eac4d1c9f16710ca;hp=d954f5cff430beb22e571b44648a56a039c6c120 Merge "Deprecate model-flow-management as it was never used and is confusing people." --- diff --git a/features/extras/pom.xml b/features/extras/pom.xml new file mode 100644 index 0000000000..4563190b9f --- /dev/null +++ b/features/extras/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + org.opendaylight.controller + commons.opendaylight + 1.4.2-SNAPSHOT + ../../opendaylight/commons/opendaylight + + extras-features + kar + ${project.artifactId} + Base Features POM + + features.xml + 3.9.2.Final + + + + + true + src/main/resources + + + + + org.apache.karaf.tooling + karaf-maven-plugin + ${karaf.version} + true + + + features-create-kar + + features-create-kar + + + ${project.build.directory}/classes/${features.file} + + + + + + + org.apache.maven.plugins + maven-resources-plugin + + + filter + + resources + + generate-resources + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + attach-artifacts + + attach-artifact + + package + + + + ${project.build.directory}/classes/${features.file} + xml + features + + + + + + + + + diff --git a/features/extras/src/main/resources/features.xml b/features/extras/src/main/resources/features.xml new file mode 100644 index 0000000000..3be66d9a6e --- /dev/null +++ b/features/extras/src/main/resources/features.xml @@ -0,0 +1,34 @@ + + + + + mvn:org.osgi/org.osgi.compendium/${osgi.compendium.version} + + + + mvn:org.scala-lang/scala-library/${scala.version}.${scala.micro.version} + mvn:org.scala-lang/scala-reflect/${scala.version}.${scala.micro.version} + + + + odl-extras-scala + mvn:com.typesafe/config/${typesafe.config.version} + mvn:com.typesafe.akka/akka-actor_${scala.version}/${akka.version} + mvn:com.typesafe.akka/akka-slf4j_${scala.version}/${akka.version} + mvn:com.typesafe.akka/akka-osgi_${scala.version}/${akka.version} + + + + odl-extras-akka-system + wrap:mvn:org.uncommons.maths/uncommons-maths/${uncommons.maths.version} + mvn:com.google.protobuf/protobuf-java/${protobuf.version} + wrap:mvn:io.netty/netty/${netty3.version} + mvn:com.typesafe.akka/akka-remote_${scala.version}/${akka.version} + mvn:com.typesafe.akka/akka-cluster_${scala.version}/${akka.version} + + + + wrap:mvn:org.iq80.leveldb/leveldb/${leveldb.version} + wrap:mvn:org.fusesource.leveldbjni/leveldbjni-all/${leveldbjni.version} + + diff --git a/opendaylight/commons/opendaylight/pom.xml b/opendaylight/commons/opendaylight/pom.xml index 61d7f87e32..bf84183b75 100644 --- a/opendaylight/commons/opendaylight/pom.xml +++ b/opendaylight/commons/opendaylight/pom.xml @@ -18,7 +18,7 @@ - 2.3.2 + 2.3.4 1.0.0 0.4.2-SNAPSHOT 0.0.1-SNAPSHOT @@ -62,6 +62,7 @@ 2.3.2 0.1.2-SNAPSHOT 0.5.2-SNAPSHOT + 1.4 0.2.5-SNAPSHOT 0.4.3-SNAPSHOT 0.4.3-SNAPSHOT @@ -111,10 +112,13 @@ 1.0.0-SNAPSHOT 3.0.0 3.0.1 + 0.7 + 1.8 1.0.0 1.0.9 0.4.2-SNAPSHOT 3.0.5 + 1.9.4 1.1-SNAPSHOT 0.2.5-SNAPSHOT 0.0.3-SNAPSHOT @@ -128,7 +132,10 @@ 1.4.2-SNAPSHOT 2013.08.27.4-SNAPSHOT 0.0.2-SNAPSHOT + 1.1.6 + 1.1.6 1.0-alpha-2 + 2.5.0 0.5.0-SNAPSHOT 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT @@ -142,8 +149,10 @@ 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT - 2.11 + 2.10 + 4 0.4.2-SNAPSHOT + 1.2.4 dav:http://nexus.opendaylight.org/content/sites/site ${user.name}-private-view https://sonar.opendaylight.org/ @@ -156,6 +165,7 @@ 3.1.4.RELEASE 3.1.3.RELEASE 3.1.3.RELEASE + org.openflow.openflowj,net.sf.jung2 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT 0.5.1-SNAPSHOT @@ -169,6 +179,8 @@ 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT 0.4.2-SNAPSHOT + 1.2.0 + 1.2.2 0.4.2-SNAPSHOT 0.0.2-SNAPSHOT 0.4.2-SNAPSHOT diff --git a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/pom.xml b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/pom.xml index 6ddb1306c5..ae041f4585 100644 --- a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/pom.xml +++ b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/pom.xml @@ -20,6 +20,7 @@ org.opendaylight.controller config-api + ${config.version} diff --git a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__-impl.yang b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__-impl.yang index 8c1dab1495..639610f3d5 100644 --- a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__-impl.yang +++ b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__-impl.yang @@ -1,4 +1,3 @@ -// vi: set smarttab et sw=4 tabstop=4: module ${module-name}-${module-implementation-name} { yang-version 1; diff --git a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__.yang b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__.yang index 2afc91e8e3..f77df5a22d 100644 --- a/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__.yang +++ b/opendaylight/config/config-module-archetype/src/main/resources/archetype-resources/src/main/yang/__module-name__.yang @@ -1,4 +1,3 @@ -// vi: set smarttab et sw=4 tabstop=4: module ${module-name} { yang-version 1; diff --git a/opendaylight/config/config-util/pom.xml b/opendaylight/config/config-util/pom.xml index b6e2efcd45..7429f4d574 100644 --- a/opendaylight/config/config-util/pom.xml +++ b/opendaylight/config/config-util/pom.xml @@ -8,7 +8,7 @@ .. config-util - jar + bundle ${project.artifactId} @@ -42,6 +42,15 @@ 1 + + org.apache.felix + maven-bundle-plugin + + + org.opendaylight.controller.config.util + + + diff --git a/opendaylight/config/feature/src/main/resources/features.xml b/opendaylight/config/feature/src/main/resources/features.xml index 81972c3661..a84a74305f 100644 --- a/opendaylight/config/feature/src/main/resources/features.xml +++ b/opendaylight/config/feature/src/main/resources/features.xml @@ -1,27 +1,28 @@ - - odl-config-subsystem - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.2.0 http://karaf.apache.org/xmlns/features/v1.2.0"> + + odl-config-subsystem + - - yangtools-concepts - yangtools-binding - yangtools-binding-generator - odl-mdsal-commons - mvn:org.opendaylight.controller/config-api/${project.version} - mvn:org.opendaylight.controller/config-manager/${project.version} - mvn:org.opendaylight.controller/yang-jmx-generator/${project.version} - mvn:org.opendaylight.controller/config-persister-api/${project.version} - mvn:org.opendaylight.controller/config-persister-file-xml-adapter/${project.version} - mvn:org.opendaylight.controller/config-persister-directory-xml-adapter/${project.version} - mvn:org.opendaylight.controller/shutdown-api/${project.version} - mvn:org.opendaylight.controller/shutdown-impl/${project.version} - mvn:org.osgi/org.osgi.core/${osgi.core.version} - wrap:mvn:com.google.guava/guava/${guava.version} - mvn:org.javassist/javassist/${javassist.version} - + + yangtools-concepts + yangtools-binding + yangtools-binding-generator + odl-mdsal-commons + mvn:org.opendaylight.controller/config-api/${project.version} + mvn:org.opendaylight.controller/config-util/${project.version} + mvn:org.opendaylight.controller/config-manager/${project.version} + mvn:org.opendaylight.controller/yang-jmx-generator/${project.version} + mvn:org.opendaylight.controller/config-persister-api/${project.version} + mvn:org.opendaylight.controller/config-persister-file-xml-adapter/${project.version} + mvn:org.opendaylight.controller/config-persister-directory-xml-adapter/${project.version} + mvn:org.opendaylight.controller/shutdown-api/${project.version} + mvn:org.opendaylight.controller/shutdown-impl/${project.version} + mvn:org.osgi/org.osgi.core/${osgi.core.version} + wrap:mvn:com.google.guava/guava/${guava.version} + mvn:org.javassist/javassist/${javassist.version} + \ No newline at end of file diff --git a/opendaylight/config/pom.xml b/opendaylight/config/pom.xml index 76fbd7f6e5..a8a4c87a4e 100644 --- a/opendaylight/config/pom.xml +++ b/opendaylight/config/pom.xml @@ -1,5 +1,4 @@ - 4.0.0 diff --git a/opendaylight/distribution/opendaylight-karaf/pom.xml b/opendaylight/distribution/opendaylight-karaf/pom.xml index d8f6ba1526..221bfa78e8 100644 --- a/opendaylight/distribution/opendaylight-karaf/pom.xml +++ b/opendaylight/distribution/opendaylight-karaf/pom.xml @@ -54,6 +54,13 @@ + + org.opendaylight.controller + extras-features + ${project.version} + kar + runtime + org.opendaylight.controller config-features diff --git a/opendaylight/distribution/opendaylight/pom.xml b/opendaylight/distribution/opendaylight/pom.xml index ae99697105..d238ee75cc 100644 --- a/opendaylight/distribution/opendaylight/pom.xml +++ b/opendaylight/distribution/opendaylight/pom.xml @@ -842,6 +842,10 @@ org.opendaylight.controller config-manager + + org.opendaylight.controller + config-util + org.opendaylight.controller config-netconf-connector diff --git a/opendaylight/md-sal/model/model-flow-base/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/flow/types/port/rev130925/PortNumberBuilder.java b/opendaylight/md-sal/model/model-flow-base/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/flow/types/port/rev130925/PortNumberBuilder.java new file mode 100644 index 0000000000..ff78a7478d --- /dev/null +++ b/opendaylight/md-sal/model/model-flow-base/src/main/java/org/opendaylight/yang/gen/v1/urn/opendaylight/flow/types/port/rev130925/PortNumberBuilder.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2014 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.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925; +import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.CommonPort.PortNumber; + + +public class PortNumberBuilder { + + public static PortNumber getDefaultInstance(java.lang.String defaultValue) { + try { + long uint32 = Long.parseLong(defaultValue); + return new PortNumber(uint32); + } catch(NumberFormatException e){ + return new PortNumber(defaultValue); + } + } + +} diff --git a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareConsumer.java b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareConsumer.java index 4327451d21..bcbd6879d0 100644 --- a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareConsumer.java +++ b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareConsumer.java @@ -10,17 +10,64 @@ package org.opendaylight.controller.sal.binding.api; import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext; /** - * - * Defines the component of controller and supplies additional metadata. A - * component of the controller or application supplies a concrete implementation - * of this interface. - * - * A user-implemented component (application) which facilitates the SAL and SAL - * services to access infrastructure services or providers' functionality. - * - * - * - */ +* +* A developer implemented component that gets registered with the Broker. +* +* Semantically, a consumer may: +* +*
    +*
  1. Subscribe for Notifications
  2. +*
  3. Invoke RPCs
  4. +*
  5. Read from either the operational or config data tree
  6. +*
  7. Write to the config data tree
  8. +*
+* If you need to: +*
    +*
  1. Emit Notifications
  2. +*
  3. Provide the implementation of RPCs
  4. +*
  5. Write to the operational data tree
  6. +*
+* +* Consider using a BindingAwareProvider +* +* Examples: +* +* To get a NotificationService: +* +* {code +* public void onSessionInitiated(ProviderContext session) { +* NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class); +* notificationService.publish(notification) +* } +* where notification is an instance of a modeled Notification. +* For more information on sending notifications via the NotificationProviderService +* @see org.opendaylight.controller.sal.binding.api.NotificationProviderService +* +* +* A consumer can *invoke* and RPC ( ie, call foo(fooArgs)) but it cannot register an RPC +* implementation with the MD-SAL that others can invoke(call). +* To get an invokable RPC: +* +* {code +* public void onSessionInitiated(ProviderContext session) { +* MyService rpcFlowSalService = session.getRpcService(MyService.class); +* } +* +* Where MyService.class is a Service interface generated from a yang model with RPCs modeled in it. The returned +* rpcFlowSalService can be used like any other object by invoking its methods. Note, nothing special needs to be done +* for RoutedRPCs. They just work. +* +* To get a DataBroker to allow access to the data tree: +* +* {code +* public void onSessionInitiated(final ProviderContext session) { +* DataBroker databroker = session.getSALService(BindingDataBroker.class); +* } +* } +* @see org.opendaylight.controller.md.sal.common.api.data.BindingDataBroker +* for more info on using the DataBroker. +* +*/ public interface BindingAwareConsumer { /** diff --git a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareProvider.java b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareProvider.java index 0812e5f53c..cb26cad2f3 100644 --- a/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareProvider.java +++ b/opendaylight/md-sal/sal-binding-api/src/main/java/org/opendaylight/controller/sal/binding/api/BindingAwareProvider.java @@ -15,37 +15,131 @@ import org.opendaylight.yangtools.yang.binding.RpcService; /** * - * Defines the component of controller and supplies additional metadata. A - * component of the controller or application supplies a concrete implementation - * of this interface. + * A developer implemented component that gets registered with the Broker. * + * Semantically, a provider may: + * + *
    + *
  1. Emit Notifications
  2. + *
  3. Provide the implementation of RPCs
  4. + *
  5. Write to the operational data tree
  6. + *
+ * + * If a class is not doing at least one of those three, consider using + * a BindingAwareConsumer instead: + * @see org.opendaylight.controller.sal.binding.api.BindingAwareConsumer + * + *

+ * + *In addition, a BindingAwareProvider can in pursuit of its goals: + * + *

    + *
  1. Subscribe for Notifications
  2. + *
  3. Invoke RPCs
  4. + *
  5. Read from either the operational or config data tree
  6. + *
  7. Write to the config data tree
  8. + *
+ * (All of the above are things a Consumer can also do). + * + *

+ * + * Examples: + * + *

+ * + * To get a NotificationService: + * + * {code + * public void onSessionInitiated(ProviderContext session) { + * NotificationProviderService notificationService = session.getSALService(NotificationProviderService.class); + * } + * For more information on sending notifications via the NotificationProviderService + * @see org.opendaylight.controller.sal.binding.api.NotificationProviderService + * + * To register an RPC implementation: + * + * {code + * public void onSessionInitiated(ProviderContext session) { + * RpcRegistration registration = session.addRpcImplementation(MyService.class, myImplementationInstance); + * } + * + *

+ * + * Where MyService.class is a Service interface generated from a yang model with RPCs modeled in it and myImplementationInstance + * is an instance of a class that implements MyService. + * + * To register a Routed RPC Implementation: + * {code + * public void onSessionInitiated(ProviderContext session) { + * RoutedRpcRegistration flowRegistration = session.addRoutedRpcImplementation(SalFlowService.class, salFlowServiceImplementationInstance); + flowRegistration.registerPath(NodeContext.class, nodeInstanceId); + * } + * } + * + * Where SalFlowService.class is a Service interface generated from a yang model with RPCs modeled in it and salFlowServiceImplementationInstance is an instance + * of a class that implements SalFlowService. *

- * A user-implemented component (application) which facilitates the SAL and SAL - * services to access infrastructure services and to provide functionality to - * {@link Consumer}s and other providers. + * The line: + * {code + * flowRegistration.registerPath(NodeContext.class, nodeInstanceId); + * } + * Is indicating that the RPC implementation is registered to handle RPC invocations that have their NodeContext pointing to the node with instance id nodeInstanceId. + * This bears a bit of further explanation. RoutedRPCs can be 'routed' to an implementation based upon 'context'. 'context' is a pointer (instanceId) to some place + * in the data tree. In this example, the 'context' is a pointer to a Node. In this way, a provider can register its ability to provide a service for a particular + * Node, but not *all* Nodes. The Broker routes the RPC by 'context' to the correct implementation, without the caller having to do extra work. Because of this when + * a RoutedRPC is registered, it needs to also be able to indicate for which 'contexts' it is providing an implementation. + * + * An example of a Routed RPC would be an updateFlow(node, flow) that would be routed based on node to the provider which had registered to provide + * it *for that node*. + * + *

* + * To get a DataBroker to allow access to the data tree: + * + * {code + * public void onSessionInitiated(final ProviderContext session) { + * DataBroker databroker = session.getSALService(BindingDataBroker.class); + * } + * } + * @see org.opendaylight.controller.md.sal.common.api.data.BindingDataBroker + * for more info on using the DataBroker. * */ public interface BindingAwareProvider { /** - * Returns a set of provided implementations of YANG modules and their rpcs. + * @deprecated * + * This interface was originally intended to solve problems of how to get Implementations + * of functionality from a provider, but that is no longer necessary because the Provider + * Registers RPCs in onSessionInitiated. * - * @return Set of provided implementation of YANG modules and their Rpcs + * Recommend: + * {code + * public Collection getImplementations() { + * return Collections.emptySet(); + * } + * } */ + @Deprecated Collection getImplementations(); /** - * Gets a set of implementations of provider functionality to be registered - * into system during the provider registration to the SAL. + * @deprecated * - *

- * This method is invoked by {@link Broker#registerProvider(Provider)} to - * learn the initial provided functionality + * This interface was originally intended to solve problems of how to get Functionality + * a provider could provide, but that is no longer necessary because the Provider + * Registers RPCs in onSessionInitiated. + * + * Recommend: + * {code + * public Collection getFunctionality() { + * return Collections.emptySet(); + * } + * } * - * @return Set of provider's functionality. */ + @Deprecated Collection getFunctionality(); /** @@ -58,12 +152,38 @@ public interface BindingAwareProvider { * * */ + @Deprecated public interface ProviderFunctionality { } - + /** + * Callback signaling initialization of the consumer session to the SAL. + * + * The consumer MUST use the session for all communication with SAL or + * retrieving SAL infrastructure services. + * + * This method is invoked by + * {@link BindingAwareBroker#registerProvider(BindingAwareProvider)} + * + * @param session Unique session between consumer and SAL. + */ void onSessionInitiated(ProviderContext session); + /* + * @deprecated + * + * A provider was at one point considered an extension of a consumer, thus this + * call. It is deprecated and the @see org.opendaylight.controller.sal.binding.api.BindingAwareConsumer#onSessionInitiated + * used, or you should simply use {@link #onSessionInitiated(ProviderContext)} + * + * Recommend: + * {code + * public final void onSessionInitialized(ConsumerContext session) { + * // NOOP - as method is deprecated + * } + * } + */ + @Deprecated void onSessionInitialized(ConsumerContext session); } diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java index fe8c4a151c..6e4b2d8d99 100644 --- a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/sal/binding/impl/connect/dom/BindingIndependentConnector.java @@ -602,7 +602,7 @@ public class BindingIndependentConnector implements // final Optional> rpcInterface = mappingService.getRpcServiceClassFor( name.getNamespace().toString(), name.getFormattedRevision()); if (rpcInterface.isPresent()) { - getRpcForwarder(rpcInterface.get(), null).registerToBidningBroker(); + getRpcForwarder(rpcInterface.get(), null).registerToBindingBroker(); } } @@ -621,6 +621,7 @@ public class BindingIndependentConnector implements // private final WeakHashMap strategiesByMethod = new WeakHashMap<>(); private final RpcService proxy; private ObjectRegistration forwarderRegistration; + private boolean registrationInProgress = false; public DomToBindingRpcForwarder(final Class service) { this.rpcServiceType = new WeakReference>(service); @@ -680,7 +681,8 @@ public class BindingIndependentConnector implements // * */ public void registerToDOMBroker() { - if(forwarderRegistration == null) { + if(!registrationInProgress && forwarderRegistration == null) { + registrationInProgress = true; CompositeObjectRegistrationBuilder builder = CompositeObjectRegistration.builderFor(this); try { for (QName rpc : supportedRpcs) { @@ -690,6 +692,7 @@ public class BindingIndependentConnector implements // LOG.error("Could not forward Rpcs of type {}", rpcServiceType.get(), e); } this.forwarderRegistration = builder.toInstance(); + registrationInProgress = false; } } @@ -801,12 +804,15 @@ public class BindingIndependentConnector implements // * creating forwarding loop. * */ - public void registerToBidningBroker() { - if(forwarderRegistration == null) { + public void registerToBindingBroker() { + if(!registrationInProgress && forwarderRegistration == null) { try { + registrationInProgress = true; this.forwarderRegistration = baRpcRegistry.addRpcImplementation((Class)rpcServiceType.get(), proxy); } catch (Exception e) { LOG.error("Unable to forward RPCs for {}",rpcServiceType.get(),e); + } finally { + registrationInProgress = false; } } } diff --git a/opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java b/opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java index 5ede600d97..c389618f2e 100644 --- a/opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java +++ b/opendaylight/md-sal/sal-binding-it/src/main/java/org/opendaylight/controller/test/sal/binding/it/TestHelper.java @@ -54,6 +54,7 @@ public class TestHelper { mavenBundle("io.netty", "netty-transport").versionAsInProject(), // mavenBundle(CONTROLLER, "config-manager").versionAsInProject(), // // + mavenBundle(CONTROLLER, "config-util").versionAsInProject(), // // mavenBundle("commons-io", "commons-io").versionAsInProject(), // mavenBundle(CONTROLLER, "config-manager").versionAsInProject(), // mavenBundle(CONTROLLER, "yang-jmx-generator").versionAsInProject(), // diff --git a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java index 21f94474f8..f9e6239bed 100644 --- a/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java +++ b/opendaylight/md-sal/sal-netconf-connector/src/main/java/org/opendaylight/controller/sal/connect/netconf/schema/mapping/NetconfMessageTransformer.java @@ -55,7 +55,7 @@ public class NetconfMessageTransformer implements MessageTransformer notifications = ctx.getNotifications(); final Document document = message.getDocument(); - return XmlDocumentUtils.notificationToDomNodes(document, Optional.fromNullable(notifications)); + return XmlDocumentUtils.notificationToDomNodes(document, Optional.fromNullable(notifications), ctx); } @Override diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java new file mode 100644 index 0000000000..521a55df68 --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/java/org/opendaylight/controller/sal/connect/netconf/NetconfToNotificationTest.java @@ -0,0 +1,64 @@ +package org.opendaylight.controller.sal.connect.netconf; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; + +import java.io.InputStream; +import java.util.Collections; +import java.util.List; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilderFactory; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.controller.sal.connect.netconf.schema.mapping.NetconfMessageTransformer; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; +import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser; +import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.w3c.dom.Document; + +/** + * @author Lukas Sedlak + */ +public class NetconfToNotificationTest { + + NetconfMessageTransformer messageTransformer; + + NetconfMessage userNotification; + + @Before + public void setup() throws Exception { + final List modelsToParse = Collections.singletonList(getClass().getResourceAsStream("/schemas/user-notification.yang")); + final YangContextParser parser = new YangParserImpl(); + final Set modules = parser.parseYangModelsFromStreams(modelsToParse); + assertTrue(!modules.isEmpty()); + final SchemaContext schemaContext = parser.resolveSchemaContext(modules); + assertNotNull(schemaContext); + + messageTransformer = new NetconfMessageTransformer(); + messageTransformer.onGlobalContextUpdated(schemaContext); + + final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + InputStream notifyPayloadStream = getClass().getResourceAsStream("/notification-payload.xml"); + assertNotNull(notifyPayloadStream); + + final Document doc = XmlUtil.readXmlToDocument(notifyPayloadStream); + assertNotNull(doc); + userNotification = new NetconfMessage(doc); + } + + @Test + public void test() throws Exception { + final CompositeNode root = messageTransformer.toNotification(userNotification); + + assertNotNull(root); + assertEquals(6, root.size()); + assertEquals("user-visited-page", root.getKey().getLocalName()); + } +} diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml new file mode 100644 index 0000000000..837fcc1a70 --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/notification-payload.xml @@ -0,0 +1,14 @@ + +2014-07-08T11:20:48UTC + + ui:public-user + 172.23.29.104 + 00:11:00:ff:dd:02 + Chrome 35.0.1916.153 m + + Slovakia + UTC/GMT+2 + + 2014-07-08 11:20:48 + + \ No newline at end of file diff --git a/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang new file mode 100644 index 0000000000..50d220689f --- /dev/null +++ b/opendaylight/md-sal/sal-netconf-connector/src/test/resources/schemas/user-notification.yang @@ -0,0 +1,56 @@ +module user-notification { + yang-version 1; + namespace "org:opendaylight:notification:test:ns:yang:user-notification"; + prefix "user"; + + organization "Cisco Systems"; + contact "Lukas Sedlak"; + description "Test model for testing notifications"; + + revision "2014-07-08" { + description "Initial revision"; + } + + identity user-identity { + description "Identity of user incoming to Web Page"; + } + + identity public-user { + base user-identity; + description "Identity of random public non-registered user"; + } + + notification user-visited-page { + leaf incoming-user { + type identityref { + base "user-identity"; + } + } + + leaf ip-address { + type string; + } + + leaf mac { + type string; + } + + leaf browser-id { + type string; + } + + container region { + leaf name { + type string; + } + + leaf time-zone { + type string; + } + } + + leaf visiting-date { + type string; + } + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/samples/toaster-consumer/pom.xml b/opendaylight/md-sal/samples/toaster-consumer/pom.xml index f6a8306a98..6e720299d8 100644 --- a/opendaylight/md-sal/samples/toaster-consumer/pom.xml +++ b/opendaylight/md-sal/samples/toaster-consumer/pom.xml @@ -23,6 +23,10 @@ org.opendaylight.controller config-api + + org.opendaylight.controller + sal-common-util + org.opendaylight.controller sal-binding-api diff --git a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/config/yang/config/kitchen_service/impl/KitchenServiceModule.java b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/config/yang/config/kitchen_service/impl/KitchenServiceModule.java index a23def6262..88281bd593 100644 --- a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/config/yang/config/kitchen_service/impl/KitchenServiceModule.java +++ b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/config/yang/config/kitchen_service/impl/KitchenServiceModule.java @@ -9,6 +9,8 @@ */ package org.opendaylight.controller.config.yang.config.kitchen_service.impl; +import java.util.concurrent.Future; + import org.opendaylight.controller.sample.kitchen.api.EggsType; import org.opendaylight.controller.sample.kitchen.api.KitchenService; import org.opendaylight.controller.sample.kitchen.impl.KitchenServiceImpl; @@ -16,6 +18,7 @@ import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService; import org.opendaylight.yangtools.concepts.Registration; import org.opendaylight.yangtools.yang.binding.NotificationListener; +import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,7 +66,9 @@ public final class KitchenServiceModule extends AbstractKitchenServiceModule { } @Override - public boolean makeBreakfast( final EggsType eggs, final Class toast, final int toastDoneness ) { + public Future> makeBreakfast( final EggsType eggs, + final Class toast, + final int toastDoneness ) { return kitchenService.makeBreakfast( eggs, toast, toastDoneness ); } } diff --git a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/api/KitchenService.java b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/api/KitchenService.java index ef9c122ec1..7e7f342962 100644 --- a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/api/KitchenService.java +++ b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/api/KitchenService.java @@ -1,7 +1,11 @@ package org.opendaylight.controller.sample.kitchen.api; +import java.util.concurrent.Future; + import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType; +import org.opendaylight.yangtools.yang.common.RpcResult; public interface KitchenService { - boolean makeBreakfast( EggsType eggs, Class toast, int toastDoneness ); + Future> makeBreakfast( EggsType eggs, Class toast, + int toastDoneness ); } diff --git a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java index 911a8c87d7..50ae8fd04f 100644 --- a/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java +++ b/opendaylight/md-sal/samples/toaster-consumer/src/main/java/org/opendaylight/controller/sample/kitchen/impl/KitchenServiceImpl.java @@ -1,10 +1,18 @@ package org.opendaylight.controller.sample.kitchen.impl; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; - +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import org.opendaylight.controller.config.yang.config.kitchen_service.impl.KitchenServiceRuntimeMXBean; +import org.opendaylight.controller.sal.common.util.RpcErrors; +import org.opendaylight.controller.sal.common.util.Rpcs; import org.opendaylight.controller.sample.kitchen.api.EggsType; import org.opendaylight.controller.sample.kitchen.api.KitchenService; +import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInput; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.MakeToastInputBuilder; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToastType; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterListener; @@ -12,16 +20,31 @@ import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120 import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterRestocked; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.ToasterService; import org.opendaylight.yang.gen.v1.http.netconfcentral.org.ns.toaster.rev091120.WheatBread; +import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity; +import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; +import com.google.common.util.concurrent.AsyncFunction; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.JdkFutureAdapters; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ListeningExecutorService; +import com.google.common.util.concurrent.MoreExecutors; + public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntimeMXBean, ToasterListener { private static final Logger log = LoggerFactory.getLogger( KitchenServiceImpl.class ); private final ToasterService toaster; + private final ListeningExecutorService executor = + MoreExecutors.listeningDecorator( Executors.newCachedThreadPool() ); + private volatile boolean toasterOutOfBread; public KitchenServiceImpl(ToasterService toaster) { @@ -29,39 +52,102 @@ public class KitchenServiceImpl implements KitchenService, KitchenServiceRuntime } @Override - public boolean makeBreakfast( EggsType eggs, Class toast, int toastDoneness ) { + public Future> makeBreakfast( EggsType eggsType, Class toastType, + int toastDoneness ) { + + // Call makeToast and use JdkFutureAdapters to convert the Future to a ListenableFuture, + // The OpendaylightToaster impl already returns a ListenableFuture so the conversion is + // actually a no-op. + + ListenableFuture> makeToastFuture = JdkFutureAdapters.listenInPoolThread( + makeToast( toastType, toastDoneness ), executor ); + + ListenableFuture> makeEggsFuture = makeEggs( eggsType ); + + // Combine the 2 ListenableFutures into 1 containing a list of RpcResults. + + ListenableFuture>> combinedFutures = + Futures.allAsList( ImmutableList.of( makeToastFuture, makeEggsFuture ) ); + + // Then transform the RpcResults into 1. + + return Futures.transform( combinedFutures, + new AsyncFunction>,RpcResult>() { + @Override + public ListenableFuture> apply( List> results ) + throws Exception { + boolean atLeastOneSucceeded = false; + Builder errorList = ImmutableList.builder(); + for( RpcResult result: results ) { + if( result.isSuccessful() ) { + atLeastOneSucceeded = true; + } + + if( result.getErrors() != null ) { + errorList.addAll( result.getErrors() ); + } + } + + return Futures.immediateFuture( + Rpcs. getRpcResult( atLeastOneSucceeded, errorList.build() ) ); + } + } ); + } + + private ListenableFuture> makeEggs( EggsType eggsType ) { + + return executor.submit( new Callable>() { + + @Override + public RpcResult call() throws Exception { + + // We don't actually do anything here - just return a successful result. + return Rpcs. getRpcResult( true, Collections.emptyList() ); + } + } ); + } + + private Future> makeToast( Class toastType, + int toastDoneness ) { if( toasterOutOfBread ) { log.info( "We're out of toast but we can make eggs" ); - return true; + return Futures.immediateFuture( Rpcs. getRpcResult( true, + Arrays.asList( RpcErrors.getRpcError( "", "partial-operation", null, + ErrorSeverity.WARNING, + "Toaster is out of bread but we can make you eggs", + ErrorType.APPLICATION, null ) ) ) ); } // Access the ToasterService to make the toast. - // We don't actually make the eggs for this example - sorry. - MakeToastInputBuilder toastInput = new MakeToastInputBuilder(); - toastInput.setToasterDoneness( (long) toastDoneness); - toastInput.setToasterToastType( toast ); - try { - RpcResult result = toaster.makeToast( toastInput.build() ).get(); + MakeToastInput toastInput = new MakeToastInputBuilder() + .setToasterDoneness( (long) toastDoneness ) + .setToasterToastType( toastType ) + .build(); + + return toaster.makeToast( toastInput ); + } + @Override + public Boolean makeScrambledWithWheat() { + try { + // This call has to block since we must return a result to the JMX client. + RpcResult result = makeBreakfast( EggsType.SCRAMBLED, WheatBread.class, 2 ).get(); if( result.isSuccessful() ) { - log.info( "makeToast succeeded" ); + log.info( "makeBreakfast succeeded" ); } else { - log.warn( "makeToast failed: " + result.getErrors() ); + log.warn( "makeBreakfast failed: " + result.getErrors() ); } return result.isSuccessful(); + } catch( InterruptedException | ExecutionException e ) { - log.warn( "Error occurred during toast creation" ); + log.warn( "An error occurred while maing breakfast: " + e ); } - return false; - } - @Override - public Boolean makeScrambledWithWheat() { - return makeBreakfast( EggsType.SCRAMBLED, WheatBread.class, 2 ); + return Boolean.FALSE; } /** diff --git a/opendaylight/md-sal/samples/toaster-it/src/test/java/org/opendaylight/controller/sample/toaster/it/ToasterTest.java b/opendaylight/md-sal/samples/toaster-it/src/test/java/org/opendaylight/controller/sample/toaster/it/ToasterTest.java index 907b35475f..30f1762197 100644 --- a/opendaylight/md-sal/samples/toaster-it/src/test/java/org/opendaylight/controller/sample/toaster/it/ToasterTest.java +++ b/opendaylight/md-sal/samples/toaster-it/src/test/java/org/opendaylight/controller/sample/toaster/it/ToasterTest.java @@ -93,13 +93,13 @@ public class ToasterTest { long toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade"); assertEquals(0, toastsMade); - boolean toasts = true; + boolean success = true; // Make toasts using OSGi service - toasts &= kitchenService.makeBreakfast( EggsType.SCRAMBLED, HashBrown.class, 4); - toasts &= kitchenService.makeBreakfast( EggsType.POACHED, WhiteBread.class, 8 ); + success &= kitchenService.makeBreakfast( EggsType.SCRAMBLED, HashBrown.class, 4).get().isSuccessful(); + success &= kitchenService.makeBreakfast( EggsType.POACHED, WhiteBread.class, 8 ).get().isSuccessful(); - Assert.assertTrue("Not all toasts done by " + kitchenService, toasts); + Assert.assertTrue("Not all breakfasts succeeded", success); // Verify toasts made count on provider via JMX/config-subsystem toastsMade = (long) platformMBeanServer.getAttribute(providerOn, "ToastsMade"); diff --git a/opendaylight/md-sal/samples/toaster-it/src/test/resources/logback.xml b/opendaylight/md-sal/samples/toaster-it/src/test/resources/logback.xml index f904f9726e..d76c2d3c22 100644 --- a/opendaylight/md-sal/samples/toaster-it/src/test/resources/logback.xml +++ b/opendaylight/md-sal/samples/toaster-it/src/test/resources/logback.xml @@ -10,7 +10,9 @@ - + + + diff --git a/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java b/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java index ec352e8f51..d2b0f90194 100644 --- a/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java +++ b/opendaylight/md-sal/samples/toaster-provider/src/main/java/org/opendaylight/controller/sample/toaster/provider/OpendaylightToaster.java @@ -9,19 +9,24 @@ package org.opendaylight.controller.sample.toaster.provider; import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; import org.opendaylight.controller.config.yang.config.toaster_provider.impl.ToasterProviderRuntimeMXBean; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.DataChangeListener; +import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.TransactionStatus; import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException; import org.opendaylight.controller.sal.binding.api.NotificationProviderService; import org.opendaylight.controller.sal.common.util.RpcErrors; import org.opendaylight.controller.sal.common.util.Rpcs; @@ -44,7 +49,13 @@ import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Function; +import com.google.common.base.Optional; +import com.google.common.util.concurrent.AsyncFunction; +import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; public class OpendaylightToaster implements ToasterService, ToasterProviderRuntimeMXBean, DataChangeListener, AutoCloseable { @@ -61,10 +72,9 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti private final ExecutorService executor; - // As you will see we are using multiple threads here. Therefore we need to be careful about concurrency. - // In this case we use the taskLock to provide synchronization for the current task. - private volatile Future> currentTask; - private final Object taskLock = new Object(); + // The following holds the Future for the current make toast task. + // This is used to cancel the current toast. + private final AtomicReference> currentMakeToastTask = new AtomicReference<>(); private final AtomicLong amountOfBreadInStock = new AtomicLong( 100 ); @@ -83,7 +93,7 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti public void setDataProvider(final DataBroker salDataProvider) { this.dataProvider = salDataProvider; - updateStatus(); + setToasterStatusUp( null ); } /** @@ -97,22 +107,30 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti if (dataProvider != null) { WriteTransaction t = dataProvider.newWriteOnlyTransaction(); t.delete(LogicalDatastoreType.OPERATIONAL,TOASTER_IID); - t.commit().get(); // FIXME: This call should not be blocking. + ListenableFuture> future = t.commit(); + Futures.addCallback( future, new FutureCallback>() { + @Override + public void onSuccess( RpcResult result ) { + LOG.debug( "Delete Toaster commit result: " + result ); + } + + @Override + public void onFailure( Throwable t ) { + LOG.error( "Delete of Toaster failed", t ); + } + } ); } } - private Toaster buildToaster() { - // We don't need to synchronize on currentTask here b/c it's declared volatile and - // we're just doing a read. - boolean isUp = currentTask == null; + private Toaster buildToaster( ToasterStatus status ) { // note - we are simulating a device whose manufacture and model are // fixed (embedded) into the hardware. // This is why the manufacture and model number are hardcoded. - ToasterBuilder tb = new ToasterBuilder(); - tb.setToasterManufacturer(TOASTER_MANUFACTURER).setToasterModelNumber(TOASTER_MODEL_NUMBER) - .setToasterStatus(isUp ? ToasterStatus.Up : ToasterStatus.Down); - return tb.build(); + return new ToasterBuilder().setToasterManufacturer( TOASTER_MANUFACTURER ) + .setToasterModelNumber( TOASTER_MODEL_NUMBER ) + .setToasterStatus( status ) + .build(); } /** @@ -133,57 +151,148 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti } /** - * RestConf RPC call implemented from the ToasterService interface. + * RPC call implemented from the ToasterService interface that cancels the current + * toast, if any. */ @Override public Future> cancelToast() { - synchronized (taskLock) { - if (currentTask != null) { - currentTask.cancel(true); - currentTask = null; - } + + Future current = currentMakeToastTask.getAndSet( null ); + if( current != null ) { + current.cancel( true ); } + // Always return success from the cancel toast call. - return Futures.immediateFuture(Rpcs. getRpcResult(true, Collections. emptySet())); + return Futures.immediateFuture( Rpcs. getRpcResult( true, + Collections.emptyList() ) ); } /** - * RestConf RPC call implemented from the ToasterService interface. + * RPC call implemented from the ToasterService interface that attempts to make toast. */ @Override public Future> makeToast(final MakeToastInput input) { LOG.info("makeToast: " + input); - synchronized (taskLock) { - if (currentTask != null) { - // return an error since we are already toasting some toast. - LOG.info( "Toaster is already making toast" ); + final SettableFuture> futureResult = SettableFuture.create(); - RpcResult result = Rpcs. getRpcResult(false, null, Arrays.asList( - RpcErrors.getRpcError( "", "in-use", null, ErrorSeverity.WARNING, - "Toaster is busy", ErrorType.APPLICATION, null ) ) ); - return Futures.immediateFuture(result); - } - else if( outOfBread() ) { - RpcResult result = Rpcs. getRpcResult(false, null, Arrays.asList( - RpcErrors.getRpcError( "out-of-stock", "resource-denied", null, null, - "Toaster is out of bread", - ErrorType.APPLICATION, null ) ) ); - return Futures.immediateFuture(result); - } - else { - // Notice that we are moving the actual call to another thread, - // allowing this thread to return immediately. - // The MD-SAL design encourages asynchronus programming. If the - // caller needs to block until the call is - // complete then they can leverage the blocking methods on the - // Future interface. - currentTask = executor.submit(new MakeToastTask(input)); + checkStatusAndMakeToast( input, futureResult ); + + return futureResult; + } + + private List makeToasterOutOfBreadError() { + return Arrays.asList( + RpcErrors.getRpcError( "out-of-stock", "resource-denied", null, null, + "Toaster is out of bread", + ErrorType.APPLICATION, null ) ); + } + + private List makeToasterInUseError() { + return Arrays.asList( + RpcErrors.getRpcError( "", "in-use", null, ErrorSeverity.WARNING, + "Toaster is busy", ErrorType.APPLICATION, null ) ); + } + + private void checkStatusAndMakeToast( final MakeToastInput input, + final SettableFuture> futureResult ) { + + // Read the ToasterStatus and, if currently Up, try to write the status to Down. + // If that succeeds, then we essentially have an exclusive lock and can proceed + // to make toast. + + final ReadWriteTransaction tx = dataProvider.newReadWriteTransaction(); + ListenableFuture> readFuture = + tx.read( LogicalDatastoreType.OPERATIONAL, TOASTER_IID ); + + final ListenableFuture> commitFuture = + Futures.transform( readFuture, new AsyncFunction, + RpcResult>() { + + @Override + public ListenableFuture> apply( + Optional toasterData ) throws Exception { + + ToasterStatus toasterStatus = ToasterStatus.Up; + if( toasterData.isPresent() ) { + toasterStatus = ((Toaster)toasterData.get()).getToasterStatus(); + } + + LOG.debug( "Read toaster status: {}", toasterStatus ); + + if( toasterStatus == ToasterStatus.Up ) { + + if( outOfBread() ) { + LOG.debug( "Toaster is out of bread" ); + + return Futures.immediateFuture( Rpcs.getRpcResult( + false, null, makeToasterOutOfBreadError() ) ); + } + + LOG.debug( "Setting Toaster status to Down" ); + + // We're not currently making toast - try to update the status to Down + // to indicate we're going to make toast. This acts as a lock to prevent + // concurrent toasting. + tx.put( LogicalDatastoreType.OPERATIONAL, TOASTER_IID, + buildToaster( ToasterStatus.Down ) ); + return tx.commit(); + } + + LOG.debug( "Oops - already making toast!" ); + + // Return an error since we are already making toast. This will get + // propagated to the commitFuture below which will interpret the null + // TransactionStatus in the RpcResult as an error condition. + return Futures.immediateFuture( Rpcs.getRpcResult( + false, null, makeToasterInUseError() ) ); + } + } ); + + Futures.addCallback( commitFuture, new FutureCallback>() { + @Override + public void onSuccess( RpcResult result ) { + if( result.getResult() == TransactionStatus.COMMITED ) { + + // OK to make toast + currentMakeToastTask.set( executor.submit( + new MakeToastTask( input, futureResult ) ) ); + } else { + + LOG.debug( "Setting error result" ); + + // Either the transaction failed to commit for some reason or, more likely, + // the read above returned ToasterStatus.Down. Either way, fail the + // futureResult and copy the errors. + + futureResult.set( Rpcs.getRpcResult( false, null, result.getErrors() ) ); + } } - } - updateStatus(); - return currentTask; + @Override + public void onFailure( Throwable ex ) { + if( ex instanceof OptimisticLockFailedException ) { + + // Another thread is likely trying to make toast simultaneously and updated the + // status before us. Try reading the status again - if another make toast is + // now in progress, we should get ToasterStatus.Down and fail. + + LOG.debug( "Got OptimisticLockFailedException - trying again" ); + + checkStatusAndMakeToast( input, futureResult ); + + } else { + + LOG.error( "Failed to commit Toaster status", ex ); + + // Got some unexpected error so fail. + futureResult.set( Rpcs. getRpcResult( false, null, Arrays.asList( + RpcErrors.getRpcError( null, null, null, ErrorSeverity.ERROR, + ex.getMessage(), + ErrorType.APPLICATION, ex ) ) ) ); + } + } + } ); } /** @@ -195,17 +304,15 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti public Future> restockToaster(final RestockToasterInput input) { LOG.info( "restockToaster: " + input ); - synchronized( taskLock ) { - amountOfBreadInStock.set( input.getAmountOfBreadToStock() ); + amountOfBreadInStock.set( input.getAmountOfBreadToStock() ); - if( amountOfBreadInStock.get() > 0 ) { - ToasterRestocked reStockedNotification = - new ToasterRestockedBuilder().setAmountOfBread( input.getAmountOfBreadToStock() ).build(); - notificationProvider.publish( reStockedNotification ); - } + if( amountOfBreadInStock.get() > 0 ) { + ToasterRestocked reStockedNotification = new ToasterRestockedBuilder() + .setAmountOfBread( input.getAmountOfBreadToStock() ).build(); + notificationProvider.publish( reStockedNotification ); } - return Futures.immediateFuture(Rpcs. getRpcResult(true, Collections. emptySet())); + return Futures.immediateFuture(Rpcs. getRpcResult(true, Collections.emptyList())); } /** @@ -225,19 +332,38 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti return toastsMade.get(); } - private void updateStatus() { - if (dataProvider != null) { - WriteTransaction tx = dataProvider.newWriteOnlyTransaction(); - tx.put(LogicalDatastoreType.OPERATIONAL,TOASTER_IID, buildToaster()); + private void setToasterStatusUp( final Function resultCallback ) { - try { - tx.commit().get(); - } catch (InterruptedException | ExecutionException e) { - LOG.warn("Failed to update toaster status, operational otherwise", e); + WriteTransaction tx = dataProvider.newWriteOnlyTransaction(); + tx.put( LogicalDatastoreType.OPERATIONAL,TOASTER_IID, buildToaster( ToasterStatus.Up ) ); + + ListenableFuture> commitFuture = tx.commit(); + + Futures.addCallback( commitFuture, new FutureCallback>() { + @Override + public void onSuccess( RpcResult result ) { + if( result.getResult() != TransactionStatus.COMMITED ) { + LOG.error( "Failed to update toaster status: " + result.getErrors() ); + } + + notifyCallback( result.getResult() == TransactionStatus.COMMITED ); } - } else { - LOG.trace("No data provider configured, not updating status"); - } + + @Override + public void onFailure( Throwable t ) { + // We shouldn't get an OptimisticLockFailedException (or any ex) as no + // other component should be updating the operational state. + LOG.error( "Failed to update toaster status", t ); + + notifyCallback( false ); + } + + void notifyCallback( boolean result ) { + if( resultCallback != null ) { + resultCallback.apply( result ); + } + } + } ); } private boolean outOfBread() @@ -245,19 +371,22 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti return amountOfBreadInStock.get() == 0; } - private class MakeToastTask implements Callable> { + private class MakeToastTask implements Callable { final MakeToastInput toastRequest; + final SettableFuture> futureResult; - public MakeToastTask(final MakeToastInput toast) { - toastRequest = toast; + public MakeToastTask( final MakeToastInput toastRequest, + final SettableFuture> futureResult ) { + this.toastRequest = toastRequest; + this.futureResult = futureResult; } @Override - public RpcResult call() { + public Void call() { try { - // make toast just sleeps for n secondn per doneness level. + // make toast just sleeps for n seconds per doneness level. long darknessFactor = OpendaylightToaster.this.darknessFactor.get(); Thread.sleep(darknessFactor * toastRequest.getToasterDoneness()); @@ -275,15 +404,26 @@ public class OpendaylightToaster implements ToasterService, ToasterProviderRunti notificationProvider.publish( new ToasterOutOfBreadBuilder().build() ); } - synchronized (taskLock) { - currentTask = null; - } + // Set the Toaster status back to up - this essentially releases the toasting lock. + // We can't clear the current toast task nor set the Future result until the + // update has been committed so we pass a callback to be notified on completion. + + setToasterStatusUp( new Function() { + @Override + public Void apply( Boolean result ) { + + currentMakeToastTask.set( null ); + + LOG.debug("Toast done"); - updateStatus(); + futureResult.set( Rpcs.getRpcResult( true, null, + Collections.emptyList() ) ); - LOG.debug("Toast done"); + return null; + } + } ); - return Rpcs. getRpcResult(true, null, Collections. emptySet()); + return null; } } } diff --git a/opendaylight/netconf/config-netconf-connector/pom.xml b/opendaylight/netconf/config-netconf-connector/pom.xml index c10f0a9089..8a29139854 100644 --- a/opendaylight/netconf/config-netconf-connector/pom.xml +++ b/opendaylight/netconf/config-netconf-connector/pom.xml @@ -105,7 +105,6 @@ org.opendaylight.controller.netconf.confignetconfconnector.transactions, org.opendaylight.controller.netconf.confignetconfconnector.util, org.opendaylight.controller.netconf.confignetconfconnector.osgi, - org.opendaylight.controller.config.util, org.opendaylight.controller.netconf.confignetconfconnector.exception, com.google.common.base, com.google.common.collect, @@ -130,7 +129,9 @@ com.google.common.io, org.opendaylight.yangtools.yang.model.api.type, org.opendaylight.yangtools.sal.binding.generator.spi, - org.opendaylight.yangtools.sal.binding.yang.types, + org.opendaylight.yangtools.sal.binding.yang.types, + org.opendaylight.controller.config.util + diff --git a/pom.xml b/pom.xml index 012d9399b8..af84002429 100644 --- a/pom.xml +++ b/pom.xml @@ -129,6 +129,7 @@ features/base features/controller features/adsal + features/extras opendaylight/dummy-console opendaylight/karaf-branding opendaylight/distribution/opendaylight-karaf