2 * Copyright (c) 2019 Nordix Foundation. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.remote.rpc.registry;
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
26 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
27 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public final class ActionRoutingTable extends AbstractRoutingTable<ActionRoutingTable, DOMActionInstance> {
32 private static final class Proxy implements Externalizable {
33 private static final long serialVersionUID = 1L;
34 private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
36 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
37 private Collection<DOMActionInstance> actions;
38 private ActorRef opsInvoker;
40 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
41 // be able to create instances via reflection.
42 @SuppressWarnings("checkstyle:RedundantModifier")
47 Proxy(final ActionRoutingTable table) {
48 actions = table.getItems();
49 opsInvoker = table.getInvoker();
53 public void writeExternal(final ObjectOutput out) throws IOException {
54 LOG.debug("serializing ActionRoutingTable.");
55 out.writeObject(Serialization.serializedActorPath(opsInvoker));
57 final NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out);
58 nnout.writeInt(actions.size());
59 for (DOMActionInstance id : actions) {
60 nnout.writeSchemaPath(id.getType());
61 YangInstanceIdentifier actionPath = YangInstanceIdentifier.create(
62 new YangInstanceIdentifier.NodeIdentifier(id.getType().getLastComponent()));
63 nnout.writeYangInstanceIdentifier(actionPath);
68 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
69 LOG.debug("deserializing ActionRoutingTable");
70 opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
72 final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
73 final int size = nnin.readInt();
74 actions = new ArrayList<>(size);
75 for (int i = 0; i < size; ++i) {
76 actions.add(DOMActionInstance.of(nnin.readSchemaPath(), LogicalDatastoreType.OPERATIONAL,
77 nnin.readYangInstanceIdentifier()));
81 private Object readResolve() {
82 return new ActionRoutingTable(opsInvoker, actions);
86 private static final long serialVersionUID = 1L;
87 private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
89 ActionRoutingTable(final ActorRef invoker, final Collection<DOMActionInstance> actions) {
90 super(invoker, actions);
93 ActionRoutingTable updateActions(final Collection<DOMActionInstance> toAdd,
94 final Collection<DOMActionInstance> toRemove) {
95 LOG.debug("Updating actions in ActionRoutingTable");
96 final Set<DOMActionInstance> newActions = new HashSet<>(getItems());
97 newActions.addAll(toAdd);
98 newActions.removeAll(toRemove);
99 return new ActionRoutingTable(getInvoker(), newActions);
103 Object writeReplace() {
104 return new Proxy(this);