All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.yangcentral.yangkit.model.impl.stmt.MustSupportImpl Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
package org.yangcentral.yangkit.model.impl.stmt;

import org.yangcentral.yangkit.base.ErrorCode;
import org.yangcentral.yangkit.common.api.exception.Severity;
import org.yangcentral.yangkit.common.api.validate.ValidatorResult;
import org.yangcentral.yangkit.common.api.validate.ValidatorResultBuilder;
import org.yangcentral.yangkit.common.api.validate.ValidatorResultBuilderFactory;
import org.yangcentral.yangkit.model.api.stmt.Must;
import org.yangcentral.yangkit.model.api.stmt.MustSupport;
import org.yangcentral.yangkit.model.api.stmt.SchemaNode;
import org.yangcentral.yangkit.util.ModelUtil;
import org.yangcentral.yangkit.xpath.YangXPath;
import org.yangcentral.yangkit.xpath.impl.XPathUtil;
import org.yangcentral.yangkit.xpath.impl.YangXPathContext;
import org.yangcentral.yangkit.xpath.impl.YangXPathValidator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class MustSupportImpl implements MustSupport {
   private List musts = new ArrayList<>();
   private Object contextNode;
   private SchemaNode self;

   public Must getMust(int index) {
      return this.musts.get(index);
   }

   public Must getMust(String condition) {
      Iterator mustIterator = this.musts.iterator();

      Must must;
      do {
         if (!mustIterator.hasNext()) {
            return null;
         }

         must = mustIterator.next();
      } while(!must.getArgStr().equals(condition));

      return must;
   }

   public List getMusts() {
      return Collections.unmodifiableList(this.musts);
   }

   public void setMusts(List musts) {
      this.musts = musts;
   }

   public ValidatorResult addMust(Must must) {
      ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
      Must orig = this.getMust(must.getArgStr());
      if (null != orig) {
         validatorResultBuilder.addRecord(ModelUtil.reportDuplicateError(orig, must, Severity.WARNING));
         return validatorResultBuilder.build();
      } else {
         this.musts.add(must);
         return validatorResultBuilder.build();
      }
   }

   public void removeMust(String condition) {
      int size = this.musts.size();
      int index = -1;

      for(int i = 0; i < size; ++i) {
         Must must = this.musts.get(i);
         if (must != null && must.getArgStr().equals(condition)) {
            index = i;
            break;
         }
      }

      if (index != -1) {
         this.musts.remove(index);
      }

   }
   /**
    * remove all musts
    * @version 1.0.0
    * @author frank feng
    * @since 7/21/2022
    */
   public void removeMusts(){
      this.musts.clear();
   }
   public ValidatorResult updateMust(Must must) {
      ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();
      int size = this.musts.size();
      int index = -1;

      for(int i = 0; i < size; ++i) {
         Must m = (Must)this.musts.get(i);
         if (m != null && m.getArgStr().equals(must.getArgStr())) {
            index = i;
            break;
         }
      }

      if (index != -1) {
         this.musts.set(index, must);
      } else {
         validatorResultBuilder.addRecord(ModelUtil.reportError(must,
                 ErrorCode.PROPERTY_NOT_MATCH.toString(new String[]{"name=must"})));
      }

      return validatorResultBuilder.build();
   }

   public Object getContextNode() {
      return this.contextNode;
   }

   public void setContextNode(Object contextNode) {
      this.contextNode = contextNode;
   }

   public SchemaNode getSelf() {
      return this.self;
   }

   public void setSelf(SchemaNode self) {
      this.self = self;
   }

   public ValidatorResult validateMusts() {
      ValidatorResultBuilder validatorResultBuilder = new ValidatorResultBuilder();

      for (Must must : this.musts) {
         YangXPath xpath = must.getXPathExpression();
         if (xpath == null) {
            continue;
         }
         Object contextNode = XPathUtil.getXPathContextNode(self);
         YangXPathContext yangXPathContext = new YangXPathContext(must.getContext(), contextNode, this.self);
         xpath.setXPathContext(yangXPathContext);
         YangXPathValidator yangXPathValidator = new YangXPathValidator(xpath, yangXPathContext, new ValidatorResultBuilderFactory());
         validatorResultBuilder.merge(yangXPathValidator.visit(xpath.getRootExpr(), contextNode));
      }

      return validatorResultBuilder.build();
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy