Do not pretty-print body class
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / MountPointData.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.util;
9
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import java.io.IOException;
15 import java.util.ArrayList;
16 import java.util.EnumMap;
17 import java.util.List;
18 import java.util.Map;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.yangtools.concepts.AbstractSimpleIdentifiable;
21 import org.opendaylight.yangtools.rfc8528.data.api.MountPointChild;
22 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
23 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContextFactory;
24 import org.opendaylight.yangtools.rfc8528.data.api.MountPointIdentifier;
25 import org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension;
26 import org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * YANG Schema Mount-supported data attached to either a {@code list} item or a {@code container}.
34  */
35 @Beta
36 public final class MountPointData extends AbstractSimpleIdentifiable<MountPointIdentifier> {
37     private static final Logger LOG = LoggerFactory.getLogger(MountPointData.class);
38
39     private final Map<ContainerName, MountPointChild> yangLib = new EnumMap<>(ContainerName.class);
40     private final List<MountPointChild> children = new ArrayList<>();
41     private final MountPointContextFactory contextFactory;
42
43     private MountPointChild schemaMounts;
44
45     MountPointData(final MountPointIdentifier mountId, final MountPointContextFactory contextFactory) {
46         super(mountId);
47         this.contextFactory = requireNonNull(contextFactory);
48     }
49
50     public void setContainer(final @NonNull ContainerName containerName, final @NonNull MountPointChild data) {
51         final MountPointChild prev = yangLib.putIfAbsent(containerName, requireNonNull(data));
52         checkState(prev == null, "Attempted to duplicate container %s data %s with %s", containerName, prev, data);
53         addChild(data);
54     }
55
56     public void setSchemaMounts(final @NonNull MountPointChild data) {
57         checkState(schemaMounts == null, "Attempted to reset schema-mounts from %s to %s", schemaMounts, data);
58         schemaMounts = requireNonNull(data);
59         addChild(data);
60     }
61
62     public void addChild(final @NonNull MountPointChild data) {
63         children.add(requireNonNull(data));
64     }
65
66     void write(final @NonNull NormalizedNodeStreamWriter writer) throws IOException {
67         final StreamWriterMountPointExtension mountWriter = writer.getExtensions()
68             .getInstance(StreamWriterMountPointExtension.class);
69         if (mountWriter == null) {
70             LOG.debug("Writer {} does not support mount points, ignoring data in {}", writer, getIdentifier());
71             return;
72         }
73
74         final MountPointContext mountCtx;
75         try {
76             mountCtx = contextFactory.createContext(yangLib, schemaMounts);
77         } catch (YangParserException e) {
78             throw new IOException("Failed to resolve mount point " + getIdentifier(), e);
79         }
80         try (NormalizedNodeStreamWriter nestedWriter = mountWriter.startMountPoint(getIdentifier(), mountCtx)) {
81             for (MountPointChild child : children) {
82                 child.writeTo(nestedWriter, mountCtx);
83             }
84         }
85     }
86 }