001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.oscal.lib.profile.resolver.support;
028
029import gov.nist.secauto.metaschema.model.common.metapath.item.IDocumentNodeItem;
030import gov.nist.secauto.metaschema.model.common.metapath.item.IRequiredValueModelNodeItem;
031import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
032
033import edu.umd.cs.findbugs.annotations.NonNull;
034
035/**
036 * Used to visit a catalog containing groups and controls.
037 *
038 * @param <T>
039 *          the type of the state object used to pass calling context
040 *          information
041 * @param <R>
042 *          the type of the result for visiting a collection of groups and/or
043 *          controls
044 */
045public abstract class AbstractCatalogVisitor<T, R> implements ICatalogVisitor<T, R> {
046
047  protected abstract R newDefaultResult(T state);
048
049  protected abstract R aggregateResults(R first, R second, T state);
050
051  protected R visitCatalog(@NonNull IDocumentNodeItem catalogDocument, T state) {
052    return visitGroupContainer(catalogDocument.getRootAssemblyNodeItem(), newDefaultResult(state), state);
053  }
054
055  /**
056   * Visit the child groups and controls (in that order) of a given catalog or
057   * group container.
058   *
059   * @param catalogOrGroup
060   *          the catalog or group Metapath item currently being visited
061   * @param initialResult
062   *          the initial result value to use when aggregating child results
063   * @param state
064   *          the current visitor state
065   * @return a meaningful result of the given type
066   */
067  protected R visitGroupContainer(
068      @NonNull IRequiredValueModelNodeItem catalogOrGroup,
069      R initialResult,
070      T state) {
071    R result = catalogOrGroup.getModelItemsByName("group").stream()
072        .map(groupItem -> {
073          return visitGroupItem(ObjectUtils.requireNonNull(groupItem), state);
074        })
075        .reduce(initialResult, (first, second) -> aggregateResults(first, second, state));
076    return visitControlContainer(catalogOrGroup, result, state);
077  }
078
079  /**
080   * Called when visiting a group.
081   * <p>
082   * This method will first visit the group's children, then the group itself.
083   *
084   * @param item
085   *          the group Metapath item to visit
086   * @param state
087   *          the current visitor state
088   * @return a meaningful result of the given type
089   */
090  protected R visitGroupItem(@NonNull IRequiredValueModelNodeItem item, T state) {
091    R childResult = visitGroupContainer(item, newDefaultResult(state), state);
092    return visitGroupInternal(item, childResult, state);
093  }
094
095  /**
096   * Called when visiting a group after visiting it's children.
097   *
098   * @param item
099   *          the group Metapath item currently being visited
100   * @param childResult
101   *          the result of visiting the group's children
102   * @param state
103   *          the current visitor state
104   * @return a meaningful result of the given type
105   */
106  protected R visitGroupInternal(
107      @NonNull IRequiredValueModelNodeItem item,
108      R childResult,
109      T state) {
110    return visitGroup(item, childResult, state);
111  }
112
113  /**
114   * Visit the child controls (in that order) of a given catalog, group, or
115   * control container.
116   *
117   * @param catalogOrGroupOrControl
118   *          the catalog, group, or control Metapath item currently being visited
119   * @param initialResult
120   *          the initial result value to use when aggregating child results
121   * @param state
122   *          the current visitor state
123   * @return a meaningful result of the given type
124   */
125  protected R visitControlContainer(
126      @NonNull IRequiredValueModelNodeItem catalogOrGroupOrControl,
127      R initialResult,
128      T state) {
129    return catalogOrGroupOrControl.getModelItemsByName("control").stream()
130        .map(controlItem -> {
131          return visitControlItem(ObjectUtils.requireNonNull(controlItem), state);
132        })
133        .reduce(initialResult, (first, second) -> aggregateResults(first, second, state));
134  }
135
136  /**
137   * Called when visiting a control.
138   * <p>
139   * This method will first visit the control's children, then the control itself.
140   *
141   * @param item
142   *          the group Metapath item to visit
143   * @param state
144   *          the current visitor state
145   * @return a meaningful result of the given type
146   */
147  protected R visitControlItem(@NonNull IRequiredValueModelNodeItem item, T state) {
148    R childResult = visitControlContainer(item, newDefaultResult(state), state);
149    return visitControlInternal(item, childResult, state);
150  }
151
152  /**
153   * Called when visiting a control after visiting it's children.
154   *
155   * @param controlItem
156   *          the Metapath item for the control currently being visited
157   * @param childResult
158   *          the result of visiting the control's children
159   * @param state
160   *          the calling context information
161   * @return a meaningful result of the given type
162   */
163  protected R visitControlInternal(@NonNull IRequiredValueModelNodeItem controlItem, R childResult, T state) {
164    return visitControl(controlItem, childResult, state);
165  }
166}